我有一个c ++ / cx项目,我想在其中使用DirectX-11渲染网格(从FBX文件加载)。 FBX文件包含纹理坐标,但是在渲染它们时,这些发送到顶点着色器的UV坐标是完全错误的。
外观如下:
这些应该是2个人骑自行车的人,但纹理映射完全错误。
顶点描述如下:
D3D11_INPUT_ELEMENT_DESC _vertexDescriptions[3] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
请注意,渲染时“位置”和“正常”数据是正确的,只是纹理坐标错误/缺失。
VertexShader输入和-输出:
struct VertexShaderInput
{
float3 position : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 color : COLOR0;
float2 uv : TEXCOORD0;
float4 position : SV_POSITION;
};
PixelShader输入:
struct PixelShaderInput
{
float4 color: COLOR0;
float2 uv : TEXCOORD0;
};
以下是fbx文件的摘要:
LayerElementUV: 0 {
Version: 101
Name: "UVTex"
MappingInformationType: "ByPolygonVertex"
ReferenceInformationType: "IndexToDirect"
UV: 0.118273,0.459037,0.598600,0.180400,0.647187,0.129884,
//...
UVIndex: 2136,255,879,734,2136,734,3602,1034,1034,3602,6071,1176,
//...
Visual Studio将fbx文件内置到cmo文件中,并使用directxtk像这样加载:
_effectFactory = std::make_unique<EffectFactory>(_d3dDevice.Get());
_playerModel = Model::CreateFromCMO(_d3dDevice.Get(), L"Ambiorix.Track3DComponent\\PlayerModel.cmo", *_effectFactory);
然后像这样初始化PlayerMesh(_playerModel传递给该函数):
task<void> PlayerMesh::InitializeAsync(Model *const model)
{
_logger->Trace(L"PlayerMesh.InitializeAsync()");
_isInitialized = false;
_primitiveTopology = model->meshes[0]->meshParts[0]->primitiveType;
_vertexBufferStride = model->meshes[0]->meshParts[0]->vertexStride;
_vertexBufferOffset = model->meshes[0]->meshParts[0]->vertexOffset;
_vertexBuffer = model->meshes[0]->meshParts[0]->vertexBuffer;
_inputLayout = model->meshes[0]->meshParts[0]->inputLayout;
_indexFormat = model->meshes[0]->meshParts[0]->indexFormat;
_indexCount = model->meshes[0]->meshParts[0]->indexCount;
_indexBuffer = model->meshes[0]->meshParts[0]->indexBuffer;
std::vector<task<void>> tasks;
tasks.push_back(CreateVertexShaderAsync(_vertexShaderRelativeFilePath, _vertexDescriptions, ARRAYSIZE(_vertexDescriptions)));
tasks.push_back(CreatePixelShaderAsync(_pixelShaderRelativeFilePath));
return when_all(tasks.begin(), tasks.end())
.then([this]
{
CreateSamplerState();
auto createTextureResult = CreateDDSTextureFromFile(_d3dDevice.Get(), _textureRelativeFilePath->Data(), nullptr, _texture.ReleaseAndGetAddressOf());
if (FAILED(createTextureResult))
{
_logger->Error(L"Failed to create texture.");
return;
}
PlayerMeshConstantBuffer solidColorConstantBuffer;
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &solidColorConstantBuffer;
InitData.SysMemPitch = 0u;
InitData.SysMemSlicePitch = 0u;
CD3D11_BUFFER_DESC bufferDescription(sizeof(PlayerMeshConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
auto createConstantBuffer = _d3dDevice->CreateBuffer(&bufferDescription, &InitData, _constantBuffer.GetAddressOf());
if (FAILED(createConstantBuffer))
{
_logger->Error(L"PlayerMesh.InitializeAsync() | Failed to create constant buffer.");
return;
}
_isInitialized = true;
_logger->Trace("PlayerMesh.InitializeAsync() | Done.");
});
}
我可以共享更多代码,如果有帮助,可以共享整个fbx文件,只是问一下,我真的不知道还有什么可能有用的。
如果有人可以阐明如何在着色器中正确获取uv坐标,将不胜感激。
有什么帮助吗?
编辑:我最初是说UV的位置为0,但我注意到具有
struct VertexShaderOutput
{
float4 color : COLOR0;
float2 uv : TEXCOORD;
float4 position : SV_POSITION;
};
代替
struct VertexShaderOutput
{
float4 color : COLOR0;
float4 position : SV_POSITION;
float2 uv : TEXCOORD;
};
解决了这个问题,不知道为什么。