• Advertisement
Sign in to follow this  

Pixel shader through a 3D quad

This topic is 2764 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

hey all,

my goal is to stretch a "darker" glass (or anything else, I know it could be done by texture colors, but this must be pixelshader, later I want to do more complex effects) to a texture quad. Just like a darker glass on a car, or sunglasses. Sadly the result now is this:



I must admit it's not a wonder. I pass the whole background texture, so it works as it should. But I want it to consider the camera position, and do the effect on that picture I would normally see through that "window". Im using DX8, PS1.4, but any idea is accepted.

Share this post


Link to post
Share on other sites
Advertisement
In your vertex shader, calculate the quad's texture coordinates from the projected xy position of the vertices instead of passing the quad's original texture coordinates through.

In a typical projection setup, the clip space (essentially, the logical bounds of the render target) range is -1...1 for both x and y. So scale and bias them to 0...1 to get the necessary texture coordinates.

Share this post


Link to post
Share on other sites
Look up 'projective texturing' -- you can transform your vertex-positions with the world-view-projection to generate appropriate texture coordinates (i.e. the vertices position on the screen).

Share this post


Link to post
Share on other sites
I see, and can it all be shown by some concrete code? :-)

Share this post


Link to post
Share on other sites
Are you using vertex shaders, or fixed-function vertex processing?

In your vertex shader, it'd be something like
OUT.Position = /*transform IN.Position*/;
OUT.TexCoord = OUT.Position*0.5+0.5;

Share this post


Link to post
Share on other sites
...whereas in fixed vertex pipeline you would have to write a horrible hackish mess of SetTextureStageState and SetTransform calls to achieve the same simple result :)

Share this post


Link to post
Share on other sites
We are getting to the end. This topic http://www.gamedev.net/community/forums/topic.asp?topic_id=391846 wants something like that, in it was succeed for him. But I still have problems:

description of your image

it only works from a point of view. The left picture is what I want. But from another camera angle, it works bad :S

My shaders:



sampler2D input0 : register(s0);
float4 PSmain(float4 uv : TEXCOORD0, float4 uv2 : TEXCOORD1) : COLOR
{
float4 Color = tex2D(input1, uv2.xy);
Color = tex2D(input0, uv.xy);
Color.r = Color.r / 2; Color.b = Color.b / 2; Color.g = Color.g / 2;
return Color;
}


matrix World : WORLD;
matrix Projection : PROJECTION;
matrix View : VIEW;
struct VS_OUTPUT
{
float4 Position : POSITION0;
float4 VertexColor : COLOR0;
float4 Tex0 : TEXCOORD0;
};
VS_OUTPUT main(float4 pos : POSITION0, float4 color_in : COLOR0, float4 tex0_in: TEXCOORD0)
{
VS_OUTPUT outVS = (VS_OUTPUT)0;
matrix mat = mul(World, View);
mat = mul(mat, Projection);
outVS.Position = mul(pos, mat);
outVS.VertexColor = color_in;
outVS.Tex0.x = 0.5 * (outVS.Position.w + outVS.Position.x);
outVS.Tex0.y = 0.5 * (outVS.Position.w - outVS.Position.y);
outVS.Tex0.z = outVS.Position.w;
return outVS;
}

Share this post


Link to post
Share on other sites
Alright I made it work :) I dont know why I saw different "projection texturing" methods, these shaders works:

Vertex:

matrix World : WORLD;
matrix Projection : PROJECTION;
matrix View : VIEW;
matrix TexTransform;
struct VS_OUTPUT
{
float4 Position : POSITION0;
float4 VertexColor : COLOR0;
float4 Tex0 : TEXCOORD0;
};
VS_OUTPUT main(float4 pos : POSITION0, float4 color_in : COLOR0, float4 tex0_in: TEXCOORD0)
{
VS_OUTPUT outVS = (VS_OUTPUT)0;
matrix mat = mul(World, View);
mat = mul(mat, Projection);
outVS.Position = mul(pos, mat);
outVS.VertexColor = color_in;
outVS.Tex0 = mul(pos, mat);
return outVS;
';



Pixel:

sampler2D input0 : register(s0);
sampler2D input1 : register(s1);
float4 main(float4 uv : TEXCOORD0, float4 uv2 : TEXCOORD1) : COLOR
{
float4 Color2 = tex2D(input1, uv2);
float4 Color = uv;
Color.x = ((Color.x / Color.w) * 0.5 ) + 0.5;
Color.y = ((Color.y / Color.w) * -0.5 ) + 0.5;
Color.z = 1; Color.w = 1;
uv2.x = uv2.x + Color.r;
uv2.y = uv2.y + Color.g;
uv2.x = uv2.x + (sin(uv2.x*200)*0.01);
Color = tex2D(input0, uv2);
Color.r = Color.r / 2;Color.g = Color.g / 2;Color.b = Color.b / 2;
return Color;
}



this is good now, this time I want to apply "waves" to a "sea surface". This is the result:

description of your image

now lets suppose its a water surface and the waves, and we are watching it a very low fow (if we were ducking at the shore). The "nearer" waves (circle A) looks normal, but the "farther" waves (circle B) should be smaller, because they are far. How could I lean the effect itself? Everybody get me?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement