• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Vertex world position

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1-Tau-  Members

Posted 01 June 2012 - 03:53 AM

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);
}

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?

### #2TiagoCosta  Members

Posted 01 June 2012 - 04:21 AM

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, 01 June 2012 - 04:47 AM.

### #3-Tau-  Members

Posted 01 June 2012 - 05:18 AM

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);

### #4-Tau-  Members

Posted 02 June 2012 - 03:51 AM

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?

### #5TiagoCosta  Members

Posted 02 June 2012 - 07:28 AM

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, 02 June 2012 - 07:33 AM.

### #6-Tau-  Members

Posted 02 June 2012 - 07:54 AM

Changing POSITION1 to TEXCOORD1 worked thanks a lot

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.