|
|
由于用D3D内置的 D3DFVF_XYZRHW 无法变换绘图坐标, 所以我就写了个自己绘制2D图像的着色器
只写了最基本的2D绘制功能, 如果有需要可以自己添加其他处理功能
// 纹理对象
texture g_Texture;
// 顶点变换矩阵
float4x4 g_matVertexTransform;
// 纹理变换矩阵
float4x4 g_matTextureTransform;
// 纹理采样器
sampler TextureSampler =
sampler_state
{
Texture = <g_Texture>;
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
// 顶点着色器输出结构
struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Diffuse : COLOR0;
float2 TexUV : TEXCOORD0;
};
// 顶点着色器
VS_OUTPUT VSMain(float2 Pos : POSITION, float4 Diffuse : COLOR0, float2 TexUV : TEXCOORD0)
{
VS_OUTPUT Out;
Out.Pos = float4(mul(float4(Pos, 1.0f, 1.0f), g_matVertexTransform).xy, 0.0f, 1.0f);
Out.Diffuse = Diffuse;
Out.TexUV = TexUV;
return Out;
}
// 像素着色器
float4 PSMain(VS_OUTPUT In) : COLOR
{
// 采样像素与漫射颜色混合
return tex2D(TextureSampler, mul(float4(In.TexUV, 1.0f, 1.0f), g_matTextureTransform).xy) * In.Diffuse;
}
// 2D图形处理
technique TShader
{
pass P0
{
VertexShader = compile vs_2_0 VSMain();
PixelShader = compile ps_2_0 PSMain();
}
}
-------------------------------------
顶点结构:
struct ScreenVertex
{
float x, y; // 2D屏幕坐标(取值范围 -1.0 ~ 1.0)
D3DCOLOR color; // 颜色
float tu, tv; // 纹理坐标
};
D3DVERTEXELEMENT9 decl[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 8, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
}; |
|