Merge branch 'master' into master

pull/3042/head
Adrian Iusca 2020-03-05 10:29:21 +02:00 committed by GitHub
commit 067677595c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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,8 +324,9 @@ 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;
@ -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,43 +887,33 @@ 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;
aiBone *bone = new aiBone(); for (uint32_t i = 0; i < numBones; ++i) {
const std::vector<aiVertexWeight> &weights = weighting[i];
if (weights.size() > 0) {
aiBone *bone = new aiBone();
Ref<Node> joint = node.skin->jointNames[i]; Ref<Node> joint = node.skin->jointNames[i];
if (!joint->name.empty()) { if (!joint->name.empty()) {
bone->mName = joint->name; bone->mName = joint->name;
} else { } else {
// Assimp expects each bone to have a unique name. // Assimp expects each bone to have a unique name.
static const std::string kDefaultName = "bone_"; static const std::string kDefaultName = "bone_";
char postfix[10] = { 0 }; char postfix[10] = { 0 };
ASSIMP_itoa10(postfix, i); ASSIMP_itoa10(postfix, i);
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); bone->mNumWeights = static_cast<uint32_t>(weights.size());
std::vector<aiVertexWeight> &weights = weighting[i];
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,8 +1236,9 @@ 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)
@ -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;