Vertex world position

Started by
4 comments, last by -Tau- 11 years, 10 months ago
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?
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.
http://img.pictureup...e340aee4376.png dcaust==0
http://img.pictureup...da31ccdce40.png dcaust==0.05
http://img.pictureup...fbf4aaec9e7.png dcaust==0.2

About wPos, i am not sure if there is the correct y value, like i said i know only basics about HLSL, that's why i posted vertex shader code wPos = mul(iPos, g_mWorld);
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?
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.
Changing POSITION1 to TEXCOORD1 worked thanks a lotsmile.png

This topic is closed to new replies.

Advertisement