I’m looking for a way to project a world-position on a specific “view-depth” but keeping it’s screen-space position the same.
I have this working using Unity’s Camera functions, but I’m looking for a way to do this with matrix/vector math only.
The code that works looks like this:
var camera = Camera.main;
var screenPos = camera.WorldToScreenPoint(worldPos);
var newWorld = camera.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, 100));
worldPos is the input position to transform and newWorld is the result of the operation.
Now I want to do this without using Camera.WorldToScreenPoint and Camera.ScreenToWorldPoint.
I thought I could get the camera matrices, concatenate them somehow and then send my many positions through them to do the transformations. But all my attempts at it failed so far.
The nearest thing I can think of is taking the camera and target positions and use a ray distance to “project” the target position closer or further away from the camera. The way you describe it I read it as “move object closer to camera” (or further away).
It may not be perfectly accurate as the camera position is slightly behind the view plane, but if you know the distance of the view plane from the camera position you could use a “ray/vector intersects plane” test.
Yes, you could think of it as “move object closer to camera, but keep the world position at the same screen coordinate”. If it’s one pixel off, that’s not an issue. How would the code for it look like?
Because it should be more efficient to perform matrix/vector calculations instead of making thousands of calls to Camera.WorldToScreenPoint and Camera.ScreenToWorldPoint which probably reach out into native-land. Ideally, I could even optimize the “transform positions” loop by incorporating it into a burst job, but that’s for later, once I get it working.