Sign in to follow this  
mr_malee

Project Camera View onto plane through Matrix

Recommended Posts

Hi, I'm trying to project a 3D camera's view onto a plane. I currently have this working by:

 

1. Find normalized viewport position (-1 to 1)

2. Project this coordinate into 3d using the inverse of the cameras View and Projection matrices

3. Find the direction of this coordinate relative to the camera view

4. Find intersection of this direction onto plane.

 

I'm doing this inside a shader but wondering if I could simplify it by constructing some sort of matrix that finds the plane intersection for a normalized viewpoint.

Not sure if that's even possible. Here's a picture that might better explain:

 

projection.jpg

 

What I currently have:

float4 unproject(float x, float y, float z) 
{
	// unproject the point
	// _ProjMatrix is the inverse of a cameras view and projection matrices
	// _ProjMatrix = inverse(cam.projectionMatrix * cam.viewMatrix)

	float4 pos = mul(_ProjMatrix, float4(x, y, z, 1));

	return pos * (1.0 / pos.w);
}

v2f vert (appdata v)
{
	v2f o;

	//convert the mesh vertex into local viewport space

	float4 vp = v.vertex;
	float x = vp.x / _Size;
	float y = vp.z / _Size;

	//unproject into the near and far clipping planes of the camera

	float4 p1 = unproject(x, y, -1);
	float4 p2 = unproject(x, y, 1);

	//find the direction of this viewport point

	float4 dir = normalize(p2 - p1);

	//project this viewport direction onto the world plane
	
	float distanceOfVertex = mul(_WorldMatrix, p1.xyz).y;
	float lengthOfLightDirectionInY = mul(_WorldMatrix, dir.xyz).y; 

	dir = dir * (distanceOfVertex / -lengthOfLightDirectionInY);

	//output vertex

	o.vertex = mul(UNITY_MATRIX_VP, p1 + dir);
}

As you can see there's lots of multiplication here. First I have to find the near and far projections, find the normal between them, find the projection of that normal on the plane.

Wondering if this could be simplified if I could just take that viewport position and multiply by the camera projection and view + some plane intersection matrix.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this