- Home /
_WorldSpaceCameraPos and converting to object space
I'm writing a shader from scratch to try and understand all the underlying math, but this is stumping me; shouldn't converting the _WorldCameraPos to object space produce the same result as converting the vertex to world space?
o.ps = mul((float3x3)_World2Object, _WorldSpaceCameraPos - mul(_Object2World, v.vertex).xyz);
o.ps = mul((float3x3)_World2Object, _WorldSpaceCameraPos) - v.vertex.xyz;
Answer by tanoshimi · Jul 11, 2014 at 06:41 PM
I don't think World2Object and Object2World are inverses (i.e. mul(_Object2World, _World2Object) is not the identity matrix), and this is suggested at http://answers.unity3d.com/questions/282867/unity-shader-worldobject-matrix-1.html
To get the inverse of Object2World you need to multiply World2Object by unity_Scale.w, except for the bottom-right element. Try using this:
float4x4 obj2WorldInverse = _World2Object * unity_Scale.w;
obj2WorldInverse[3][3] = 1.0;
Aaah, you're right! I don't know how I didn't think to check whether they were actually inverses! Fixed my transform operations, thank you!