zadvorsky / three.bas

THREE.JS Buffer Animation System

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prefab Geometry vs. Instanced Geometry

juliekoubova opened this issue · comments

Hello 😄 What is the difference between prefab geometry in three-bas and instanced geometry in three.js itself? They seem kinda similar to me, but I'm probably missing something basic.

I'm sorry, I know this is not really an issue. Is there a better place to ask these questions? Thank you ❤️

Asking questions here is fine, at least for now.

When I first created this extension, THREE.js instanced geometries didn't exist (or I didn't know about them). Instanced geometries rely on a WebGL extension that changes how vertex attributes are used. This extension is fairly widely supported, but may be missing on some devices / browsers.

With instanced geometries, the default geometry attributes (position, normal, uv) are stored once (this defines the instance). Additional custom attributes (like a position / rotation / scale offset) are then stored once per instance.

With the prefab geometry, the default geometry attributes are simply duplicated, and custom attributes are stored once per vertex.

Instanced geometries are substantially more memory efficient, but you lose some flexibility; you can't have per-vertex variations for custom attributes, or per-instance variations for default attributes.

I recently added InstancedPrefabBufferGeometry, which which has a similar API as PrefabBufferGeometry, but uses the instanced geometry under the hood. See this example.

Thank you for the detailed explanation! 😄

Is it okay and supported to use the standard InstancedBufferGeometry with three-bas materials? I'm building several buffers in a loop and it is just seems easier to build them directly rather than going thru InstancedPrefabBufferGeometry.setPrefabData...

And thanks for creating this project. It makes writing shaders so much easier and more organized, especially for a 3D graphics noob like me 🤣

Yes, all three-bas materials should work with InstancedBufferGeometry (or any other buffer geometry). As you can see in the source, InstancedPrefabBufferGeometry is a thin wrapper that only copies the api from my other geometries.