延迟着色有别于以前的流程,光照信息不在固定管线第一阶段或VS里面计算,而是在PS里面计算。目的就是为了提高性能,一个是在3D空间计算,一个是在2D空间计算
分两步
一 把位置,发现,材质等信息渲染到纹理,纹理需要创建浮点型的。这一步需要利用d3d多渲染目标
float4x4 matWorld;
float4x4 matViewProjection;
struct VS_INPUT
{
float3 Pos: POSITION;
float3 Normal: NORMAL;
};
struct VS_OUTPUT
{
float4 Pos: POSITION;
float3 PosInWorld: TEXCOORD0;
float3 NormalInWorld: TEXCOORD1;
};
VS_OUTPUT vs_main( VS_INPUT In )
{
VS_OUTPUT Out;
Out.Pos = mul(float4(In.Pos,1.0),matViewProjection );
Out.PosInWorld = mul(float4(In.Pos,1.0),matWorld);
Out.NormalInWorld = mul(In.Normal,matWorld);
return Out;
}
float4x4 matViewProjection;
struct VS_INPUT
{
float3 Pos: POSITION;
float3 Normal: NORMAL;
};
struct VS_OUTPUT
{
float4 Pos: POSITION;
float3 PosInWorld: TEXCOORD0;
float3 NormalInWorld: TEXCOORD1;
};
VS_OUTPUT vs_main( VS_INPUT In )
{
VS_OUTPUT Out;
Out.Pos = mul(float4(In.Pos,1.0),matViewProjection );
Out.PosInWorld = mul(float4(In.Pos,1.0),matWorld);
Out.NormalInWorld = mul(In.Normal,matWorld);
return Out;
}
struct PS_INPUT
{
float3 PosInWorld: TEXCOORD0;
float3 NormalInWorld: TEXCOORD1;
};
struct PS_OUTPUT
{
float4 PosInWorld : COLOR0;
float4 NormalInWorld : COLOR1;
};
PS_OUTPUT ps_main( PS_INPUT In )
{
PS_OUTPUT Out;
Out.PosInWorld = float4(In.PosInWorld,1.0);
Out.NormalInWorld = float4(In.NormalInWorld,1.0);
return Out;
}
{
float3 PosInWorld: TEXCOORD0;
float3 NormalInWorld: TEXCOORD1;
};
struct PS_OUTPUT
{
float4 PosInWorld : COLOR0;
float4 NormalInWorld : COLOR1;
};
PS_OUTPUT ps_main( PS_INPUT In )
{
PS_OUTPUT Out;
Out.PosInWorld = float4(In.PosInWorld,1.0);
Out.NormalInWorld = float4(In.NormalInWorld,1.0);
return Out;
}
二 计算光照信息
float4x4 matWorld;
float4x4 matViewProjection;
struct VS_INPUT
{
float3 Pos: POSITION;
};
struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 TexCoord: TEXCOORD0;
};
VS_OUTPUT vs_main( VS_INPUT In )
{
VS_OUTPUT Out;
Out.Pos = float4(sign(In.Pos.xy),0.0,1.0);
Out.TexCoord.x = (Out.Pos.x+1.0) * 0.5;
Out.TexCoord.y = 1.0 - ((Out.Pos.y+1.0) * 0.5);
return Out;
}
float4x4 matViewProjection;
struct VS_INPUT
{
float3 Pos: POSITION;
};
struct VS_OUTPUT
{
float4 Pos: POSITION;
float2 TexCoord: TEXCOORD0;
};
VS_OUTPUT vs_main( VS_INPUT In )
{
VS_OUTPUT Out;
Out.Pos = float4(sign(In.Pos.xy),0.0,1.0);
Out.TexCoord.x = (Out.Pos.x+1.0) * 0.5;
Out.TexCoord.y = 1.0 - ((Out.Pos.y+1.0) * 0.5);
return Out;
}
float4 ObjColor;
float4 ClearColor;
float3 LightPos;
sampler2D PosMap;
sampler2D NormalMap;
struct PS_INPUT
{
float2 TexCoord: TEXCOORD0;
};
struct PS_OUTPUT
{
float4 Color : COLOR0;
};
PS_OUTPUT ps_main( PS_INPUT In )
{
PS_OUTPUT Out;
float4 pos = tex2D(PosMap,In.TexCoord);
if (pos.w==0.0) // If nothing is drawn at this pixel, just output clear color
{
Out.Color = ClearColor;
} // End if
else
{
float3 normal = normalize(tex2D(NormalMap,In.TexCoord).xyz);
float3 toLight = normalize(LightPos - pos.xyz);
float diffuse = dot(normal,toLight);
Out.Color = ObjColor * diffuse;
} // End else
return Out;
}
float4 ClearColor;
float3 LightPos;
sampler2D PosMap;
sampler2D NormalMap;
struct PS_INPUT
{
float2 TexCoord: TEXCOORD0;
};
struct PS_OUTPUT
{
float4 Color : COLOR0;
};
PS_OUTPUT ps_main( PS_INPUT In )
{
PS_OUTPUT Out;
float4 pos = tex2D(PosMap,In.TexCoord);
if (pos.w==0.0) // If nothing is drawn at this pixel, just output clear color
{
Out.Color = ClearColor;
} // End if
else
{
float3 normal = normalize(tex2D(NormalMap,In.TexCoord).xyz);
float3 toLight = normalize(LightPos - pos.xyz);
float diffuse = dot(normal,toLight);
Out.Color = ObjColor * diffuse;
} // End else
return Out;
}