NormalMapping-HLSL
extern uniform float4 g_vec4ViewCoord;
extern uniform float4 g_vec4LightCoord;
extern uniform float4x4 m_mat4ModelInvert;
extern uniform float4x4 g_mat4ModelViewProj;
// @dec: The import of vertex shader.
struct VS_INPUT
{
float3 m_vec3Tangent : TANGENT;
float3 m_vec3Binormal : BINORMAL;
float3 m_vec3Normal : NORMAL;
float2 m_vec2TexCoord : TEXCOORD0;
float3 m_vec3VertexCoord : POSITION;
};
// @desc: The export of vertex shader.
struct VS_OUTPUT
{
float4 m_vec4ClipCoord : POSITION;
float2 m_vec2TexCoord : TEXCOORD0;
float3 m_vec3TangentViewDirection : TEXCOORD1;
float3 m_vec3TangentLightDirection : TEXCOORD2;
};
// @desc: The entry of vertex shader.
VS_OUTPUT VS_Main(const VS_INPUT In)
{
// Calculate the view direction and light direction in model space.
VS_OUTPUT Out;
const float3 vec3ViewDirection = In.m_vec3VertexCoord - mul(g_vec4ViewCoord, m_mat4ModelInvert).xyz;
const float3 vec3LightDirection = mul(g_vec4LightCoord, m_mat4ModelInvert).xyz - In.m_vec3VertexCoord;
// Calculate the view direction and light direction in tangent space.
const float3x3 mat3Tangent = { In.m_vec3Tangent, In.m_vec3Binormal, In.m_vec3Normal };
Out.m_vec3TangentViewDirection = mul(mat3Tangent, vec3ViewDirection);
Out.m_vec3TangentLightDirection = mul(mat3Tangent, vec3LightDirection);
// Pass texture coordinate and calculate the coordinate in clipping space.
Out.m_vec2TexCoord = In.m_vec2TexCoord;
Out.m_vec4ClipCoord = mul(float4(In.m_vec3VertexCoord, 1.0f), g_mat4ModelViewProj);
return Out;
}
extern uniform float3 g_vec3Irradiance;
extern uniform float3 g_vec3Diffuse;
extern uniform float3 g_vec3Specular;
extern uniform float g_fSmoothness;
extern uniform Texture2D g_SurfaceMap;
sampler2D g_SurfaceSampler = sampler_state
{
Texture = <g_SurfaceMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
extern uniform Texture2D g_NormalMap;
sampler2D g_NormalSampler = sampler_state
{
Texture = <g_NormalMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
// @desc: The import of pixel shader.
struct PS_INPUT
{
float2 m_vec2TexCoord : TEXCOORD0;
float3 m_vec3TangentViewDirection : TEXCOORD1;
float3 m_vec3TangentLightDirection : TEXCOORD2;
};
// @desc: The entry of pixel shader.
float4 PS_Main(const PS_INPUT In) : COLOR
{
// Calculate diffuse gene.
const float3 vec3TangentLightDirection = normalize(In.m_vec3TangentLightDirection);
const float3 vec3DisturbNormal = normalize( (tex2D(g_NormalSampler, In.m_vec2TexCoord).xyz - 0.5f)*2.0f );
const float fDiffuseGene = max(dot(vec3TangentLightDirection, vec3DisturbNormal), 0.0f);
// Calculate specular gene.
float fSpecularGene = 0.0f;
if (fDiffuseGene > 0.0f)
{
const float3 vec3TangentViewDirection = normalize(In.m_vec3TangentViewDirection);
const float3 vec3Reflection = reflect(vec3TangentViewDirection, vec3DisturbNormal);
fSpecularGene = pow(max(dot(vec3Reflection, vec3TangentLightDirection), 0.0f), g_fSmoothness);
}
// Sampler the texture of surface.
const float4 vec4Surface = tex2D(g_SurfaceSampler, In.m_vec2TexCoord);
// Caluate the exitance of light.
const float3 vec3Exitance = (fDiffuseGene*g_vec3Diffuse*vec4Surface.rgb + fSpecularGene*g_vec3Specular)*g_vec3Irradiance;
return float4(vec3Exitance, 1.0f);
}
// @desc: Technique and pass.
technique NormalMapping
{
pass P0
{
VertexShader = compile vs_1_1 VS_Main();
PixelShader = compile ps_2_0 PS_Main();
}
}
extern uniform float4 g_vec4LightCoord;
extern uniform float4x4 m_mat4ModelInvert;
extern uniform float4x4 g_mat4ModelViewProj;
// @dec: The import of vertex shader.
struct VS_INPUT
{
float3 m_vec3Tangent : TANGENT;
float3 m_vec3Binormal : BINORMAL;
float3 m_vec3Normal : NORMAL;
float2 m_vec2TexCoord : TEXCOORD0;
float3 m_vec3VertexCoord : POSITION;
};
// @desc: The export of vertex shader.
struct VS_OUTPUT
{
float4 m_vec4ClipCoord : POSITION;
float2 m_vec2TexCoord : TEXCOORD0;
float3 m_vec3TangentViewDirection : TEXCOORD1;
float3 m_vec3TangentLightDirection : TEXCOORD2;
};
// @desc: The entry of vertex shader.
VS_OUTPUT VS_Main(const VS_INPUT In)
{
// Calculate the view direction and light direction in model space.
VS_OUTPUT Out;
const float3 vec3ViewDirection = In.m_vec3VertexCoord - mul(g_vec4ViewCoord, m_mat4ModelInvert).xyz;
const float3 vec3LightDirection = mul(g_vec4LightCoord, m_mat4ModelInvert).xyz - In.m_vec3VertexCoord;
// Calculate the view direction and light direction in tangent space.
const float3x3 mat3Tangent = { In.m_vec3Tangent, In.m_vec3Binormal, In.m_vec3Normal };
Out.m_vec3TangentViewDirection = mul(mat3Tangent, vec3ViewDirection);
Out.m_vec3TangentLightDirection = mul(mat3Tangent, vec3LightDirection);
// Pass texture coordinate and calculate the coordinate in clipping space.
Out.m_vec2TexCoord = In.m_vec2TexCoord;
Out.m_vec4ClipCoord = mul(float4(In.m_vec3VertexCoord, 1.0f), g_mat4ModelViewProj);
return Out;
}
extern uniform float3 g_vec3Irradiance;
extern uniform float3 g_vec3Diffuse;
extern uniform float3 g_vec3Specular;
extern uniform float g_fSmoothness;
extern uniform Texture2D g_SurfaceMap;
sampler2D g_SurfaceSampler = sampler_state
{
Texture = <g_SurfaceMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
extern uniform Texture2D g_NormalMap;
sampler2D g_NormalSampler = sampler_state
{
Texture = <g_NormalMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
// @desc: The import of pixel shader.
struct PS_INPUT
{
float2 m_vec2TexCoord : TEXCOORD0;
float3 m_vec3TangentViewDirection : TEXCOORD1;
float3 m_vec3TangentLightDirection : TEXCOORD2;
};
// @desc: The entry of pixel shader.
float4 PS_Main(const PS_INPUT In) : COLOR
{
// Calculate diffuse gene.
const float3 vec3TangentLightDirection = normalize(In.m_vec3TangentLightDirection);
const float3 vec3DisturbNormal = normalize( (tex2D(g_NormalSampler, In.m_vec2TexCoord).xyz - 0.5f)*2.0f );
const float fDiffuseGene = max(dot(vec3TangentLightDirection, vec3DisturbNormal), 0.0f);
// Calculate specular gene.
float fSpecularGene = 0.0f;
if (fDiffuseGene > 0.0f)
{
const float3 vec3TangentViewDirection = normalize(In.m_vec3TangentViewDirection);
const float3 vec3Reflection = reflect(vec3TangentViewDirection, vec3DisturbNormal);
fSpecularGene = pow(max(dot(vec3Reflection, vec3TangentLightDirection), 0.0f), g_fSmoothness);
}
// Sampler the texture of surface.
const float4 vec4Surface = tex2D(g_SurfaceSampler, In.m_vec2TexCoord);
// Caluate the exitance of light.
const float3 vec3Exitance = (fDiffuseGene*g_vec3Diffuse*vec4Surface.rgb + fSpecularGene*g_vec3Specular)*g_vec3Irradiance;
return float4(vec3Exitance, 1.0f);
}
// @desc: Technique and pass.
technique NormalMapping
{
pass P0
{
VertexShader = compile vs_1_1 VS_Main();
PixelShader = compile ps_2_0 PS_Main();
}
}

浙公网安备 33010602011771号