Download

Download page is moved to the Wiki site:

www.habib.wikidot.com

 

PDF version of the document

PDF document – last update: 20-05-2008


Shader Code of Lake Water Demo Application

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;

float4x4 preViewProjection = mul (xView, xProjection);

float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);

float4x4 preReflectionViewProjection = mul (xReflectionView, xProjection);

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

}

}


Shader code of Ocean Water Demo Application

////// STRUCTURES

struct vs_in

{

float4 position : POSITION;

};

struct vs_out

{

float4 position : POSITION;

float2 texCoord0 : TEXCOORD0;

float3 norm : TEXCOORD1;

float4 RefractionMapSamplingPos : TEXCOORD2;

float4 phase : TEXCOORD3;

float3 eyeVector : TEXCOORD4;

float3 view : TEXCOORD5;

float4 Position3D : TEXCOORD6;

};

/*********************************************

Vertex SHADER

**********************************************/

vs_out WaterVS(vs_in IN)

{

// variables

vs_out OUT;

float4 pPos = mul(IN.position, World);

//calculating the correction

float4 crrectionPhase = (WaveDirX * pPos.x + WaveDirY * pPos.y) * SpaceFreq + 10 * Time * TimeFreq;

float4 cCos,cSin;

sincos(crrectionPhase,cSin,cCos);

float correctionHeight = dot(cSin,Amplitudes/2);

//radial waves

float distance = length(float2(pPos.x-64*xIslandScale,pPos.y-64*xIslandScale));

float Phase = distance * -rSpaceFreq + Time * 2 * -TimeFreq;

//Calculating waveheight

float Cos,Sin;

int power = 7;

sincos(Phase,Sin,Cos);

float temp2 = 1;

if (Cos*Sin < 0) temp2 = 2;

float WaveHeight = clamp(pow(Sin,power)*rAmplitudes*temp2-(rAmplitudes*(temp2-1)),0,rAmplitudes) + (xIslandScale*128-distance)/xWaterSlope/2+xWaterLevel + correctionHeight;

float4 newPos = IN.position + float4(0,0,WaveHeight,0);

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

OUT.position = mul(newPos,WorldViewProj);

float3 worldPos = mul(IN.position, World);

OUT.texCoord0 = float2(worldPos.x/10, worldPos.y/10)+float2(-0.01f,0.04f)*Time;

OUT.view = normalize(worldPos – EyePos.xyz);

float temp = 0;

if (Sin > 0) temp = power*Cos*pow(Sin,power-1);

float4 CosWaveHeights = (temp+dot(cSin,Amplitudes/2)) * Amplitudes * SpaceFreq;

OUT.norm = normalize(cross( float3(0,1,0), float3(1,0,0)));

OUT.RefractionMapSamplingPos = mul(newPos, WorldViewProj);

OUT.phase =Sin*Sin/2+0.5;

OUT.eyeVector = normalize(EyePos-newPos);

OUT.Position3D = pPos;

return OUT;

}

/*********************************************

PIXEL SHADER

**********************************************/

float4 WaterPS(vs_out IN) : COLOR

{

float3 normal = tex2D(BumpMapSampler,IN.texCoord0.xy)* 2.0 – 1.0;

float3 newNormal = normalize(normalize(IN.norm) + normal);

//lighting factor computation

float3 LightDirection = normalize(float3(-13,-2,4));

float lightingFactor = saturate(saturate(dot(normal, LightDirection)) + xAmbient); //newNormal volt

/*********************************************

// REFLECTIVE AND REFRACTIVE COLORS

**********************************************/

float2 RefractionSampleTexCoords;

RefractionSampleTexCoords.x = IN.RefractionMapSamplingPos.x/IN.RefractionMapSamplingPos.w/2.0f + 0.5f;

RefractionSampleTexCoords.y = -IN.RefractionMapSamplingPos.y/IN.RefractionMapSamplingPos.w/2.0f + 0.5f;

float4 refractiveColor = tex2D(RefractionTextureSampler, RefractionSampleTexCoords-newNormal/40);

float4 reflectiveColor = tex2D(ReflectionTextureSampler, RefractionSampleTexCoords+newNormal/40);

float phase = (rAmplitudes-clamp(IN.phase,0,rAmplitudes));

float fresnelTerm = saturate(length(EyePos.xy-IN.Position3D.xy)/xFresnelDistance)+0.0000001;

float3 finalColor = reflectiveColor*fresnelTerm+ refractiveColor * (1-fresnelTerm);

/*********************************************

Adding dull color

**********************************************/

float3 dullColor = float3(0.1,0.25,0.5);

xDullFactor = clamp(xDullFactor* saturate(length(EyePos.xy-IN.Position3D.xy)/100)+IN.phase*IN.phase/2,0,1);

finalColor= finalColor * (1.0-xDullFactor) + dullColor*xDullFactor;

/*********************************************

Specular lights

**********************************************/

float3 lightDir = normalize(float3(10.84,-12.99,3));

float3 eyeVector = normalize(EyePos.xyz – IN.Position3D.xyz);

float3 halfVector = normalize(lightDir + eyeVector);

float temp = 0;

temp = pow(dot(halfVector,normalize(IN.norm+normal/1.5)),200);

float3 specColor = float3(0.98,0.97,0.7)*temp;

finalColor = finalColor*lightingFactor+specColor;

return float4(finalColor, 1.0f);

}

///////////////// TECHNIQUE DEFINITION

technique Water

{

pass P0

{

VertexShader = compile vs_2_0 WaterVS();

PixelShader = compile ps_2_0 WaterPS();

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

2 Responses to “Download”

RSS Feed for Habib's Water Shaders Comments RSS Feed

The source code of the Ocean Water Demo application can be downloaded from:
http://rapidshare.com/files/125960765/ocean_water.rar.html

It used DirectX and not XNA.

Well, that’s not quite the complete shader, andsome XNA code showing the usage would be helpful as well. However, the articles you have posted here are EXCELLENT. Looking forward to seeing more!


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: