How to sampler TextureArray with a index from attribute?

Hi~ everyone
I want to write a pbrCustomMaterial to support instance to sample lightMap. In Unity each meshRender has lightMapIndex,lightMapScaleOffset to sample lightmap.
image
I try it in an PG (https://playground.babylonjs.com/#PDUSRT#1),it seems that webgl don’t support dynamic array index.
image
What can I do?
Thanking for reply :gift:

I got this same problem some time ago.
I solved it by sampling every textures (3 in your case) and multiply the result by 0 if it’s not the expected texture.

Like this :

Do you know a better way @Evgeni_Popov ?

1 Like

That is a way to solve this question :grinning:

Currently, Custom and PBRCustom materials don’t support array textures.

This PR will support them:

Once the PR is merged, you must pass a 3rd empty parameter when calling AddUniform and use material.onBindObservable to set the texture array:

Also, vInstanceLightMapIndex must be interpolated “flat” to keep the same value during rasterization. So you have to add the keyword “flat” in the declaration of vInstanceLightMapIndex, but this only works if you use the keywords “in” / “out” instead of “varying”: see the PG above.

You should get this instead of 3 white cubes with the PG above when the PR is merged:

As for a better way to do it, you could use a 2DArrayTexture if all your textures are the same size, with the added advantage that you are not limited to only a few textures as when you use a uniform array (generally, a fragment shader does not support more than 16 samplers).

4 Likes

Wow!!! Thank you for your help and work!!!