Merge remote-tracking branch 'origin/master'

pull/3042/head
iamAdrianIusca 2020-03-09 22:19:22 +02:00
commit 8f41516637
1 changed files with 41 additions and 34 deletions

View File

@ -111,8 +111,9 @@ const aiImporterDesc *glTF2Importer::GetInfo() const {
bool glTF2Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const { bool glTF2Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const {
const std::string &extension = GetExtension(pFile); const std::string &extension = GetExtension(pFile);
if (extension != "gltf" && extension != "glb") if (extension != "gltf" && extension != "glb") {
return false; return false;
}
if (pIOHandler) { if (pIOHandler) {
glTF2::Asset asset(pIOHandler); glTF2::Asset asset(pIOHandler);
@ -323,10 +324,11 @@ static inline bool CheckValidFacesIndices(aiFace *faces, unsigned nFaces, unsign
for (unsigned i = 0; i < nFaces; ++i) { for (unsigned i = 0; i < nFaces; ++i) {
for (unsigned j = 0; j < faces[i].mNumIndices; ++j) { for (unsigned j = 0; j < faces[i].mNumIndices; ++j) {
unsigned idx = faces[i].mIndices[j]; unsigned idx = faces[i].mIndices[j];
if (idx >= nVerts) if (idx >= nVerts) {
return false; return false;
} }
} }
}
return true; return true;
} }
#endif // ASSIMP_BUILD_DEBUG #endif // ASSIMP_BUILD_DEBUG
@ -861,7 +863,19 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &
if (node.skin) { if (node.skin) {
for (int primitiveNo = 0; primitiveNo < count; ++primitiveNo) { for (int primitiveNo = 0; primitiveNo < count; ++primitiveNo) {
aiMesh *mesh = pScene->mMeshes[meshOffsets[mesh_idx] + primitiveNo]; aiMesh *mesh = pScene->mMeshes[meshOffsets[mesh_idx] + primitiveNo];
mesh->mNumBones = static_cast<unsigned int>(node.skin->jointNames.size()); unsigned int numBones =static_cast<unsigned int>(node.skin->jointNames.size());
std::vector<std::vector<aiVertexWeight>> weighting(numBones);
BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting);
unsigned int realNumBones = 0;
for (uint32_t i = 0; i < numBones; ++i) {
if (weighting[i].size() > 0) {
realNumBones++;
}
}
mesh->mNumBones = static_cast<unsigned int>(realNumBones);
mesh->mBones = new aiBone *[mesh->mNumBones]; mesh->mBones = new aiBone *[mesh->mNumBones];
// GLTF and Assimp choose to store bone weights differently. // GLTF and Assimp choose to store bone weights differently.
@ -873,13 +887,14 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &
// both because it's somewhat slow and because, for many applications, // both because it's somewhat slow and because, for many applications,
// we then need to reconvert the data back into the vertex-to-bone // we then need to reconvert the data back into the vertex-to-bone
// mapping which makes things doubly-slow. // mapping which makes things doubly-slow.
std::vector<std::vector<aiVertexWeight>> weighting(mesh->mNumBones);
BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting);
mat4 *pbindMatrices = nullptr; mat4 *pbindMatrices = nullptr;
node.skin->inverseBindMatrices->ExtractData(pbindMatrices); node.skin->inverseBindMatrices->ExtractData(pbindMatrices);
for (uint32_t i = 0; i < mesh->mNumBones; ++i) { int cb = 0;
for (uint32_t i = 0; i < numBones; ++i) {
const std::vector<aiVertexWeight> &weights = weighting[i];
if (weights.size() > 0) {
aiBone *bone = new aiBone(); aiBone *bone = new aiBone();
Ref<Node> joint = node.skin->jointNames[i]; Ref<Node> joint = node.skin->jointNames[i];
@ -893,23 +908,12 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &
bone->mName = (kDefaultName + postfix); bone->mName = (kDefaultName + postfix);
} }
GetNodeTransform(bone->mOffsetMatrix, *joint); GetNodeTransform(bone->mOffsetMatrix, *joint);
CopyValue(pbindMatrices[i], bone->mOffsetMatrix); CopyValue(pbindMatrices[i], bone->mOffsetMatrix);
std::vector<aiVertexWeight> &weights = weighting[i];
bone->mNumWeights = static_cast<uint32_t>(weights.size()); bone->mNumWeights = static_cast<uint32_t>(weights.size());
if (bone->mNumWeights > 0) {
bone->mWeights = new aiVertexWeight[bone->mNumWeights]; bone->mWeights = new aiVertexWeight[bone->mNumWeights];
memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight)); memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight));
} else { mesh->mBones[cb++] = bone;
// Assimp expects all bones to have at least 1 weight.
bone->mWeights = new aiVertexWeight[1];
bone->mNumWeights = 1;
bone->mWeights->mVertexId = 0;
bone->mWeights->mWeight = 0.f;
} }
mesh->mBones[i] = bone;
} }
if (pbindMatrices) { if (pbindMatrices) {
@ -1232,9 +1236,10 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
int numEmbeddedTexs = 0; int numEmbeddedTexs = 0;
for (size_t i = 0; i < r.images.Size(); ++i) { for (size_t i = 0; i < r.images.Size(); ++i) {
if (r.images[i].HasData()) if (r.images[i].HasData()) {
numEmbeddedTexs += 1; numEmbeddedTexs += 1;
} }
}
if (numEmbeddedTexs == 0) if (numEmbeddedTexs == 0)
return; return;
@ -1244,7 +1249,9 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
// Add the embedded textures // Add the embedded textures
for (size_t i = 0; i < r.images.Size(); ++i) { for (size_t i = 0; i < r.images.Size(); ++i) {
Image &img = r.images[i]; Image &img = r.images[i];
if (!img.HasData()) continue; if (!img.HasData()) {
continue;
}
int idx = mScene->mNumTextures++; int idx = mScene->mNumTextures++;
embeddedTexIdxs[i] = idx; embeddedTexIdxs[i] = idx;