Instances with ShaderMaterial

Hey, I have a problem drawing instances with ShaderMaterial. They don’t seem to be drawn.
I searched it and others had the same problem years ago.

Is this fixed in latest version of Babylon? What is the standard way to use ShaderMaterial on instances?

1 Like

Hi @vuoriov4

I used this PG this morning for some test : https://www.babylonjs-playground.com/#B2NZ1M#11
Maybe that’s what you were looking for :slight_smile:

5 Likes

Thanks I got it working with this example. Finally my game has some explosions :smiley:

Another problem: How to access the instance index inside the shader?
I’ll post here if I figure it out…

use gl_InstanceID. it returns an int that you can cast to a float.

1 Like

Thank you it worked. For anyone having the same problem here’s the solution written out.

Vertex shader

flat varying int iid;
void main(void) {
    ...
    iid = gl_InstanceID;
}

Fragment

flat varying int iid;  
void main(void) {
    float(iid); // instance index as float
}
1 Like

Sorry to dig/necro this but it’s a top result in google for this problem and it doesn’t work in Typescript

blocksRender.ts:171 Uncaught TypeError: Cannot set property material of [object Object] which has only a getter

What is the suggestion for Typescript? Why do we even need to reassign the same material to the instance?

Thanks!

/edit

A minute later I combed over the code and realized we don’t need to reassign the same material. My shader uses the distance from the camera to the instance, but I wasn’t transforming the position by the included finalWorld from #include<instancesVertex> T_T

So for some clarity:
The shader example uses:
#include<instancesDeclaration>
and
#include<instancesVertex>
and multiplies/transforms the original vertex position by the finalWorld instance transform instead of using the position attribute directly.

1 Like