Hey guys! I don’t know much about shaders, but I found this shader on the internet. I turned culling off for both of the passes. My character consists of some sprites that are flipped, and I wanted them all to be visible and to cast shadows on the same side. I only vaguely understand the code. Just enough to get it working for my purposes. So, my question is two-fold:
-
Why are there no Pass{} expressions? Aren’t you supposed to surround each pass with one? Or is that optional?
-
How do I add shadow collections to the shader? I tried adding this section of code, but it didn’t do anything:
// Pass to render object as a shadow collector
Pass {
Name “ShadowCollector”
Tags { “LightMode” = “ShadowCollector” }Cull Off//new Lighting On //new 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 }
Shader “Sprites/Bumped Diffuse both sides with Shadows 2”
{
Properties
{
[PerRendererData] _MainTex (“Base (RGB)”, 2D) = “white” {}
_BumpMap (“Normalmap”, 2D) = “bump” {}
_Color (“Tint”, Color) = (1,1,1,1)
[MaterialToggle] PixelSnap (“Pixel snap”, Float) = 0
_Cutoff (“Alpha Cutoff”, Range (0,1)) = 0.5
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="TransparentCutOut"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
LOD 300
// Render back faces first
Cull Off
Lighting On
ZWrite Off
Fog { Mode Off }
CGPROGRAM
#pragma surface surf Lambert alpha vertex:vert addshadow alphatest:_Cutoff
#pragma multi_compile DUMMY PIXELSNAP_ON
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
float _ScaleX;
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
fixed4 color : COLOR;
};
void vert (inout appdata_full v, out Input o)
{
#if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
v.vertex = UnityPixelSnap (v.vertex);
#endif
float3 normal = v.normal;
v.normal = float3(0,0,-1);
v.tangent = float4(1, 0, 0, 1);
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color += _Color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
o.Albedo = c.rgb;
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
// Now render front faces first
Cull Off
Lighting On
ZWrite Off
Fog { Mode Off }
CGPROGRAM
#pragma surface surf Lambert alpha vertex:vert addshadow alphatest:_Cutoff
#pragma multi_compile DUMMY PIXELSNAP_ON
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
float _ScaleX;
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
fixed4 color: COLOR;
};
void vert (inout appdata_full v, out Input o)
{
#if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
v.vertex = UnityPixelSnap (v.vertex);
#endif
float3 normal = v.normal;
v.normal = float3(0,0,1);
v.tangent = float4(1, 0, 0, 1);
UNITY_INITIALIZE_OUTPUT(Input, o);
o.color += _Color;
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
o.Albedo = c.rgb;
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Normal.z *= -1;
}
ENDCG
// Pass to render object as a shadow collector
Pass {
Name "ShadowCollector"
Tags { "LightMode" = "ShadowCollector" }
Cull Off//new
Lighting On //new
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
}
}
Fallback "Transparent/Cutout/Diffuse"
}