这一节阅读FX文件。阅读顺序基本和程序执行情况一致。
首先执行的是渲染阴影图。
//-----------------------------------------------------------------------------
// Technique: RenderShadow
// Desc: Renders the shadow map
//-----------------------------------------------------------------------------
technique RenderShadow
{
pass p0
{
VertexShader = compile vs_2_0 VertShadow();
PixelShader = compile ps_2_0 PixShadow();
}
}
//-----------------------------------------------------------------------------
// Vertex Shader: VertShadow
// Desc: Process vertex for the shadow map
//-----------------------------------------------------------------------------
void VertShadow( float4 Pos : POSITION,
float3 Normal : NORMAL,
out float4 oPos : POSITION,
out float2 Depth : TEXCOORD0 )
{
//
// Compute the projected coordinates
//
oPos = mul( Pos, g_mWorldView );
oPos = mul( oPos, g_mProj );
//
// Store z and w in our spare texcoord
//
Depth.xy = oPos.zw;
}
顶点着色器主要就将顶点变换到灯坐标系中,并且输出顶点深度。
//-----------------------------------------------------------------------------
// Pixel Shader: PixShadow
// Desc: Process pixel for the shadow map
//-----------------------------------------------------------------------------
void PixShadow( float2 Depth : TEXCOORD0,
out float4 Color : COLOR )
{
//
// Depth is z / w
//
Color = Depth.x / Depth.y;
}
像素着色器则将每一个像素都变换到“屏幕”空间中。这样就得到了每个像素的深度。
接下来就应该到利用阴影图渲染场景的部分了。
//-----------------------------------------------------------------------------
// Vertex Shader: VertScene
// Desc: Process vertex for scene
//-----------------------------------------------------------------------------
void VertScene( float4 iPos : POSITION,
float3 iNormal : NORMAL,
float2 iTex : TEXCOORD0,
out float4 oPos : POSITION,
out float2 Tex : TEXCOORD0,
out float4 vPos : TEXCOORD1,
out float3 vNormal : TEXCOORD2,
out float4 vPosLight : TEXCOORD3 )
{
//
// Transform position to view space
//
vPos = mul( iPos, g_mWorldView );
//
// Transform to screen coord
//
oPos = mul( vPos, g_mProj );
//
// Compute view space normal
//
vNormal = mul( iNormal, (float3x3)g_mWorldView );
//
// Propagate texture coord
//
Tex = iTex;
//
// Transform the position to light projection space, or the
// projection space as if the camera is looking out from
// the spotlight.
//
vPosLight = mul( vPos, g_mViewToLightProj );
}
注意,输出中一共有三个坐标,一个是正常的屏幕坐标,一个是视场空间中的坐标,一个是光投影空间的坐标。
//-----------------------------------------------------------------------------
// Pixel Shader: PixScene
// Desc: Process pixel (do per-pixel lighting) for enabled scene
//-----------------------------------------------------------------------------
float4 PixScene( float2 Tex : TEXCOORD0,
float4 vPos : TEXCOORD1,
float3 vNormal : TEXCOORD2,
float4 vPosLight : TEXCOORD3 ) : COLOR
{
float4 Diffuse;
// vLight is the unit vector from the light to this pixel
float3 vLight = normalize( float3( vPos - g_vLightPos ) );
// Compute diffuse from the light
if( dot( vLight, g_vLightDir ) > g_fCosTheta ) // Light must face the pixel (within Theta)
{
// Pixel is in lit area. Find out if it's
// in shadow using 2x2 percentage closest filtering
//transform from RT space to texture space.
float2 ShadowTexC = 0.5 * vPosLight.xy / vPosLight.w + float2( 0.5, 0.5 );
ShadowTexC.y = 1.0f - ShadowTexC.y;
// transform to texel space
float2 texelpos = SMAP_SIZE * ShadowTexC;
// Determine the lerp amounts
float2 lerps = frac( texelpos );
//read in bilerp stamp, doing the shadow checks
float sourcevals[4];
sourcevals[0] = (tex2D( g_samShadow, ShadowTexC ) + SHADOW_EPSILON < vPosLight.z / vPosLight.w)? 0.0f: 1.0f;
sourcevals[1] = (tex2D( g_samShadow, ShadowTexC + float2(1.0/SMAP_SIZE, 0) ) + SHADOW_EPSILON < vPosLight.z / vPosLight.w)? 0.0f: 1.0f;
sourcevals[2] = (tex2D( g_samShadow, ShadowTexC + float2(0, 1.0/SMAP_SIZE) ) + SHADOW_EPSILON < vPosLight.z / vPosLight.w)? 0.0f: 1.0f;
sourcevals[3] = (tex2D( g_samShadow, ShadowTexC + float2(1.0/SMAP_SIZE, 1.0/SMAP_SIZE) ) + SHADOW_EPSILON < vPosLight.z / vPosLight.w)? 0.0f: 1.0f;
// lerp between the shadow values to calculate our light amount
float LightAmount = lerp( lerp( sourcevals[0], sourcevals[1], lerps.x ),
lerp( sourcevals[2], sourcevals[3], lerps.x ),
lerps.y );
// Light it
Diffuse = ( saturate( dot( -vLight, normalize( vNormal ) ) ) * LightAmount * ( 1 - g_vLightAmbient ) + g_vLightAmbient )
* g_vMaterial;
} else
{
Diffuse = g_vLightAmbient * g_vMaterial;
}
return tex2D( g_samScene, Tex ) * Diffuse;
}
注释比较多,就不说了。
例子中还有计算灯罩的技术和代码,但实际上删除这些代码对本例子并没有影响,因为光照在上面的效果框架已经计算好了,删除后场景中则会不渲染灯光源。