I tried to pass a color named color5 to an instance. I have registred a buffer but it doesn’t work anyway. What am I missed here, could you please take a look?
PG: Playground
See here: https://playground.babylonjs.com/#ASP6IA#1
tl;dr: attributes not uniforms, “worldViewProjection” does not seem to work, and this one was super mean: Instances with ShaderMaterial (see the #includes )
Thanks for fixing it!
As for worldViewProjection, I took that from Babylon docs, here: PG. From here: Babylon.js docs
Oh, I see now that regular meshes is not instances so I need that include.
So, I can’t pass uniforms to a fragment shader’s instances but attributes only? And I should pass those attributes from vertex shader to a fragment shader to use it in a fragment shader?
I do not know if there is a shortcut but usually, if you want to access attributes in your fragment shader, you have to pass them via varying.
As for attributes and instances. A uniform is per mesh per material, right? And if instances share their material with their source mesh, I do not see how you could pass a uniform to an instance.
As far as I understand, mesh.registerInstancedBuffer sets up a vertex buffer: i.e. your Color3 becomes a color per each vertex. And vertex buffers hold data per vertex which you access as attribute in a shader.
Woah, it looks like I get some sort of overhead here as I wanted to pass uv_position and uv_size vectors as well for having different UVs for all the instances. So what are profits in instances then? Are there any in this case?
I have one material and one texture between all the instances, but each instance will have different uv_pos and uv_size as the instances are all planes (like a sprite).
Using worldViewProjection
does not work with instances because #include<instancesVertex>
calculates a new final world matrix for the instance like this: mat4 finalWorld = mat4(world0, world1, world2, world3);
. So, you must use finalMatrix
in the calculation to project p
.
In fact, registerInstanceBuffer
does sets a vertex buffer, but the data inside this buffer is per instance, not per vertex (there’s a special parameter in a vertex buffer that let’s you indicate that the data are not per vertex but per instnace).
Oh, interesting I never thought about doing that. That might open some doors…
I would hope that you only have the addtional memory cost but can keep the performance benefit of instances?
Yes, creating a vertex buffer that holds data per instance have an additional memory cost, but very limited, as it is per instance and not per vertex.