Jump to content
  • Advertisement
Sign in to follow this  
-Tau-

Vertex world position

This topic is 2270 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 all,
I am working on a submarine game. I have a caustic effect and i want the effect to fade with increasing depth. I don't know much about hlsl programming only some basics that allows me to draw basic scene with some effects.
I was thinking about using vertex * worldmatrix y value to get the height but it doesn't work how i want.

void VertScene( float4 iPos : POSITION,
float3 iNormal : NORMAL,
float2 iTex : TEXCOORD0,
out float4 wPos : POSITION1,

out float4 oPos : POSITION,
out float2 oTex : TEXCOORD0,
out float oCamDist : TEXCOORD4,

out float4 vPos : TEXCOORD1,
out float3 vNormal : TEXCOORD2,
out float4 vPosLight : TEXCOORD3,
out float2 caustCoord : TEXCOORD5)
{
wPos = mul( iPos, g_mWorld );
vPos = mul( wPos, g_mView );
oPos = mul( vPos, g_mProj );
oTex = iTex;
oCamDist=oPos.z;
vNormal = mul(iNormal, (float3x3)mul(g_mWorld, g_mView));
vPosLight = mul( vPos, g_mViewToLightProj );
float2x2 rotation = {COS_0_15F, -SIN_0_15F, SIN_0_15F, COS_0_15F};
caustCoord=mul(wPos.xz, rotation);
}

/////// Pixel shader
float2 movement = caustCoord.xy;
movement.x = movement.x + cos(g_fTime * 0.2f*20) * 0.3f;
movement.y = movement.y + sin(g_fTime * 0.3f*20) * 0.2f;
float4 cautictex = tex2D(CausticTextureSampler, movement.xy * 0.07f);
float dcaust=0.2*(1.0-saturate((400.5-wPos.y)/20.0));

float4 ps_LightAmbient = dcaust * cautictex+
0.2 * g_LightAmbient +
0.6 * g_LightAmbient * saturate( dot( -normalize( float3( vPos - g_sun_poz ) ), normalize( vNormal ) ) );


saturate((400.5-wPos.y)/20.0) doesn't seem to work, (400.5 is the water surface position, 20.0 is the max depth at witch caustic effect should be visible), when i use constant 0 - 0.2 it works well.
What am i doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Assuming that the water level is at y = 400.5 (where the water starts) you can define the max depth the caustics can reach (e.g. y = 380.5). Then you can calculate a linearly fade the effect like this:


m = 1 / (waterLevel - maDepth);

b = -maxDepth/m;

float dcaust = saturate ( (wPos.y*m) + b);


float dcaust = saturate( (0.05*wPos.y) - 19.025 );

or instead of linear attenuation try this:


float dcaust = saturate( (0.05*wPos.y) - 19.025 )
dcaust *= dcaust;



Anyway, are you sure wPos.y contains the correct value? Why do you say your function doesn't work? Some screenshots might help. Edited by TiagoCosta

Share this post


Link to post
Share on other sites
Well wPos.y is not what i needed.
I will ask this way: How do i get y value of a vertex in pixel shader?

Share this post


Link to post
Share on other sites
I'm not 100% familiar with SM 3.0 semantics, but instead of:
out float4 wPos : POSITION1

try:
out float4 wPos : TEXCOORD1

Dont forget to also change it in the pixel shader input

Because if you use POSITION[n] semantic as output of the vertex shader w-divide will be performed before rasterization. Edited by TiagoCosta

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!