# Shader Material doesn't work with instances

**URL:** <https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793>\
**Category:** Bugs\
**Created:** [January 16, 2019, 3:39pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793 "2019-01-16T15:39:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Null](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/null/32/1947_2.png) [@Null](https://forum.babylonjs.com/u/Null)\
**Post date:** [January 16, 2019, 3:39pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/1 "2019-01-16T15:39:35Z")

</div>

Shader Materials don’t work with instances. Even after including instancesDeclaration and instancesVertex, the material doesn’t work because its missing the attributes “world0”, “world1”, “world2”, “world3”. I have fixed it by adding them in the materials instance, however during the process I found out that the code for it was there yet it didn’t work. The problem is in :

MaterialHelper.PrepareAttributesForInstances = function (attribs, defines) {  
if (defines[“INSTANCES”]) {  
attribs.push(“world0”);  
attribs.push(“world1”);  
attribs.push(“world2”);  
attribs.push(“world3”);  
}  
};

More exactly the if (defines[“INSTANCES”]) is never true, seems like the value isn’t “INSTANCES” but “#include INSTANCES”.

The test has been done with BABYLON 4.0.

Cheers!

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [January 16, 2019, 4:29pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/2 "2019-01-16T16:29:18Z")

</div>

When creating your shaderMaterial using the constructor you can just add the defines your want. Just include INSTANCES in the list of custom defines.

If this does not work, please create a PG as this is supposed to work

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 17, 2021, 1:55pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/3 "2021-04-17T13:55:23Z")

</div>

> [@Null](#):
>
> defines[“INSTANCES”]

Good Day @Deltakosh .

I attempted the “INSTANCES” defines approach without success.  
I followed up by manually adding the world0, world1, world2, and world3 vec4 attributes but it results in weird clipping effects. Can you please provide some guidance?

Here are two playgrounds:

1. [Using defines “INSTANCES”](https://www.babylonjs-playground.com/#QENFPC#53).
2. [Manual hack.](https://www.babylonjs-playground.com/#QENFPC#51)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [April 17, 2021, 4:23pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/4 "2021-04-17T16:23:19Z")

</div>

Adding @Evgeni_Popov who is amazing with instances 🙂

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 17, 2021, 4:42pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/5 "2021-04-17T16:42:02Z")

</div>

This thread should help:

> [@Instances with ShaderMaterial](https://forum.babylonjs.com/t/instances-with-shadermaterial/8201):
>
> 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?

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 17, 2021, 5:52pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/6 "2021-04-17T17:52:51Z")

</div>

Thanks @Evgeni_Popov . Any idea why I am getting this error:

`VERTEX SHADER ERROR: 0:21: 'flat' : Illegal use of reserved word`

I know I am not making use of the gl\_InstanceID yet, but I would like to in the future.

[https://www.babylonjs-playground.com/#QENFPC#63](https://www.babylonjs-playground.com/#QENFPC#63)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 17, 2021, 6:15pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/7 "2021-04-17T18:15:18Z")

</div>

I don’t have this error, everything is ok on my side…

I do have the error in WebGL1, though, so I assume this keyword is illegal in WebGL1 (maybe you are testing with Safari which is using WebGL1?). In any case, I think `int` variables are not supported in WebGL1.

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 17, 2021, 6:20pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/8 "2021-04-17T18:20:20Z")

</div>

Good to know thanks @Evgeni_Popov .

In the same setup the first 6 instances are not rendering. Any clue to why?

[https://www.babylonjs-playground.com/#QENFPC#66](https://www.babylonjs-playground.com/#QENFPC#66)

 ![First_seven_instances_not_rendered](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/a/a0feb4cf9f2b90c13b99333e7a6792764597d02e.png)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 17, 2021, 6:26pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/9 "2021-04-17T18:26:03Z")

</div>

You don’t have to manage the world0-\>world3 buffers, it is done automatically by the system:

[https://www.babylonjs-playground.com/#QENFPC#67](https://www.babylonjs-playground.com/#QENFPC#67)

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 17, 2021, 6:33pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/10 "2021-04-17T18:33:47Z")

</div>

Bless you @Evgeni_Popov !

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 17, 2021, 10:52pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/11 "2021-04-17T22:52:13Z")

</div>

I am attempting to pull in just the morphTarget position logic and run a test on the first target set at full influence. I get this error:

```auto

Error: VERTEX SHADER ERROR: 0:32: 'readVector3FromRawSampler' : no matching overloaded function found

```

I see this function in the Babylon source clearly as shown below which takes the integer pointing to the morphTarget index and the float associated with the vertexIndex. I don’t understand why it then says there is NO MATCHING OVERLOADED FUNCTION.

Here is the [PG](https://www.babylonjs-playground.com/#QENFPC#70).

```
#ifdef MORPHTARGETS
	uniform float morphTargetInfluences[NUM_MORPH_INFLUENCERS];

	#ifdef MORPHTARGETS_TEXTURE	
		precision mediump sampler2DArray;		
		uniform float morphTargetTextureIndices[NUM_MORPH_INFLUENCERS];
		uniform vec3 morphTargetTextureInfo;
		uniform sampler2DArray morphTargets;

		vec3 readVector3FromRawSampler(int targetIndex, float vertexIndex)
		{			
			float y = floor(vertexIndex / morphTargetTextureInfo.y);
			float x = vertexIndex - y * morphTargetTextureInfo.y;
			vec3 textureUV = vec3((x + 0.5) / morphTargetTextureInfo.y, (y + 0.5) / morphTargetTextureInfo.z, morphTargetTextureIndices[targetIndex]);
			return texture(morphTargets, textureUV).xyz;
		}
	#endif
#endif
```

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 18, 2021, 1:15am UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/12 "2021-04-18T01:15:15Z")

</div>

You can use the `#includes` that already exist to inject the morph target code:

[https://www.babylonjs-playground.com/#QENFPC#75](https://www.babylonjs-playground.com/#QENFPC#75)

Also, you should let the `ShaderMaterial` class add the correct `#defines` depending on the fact you use instances / morph targets (that’s why I pass an empty `defines` array).

Note however that for the time being, the `ShaderMaterial` class does not handle morph targets that are using a texture, so you must pass the right parameters by hand (see PG lines 118-119).

Note also that there’s no morphing going on in your PG because `basisModel.morphTargetManager.numInfluencers = 0`: I have used another model in the PG above that does have `numInfluencers > 0`, so you will see that it works.

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 18, 2021, 2:34am UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/13 "2021-04-18T02:34:13Z")

</div>

Thank you @Evgeni_Popov ! Have a wonderful day!

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 18, 2021, 11:06am UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/14 "2021-04-18T11:06:24Z")

</div>

> [@Evgeni\_Popov](#):
>
> [https://www.babylonjs-playground.com/#QENFPC#75](https://www.babylonjs-playground.com/#QENFPC#75)

Good Day @Evgeni_Popov !

I am attempting to store some offset data in a raw texture and and trying to retrieve it in the vertex shader. The offsets for updating the vertex positions don’t seem to be working in this [PG](https://playground.babylonjs.com/#7SUTUC#2). Do you know why?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 18, 2021, 2:01pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/15 "2021-04-18T14:01:20Z")

</div>

It does work but you should use bigger values than 1 because you get an offset of 1/255 in the shader, which is not really visible:

[https://playground.babylonjs.com/#7SUTUC#4](https://playground.babylonjs.com/#7SUTUC#4)

Also, I think you will need to disable mip mapping and use a nearest filter for your texture to avoid having trilinear/bilinear filtering when doing the lookup.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 19, 2021, 2:03pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/16 "2021-04-19T14:03:07Z")

</div>

PR to add support for morph target texture to `ShaderMaterial`:

> <https://github.com/BabylonJS/Babylon.js/pull/10219>
>
> See https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793…/11

Once it’s in, this PG will work: [https://playground.babylonjs.com/#QENFPC#79](https://playground.babylonjs.com/#QENFPC#79)

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 20, 2021, 4:38pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/17 "2021-04-20T16:38:23Z")

</div>

> [@Evgeni\_Popov](#):
>
> [https://playground.babylonjs.com/#QENFPC#79](https://playground.babylonjs.com/#QENFPC#79)

Thanks @Evgeni_Popov ! In the meantime, I am trying a simpler, somewhat lossy approach to passing the morph positions via an RGBTexture uniform. It seems that this would work just as the simple box geometry example I did earlier however I test the recovered offsets from the texture method and they all seem to return zero. Any idea why the recovered offsets from the texture method all return zero @Evgeni_Popov ?

The goal in this example is to fully morph to a single chosen target per instance.

I have console logged the original positions along with the expected tranforms and lossy inverse transforms of the morph target position data for each target.

The inverse transform function I use for console logging is slightly different from that in the vertex shader due to the expectation that the texture sampling method will return normalized results.

> **[Babylon.js Playground](https://playground.babylonjs.com/#WPJKLA%236)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 20, 2021, 8:24pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/18 "2021-04-20T20:24:08Z")

</div>

You have to pass a Uint8Array to `CreateRGBTexture`, not a regular array:

[https://playground.babylonjs.com/#WPJKLA#12](https://playground.babylonjs.com/#WPJKLA#12)

---

<div class="post-metadata">

**Author:** ![SirFizX](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sirfizx/32/11703_2.png) [@SirFizX](https://forum.babylonjs.com/u/SirFizX)\
**Post date:** [April 20, 2021, 9:12pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/19 "2021-04-20T21:12:47Z")

</div>

> [@Evgeni\_Popov](#):
>
> [https://playground.babylonjs.com/#WPJKLA#12](https://playground.babylonjs.com/#WPJKLA#12)

Thank you @Evgeni_Popov . ❤ I am getting unexpected results from the applied morph position offsets. Is it perhaps because I didn’t inject a 255 for the alpha channel every fourth element of the data array @Evgeni_Popov ? I say this because the texture method sample returns a vec4 and I am only constituting the data array with r,g,b not a. Or is the CreateRBGTexture method setting the texture binding level to GL\_RGB thus informing the texture method to always assign 1.0f to the 4th component?

In the following PG, the clone in the middle is alternating between the two morph representations which I am expecting to see in the instances on either side.

> **[Babylon.js Playground](https://playground.babylonjs.com/#WPJKLA%2315)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 21, 2021, 12:31pm UTC](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793/20 "2021-04-21T12:31:39Z")

</div>

The sampling mode is not passed correctly when creating the texture, it should be the 7th parameter:

[https://playground.babylonjs.com/#WPJKLA#17](https://playground.babylonjs.com/#WPJKLA#17)

In effect, you had a Y inversion on your texture.

[Next page](https://forum.babylonjs.com/t/shader-material-doesnt-work-with-instances/793.md?page=2)
