I’m trying to get TilemapRenderer work with a texture array. Previously I was using a texture atlas and I was splitting it in subtextures with the Sprite Editor.
Everything works fine, except that I don’t know, inside the fragment shader, how to compute the right slice with the specific subtexture. I would’ve used the UVs to compute the right UVW of the texture array, but I got surprised when I found out that inside the fragment shader the UVs were referring to the single subtextures and not to the whole original texture atlas.
So I’d like to know how exactly TilemapRenderer work, how it pass the specific subtexture to the fragment shader.
Thank you very much.
This is my modified shader UnitySprites.cginc (included in Sprites-Default.shader). The texture array works great, but I don’t know how to get the right slice.
I cannot figure out why IN.texcoord are normalized sprite coordinates and not normalized texture coordinates. How does it happen that in the original shader _MainTex refers to a single sprite and not to the whole atlas?
UnitySprites.cginc
//...
UNITY_DECLARE_TEX2DARRAY(_MainTex);
sampler2D _AlphaTex;
fixed4 SampleSpriteTexture (float2 uv)
{
// fixed4 color = tex2D (_MainTex, uv); Original way to sample the 2D texture
// ---> Customization to use a texture array
float slice = 8; // Any way to know which part of the atlas I am drawing?
float3 uvw = float3(uv.x, uv.y, slice);
fixed4 color = UNITY_SAMPLE_TEX2DARRAY(_MainTex, uvw);
// <---
#if ETC1_EXTERNAL_ALPHA
//fixed4 alpha = tex2D (_AlphaTex, uv); //external alpha is unnecessary in my game
//color.a = lerp (color.a, alpha.r, _EnableExternalAlpha);
#endif
return color;
}
fixed4 SpriteFrag(v2f IN) : SV_Target
{
fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color;
c.rgb *= c.a;
return c;
}
//...
EDIT: Solved. It was a silly error. TilemapRenderer works in the expected way.