Water Shader

Forum for plugin, effect and behavior programmers. Please use the Help & Support forum for help using Construct.

Post » Mon May 11, 2009 12:27 pm

Hi,

I have this water shader HLSL code:

[code:2191t6d9]struct WaterVertexToPixel

{

float4 Position : POSITION;

float4 ReflectionMapSamplingPos : TEXCOORD1;

float2 BumpMapSamplingPos : TEXCOORD2;

float4 RefractionMapSamplingPos : TEXCOORD3;

float4 Position3D : TEXCOORD4;

};

struct WaterPixelToFrame

{

float4 Color : COLOR0;

};

WaterVertexToPixel WaterVS(float4 inPos : POSITION, float2 inTex: TEXCOORD)

{

WaterVertexToPixel Output = (WaterVertexToPixel)0;

float44 preViewProjection = mul (xView, xProjection);

float44 preWorldViewProjection = mul (xWorld, preViewProjection);

float44 preReflectionViewProjection = mul (xReflectionView, xProjection);

float44 preWorldReflectionViewProjection = mul (xWorld, preReflectionViewProjection);

Output.Position = mul(inPos, preWorldViewProjection);

Output.ReflectionMapSamplingPos = mul(inPos, preWorldReflectionViewProjection);

Output.RefractionMapSamplingPos = mul(inPos, preWorldViewProjection);

Output.Position3D = inPos;

float4 absoluteTexCoords = float4(inTex, 0, 1);

float4 rotatedTexCoords = mul(absoluteTexCoords, xWindDirection);

float2 moveVector = float2(0, 1);

// moving the water

Output.BumpMapSamplingPos = rotatedTexCoords.xy/xWaveLength + xTime*xWindForce*moveVector.xy;

return Output;

}

WaterPixelToFrame WaterPS(WaterVertexToPixel PSIn)

{

WaterPixelToFrame Output = (WaterPixelToFrame)0;

float2 ProjectedTexCoords;

ProjectedTexCoords.x = PSIn.ReflectionMapSamplingPos.x/PSIn.ReflectionMapSamplingPos.w/2.0f + 0.5f;

ProjectedTexCoords.y = -PSIn.ReflectionMapSamplingPos.y/PSIn.ReflectionMapSamplingPos.w/2.0f + 0.5f;

// sampling the bump map

float4 bumpColor = tex2D(WaterBumpMapSampler, PSIn.BumpMapSamplingPos);

// perturbation of the color

float2 perturbation = xWaveHeight*(bumpColor.rg 0.5f);

// the final texture coordinates

float2 perturbatedTexCoords = ProjectedTexCoords + perturbation;

float4 reflectiveColor = tex2D(ReflectionSampler, perturbatedTexCoords);

float2 ProjectedRefrTexCoords;

ProjectedRefrTexCoords.x = PSIn.RefractionMapSamplingPos.x/PSIn.RefractionMapSamplingPos.w/2.0f + 0.5f;

ProjectedRefrTexCoords.y = -PSIn.RefractionMapSamplingPos.y/PSIn.RefractionMapSamplingPos.w/2.0f + 0.5f;

float2 perturbatedRefrTexCoords = ProjectedRefrTexCoords + perturbation;

float4 refractiveColor = tex2D(RefractionSampler, perturbatedRefrTexCoords);

float3 eyeVector = normalize(xCamPos PSIn.Position3D);

float3 normalVector = float3(0,0,1);

/////////////////////////////////////////////////

// FRESNEL TERM APPROXIMATION

/////////////////////////////////////////////////

float fresnelTerm = (float)0;

if ( fresnelMode == 0 )

{

fresnelTerm = 1-dot(eyeVector, normalVector)*1.3f;

} else

if ( fresnelMode == 1 )

{

fresnelTerm = 0.02+0.97f*pow((1-dot(eyeVector, normalVector)),5);

} else

if ( fresnelMode == 2 )

{

float fangle = 1.0f+dot(eyeVector, normalVector);

fangle = pow(fangle,5);

// fresnelTerm = fangle*50;

fresnelTerm = 1/fangle;

}

// fresnelTerm = (1/pow((fresnelTerm+1.0f),5))+0.2f; //

//Hardness factor user input

fresnelTerm = fresnelTerm * xDrawMode;

//just to be sure that the value is between 0 and 1;

fresnelTerm = fresnelTerm < 0? 0 : fresnelTerm;

fresnelTerm = fresnelTerm > 1? 1 : fresnelTerm;

// creating the combined color

float4 combinedColor = refractiveColor*(1-fresnelTerm) + reflectiveColor*(fresnelTerm);

/////////////////////////////////////////////////

// WATER COLORING

/////////////////////////////////////////////////

float4 dullColor = float4(0.1f, 0.1f, 0.2f, 1.0f);

float dullBlendFactor = xdullBlendFactor;

Output.Color = (dullBlendFactor*dullColor + (1-dullBlendFactor)*combinedColor);

/////////////////////////////////////////////////

// Specular Highlights

/////////////////////////////////////////////////

float4 speccolor;

float3 lightSourceDir = normalize(float3(0.1f,0.6f,0.5f));

float3 halfvec = normalize(eyeVector+lightSourceDir+float3(perturbation.x*specPerturb,perturbation.y*specPerturb,0));

float3 temp = 0;

temp.x = pow(dot(halfvec,normalVector),specPower);

speccolor = float4(0.98,0.97,0.7,0.6);

speccolor = speccolor*temp.x;

speccolor = float4(speccolor.x*speccolor.w,speccolor.y*speccolor.w,speccolor.z*speccolor.w,0);

Output.Color = Output.Color + speccolor;

return Output;

}

technique Water

{

pass Pass0

{

VertexShader = compile vs_2_0 WaterVS();

PixelShader = compile ps_2_0 WaterPS();

}

}[/code:2191t6d9]

And I would like to use it in Construct, but can someone convert this to make it work on Construct?

When I try to use it in Construct, it gives to me an error about something like a perspective projection..

Help, please?
B
2
S
1
G
3
Posts: 193
Reputation: 1,486

Post » Mon May 11, 2009 4:16 pm

Construct does not support vertex shaders, so I don't know how this can be converted. Where did you find the FX file?
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,468

Post » Mon May 11, 2009 7:04 pm

I understood.. Thanks.

I found it on a tutorial about shaders if I remember correctly..
B
2
S
1
G
3
Posts: 193
Reputation: 1,486

Post » Tue May 12, 2009 7:19 am

for water effects look at the reflections thread.

[url:18xw6hm4]http://www.scirra.com/phpBB3/viewtopic.php?f=2&t=3582&st=0&sk=t&sd=a[/url:18xw6hm4]
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Tue May 12, 2009 10:56 am

Thanks.. Now I try making my own top-down water reflection effect.
B
2
S
1
G
3
Posts: 193
Reputation: 1,486

Post » Wed May 13, 2009 1:24 am

top down shouldnt be too hard, you just need to have the sky and have a seperate character sprite of his underside on a layer below the ground, then use the erase effect over the ground layer for puddles or wtv, the result will look reflecty :P

oh yah throwing in some magnify ripple effects when you step would probably look cool too.
B
52
S
7
G
6
Posts: 1,945
Reputation: 7,610


Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 0 guests