Using sampler2D Arrays

I’m using defold 1.9.2

I use individual shadow maps for each light source.
I realized that I can declare a sampler in the shader program in this way:

uniform sampler2D light_shadow_map[MAX_LIGHTS];

Each shadow map is a RenderTarger that is rendered in a render script. How can I pass them to the shader? render.enable_texture will not work here. This is a rather trivial task, but I have not been able to find an answer to this question.

Using sampler2DArray seems limited too, I would like to avoid using atlases, especially in my case even that is not suitable since all my render targets are not resources and are created in rantime.

Are there any ideas on this? Thanks!

We currently don’t have support for setting a sampler uniform in an array (although I suppose we could/should), and as far as array textures go we only use them for paged atlases internally right now. I’d like to add support to the resource.create_texture to support creating arrayed textures, since it can be useful for things like shadow maps. There’s an issue here Add support for creating 3D and array textures in runtime · Issue #9033 · defold/defold · GitHub for that, but I haven’t had time to look at it yet.

But in the meantime, I would probably solve this by using a 2D texture as a flat shadow cache by “packing” the shadow maps in a fixed grid of the render target. You should be able to render to a subregion by configuring the viewport, and then scale + offset the UV coords into the shadow map to get the map in the cache.

1 Like