VertexShader 顶点着色器 替换的是固定渲染管线中的变换和光照阶段。
PixelShader 像素着色器 替换的是固定渲染管线中的多纹理、光栅化阶段。
所以,VS操纵的是顶点、法线、多边形这类对象,PS操纵的是像素,每个像素点,材质这类的对象。
float4x4 mWorldViewProj : WORLDVIEWPROJECTION;
string XFile = "teapot.x"; // Model to load
//顶点着色器,代替变换和光照阶段
void VS_Main(in float4 vPos : POSITION,
in float2 vTex : TEXCOORD0,
out float4 oPos : POSITION,
out float2 oTex : TEXCOORD0)
{
//VS_OUTPUT OUT = (VS_OUTPUT)0;
oPos = mul(vPos, mWorldViewProj);
oTex = vTex;
//return OUT;
}
//像素着色器,替换多纹理阶段,纹理混合
//用于处理色彩
//像素是由RGBA四个分量混合的。
void PS_Main(in float2 vTex : TEXCOORD0,
out float4 oCol : COLOR0)
{
oCol = tex2D(DiffuseSampler, vTex);
oCol.r = 1.0f;
oCol.g = 0.0f;
oCol.b = 0.0f;
oCol.a = 0.5f;
}
technique tec0
{
pass p0
{
// Set up reasonable material defaults
MaterialAmbient = {1.0, 1.0, 1.0, 1.0};
MaterialDiffuse = {1.0, 0.0, 0.0, 1.0}; //RGBA
MaterialSpecular = {1.0, 1.0, 1.0, 1.0};
MaterialPower = 40.0f;
LightType[0] = DIRECTIONAL;
LightDiffuse[0] = {1.0, 1.0, 1.0, 1.0};
LightSpecular[0] = {1.0, 1.0, 1.0, 1.0};
LightAmbient[0] = {0.1, 0.1, 0.1, 1.0};
LightDirection[0] = <lightDir>; // Use the vector parameter defined above
LightRange[0] = 100000.0;
LightEnable[0] = True;
Lighting = True;
SpecularEnable = True;
vertexShader = compile vs_2_0 VS_Main();
pixelShader = compile ps_2_0 PS_Main();
// Set up one directional light
// Turn lighting on and use light 0
// Set up texture stage 0
Texture[0] = <tex0>; // Use the texture parameter defined above
ColorOp[0] = Modulate;
ColorArg1[0] = Texture;
ColorArg2[0] = Diffuse;
AlphaOp[0] = Modulate;
AlphaArg1[0] = Texture;
AlphaArg2[0] = Diffuse;
MinFilter[0] = Linear;
MagFilter[0] = Linear;
MipFilter[0] = Linear;
// Disable texture stage 1
ColorOp[1] = Disable;
AlphaOp[1] = Disable;
}
}
程序加载一个茶壶,teapot.x文件。
想改变茶壶的颜色,就改变像素着色器的输出的rgb三个分量。r-红色,g-绿色, b-蓝色。a-透明度。
程序要用到EffectEdit。 保存为.fx文件。