撰寫效果需要您瞭解效果語法,併產生必要的狀態資訊。 您可以新增著色器狀態、紋理和取樣器狀態,以及應用程式在效果中需要的所有管線狀態。
效果語法詳述於 效果格式 (Direct3D 9)。 效果通常會封裝到效果檔案 (.fx),但也可以撰寫為應用程式中的文字字串。
效果範例
效果包含三種類型的狀態:著色器狀態、紋理和取樣器狀態,以及管線狀態。 以下是來自 BasicHLSL 範例效果的範例:
// Global variables
float4 g_MaterialAmbientColor; // Material's ambient color
float4 g_MaterialDiffuseColor; // Material's diffuse color
int g_nNumLights;
float3 g_LightDir[3]; // Light's direction in world space
float4 g_LightDiffuse[3]; // Light's diffuse color
float4 g_LightAmbient; // Light's ambient color
texture g_MeshTexture; // Color texture for mesh
float g_fTime; // App's time in seconds
float4x4 g_mWorld; // World matrix for object
float4x4 g_mWorldViewProjection; // World * View * Projection matrix
// Texture samplers
sampler MeshTextureSampler =
sampler_state
{
Texture = <g_MeshTexture>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
struct VS_OUTPUT
{
float4 Position : POSITION; // vertex position
float2 TextureUV : TEXCOORD0; // vertex texture coords
float4 Diffuse : COLOR0; // vertex diffuse color
};
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION,
float3 vNormal : NORMAL,
float2 vTexCoord0 : TEXCOORD0,
uniform int nNumLights,
uniform bool bTexture,
uniform bool bAnimate )
{
VS_OUTPUT Output;
float3 vNormalWorldSpace;
float4 vAnimatedPos = vPos;
// Animation the vertex based on time and the vertex's object space position
if( bAnimate )
vAnimatedPos += float4(vNormal, 0) * (sin(g_fTime+5.5)+0.5)*5;
// Transform the position from object space to homogeneous projection space
Output.Position = mul(vAnimatedPos, g_mWorldViewProjection);
// Transform the normal from object space to world space
vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld));
// Compute simple directional lighting equation
float3 vTotalLightDiffuse = float3(0,0,0);
for(int i=0; i < nNumLights; i++ )
vTotalLightDiffuse += g_LightDiffuse[i] * max(0,dot(vNormalWorldSpace, g_LightDir[i]));
Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse +
g_MaterialAmbientColor * g_LightAmbient;
Output.Diffuse.a = 1.0f;
// Just copy the texture coordinate through
if( bTexture )
Output.TextureUV = vTexCoord0;
else
Output.TextureUV = 0;
return Output;
}
struct PS_OUTPUT
{
float4 RGBColor : COLOR0; // Pixel color
};
PS_OUTPUT RenderScenePS( VS_OUTPUT In,
uniform bool bTexture )
{
PS_OUTPUT Output;
// Lookup mesh texture and modulate it with diffuse
if( bTexture )
Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
else
Output.RGBColor = In.Diffuse;
return Output;
}
technique RenderSceneWithTexture1Light
{
pass P0
{
VertexShader = compile vs_1_1 RenderSceneVS( 1, true, true );
PixelShader = compile ps_1_1 RenderScenePS( true );
}
}
此效果包含:
著色器狀態,這是與頂點著色器和像素著色器相關聯的所有狀態。 這是由頂點和像素著色器函式、其所需的任何全域變數,以及其輸入和輸出數據結構所定義,所有變數都列於此處:
struct VS_OUTPUT { ... }; VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, ... { ... } struct PS_OUTPUT { ... }; PS_OUTPUT RenderScenePS( VS_OUTPUT In, uniform bool bTexture ) { ... }紋理和取樣器狀態,這是紋理對象和取樣器物件的全域變數:
texture g_MeshTexture; // Color texture for mesh sampler MeshTextureSampler = sampler_state { ... };其他效果狀態。 此範例中未明確使用其他效果狀態。 如果有,則會是其套用之傳遞內的技術:
technique RenderSceneWithTexture1Light { pass P0 { // Any other effect state can be set here. VertexShader = compile vs_1_1 RenderSceneVS( 1, true, true ); PixelShader = compile ps_1_1 RenderScenePS( true ); } }
效果包含一或多個技法和處理
效果渲染選項是透過新增技巧和通道來控制。
可以使用額外的處理來建立效果,以促進更複雜的渲染效果。 此技術支援任意數目的次數:
technique T0
{
pass P0
{ ... }
pass P1
{ ... }
...
pass Pn
{ ... }
}
效果也可以使用任意數目的技術來建立。
technique T0
{
pass P0
{ ... }
}
technique T1
{
pass P0
{ ... }
pass P1
{ ... }
}
...
technique Tn
{
pass P0
{ ... }
}
technique TVertexShaderOnly
{
pass P0
{
VertexShader =
asm
{
// assembly-language shader code goes here
...
};
}
}
技術和動作可以指定任意名稱。
相關主題