So I’m trying to convert the GLSL shader at GLSL Programming/Unity/Shadows on Planes - Wikibooks, open books for an open world to CG, but something’s going wrong and I don’t even know how to start debugging it. It compiles, but I never see anything, so I’m not really sure where the vertices are going or what’s wrong. My code is as follows. I’ve got a script in the scene setting _World2Receiver to the ground plane’s worldToLocalMatrix.
Shader “PlaneShadow” {
Properties {
_Color (“Shadow’s Color”, Color) = (0,0,0,1)
}
SubShader {
Pass {
Tags { “LightMode” = “ForwardBase” }
Offset -1.0, -2.0
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include “UnityCG.cginc”
float4 _Color;
float4x4 _World2Receiver;
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert (appdata_base v)
{
// Here’s the beef
v2f o;
float4x4 modelMatrix = _Object2World;
float4x4 modelMatrixInverse = _World2Object;
modelMatrixInverse[3][3] = 1.0;
float4x4 viewMatrix = UNITY_MATRIX_MV * modelMatrixInverse;
float4 lightDirection;
lightDirection = -normalize(_WorldSpaceLightPos0); // assuming a directional light
float4 vertexInWorldSpace = mul(modelMatrix, v.vertex);
float distanceOfVertex =
(mul(_World2Receiver, vertexInWorldSpace)).y; // height over plane
float lengthOfLightDirectionInY =
(mul(_World2Receiver, lightDirection)).y; // length in y direction
lightDirection = lightDirection * (distanceOfVertex / (-lengthOfLightDirectionInY));
o.pos = mul(UNITY_MATRIX_P, mul(viewMatrix, vertexInWorldSpace + lightDirection));
return o;
// That was the beef.
}
half4 frag (v2f i) : COLOR
{
return _Color;
}
ENDCG
}
}
FallBack “Diffuse”
}