Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for more bone weights in GLTF2 #4453

Merged
merged 3 commits into from
Aug 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 30 additions & 13 deletions code/AssetLib/glTF2/glTF2Importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -970,21 +970,29 @@ static void BuildVertexWeightMapping(Mesh::Primitive &primitive, std::vector<std
struct Weights {
float values[4];
};
Weights *weights = nullptr;
attr.weight[0]->ExtractData(weights);
Weights **weights = new Weights*[attr.weight.size()];
kimkulling marked this conversation as resolved.
Show resolved Hide resolved
for (size_t w = 0; w < attr.weight.size(); ++w) {
attr.weight[w]->ExtractData(weights[w]);
}

struct Indices8 {
uint8_t values[4];
};
struct Indices16 {
uint16_t values[4];
};
Indices8 *indices8 = nullptr;
Indices16 *indices16 = nullptr;
Indices8 **indices8 = nullptr;
Indices16 **indices16 = nullptr;
if (attr.joint[0]->GetElementSize() == 4) {
attr.joint[0]->ExtractData(indices8);
indices8 = new Indices8*[attr.joint.size()];
for (size_t j = 0; j < attr.joint.size(); ++j) {
attr.joint[j]->ExtractData(indices8[j]);
}
} else {
attr.joint[0]->ExtractData(indices16);
indices16 = new Indices16 *[attr.joint.size()];
for (size_t j = 0; j < attr.joint.size(); ++j) {
attr.joint[j]->ExtractData(indices16[j]);
}
}
//
if (nullptr == indices8 && nullptr == indices16) {
Expand All @@ -993,17 +1001,26 @@ static void BuildVertexWeightMapping(Mesh::Primitive &primitive, std::vector<std
return;
}

for (size_t i = 0; i < num_vertices; ++i) {
for (int j = 0; j < 4; ++j) {
const unsigned int bone = (indices8 != nullptr) ? indices8[i].values[j] : indices16[i].values[j];
const float weight = weights[i].values[j];
if (weight > 0 && bone < map.size()) {
map[bone].reserve(8);
map[bone].emplace_back(static_cast<unsigned int>(i), weight);
for (size_t w = 0; w < attr.weight.size(); ++w) {
for (size_t i = 0; i < num_vertices; ++i) {
for (int j = 0; j < 4; ++j) {
const unsigned int bone = (indices8 != nullptr) ? indices8[w][i].values[j] : indices16[w][i].values[j];
const float weight = weights[w][i].values[j];
if (weight > 0 && bone < map.size()) {
map[bone].reserve(8);
kimkulling marked this conversation as resolved.
Show resolved Hide resolved
map[bone].emplace_back(static_cast<unsigned int>(i), weight);
}
}
}
}

for (size_t w = 0; w < attr.weight.size(); ++w) {
delete[] weights[w];
if(indices8)
delete[] indices8[w];
if (indices16)
delete[] indices16[w];
}
delete[] weights;
delete[] indices8;
delete[] indices16;
Expand Down