Pixel to Application-specified Coordante System for Unity

Hi There,
I found an example at https://docs.microsoft.com/en-us/windows/mixed-reality/locatable-camera where you convert 2D Pixel coordinates into 3D positions. How would you do this in unity (see code below)? Thank you very much in advance?

float2 ImagePosZeroToOne = float2( PixelPos.x / ImageWidth, 1.0 - (PixelPos.y / ImageHeight ) );
float2 ImagePosProjected = ( ( ImagePosZeroToOne * 2.0 ) - float2(1,1) ); // -1 to 1 space
float3 CameraSpacePos = UnProjectVector( Projection, float3( ImagePosProjected, 1) );
float3 WorldSpaceRayPoint1 = mul( CameraToWorld, float4(0,0,0,1) ); // camera location in world space
float3 WorldSpaceRayPoint2 = mul( CameraToWorld, CameraSpacePos ); // ray point in world space

public static Vector3 UnProjectVector(Matrix4x4 proj, Vector3 to)
{
Vector3 from = new Vector3(0, 0, 0);
var axsX = proj.GetRow(0);
var axsY = proj.GetRow(1);
var axsZ = proj.GetRow(2);
from.z = to.z / axsZ.z;
from.y = (to.y - (from.z * axsY.z)) / axsY.y;
from.x = (to.x - (from.z * axsX.z)) / axsX.x;
return from;
}

Sign In or Register to comment.