Search code examples
unity-game-engineshader

Shader turns either magenta or invisible when using any "LightMode" tag in Unity


So, I´ve been trying to learn how to write UnityCG shaders by watching tutorials and trying to modify those projects on my own afterwards. But every time anything involves Tags {"LightMode" = "X"} my shader just turns invisible or "error magenta", no matter which tag i use it's always one of these two outcomes. I've rummaged through every related term i could think of, and even tried reinstalling Unity in case something was corrupted, but nothing helped.

Here's a shader copied straight from here, no changes made, and doesn't work on my end.

Shader "Custom/ShadowsInFwdRenderingVertFrag" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}//Needed by the Diffuse Fallback
}
SubShader {
Tags { "RenderType"="Opaque" }
//LOD 200


Pass{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "AutoLight.cginc"

struct vertOut {
float4 pos : SV_POSITION;
LIGHTING_COORDS(0,1) // NEEDED FOR SHADOWS.
};

vertOut vert(appdata_base v)
{
vertOut o;
o.pos= UnityObjectToClipPos(v.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o); // NEEDED FOR SHADOWS.
return o;
}

float4 frag(vertOut i):COLOR
{
fixed atten = LIGHT_ATTENUATION(i);// NEEDED FOR SHADOWS.
float4 c;
c = float4(0,1,0,1) * atten;
return c;
}
ENDCG
}//Pass



// Pass to render object as a shadow caster
Pass {
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }

Fog {Mode Off}
ZWrite On ZTest LEqual Cull Off
Offset 1, 1

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
#include "AutoLight.cginc"

struct v2f {
V2F_SHADOW_CASTER;
};

v2f vert( appdata_base v )
{
v2f o;
TRANSFER_SHADOW_CASTER(o)
return o;
}

float4 frag( v2f i ) : COLOR
{
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
} //Pass



// Pass to render object as a shadow collector
Pass {
Name "ShadowCollector"
Tags { "LightMode" = "ShadowCollector" }

Fog {Mode Off}
ZWrite On ZTest LEqual

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_shadowcollector

#define SHADOW_COLLECTOR_PASS
#include "UnityCG.cginc"

struct appdata {
float4 vertex : POSITION;
};

struct v2f {
V2F_SHADOW_COLLECTOR;
};

v2f vert (appdata v)
{
v2f o;
TRANSFER_SHADOW_COLLECTOR(o)
return o;
}

fixed4 frag (v2f i) : COLOR
{
SHADOW_COLLECTOR_FRAGMENT(i)
}
ENDCG
}//Pass

} //SubShader
FallBack "Diffuse" //note: required for passes: ForwardBase, ShadowCaster, ShadowCollector
}

Here it is in magenta. Here it is invisible (but notice it's still casting shadows).

Thanks in advance, any hint is appreciated!


Solution

  • Are you using LWRP or HDRP by any chance? These pipelines have their separate light modes and won't work with the ones used for the standard pipeline. For LWRP, the light mode is called "LightweightForward".

    Here is a template for writing LWRP shaders:

    https://gist.github.com/phi-lira/225cd7c5e8545be602dca4eb5ed111ba