Question by
smoore2171 · Apr 23 at 08:23 PM ·
lightingrendering
How do you calculate unity_WorldToLight/_LightMatrix0 for directional light in C#
I need to calculate the world-to-light matrix (the shader properties unity_WorldToLight/_LightMatrix0) for a light in C#. There is no accessor on the light for this, and it is only passed into shaders. I figure it is just an ortho projection matrix for the light, but my math is failing me. Anyone know how to calculate this?
Comment
Answer by smoore2171 · Apr 24 at 03:36 PM
Matrix4x4 modelView = Matrix4x4.LookAt(m_Light.transform.position, m_Light.transform.position + m_Light.transform.forward, m_Light.transform.up);
float cookieSize = m_WorldSize;
Matrix4x4 newModel = new Matrix4x4();
Matrix4x4.Inverse3DAffine(modelView, ref newModel);
Matrix4x4 worldToLight = GL.GetGPUProjectionMatrix(Matrix4x4.Ortho(-cookieSize, cookieSize, -cookieSize, cookieSize, 0.1f, cookieSize), false) * newModel;
Is close (scale/scroll/shear/etc is all correct), but the texture is offset from where is compared to the standard unity version...
Your answer
![](https://koobas.hobune.stream/wayback/20220613055733im_/https://answers.unity.com/themes/thub/images/avi.jpg)