static const float fDark = 0.1f; // shadow
static const float fBright = 1.0f; // light
static const float fDepthOffset = 0.97f;
float4x4 matWorldViewProj;
float4x4 matWorld;
float4 vecViewPos;
float3x3 matTangent; // tangent space matrix
float4 vecSkill1; // light position (xyz), light range (w)
float4 vecSkill5; // light color (xyz)
texture mtlSkin1; // depth cubemap
texture entSkin1; // texture
texture entSkin2; // normals (rgb) + specularity (a)
samplerCUBE CubeSampler = sampler_state { Texture = <mtlSkin1>; MipFilter = Linear; MinFilter = Linear; MagFilter = Linear; };
sampler TexSampler = sampler_state { Texture = <entSkin1>; Mipfilter = Linear; };
sampler NormalSampler = sampler_state { Texture = <entSkin2>; Mipfilter = Linear; };
void OmniShadowVS (
in float4 inPos: POSITION,
in float3 inNormal: NORMAL,
in float2 inTex: TEXCOORD0,
in float4 inTangent: TEXCOORD2,
out float4 outPos: POSITION,
out float2 outTex: TEXCOORD0,
out float3 outNormal: TEXCOORD1,
out float4 outWorld: TEXCOORD2,
out float3 outViewDir: TEXCOORD3,
out float3 outLightDir: TEXCOORD4 )
{
outPos = mul( inPos, matWorldViewProj );
outTex = inTex;
outNormal = normalize( mul( inNormal, matWorld ) );
outWorld = mul( inPos, matWorld );
matTangent[0] = mul(inTangent.xyz, matWorld);
matTangent[1] = mul(cross(inTangent.xyz, inNormal)*inTangent.w, matWorld);
matTangent[2] = mul(inNormal, matWorld);
outViewDir = normalize ( mul ( matTangent, vecViewPos.xyz - outWorld.xyz ) );
outLightDir = normalize ( mul ( matTangent, vecSkill1.xyz - outWorld.xyz ) );
}
float4 OmniShadowPS (
in float4 inPos: POSITION,
in float2 inTex: TEXCOORD0,
in float3 inNormal: TEXCOORD1,
in float4 inWorld: TEXCOORD2,
in float3 inViewDir : TEXCOORD3,
in float3 inLightDir: TEXCOORD4 ) : COLOR0
{
// shadows
float fDistance = distance ( inWorld.xyz, vecSkill1.xyz );
float3 inDir = inWorld.xyz - vecSkill1.xyz;
float Depth = texCUBE ( CubeSampler, inDir ).r;
float fShadow = Depth < fDistance * fDepthOffset ? fDark : fBright;
// light influence
float fRadiance = max ( vecSkill1.w - fDistance, 0 ) / vecSkill1.w;
// specular term
float4 NormalTex = tex2D(NormalSampler, inTex);
float3 bumpNormal = NormalTex.rgb * 2 - 1;
float3 fReflect = normalize ( 2 * dot ( bumpNormal, inLightDir ) * bumpNormal - inLightDir );
float3 fSpecular = saturate ( dot ( fReflect, inViewDir ) ) * NormalTex.a;
// simple diffuse
float fDiffuse = saturate ( dot ( normalize ( -inDir ), inNormal ) );
// final color
float4 Color = tex2D ( TexSampler, inTex );
Color.rgb *= vecSkill5.xyz * ( ( fDiffuse * fShadow ) + fSpecular ) * fRadiance;
return Color;
}
technique OmniShadow
{
pass p0
{
ZWriteEnable = True;
AlphaBlendEnable = True;
AlphaTestEnable = True;
AlphaFunc = Greater;
VertexShader = compile vs_2_0 OmniShadowVS();
PixelShader = compile ps_2_0 OmniShadowPS();
}
}