X

HLSL Shadow Mapping

  • October 4, 2013
  • By admin
  • Comments Off on HLSL Shadow Mapping
  • in

Direct X High Level Shading Language

Shadow Mapping

 

1) ShadowPass

Vertax Shader

 

struct VS_INPUT

{

float4 mPosition : POSITION;

};

 

struct VS_OUTPUT

{

float4 mPosition : POSITION;

float3 lightVec : TEXCOORD1;

};

 

float4x4 gWorldMatrix;

float4x4 gLightViewMatrix;

float4x4 gLightProjectionMatrix;

float4 gLightPosition;

float distanceScale;

float Time0_X;

 

VS_OUTPUT vs_main (VS_INPUT Input)

{

VS_OUTPUT Output;

//animation light position

float3 lightPos;

lightPos.x = cos(1.321 * Time0_X);

lightPos.z = sin(0.923 * Time0_X);

lightPos.xz = 100 * normalize(lightPos.xz);

lightPos.y = 100;

//light View matrix;

float3 dirZ = -normalize(lightPos.xyz);

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

float3 dirX = cross(up, dirZ);

float3 dirY = cross(dirZ, dirX);

float4 worldPosition = mul(Input.mPosition, gWorldMatrix);

float4 pos;

float3 lightDir = worldPosition.xyz - lightPos;

pos.x = dot(dirX, lightDir);

pos.y = dot(dirY, lightDir);

pos.z = dot(dirZ, lightDir);

pos.w = 1;

Output.mPosition = mul(pos, gLightProjectionMatrix);

Output.lightVec = distanceScale * lightDir;

return Output;

}

 

 

Pixel Shader

 

struct PS_INPUT

{

float3 lightVec : TEXCOORD1;

};

 

float4 ps_main (PS_INPUT Input) : COLOR

{

float depth = length(Input.lightVec);

//float depth = length(Input.mClipPosition.z / Input.mClipPosition.w);

return float4(depth.xxx, 1.0);

}

 

 

 

 

 

2) ObjPass

Vertax Shader

 

struct VS_INPUT

{

float4 mPosition : POSITION;

float3 mNormal : NORMAL;

};

 

struct VS_OUTPUT

{

float4 mPosition : POSITION;

float3 worldNormal : TEXCOORD0;

float3 lightDir : TEXCOORD1;

float3 viewDir : TEXCOORD2;

float4 mClipPosition : TEXCOORD3;

};

 

float4x4 gWorldMatrix;

float4x4 gLightViewMatrix;

float4x4 gLightProjectionMatrix;

float4x4 gViewProjectionMatrix;

float4 gViewPosition;

float4 gLightPosition;

float Time0_X;

float distanceScale;

 

 

VS_OUTPUT vs_main (VS_INPUT Input)

{

VS_OUTPUT Output;

//animate light position

float3 lightPos;

lightPos.x = cos(1.321 * Time0_X);

lightPos.z = sin(0.923 * Time0_X);

lightPos.xz = 100 * normalize(lightPos.xz);

lightPos.y = 100;

//light View matrix;

float4x4 lightViewMatrix = gLightViewMatrix;

float3 dirZ = -normalize(lightPos.xyz);

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

float3 dirX = cross(up, dirZ);

float3 dirY = cross(dirZ, dirX);

lightViewMatrix = float4x4(

float4(dirX, -dot(lightPos.xyz, dirX)),

float4(dirY, -dot(lightPos.xyz, dirY)),

float4(dirZ, -dot(lightPos.xyz, dirZ)),

float4(0,0,0,1));

lightViewMatrix = transpose(lightViewMatrix);

//world Position

float4 worldPosition = mul(Input.mPosition, gWorldMatrix);

//depth space change

Output.mClipPosition = mul(worldPosition, lightViewMatrix);

Output.mClipPosition = mul(Output.mClipPosition, gLightProjectionMatrix);

Output.mClipPosition *= distanceScale;

//Output Position

Output.mPosition = mul(worldPosition, gViewProjectionMatrix);

Output.worldNormal = normalize(mul(Input.mNormal, (float3x3)gWorldMatrix));

Output.lightDir = distanceScale * (worldPosition.xyz - lightPos);

Output.viewDir = gViewPosition.xyz - worldPosition.xyz;

return Output;

}

 

 

Pixel Shader

 

struct PS_INPUT

{

float4 mPosition : POSITION;

float3 worldNormal : TEXCOORD0;

float3 lightDir : TEXCOORD1;

float3 viewDir : TEXCOORD2;

float4 mClipPosition : TEXCOORD3;

};

 

sampler2D ShadowSampler;

sampler2D SpotlightSampler;

float4 objColor;

float4 lightColor;

float lightAtten;

float shadowBias;

float backProjectionCut;

 

float4 ps_main (PS_INPUT Input) : COLOR

{

float3 normal = normalize(Input.worldNormal);

//radial distance

float depth = length(Input.lightDir);

//normalize lightDir

float3 lightDir = normalize(Input.lightDir / depth);

//UV

float2 UV = Input.mClipPosition.xy / Input.mClipPosition.w;

UV.y = -UV.y;

UV = UV * 0.5 + 0.5;

//UV2

Input.mClipPosition.z += 10;

float4 shadowCoord;

shadowCoord.x = 0.5*(Input.mClipPosition.z + Input.mClipPosition.x);

shadowCoord.y = 0.5*(Input.mClipPosition.z - Input.mClipPosition.y);

shadowCoord.z = 0;

shadowCoord.w = Input.mClipPosition.z;

//shadow

float shadowMap = tex2D(ShadowSampler, UV).r;

//float spotLight = tex2Dproj(SpotlightSampler, shadowCoord).r;

//float shadow = (depth < shadowMap + shadowBias);

//shadow *= spotLight;

//shadow*= (shadowCoord.w > backProjectionCut);

//diffuse lighting

float diffuse = saturate(dot(-lightDir, normal));

 

//spec

float3 viewDir = normalize(Input.viewDir);

float spec = saturate(dot(reflect(viewDir, normal), lightDir));

spec = pow(spec, 20);

spec *= diffuse;

//color final

float lightingFinal = diffuse + spec;

float3 color = objColor * lightingFinal * lightAtten * lightColor;

return float4(color, 1.0);

}

 

 

 

Contact Me

Using the contact form to send me email at below

Keep in touch with me

You can use the following information to contact me if anything need to communicate.

Name: Luke Lee (yongmin)