Jump to content
  • Advertisement
Sign in to follow this  

Project Camera View onto plane through Matrix

This topic is 750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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:




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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!