I’m trying to write a custom shader which uses multiple passes, first an arbitrary number of vert/frag passes followed by a GrabPass{} followed, finally, by a surf/surface shader pass. However, I need to use _MainTex_ST at least once in the vert/frag pass then also in the surf pass. Somehow this is causing an issue though where _MainTex_ST becomes either undeclared when it’s used in the vert pass or I get a redefinition error in the compiler.
Here is a small sample shader I made which recreates my issue. I added comments around the definition of _MainTex_ST explaining how to get either compiler error:
Shader "Custom/RedefinitionProblem"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
CGINCLUDE
#pragma target 3.0
#include "UnityCG.cginc"
struct v2f_custom
{
float2 uv : TEXCOORD0;
float4 grabPos : TEXCOORD1;
float4 pos : SV_POSITION;
};
struct appdata_custom
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST; // Commenting this out = "undeclared identifier '_MainTex_ST'" but leaving it in...
// ...causes "redefinition of '_MainTex_ST'"
v2f_custom vertBlur(appdata_custom v) {
v2f_custom o;
o.pos = UnityObjectToClipPos(v.vertex);
o.grabPos = ComputeGrabScreenPos(o.pos);
o.uv = TRANSFORM_TEX(v.uv, _MainTex); // <-- This is where "undeclared identifier '_MainTex_ST'" happens
return o;
}
ENDCG
Tags{ "Queue" = "Transparent" }
GrabPass{}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
GrabPass{}
// === Start of surface shader pass ===
Cull Back ZWrite On
Tags {
"Queue" = "Transparent"
"RenderType" = "Transparent"
"IgnoreProjector" = "True"
}
CGPROGRAM
// Physically based Standard lighting model
#pragma surface surf Standard vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
struct Input {
float2 uv_MainTex;
float4 screenPos;
float3 viewDir;
float3 worldPos;
float3 worldNormal; INTERNAL_DATA
float3 worldRefl;
float eyeDepth;
};
void vert(inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
COMPUTE_EYEDEPTH(o.eyeDepth);
}
void surf(Input i, inout SurfaceOutputStandard o) {
o.Albedo = tex2D(_MainTex, i.uv_MainTex);
}
ENDCG
}
}
How can I fix this?