Sign in to follow this  
Black Knight

Planar projection shader

Recommended Posts

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?


Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

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;

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