光照系统可以工作了,新增加一个Const Buffer专用于传递光照相关的参数:ILightingEnvState.
做光照相对D9来说简单了许多。不需要任何的setShaderParamter之类的调用了。
Simple.Lighting的shade Node 和用于光照的shader lib如下:
libLighting.hlsl:
struct xLightDesc
{
float4 m_Diffuse;
float4 m_Speculer;
float4 m_Position;
float4 m_dir;
};
cbuffer xLightState
{
int4 nLight;
xLightDesc m_Light[8];
}
float4 Phong_Lighting(float3 vNormal , float3 wPosition , float3 eyePosition)
{
float4 vTotal = float4(0.0f,0.0f,0.0f , 0.0f);
for(int i = 0 ; i < nLight.x ; i ++ )
{
xLightDesc _desc = m_Light[i];
float3 LightDirection = _desc.m_Position.xyz - wPosition;
float3 fvLightDirection = normalize( LightDirection );
float3 fvNormal = normalize( vNormal );
float fNDotL = dot( fvNormal, fvLightDirection );
float3 fvReflection = normalize( ( ( 2.0f * fvNormal ) * ( fNDotL ) ) - fvLightDirection );
float3 fvViewDirection = normalize( eyePosition - wPosition);
float fRDotV = max( 0.0f, dot( fvReflection, fvViewDirection ) );
float4 fvTotalAmbient = float4(0.0f,0.0f,0.0f,1.0f);
float4 fvTotalDiffuse = fNDotL * _desc.m_Diffuse;
float4 fvTotalSpecular = float4(_desc.m_Speculer.xyz , 1.0f) * pow( fRDotV, _desc.m_Speculer.w );
vTotal += ( saturate( fvTotalAmbient + fvTotalDiffuse + fvTotalSpecular ) );
}
return vTotal;
}
//Simple.Lighting.hlsl
///{{Slot = Simple_Lighting
Input
{
float4 Color : COLOR0;
float4 wNormal : WNORMAL;
float4 wPosition : WPOSITION;
}
Output
{
float4 color: COLOR0;
}
///}}
///>>Import = libLighting
///{{SourceCode
float4 Simple_Lighting(float4 _Color , float4 wNormal , float4 wPosition)
{
float4 eyePositon = float4(0.0f,0.0f,0.0f,1.0f);
return _Color * Phong_Lighting(wNormal.xyz , wPosition.xyz , eyePositon.xyz );
}
///}}
截个图留念一下。