我试图通过将纹理坐标硬编码到 0 和 1 来剪辑纹理,然后将包含 3x3 纹理变换矩阵的常量缓冲区发送到顶点着色器。
但是,纹理没有像我预期的那样渲染。我不确定我哪里出错了。有人可以帮忙吗?请参阅下面的代码。
为了测试,我尝试使用一个恒等矩阵来保持纹理坐标不变,但纹理以一种非常奇怪的方式显示出来。
这是纹理:(出现的颜色实际上是透明区域,除了黑色和心形较柔和的红色)
变形纹理:
HLSL:
cbuffer cbChangesPerFrame : register( b0 )
{
matrix g_Mvp;
};
cbuffer cbTexTransform : register( b2 )
{
float3x3 g_TexTransform;
};
Texture2D g_ColorMap : register(t0);
SamplerState g_ColorSampler : register(s0);
struct VS_Input
{
float4 pos : POSITION0;
float2 tex0 : TEXCOORD0;
};
struct PS_Input
{
float4 pos : SV_POSITION0;
float2 tex0 : TEXCOORD0;
};
PS_Input VS_Main(VS_Input vertex)
{
PS_Input vsOut = (PS_Input)0;
vsOut.pos = mul(vertex.pos,g_Mvp);
//vsOut.tex0 = vertex.tex0;
float3 coord = float3(vertex.tex0, 1.0);
coord = mul(coord, g_TexTransform);
vsOut.tex0 = coord.xy ;
return vsOut;
}
float4 PS_Main( PS_Input frag ) : SV_TARGET
{
return g_ColorMap.Sample( g_ColorSampler, frag.tex0 );
}
VBuffer 硬编码:
Vertex::PosTex vertices[]=
{
{XMFLOAT3( 0.5f, 0.5f, 1.0f ), XMFLOAT2( 1.0f, 0.0f )},
{XMFLOAT3( 0.5f, -0.5f, 1.0f ), XMFLOAT2( 1.0f, 1.0f )},
{XMFLOAT3( -0.5f, -0.5f, 1.0f ), XMFLOAT2( 0.0f, 1.0f )},
{XMFLOAT3( -0.5f, -0.5f, 1.0f ), XMFLOAT2( 0.0f, 1.0f )},
{XMFLOAT3( -0.5f, 0.5f, 1.0f ), XMFLOAT2( 0.0f, 0.0f )},
{XMFLOAT3( 0.5f, 0.5f, 1.0f ), XMFLOAT2( 1.0f, 0.0f )}
};
矩阵定义:
XMFLOAT3X3 f3x3;
f3x3.m[0][0] = 1.0f; f3x3.m[0][1] = 0.0f; f3x3.m[0][2] = 0.0f;
f3x3.m[1][0] = 0.0f; f3x3.m[1][1] = 1.0f; f3x3.m[1][2] = 0.0f;
f3x3.m[2][0] = 0.0f; f3x3.m[2][1] = 0.0f; f3x3.m[2][2] = 1.0f;
GAME_MANAGER->GMSetTexTransformMatrix(f3x3);
Game_Manager GMSetTransformMatrix() 定义:
void GameManager::GMSetTexTransformMatrix( const XMFLOAT3X3& rkTexTransform )
{
m_pContext->UpdateSubresource(m_pTexTransformCB,0,0,&rkTexTransform,0,0);
m_pContext->VSSetConstantBuffers(2,1,&m_pTexTransformCB);
}
缓冲区初始化:
ZeroMemory(&constDesc, sizeof(constDesc));
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth = 48;
constDesc.Usage = D3D11_USAGE_DEFAULT;
result = m_pDevice->CreateBuffer(&constDesc,0,&m_pTexTransformCB);