Hello Unity community. I’m in the process of trying to implement shadow mapping for my class’s senior project, which we will be presenting at GDC San Francisco. Things have been going well up to this point but I’m having a bit of trouble now. I’ve done a lot of searching and experimenting but I’m still not getting any shadows to show up.
What I’ve done so far:
- Using -force-opengl mode (a project decision)
- Overriding the terrain shader (FirstPass.shader). Since there’s no way to access the terrain material inspector I set the shadow map as a global shader property with Shader.SetGlobalTexture(“_ShadowMap”, renderTexture);
- Using an orthographic camera for the light cam (directional light)
- Using a surface-vertex shader combo with a custom lighting model based off the default Lambert so I can use the Alpha component of SurfaceOutput to feed in the shadow coefficient and include that in the lighting calculation
- I have the following script attached to the light cam that constructs the lightVP matrix and sets it as a global shader property
void Start () {
texMatrix = Matrix4x4.identity;
texMatrix[0,0] = 0.5f;
texMatrix[1,1] = 0.5f;
texMatrix[2,2] = 0.5f;
texMatrix[0,3] = 0.5f;
texMatrix[1,3] = 0.5f;
texMatrix[2,3] = 0.5f;
}
void LateUpdate()
{
lightVPMatrix = texMatrix;
lightVPMatrix *= this.camera.projectionMatrix;
lightVPMatrix *= this.camera.worldToCameraMatrix;
Shader.SetGlobalMatrix("_LightVPMatrix", lightVPMatrix);
}
And here is the Cg portion of the terrain shader code (nothing outside of this section has been added to or changed):
CGPROGRAM
#pragma surface surf ShadowMapping vertex:vert
#include "UnityCG.cginc"
inline fixed4 LightingShadowMapping (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
fixed shadowCoeff = s.Alpha; //NEW
fixed diff = max (0, dot (s.Normal, lightDir));
fixed4 c;
c.rgb = s.Albedo * _LightColor0.rgb * shadowCoeff * (diff * atten * 2); //ALTERED
c.a = 1.0f;
return c;
}
struct Input {
float2 uv_Control : TEXCOORD0;
float2 uv_Splat0 : TEXCOORD1;
float2 uv_Splat1 : TEXCOORD2;
float2 uv_Splat2 : TEXCOORD3;
float2 uv_Splat3 : TEXCOORD4;
float4 projTex; //NEW
};
sampler2D _Control;
sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
sampler2D _ShadowMap; //NEW
float4x4 _LightVPMatrix; //NEW
void vert(inout appdata_full v, out Input o) {
float4x4 LightMVPMatrix = mul( _Object2World, _LightVPMatrix ); //NEW
o.projTex = mul( v.vertex, LightMVPMatrix ); //NEW
}
void surf (Input IN, inout SurfaceOutput o) {
float shadowCoeff = tex2Dproj(_ShadowMap, UNITY_PROJ_COORD(IN.projTex)).r; //NEW
half4 splat_control = tex2D (_Control, IN.uv_Control);
half3 col;
col = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
o.Albedo = col;
o.Alpha = shadowCoeff; //NEW
}
ENDCG
I’m not clueless in the area of shadow mapping but I’m a rookie when it comes to shaders. Any help would be greatly appreciated! Let me know if you need more info.
A couple sources I have been using:
Cg Tutorial: Projective Texturing and Shadow Mapping
Relevant Unity Forum Thread