Texture array surface shader problem

Hi!

I’m trying to write a simple surface shader which use a texture array and choose the index by the vertex color red value. The following code doesn’t work. I already read some forums about surface shaders, checked the documentetion multiple times and can’t figure out whats wrong. I already made a custom texturearray shader which works good(no lightning) but i want a surface shader for auto generated lightning.

Shader “Custom/NewTerrainSurfaceShader”
{
Properties
{
_MainTex(“Albedo (RGB)”, 2DArray) = “white” {}
_SliceRange(“Slices”, Range(1,16)) = 5
}
SubShader
{
Tags { “RenderType” = “Opaque” }
LOD 200

CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.5
#pragma require 2darray

UNITY_DECLARE_TEX2DARRAY(_MainTex);
float _SliceRange;

struct Input
{
float2 uv_MainTex;
float4 color : COLOR;
};

void surf(Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(IN.uv_MainTex.xy, IN.color.r * _SliceRange));
o.Albedo = c.rgb;
//o.Metallic = IN.color.g;
//o.Smoothness = IN.color.b;
//o.Alpha = IN.color.a;
}
ENDCG
}
FallBack “Diffuse”
}

Thanks for the help!

What do you mean by “doesn’t work”? There’s nothing obviously wrong with the above shader code. Is it only ever showing the first layer, or is there a shader error and it’s showing pink?

That’s the problem. No error, seems to be nothing wrong. But everything purple! I deleted shader cache to reset everything, but when i try to use IN.color for albedo its not working anymore. Strange. It’s like the shader stucked in a wrong state and no refresh. I’m using Unity 2019.4.5.

Finally I figured it out: It’s not working with Universal Rendering Pipeline! It’s working without any RP.

Yep. That was going to be my response. Surface Shaders only work with the built in rendering paths. For the URPs you’ll want to use Shader Graph.

1 Like