- Home /
Normal from _CameraDepthNormals to screen space angle
Considering the normals from the depth-normals texture are in view space, I thought it'd be as simple as transforming it by the projection matrix.
float4 n = mul(UNITY_MATRIX_P, float4(normal, 1.0));
n.xz /= n.w;
float theta = atan2(n.z, n.x);
This doesn't seem to be the case, can anyone guide me in the correct direction?
Awhile after tinkering with it I've come to this as suggested by: http://stackoverflow.com/questions/724219/how-to-convert-a-3d-point-into-2d-perspective-projection
Yet it still yields improper results. I've multiplied by the inverse transpose of the model-view matrix, multiplied by the projection matrix, executed a perspective divide, and thereafter converted the coordinates into the [-1,1] range. Is there something I'm missing?
float4 n = mul(UNITY_$$anonymous$$ATRIX_IT_$$anonymous$$V, float4(normal, 0.0));
n = mul(UNITY_$$anonymous$$ATRIX_P, n);
n.xyz /= n.w;
n.x = (n.x * _ScreenParams.x) / (2.0 * n.w) + (0.5 * _ScreenParams.x);
n.y = (n.y * _ScreenParams.y) / (2.0 * n.w) + (0.5 * _ScreenParams.y);
float theta = atan2(n.y, n.x);
Answer by Nabbz0rz · Dec 04, 2014 at 08:43 PM
It looks like I was putting too much thought into it. Here's the solution:
float3 n;
n = mul((float3x3)UNITY_MATRIX_IT_MV, normal);
n = mul((float3x3)UNITY_MATRIX_P, n);
float theta = atan2(n.y, n.x);