calculate position of plane in front of camera?

Started by
4 comments, last by tonemgub 9 years, 7 months ago

My cameras fov is 45, near is 0.1.

I created a transparent plane with

aspect = screenWidth / screenHeight

hNear = 2 * Math.tan( 45 * 2 * Math.PI / 360 / 2 ) * 0.101
wNear = hNear * aspect

and set it's z position to -0.101, the plane stays always in front of the camera.

How can I calculate at which Y position I have to set the underwaterplane that only the part below water appears to be blue?

I believe I have to get the point at which the water plane ( I know the y position ) intersects the near clipping plane of the camera?

L2QCzR3.png

Advertisement

If your water plane is always horizontal, then to detect if a pixel is above or below it (and draw it with a blue tint) in the pixel shader, you could just check the current pixel's position against the water plane's Y position in screen coordinates. The pixel shader gives you the currently drawn pixel's position in screen coordinates, and to get the water plane's position in screen coordinates, you first transform it into NDCs, then into screen space. To get the NDCs, transform the v=(0, Y, 0, 1) vector through the view-projection matrix, then divide it by the resulting v.w component. To get the screen-space coordinates, first add 1.0 to the NDCs and divide them by 2, then multiply the results's x and y components with the screen's width and height to get the x and y screen position (or something like that :) ).

If you still want to use a screen-aligned polygon instead (and this might actually be better performance-wise), then the calculation to get the Y position where you must trim the screen-aligned polygon is almost the same. Your screen-aligned plane is currently defined in clip space (or "transformed") coordinates, whereas your water plane's position is in world coordinates, so you have to to get the Y position of the water into clip space as well. To do this, all you have to do is multiply v=(0, Y, 0, 1) through the view-projection matrix (no division by w is required here). The resulting Y value is where you must put the top of your screen-aligned polygon.

for one moment I thought it worked but I'm getting strange values, but I think i get what you are suggesting and it should work, basically with the view-projection matrix I can project my waterplane where I know Y from world space to clip space and then set the y value of the transparent plane so that it aligns with the waterplane..

Do I have to consider the z position of my transparent plane here ?

btw. is clip space the same as camera space ?


Do I have to consider the z position of my transparent plane here ?

Hm... I don't think so. But if you do, then you would probably have to use the same Z value as the position of your screen-aligned quad (your 0.101 value),


btw. is clip space the same as camera space ?

No. Camera space.is the same as world space, but transformed (through the view matrix) so that the camera is at the origin. Clip space is what you get after you also transform through the projection matrix.

j5CByNa.png

So just to be sure I should get this value which I can plugin as Y position for the transparent plane?

I think I'm missing something.

I can't tell if that's clipping going on, or if you're somehow also modifying the bottom vertices of your screen-aligned quad. You only have to modify the two top vertices' Y coordinates.

But it seems to me (and from your first post), that you're somehow modifying only the height of the polygon based on aspect ratio? Or, are you not using a screen-aligned quad - i.e., polygon, as I thought you were? Are you using a clipping plane for the transparent plane, or a screen-aligned polygon?

Also, when you plugin the y value, is your screen-aligned quad specified in transformed (camera space) coordinates, or in world coordinates?

Where's the water plane in this last screenshot?

This topic is closed to new replies.

Advertisement