pygfx icon indicating copy to clipboard operation
pygfx copied to clipboard

Help creating an instanced object from scratch

Open sketchpunk opened this issue 9 months ago • 1 comments

Is it possible to create an example of how someone can create an instanced mesh from scratch? Like how to setup the mesh attributes along with instanced buffers, plus a shader that will utilize all these buffers to manipulate the mesh. I'm trying to port of some things I've done in threejs over to pygfx and having a hard time with this part. I would be very grateful for any help on this.

Lastly a question. In threejs I've made Dynamic instanced meshes where I keep track of how many total instances and when I need more space I would dispose of the geometry object in the mesh and replace it with a new one that will hold the new larger size of instances. Would something like that also work in pygfx, swopping out the geometry of a mesh with a new one?

Any help would be very appreciated !

sketchpunk avatar May 17 '25 21:05 sketchpunk

Hi 👋, we have a few examples that demonstrate instanced meshes:

  • https://github.com/pygfx/pygfx/blob/main/examples/feature_demo/instancing_mesh.py
  • https://github.com/pygfx/pygfx/blob/main/examples/feature_demo/look_at_instanced.py
  • https://github.com/pygfx/pygfx/blob/main/examples/feature_demo/pbr_instanced.py

But currently not one that also demonstrates instancing in relation to custom shaders. To learn more about this and create a custom instanced object from scratch, these modules are most relevant:

Would something like that also work in pygfx, swopping out the geometry of a mesh with a new one?

Yes! Pygfx's engine can in most cases detect when buffers or geometry is replaced, and will automatically update the bindings, and even recompile the shader if needed (e.g. when the new texture has a different format). I can explain more about this when you get there :)

almarklein avatar May 21 '25 08:05 almarklein