- Home /
Why does this code work without any "Pass{}" expressions? And is there a way to add shadow collections?
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"
}
Answer by tanoshimi · Aug 08, 2015 at 08:36 PM
You don't define explicit passes for surface shaders - Unity defines them when it compiles the shader. To add shadows, you simply include the addshadow
option to the #pragma that declares the surface shader function - http://docs.unity3d.com/Manual/SL-SurfaceShaders.html.
Your link isn't working. And I'm a bit confused, do you mean that "Pass{}" is optional? Also, which #pragma do you mean? The addshadow function is already included as an option in several of the #pragma lines.
There was a dot at the end of the link. http://docs.unity3d.com/$$anonymous$$anual/SL-SurfaceShaders.html
Thanks, I will give this a read. Does anybody have a quick answer to the questions I asked? I don't want to have to learn all about shaders to accomplish this one task.
As in, how do I add the function to receive shadows to the shader that I posted?