So, I’ve been reading this article ( section 9.4 ) and wanted to create the shadow maps manually. I know that unity already gives you a lot of things to work with shadows, but I want to know exactly how it works behind the scenes, so then it is easier to understand how to use shadows with unity later.

So, what I needed was the view and projection matrix of a light to render the scene from the light’s view point( in this case I’m only using a directional light).

I thought I could use the _LightMatrix0 variable since it seemed to have the matrices I needed ( though their description doesn’t mention exactly what it does…) so I did something like this:

``````v2f vert (appdata v)
{
v2f o;
o.vertex = mul(__LightMatrix0, mul(_Object2World, v.vertex));
}

fixed4 frag (v2f i) : SV_Target
{
return fixed4(1, 1, 1, 1); // just to see if something would come out
}
``````

And nedless to say, It didn’t work. Then I thought that I could provide the view and projection matrices myself… but I couldn’t find how to create them ( best I could find was Matrix4x4.Perspective ). So then I thought I could just put a camera with the same position and direction that my light… but it didn’t seem “right”, so I decided to post a thread here and see if someone else could give me a better explanation of how to do this.

Also I tried doing something like this:

``````v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = mul(_World2Light, mul(_Object2World, v.vertex)); // o.uv is a float4
return o;
}

fixed4 frag (v2f i) : SV_Target
{
float depth = Linear01Depth(i.uv.z);
return fixed4(depth, depth, depth,1);

}
``````

But again it was all black.
So in short, I need a way to render the scene from the light’s point of view, preferably without using an extra camera. Any help would be appreciated, thanks.

So… I finally managed to do it ( render from light’s point of view), following this . I’ll post below the function that I used to create the view matrix since it was very difficult to find a nice explanation of how to do it:

``````Matrix4x4 getViewMatrix(Vector3 eye, Vector3 direction, Vector3 up)
{
Vector3 axis_Z = direction * -1 ;
Vector3 axis_X = Vector3.Cross(direction, up) / Vector3.Cross(direction, up).magnitude;
Vector3 axis_Y = Vector3.Cross(axis_Z, axis_X);
Matrix4x4 view_world = new Matrix4x4();
view_world.SetColumn(0, new Vector4(axis_X.x, axis_X.y, axis_X.z,   0));
view_world.SetColumn(1, new Vector4(axis_Y.x, axis_Y.y, axis_Y.z  , 0 ));
view_world.SetColumn(2, new Vector4(axis_Z.x, axis_Z.y, axis_Z.z  , 0 ));
view_world.SetColumn(3, new Vector4(eye.x   , eye.y   , eye.z     , 1 ));
return view_world.inverse;
}
``````

And then, to use it just do something like this:

``````getViewMatrix(light.transform.position, (light.transform.rotation * Vector3.forward).normalized, Vector3.up * -1);
``````

I still don’t understand why do I have to send the Vector3.up *- 1, but if I don’t then it will show up upside down… Probably I just misunderstood some step in the guide
Btw for this I was using just a directional light. Hopefully this will help someone, and I would greatly appreciate if somebody could point out what did I do wrong ( since I have to send the negative Up vector ). Cheers