- Home /
Question by
$$anonymous$$ · May 18, 2020 at 07:14 AM ·
cameratransitionmatrix
How calculated ViewportToWorldPoint
Hello
What does the ViewportToWorldPoint code look like? I tried to make one but it doesn't return that value...
public Vector3 ViewportToWorldPoint(Vector3 position)
{
Matrix4x4 P = Camera.main.projectionMatrix;
Matrix4x4 V = Camera.main.worldToCameraMatrix;
Matrix4x4 VP = P * V;
Vector4 projW = P * new Vector4(0.0f, 0.0f, position.z, 1.0f);
float x = 1.0f - (2.0f * position.x);
float y = 1.0f - (2.0f * position.y);
float z = projW.z / projW.w;
Vector4 worldPoint = VP.inverse * new Vector4(x, y, z, 1.0f);
worldPoint.x /= worldPoint.w;
worldPoint.y /= worldPoint.w;
worldPoint.z /= worldPoint.w;
return worldPoint;
}
Comment