fix(rendering): remap M2 vertex bone indices through bone lookup table

M2 vertex bone indices are indices into boneLookupTable, not direct bone
array indices. Without remapping, vertices weighted to higher bone
indices (cloak, cape, hair) get the wrong bone transform, causing
vertices to project wildly outward from the character.
This commit is contained in:
Kelsi 2026-04-03 22:22:51 -07:00
parent 1feb6ea63f
commit 04ad88330f
2 changed files with 19 additions and 5 deletions

View file

@ -1486,7 +1486,17 @@ void CharacterRenderer::setupModelBuffers(M2ModelGPU& gpuModel) {
auto& dst = gpuVerts[i];
dst.position = src.position;
std::memcpy(dst.boneWeights, src.boneWeights, 4);
std::memcpy(dst.boneIndices, src.boneIndices, 4);
// Remap bone indices through the bone lookup table.
// M2 vertex bone indices are lookup table indices, not direct bone indices.
for (int j = 0; j < 4; j++) {
uint8_t idx = src.boneIndices[j];
if (idx < model.boneLookupTable.size()) {
dst.boneIndices[j] = static_cast<uint8_t>(
std::min<uint16_t>(model.boneLookupTable[idx], 255));
} else {
dst.boneIndices[j] = 0;
}
}
dst.normal = src.normal;
dst.texCoords = src.texCoords[0]; // Use first UV set
dst.tangent = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f); // default