- Home /
Why are the UV's in my texture array shader always zero?
I'm currently working on a surface shader that uses a texture array. For some reason, the uv's being passed to the surf function appear to always be (0,0,0), even though I've confirmed that the uv's on the mesh (a simple quad) are [ (0,0,8) , (1,1,8) , (1,0,8) , (0,1,8) ].
I'm certain the texture array and mesh themselves are fine, because everything worked correctly when I used a vert/frag shader, but I want to switch it to a surface shader so I could easily have lighting.
Here is my shader code:
Shader "Rimply/TextureArray"
{
Properties
{
_MyArr ("Tex", 2DArray) = "" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf Lambert
#pragma target 3.5
#ifndef SHADER_TARGET_SURFACE_ANALYSIS
UNITY_DECLARE_TEX2DARRAY(_MyArr);
#endif
struct Input
{
float3 uv_MyArr;
};
void surf (Input IN, inout SurfaceOutput o)
{
#ifndef SHADER_TARGET_SURFACE_ANALYSIS
half4 c = UNITY_SAMPLE_TEX2DARRAY(_MyArr, IN.uv_MyArr);
o.Albedo = c.rgb;
#endif
}
ENDCG
}
Fallback "Diffuse"
}
Answer by Bunny83 · Apr 19, 2017 at 10:37 PM
Well, i guess that the surface shader compiler simply has trouble / doesn't support mapping a texture coordinate channel to texture arrays. The example used an explicit semantic (TEXCOORD0). However i'm not sure if you can specify that in a surface shader. Though as far as i know you can actually mix a surface function with custom vertex and fragment shaders. However i've never done that.
The macro UNITY_DECLARE_TEX2DARRAY looks like this and does two things:
#define UNITY_DECLARE_TEX2DARRAY(tex) Texture2D_Array tex; SamplerState sampler##tex
First it declares a Texture2D_Array with the provided name. Second it declares a SamplerState with the name "sampler" + yourProvidedName
So in your case it would be:
Texture2D_Array _MyArr;
SamplerState sampler_MyArr;
It could be that the uv coordinate is bound to the sampler name, but that's just a guess. You could try:
uv_sampler_MyArr
though i doubt that it works ^^.
Unfortunately that just gives me an "invalid subscript" error :( Regardless, thank you for the detailed suggestion!