- Home /
Why is my scrolling shader suddenly rendering as magenta? (May be related to fog)
Hey folks. Was writing a vfx shader which can be seen in action here. After the version seen in the webplayer, I added a new skybox, particles and fog. Everything seemed to be working well, until I reloaded the project later and the shader was rendering as magenta, with the following errors:
Shader error in 'PF/CloudScroll01': Duplicated input semantics can't change type, size, or layout ('TEXCOORD1'). at line 44 (on d3d11)
Shader error in 'PF/CloudScroll01': output TEXCOORD1 used more than once at line 44 (on d3d11)
Shader warning in 'PF/CloudScroll01': Duplicate non-system value semantic definition: output semantic 'TEXCOORD1' and output semantic 'TEXCOORD1' at line 43 (on d3d11)
Shader warning in 'PF/CloudScroll01': Duplicate non-system value semantic definition: output semantic 'TEXCOORD1' and output semantic 'TEXCOORD1' at line 44 (on d3d11)
Removing all references to fog (UNITY_FOG_COORDS(1) in struct v2f, UNITY_TRANSFER_FOG in v2f vert, UNITY_APPLY_FOG in fixed4 frag) makes the sky render 'correctly' again, but I'd like to experiment with how it interacts with fog (given the nature of the effect).
Can anybody help me get the effect working with fog and NOT rendering as magenta? Shader code follows...
Shader "PF/CloudScroll01"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_MaskTex ("Mask Texture", 2D) = "white" {}
_ScrollXSpeed("Scroll X Speed", float) = 1
_ScrollYSpeed("Scroll Y Speed", float) = 1
_MaskMul ("Mask Multiplier", float) = 1
_AlphMul ("Alpha Multiplier", float) = 1
_CloudCol ("Colour", Color) = (0,0,0,0)
}
SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True"}
LOD 100
zwrite off
Blend SrcAlpha OneMinusSrcAlpha // Makes it transparent?
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata_f
{
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1; // Getting second set of UV coords.
};
struct v2f
{
float4 vertex : SV_POSITION; // Final vertex positions in clip space (view frustrum)
float2 texcoord0 : TEXCOORD0; // Presumbably this is where the UV data gets acquired?
float2 texcoord1 : TEXCOORD1;
UNITY_FOG_COORDS(1)
UNITY_VERTEX_OUTPUT_STEREO
};
sampler2D _MainTex;
float4 _MainTex_ST; // for any tex property, Unity provides val for float4 w/"_ST" suffix. The x,y contains texture scale, and z,w contains translation (offset).
sampler2D _MaskTex;
float4 _MaskTex_ST;
float _ScrollXSpeed;
float _ScrollYSpeed;
float _MaskMul;
float _AlphMul;
fixed4 _CloudCol;
v2f vert (appdata_f v)
{
v2f o;
v.texcoord0.y += _Time * _ScrollYSpeed;
v.texcoord0.x += _Time * _ScrollXSpeed;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord0 = TRANSFORM_TEX(v.texcoord0, _MainTex);
o.texcoord1 = TRANSFORM_TEX(v.texcoord1, _MaskTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
// Fragments - pre'pixels'.
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col01 = tex2D(_MainTex, i.texcoord0);
// col01.rgb = dot(col01.rgb, _CloudCol.rgb);
fixed4 col02 = tex2D(_MaskTex, i.texcoord1);
fixed4 colOut = fixed4(col01.r, col01.b, col01.g, (col01.a * _AlphMul) * (col02.a * _MaskMul));
// apply fog
UNITY_APPLY_FOG(i.fogCoord, colOut);
return colOut;
}
ENDCG
}
}
}
Answer by BastianUrbach · Jul 18, 2018 at 03:46 PM
If I remember it correctly, the parameter of UNITY_FOG_COORDS(1) tells Unity which texcoord semantic should be used for fog. So replacing that line with UNITY_FOG_COORDS(2) should fix it.
Absolutely perfect, bang on, thank you for setting me straight. Don't suppose you know how many UNITY_FOG_COORDS there are, or what they do? (Or have a link to that info? Can't find one...!) Either way, many thanks!