Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Planar projection shader

Started by Black Knight Feb 26, 2013 at 1:03 AM 1 replies 3.6k views
Original Post
Black Knight
Black Knight

I am rendering a scene to a reflection texture and then drawing a water plane with that texture using projection. Most of the samples I have seen on the web pass in a view-projection matrix to the vertex shader and transform the vertex, then in the pixel shader they do :


projection.x = input.projectionCoords.x / input.projectionCoords.w / 2.0 + 0.5;
projection.y = input.projectionCoords.y / input.projectionCoords.w / 2.0 + 0.5;
 

I have a shader that does the following and it works too but I don't know where I found this particular code or how it gives the same results with the above code. Here is the code I have

Vertex shader : (world is an identityMatrix, viewProj is my cameras combined viewProjection matrix:


output.position3D =  mul(float4(vsin.position,1.0), world).xyz;
output.position = mul(float4(output.position3D,1.0), viewProj);

output.reflectionTexCoord.x = 0.5 * (output.position.w + output.position.x);
output.reflectionTexCoord.y = 0.5 * (output.position.w - output.position.y);
output.reflectionTexCoord.z = output.position.w;

Pixel shader :


float2 projectedTexCoord = (input.reflectionTexCoord.xy / input.reflectionTexCoord.z);
 

What confuses me is the usage of "0.5 * (output.position.w + output.position.x)" and "0.5 * (output.position.w - output.position.y)". How does this have the same effect and what does the w component mean here?

Black Knight
Black Knight

After a while I realized that it ends up being the same thing :


0.5 * (output.position.w + output.position.x);

0.5 * output.position.w + 0.5 * output.position.x
 

Then in the pixel shader :


(0.5 * output.position.w + 0.5 * output.position.x) / output.position.w

(0.5 * output.position.w)/output.position.w + (0.5 * output.position.x) / output.position.w
 

The first part becomes 0.5 :


0.5 + 0.5 * ( output.position.x / output.position.w)
 

This is equal to :


(output.position.x / output.position.w) / 2 + 0.5
 

I believe moving this calculation to the vertex shader is more efficient so I will just leave it there.

Styves
Styves

I've always found doing it this way a bit redundant because you can save yourself the trouble of messing around to get it right (and also avoiding the need to compute it per pixel or per vertex) by multiplying your matrix by a scale/bias matrix.

Something like this (pseudo code, might not work out of the box - some cases you'll need to flip the Y scale (-0.5) for the right projection, but that's easy to figure out):


Matrix44 ScaleBias = Matrix44(0.5, 0.0, 0.0, 0.0,
                              0.0, 0.5, 0.0, 0.0,
                              0.0, 0.0, 1.0, 0.0,
                              0.5, 0.5, 0.0, 1.0);

ViewProj = View * Proj * ScaleBias;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.