Hi, so I’m trying to project a screen point into the world by using matrices rather than unity’s built in functions.

The reason… I would like to project a plane mesh (with normalized vertices) in a shader from the viewpoint of a specific camera (not the current rendering camera).

Anyway, here’s what I have:

```
void OnDrawGizmos() {
if (cam) {
//get matrices
Matrix4x4 V = cam.worldToCameraMatrix;
Matrix4x4 P = cam.projectionMatrix;
Matrix4x4 PV = P * V;
Matrix4x4 PVI = PV.inverse;
//unproject some viewport points
int count = 5;
for (int y = 0; y < count; y++) {
for (int x = 0; x < count; x++) {
float x1 = Mathf.InverseLerp(0, count, x) * 2 - 1;
float x2 = Mathf.InverseLerp(0, count, x + 1) * 2 - 1;
float y1 = Mathf.InverseLerp(0, count, y) * 2 - 1;
float y2 = Mathf.InverseLerp(0, count, y + 1) * 2 - 1;
Draw2DTo3D(x1, y1, x2, y2, PVI);
}
}
}
}
private void Draw2DTo3D(float x1, float y1, float x2, float y2, Matrix4x4 PVI) {
Vector3 v1 = UnProject(x1, y1, PVI);
Vector3 v2 = UnProject(x1, y2, PVI);
Vector3 v3 = UnProject(x2, y2, PVI);
Vector3 v4 = UnProject(x2, y1, PVI);
Gizmos.color = Color.red;
Gizmos.DrawLine(v1, v2);
Gizmos.DrawLine(v2, v3);
Gizmos.DrawLine(v3, v4);
Gizmos.DrawLine(v4, v1);
}
private Vector3 UnProject(float x, float y, Matrix4x4 PVI) {
//how far along the cameras clipping planes (-1 = near, 1 = far, 0 = center)
float z = dist * 2 - 1;
//normalized screen point
Vector4 v = new Vector4(x, y, z, 1f);
//unproject the vector
v = PVI * v;
//perspective divide
float w = 1f / v.w;
v.x *= w;
v.y *= w;
v.z *= w;
return v;
}
```

From what I can tell that seems right? And it does kinda work for z values (0 and 1). But anything in between is not right. Eg:

Clearly when z = 0.5, that is not the center of the camera frustum.

I’m really stuck at this point. Maybe there’s another step involved?