RealisicWater-HLSL
extern uniform float4 g_vec4Velocity;
extern uniform float2 g_vec2Dimension;
extern uniform float3 g_vec3CenterCoord;
extern uniform float3 g_vec3ViewCoord;
extern uniform float3 g_vec3LightCoord;
extern uniform float4x4 g_mat4ViewProj;
extern uniform float4x4 g_mat4ProjTexture;
// @desc: Tangent space matrix.
const float3x3 g_mat3Tangent = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f };
// @dec: The import of vertex shader.
struct VS_INPUT
{
float2 m_vec2TexCoord : TEXCOORD0;
float2 m_vec2VertexCoord : POSITION;
};
// @desc: The export of vertex shader.
struct VS_OUTPUT
{
float4 m_vec4ClipCoord : POSITION;
float2 m_vec2ProjTexCoord : TEXCOORD0;
float4 m_vec4NormalTexCoord : TEXCOORD1;
float3 m_vec3WorldViewDirection : TEXCOORD2;
float3 m_vec3TangentViewDirection : TEXCOORD3;
float3 m_vec3TangentLightDirection : TEXCOORD4;
};
// @desc: The entry of vertex shader.
VS_OUTPUT VS_Main(const VS_INPUT In)
{
// Calculate view direction and light direction in tangnent space.
VS_OUTPUT Out;
const float4 vec4VertexCoord = { In.m_vec2VertexCoord.x*g_vec2Dimension.x + g_vec3CenterCoord.x,
g_vec3CenterCoord.y,
In.m_vec2VertexCoord.y*g_vec2Dimension.y + g_vec3CenterCoord.z, 1.0f };
Out.m_vec3WorldViewDirection = g_vec3ViewCoord - vec4VertexCoord.xyz;
Out.m_vec3TangentViewDirection = mul(g_mat3Tangent, Out.m_vec3WorldViewDirection);
Out.m_vec3TangentLightDirection = mul(g_mat3Tangent, g_vec3LightCoord - vec4VertexCoord.xyz);
// Calculate projected texture coordinate and normal texture coordinate.
const float4 vec4ProjTexCoord = mul(vec4VertexCoord, g_mat4ProjTexture);
Out.m_vec2ProjTexCoord = vec4ProjTexCoord.xy/vec4ProjTexCoord.w;
Out.m_vec4NormalTexCoord = float4(In.m_vec2TexCoord + g_vec4Velocity.xy, In.m_vec2TexCoord + g_vec4Velocity.zw);
// Calculate the coordinate of vertex in clipping space.
Out.m_vec4ClipCoord = mul(vec4VertexCoord, g_mat4ViewProj);
return Out;
}
extern uniform float3 g_vec3Irradiance;
extern uniform float3 g_vec3Diffuse;
extern uniform float3 g_vec3Specular;
extern uniform float g_fSmoothness;
extern uniform float g_fPerturbation;
extern uniform float2 g_vec2FrenelPara;
extern uniform Texture2D g_LowFrequencyNormalMap;
sampler2D g_LowFrequencyNormalSampler = sampler_state
{
Texture = <g_LowFrequencyNormalMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
extern uniform Texture2D g_HighFrequencyNormalMap;
sampler2D g_HighFrequencyNormalSampler = sampler_state
{
Texture = <g_HighFrequencyNormalMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
extern uniform Texture2D g_ReflectMap;
sampler2D g_ReflectSampler = sampler_state
{
Texture = <g_ReflectMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
extern uniform Texture2D g_RefractMap;
sampler2D g_RefractSampler = sampler_state
{
Texture = <g_RefractMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
// @desc: The original surface normal.
const float3 g_vec3OriginNormal = { 0.0f, 1.0f, 0.0f };
// @desc: The import of pixel shader.
struct PS_INPUT
{
float2 m_vec2ProjTexCoord : TEXCOORD0;
float4 m_vec4NormalTexCoord : TEXCOORD1;
float3 m_vec3WorldViewDirection : TEXCOORD2;
float3 m_vec3TangentViewDirection : TEXCOORD3;
float3 m_vec3TangentLightDirection : TEXCOORD4;
};
// @desc: The entry of pixel shader.
float4 PS_Main(const PS_INPUT In) : COLOR
{
// Calculate synthetical normal.
const float3 vec3LowFrequencyNormal = (tex2D(g_LowFrequencyNormalSampler, In.m_vec4NormalTexCoord.xy).xyz - 0.5f)*2.0f;
const float3 vec3HighFrequencyNormal = (tex2D(g_HighFrequencyNormalSampler, In.m_vec4NormalTexCoord.zw).xyz - 0.5f)*2.0f;
const float3 vec3SynthNormal = normalize(vec3LowFrequencyNormal + vec3HighFrequencyNormal);
// Calculate diffuse gene.
const float3 vec3TangentLightDirection = normalize(In.m_vec3TangentLightDirection);
const float fDiffuseGene = max(dot(vec3SynthNormal, vec3TangentLightDirection), 0.0f);
// Calculate specular gene.
float fSpecularGene = 0.0f;
if (fDiffuseGene > 0.0f)
{
const float3 vec3TangentViewDirection = normalize(In.m_vec3TangentViewDirection);
const float3 vec3Reflection = reflect(-vec3TangentLightDirection, vec3SynthNormal);
fSpecularGene = pow(max(dot(vec3Reflection, vec3TangentViewDirection), 0.0f), g_fSmoothness);
}
// Fectch the sample of reflect texture and refract texture.
const float2 vec2DistortProjTexCoord = In.m_vec2ProjTexCoord + g_fPerturbation*vec3SynthNormal.xy;
const float3 vec3Reflect = tex2D(g_ReflectSampler, vec2DistortProjTexCoord).xyz;
const float3 vec3Refract = tex2D(g_RefractSampler, vec2DistortProjTexCoord).xyz;
// Calculate fresnel law.
const float fViewGene = dot(normalize(In.m_vec3WorldViewDirection), g_vec3OriginNormal);
const float fFresnelGene = g_vec2FrenelPara.x + g_vec2FrenelPara.y*pow(1.0f - fViewGene, 5.0f);
const float3 vec3Fresnel = lerp(vec3Refract, vec3Reflect, fFresnelGene);
// Calculate the exitance of light.
const float3 vec3Exitance = (fDiffuseGene*g_vec3Diffuse*vec3Fresnel + fSpecularGene*g_vec3Specular)*g_vec3Irradiance;
return float4(vec3Exitance, 1.0f);
}
// @desc: Technique and pass.
technique FluidVision
{
pass P0
{
VertexShader = compile vs_1_0 VS_Main();
PixelShader = compile ps_2_0 PS_Main();
}
}

extern uniform float2 g_vec2Dimension;
extern uniform float3 g_vec3CenterCoord;
extern uniform float3 g_vec3ViewCoord;
extern uniform float3 g_vec3LightCoord;
extern uniform float4x4 g_mat4ViewProj;
extern uniform float4x4 g_mat4ProjTexture;
// @desc: Tangent space matrix.
const float3x3 g_mat3Tangent = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f };
// @dec: The import of vertex shader.
struct VS_INPUT
{
float2 m_vec2TexCoord : TEXCOORD0;
float2 m_vec2VertexCoord : POSITION;
};
// @desc: The export of vertex shader.
struct VS_OUTPUT
{
float4 m_vec4ClipCoord : POSITION;
float2 m_vec2ProjTexCoord : TEXCOORD0;
float4 m_vec4NormalTexCoord : TEXCOORD1;
float3 m_vec3WorldViewDirection : TEXCOORD2;
float3 m_vec3TangentViewDirection : TEXCOORD3;
float3 m_vec3TangentLightDirection : TEXCOORD4;
};
// @desc: The entry of vertex shader.
VS_OUTPUT VS_Main(const VS_INPUT In)
{
// Calculate view direction and light direction in tangnent space.
VS_OUTPUT Out;
const float4 vec4VertexCoord = { In.m_vec2VertexCoord.x*g_vec2Dimension.x + g_vec3CenterCoord.x,
g_vec3CenterCoord.y,
In.m_vec2VertexCoord.y*g_vec2Dimension.y + g_vec3CenterCoord.z, 1.0f };
Out.m_vec3WorldViewDirection = g_vec3ViewCoord - vec4VertexCoord.xyz;
Out.m_vec3TangentViewDirection = mul(g_mat3Tangent, Out.m_vec3WorldViewDirection);
Out.m_vec3TangentLightDirection = mul(g_mat3Tangent, g_vec3LightCoord - vec4VertexCoord.xyz);
// Calculate projected texture coordinate and normal texture coordinate.
const float4 vec4ProjTexCoord = mul(vec4VertexCoord, g_mat4ProjTexture);
Out.m_vec2ProjTexCoord = vec4ProjTexCoord.xy/vec4ProjTexCoord.w;
Out.m_vec4NormalTexCoord = float4(In.m_vec2TexCoord + g_vec4Velocity.xy, In.m_vec2TexCoord + g_vec4Velocity.zw);
// Calculate the coordinate of vertex in clipping space.
Out.m_vec4ClipCoord = mul(vec4VertexCoord, g_mat4ViewProj);
return Out;
}
extern uniform float3 g_vec3Irradiance;
extern uniform float3 g_vec3Diffuse;
extern uniform float3 g_vec3Specular;
extern uniform float g_fSmoothness;
extern uniform float g_fPerturbation;
extern uniform float2 g_vec2FrenelPara;
extern uniform Texture2D g_LowFrequencyNormalMap;
sampler2D g_LowFrequencyNormalSampler = sampler_state
{
Texture = <g_LowFrequencyNormalMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
extern uniform Texture2D g_HighFrequencyNormalMap;
sampler2D g_HighFrequencyNormalSampler = sampler_state
{
Texture = <g_HighFrequencyNormalMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
extern uniform Texture2D g_ReflectMap;
sampler2D g_ReflectSampler = sampler_state
{
Texture = <g_ReflectMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
extern uniform Texture2D g_RefractMap;
sampler2D g_RefractSampler = sampler_state
{
Texture = <g_RefractMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
// @desc: The original surface normal.
const float3 g_vec3OriginNormal = { 0.0f, 1.0f, 0.0f };
// @desc: The import of pixel shader.
struct PS_INPUT
{
float2 m_vec2ProjTexCoord : TEXCOORD0;
float4 m_vec4NormalTexCoord : TEXCOORD1;
float3 m_vec3WorldViewDirection : TEXCOORD2;
float3 m_vec3TangentViewDirection : TEXCOORD3;
float3 m_vec3TangentLightDirection : TEXCOORD4;
};
// @desc: The entry of pixel shader.
float4 PS_Main(const PS_INPUT In) : COLOR
{
// Calculate synthetical normal.
const float3 vec3LowFrequencyNormal = (tex2D(g_LowFrequencyNormalSampler, In.m_vec4NormalTexCoord.xy).xyz - 0.5f)*2.0f;
const float3 vec3HighFrequencyNormal = (tex2D(g_HighFrequencyNormalSampler, In.m_vec4NormalTexCoord.zw).xyz - 0.5f)*2.0f;
const float3 vec3SynthNormal = normalize(vec3LowFrequencyNormal + vec3HighFrequencyNormal);
// Calculate diffuse gene.
const float3 vec3TangentLightDirection = normalize(In.m_vec3TangentLightDirection);
const float fDiffuseGene = max(dot(vec3SynthNormal, vec3TangentLightDirection), 0.0f);
// Calculate specular gene.
float fSpecularGene = 0.0f;
if (fDiffuseGene > 0.0f)
{
const float3 vec3TangentViewDirection = normalize(In.m_vec3TangentViewDirection);
const float3 vec3Reflection = reflect(-vec3TangentLightDirection, vec3SynthNormal);
fSpecularGene = pow(max(dot(vec3Reflection, vec3TangentViewDirection), 0.0f), g_fSmoothness);
}
// Fectch the sample of reflect texture and refract texture.
const float2 vec2DistortProjTexCoord = In.m_vec2ProjTexCoord + g_fPerturbation*vec3SynthNormal.xy;
const float3 vec3Reflect = tex2D(g_ReflectSampler, vec2DistortProjTexCoord).xyz;
const float3 vec3Refract = tex2D(g_RefractSampler, vec2DistortProjTexCoord).xyz;
// Calculate fresnel law.
const float fViewGene = dot(normalize(In.m_vec3WorldViewDirection), g_vec3OriginNormal);
const float fFresnelGene = g_vec2FrenelPara.x + g_vec2FrenelPara.y*pow(1.0f - fViewGene, 5.0f);
const float3 vec3Fresnel = lerp(vec3Refract, vec3Reflect, fFresnelGene);
// Calculate the exitance of light.
const float3 vec3Exitance = (fDiffuseGene*g_vec3Diffuse*vec3Fresnel + fSpecularGene*g_vec3Specular)*g_vec3Irradiance;
return float4(vec3Exitance, 1.0f);
}
// @desc: Technique and pass.
technique FluidVision
{
pass P0
{
VertexShader = compile vs_1_0 VS_Main();
PixelShader = compile ps_2_0 PS_Main();
}
}

浙公网安备 33010602011771号