Prevent to generate redundant morph targets for glTF2
parent
c394c6fdda
commit
8845d7eed3
|
@ -453,11 +453,16 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
|
||||||
aim->mNumAnimMeshes = (unsigned int)targets.size();
|
aim->mNumAnimMeshes = (unsigned int)targets.size();
|
||||||
aim->mAnimMeshes = new aiAnimMesh *[aim->mNumAnimMeshes];
|
aim->mAnimMeshes = new aiAnimMesh *[aim->mNumAnimMeshes];
|
||||||
for (size_t i = 0; i < targets.size(); i++) {
|
for (size_t i = 0; i < targets.size(); i++) {
|
||||||
aim->mAnimMeshes[i] = aiCreateAnimMesh(aim);
|
bool needPositions = targets[i].position.size() > 0;
|
||||||
|
bool needNormals = targets[i].normal.size() > 0;
|
||||||
|
bool needTangents = targets[i].tangent.size() > 0;
|
||||||
|
// GLTF morph does not support colors and texCoords
|
||||||
|
aim->mAnimMeshes[i] = aiCreateAnimMesh(aim,
|
||||||
|
needPositions, needNormals, needTangents, false, false);
|
||||||
aiAnimMesh &aiAnimMesh = *(aim->mAnimMeshes[i]);
|
aiAnimMesh &aiAnimMesh = *(aim->mAnimMeshes[i]);
|
||||||
Mesh::Primitive::Target &target = targets[i];
|
Mesh::Primitive::Target &target = targets[i];
|
||||||
|
|
||||||
if (target.position.size() > 0) {
|
if (needPositions) {
|
||||||
aiVector3D *positionDiff = nullptr;
|
aiVector3D *positionDiff = nullptr;
|
||||||
target.position[0]->ExtractData(positionDiff);
|
target.position[0]->ExtractData(positionDiff);
|
||||||
for (unsigned int vertexId = 0; vertexId < aim->mNumVertices; vertexId++) {
|
for (unsigned int vertexId = 0; vertexId < aim->mNumVertices; vertexId++) {
|
||||||
|
@ -465,7 +470,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
|
||||||
}
|
}
|
||||||
delete[] positionDiff;
|
delete[] positionDiff;
|
||||||
}
|
}
|
||||||
if (target.normal.size() > 0) {
|
if (needNormals) {
|
||||||
aiVector3D *normalDiff = nullptr;
|
aiVector3D *normalDiff = nullptr;
|
||||||
target.normal[0]->ExtractData(normalDiff);
|
target.normal[0]->ExtractData(normalDiff);
|
||||||
for (unsigned int vertexId = 0; vertexId < aim->mNumVertices; vertexId++) {
|
for (unsigned int vertexId = 0; vertexId < aim->mNumVertices; vertexId++) {
|
||||||
|
@ -473,7 +478,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
|
||||||
}
|
}
|
||||||
delete[] normalDiff;
|
delete[] normalDiff;
|
||||||
}
|
}
|
||||||
if (target.tangent.size() > 0) {
|
if (needTangents) {
|
||||||
Tangent *tangent = nullptr;
|
Tangent *tangent = nullptr;
|
||||||
attr.tangent[0]->ExtractData(tangent);
|
attr.tangent[0]->ExtractData(tangent);
|
||||||
|
|
||||||
|
|
|
@ -44,42 +44,46 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
namespace Assimp {
|
namespace Assimp {
|
||||||
|
|
||||||
aiAnimMesh *aiCreateAnimMesh(const aiMesh *mesh)
|
aiAnimMesh *aiCreateAnimMesh(const aiMesh *mesh, bool needPositions, bool needNormals, bool needTangents, bool needColors, bool needTexCoords)
|
||||||
{
|
{
|
||||||
aiAnimMesh *animesh = new aiAnimMesh;
|
aiAnimMesh *animesh = new aiAnimMesh;
|
||||||
animesh->mNumVertices = mesh->mNumVertices;
|
animesh->mNumVertices = mesh->mNumVertices;
|
||||||
if (mesh->mVertices) {
|
if (needPositions && mesh->mVertices) {
|
||||||
animesh->mVertices = new aiVector3D[animesh->mNumVertices];
|
animesh->mVertices = new aiVector3D[animesh->mNumVertices];
|
||||||
std::memcpy(animesh->mVertices, mesh->mVertices, mesh->mNumVertices * sizeof(aiVector3D));
|
std::memcpy(animesh->mVertices, mesh->mVertices, mesh->mNumVertices * sizeof(aiVector3D));
|
||||||
}
|
}
|
||||||
if (mesh->mNormals) {
|
if (needNormals && mesh->mNormals) {
|
||||||
animesh->mNormals = new aiVector3D[animesh->mNumVertices];
|
animesh->mNormals = new aiVector3D[animesh->mNumVertices];
|
||||||
std::memcpy(animesh->mNormals, mesh->mNormals, mesh->mNumVertices * sizeof(aiVector3D));
|
std::memcpy(animesh->mNormals, mesh->mNormals, mesh->mNumVertices * sizeof(aiVector3D));
|
||||||
}
|
}
|
||||||
if (mesh->mTangents) {
|
if (needTangents && mesh->mTangents) {
|
||||||
animesh->mTangents = new aiVector3D[animesh->mNumVertices];
|
animesh->mTangents = new aiVector3D[animesh->mNumVertices];
|
||||||
std::memcpy(animesh->mTangents, mesh->mTangents, mesh->mNumVertices * sizeof(aiVector3D));
|
std::memcpy(animesh->mTangents, mesh->mTangents, mesh->mNumVertices * sizeof(aiVector3D));
|
||||||
}
|
}
|
||||||
if (mesh->mBitangents) {
|
if (needTangents && mesh->mBitangents) {
|
||||||
animesh->mBitangents = new aiVector3D[animesh->mNumVertices];
|
animesh->mBitangents = new aiVector3D[animesh->mNumVertices];
|
||||||
std::memcpy(animesh->mBitangents, mesh->mBitangents, mesh->mNumVertices * sizeof(aiVector3D));
|
std::memcpy(animesh->mBitangents, mesh->mBitangents, mesh->mNumVertices * sizeof(aiVector3D));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
|
if (needColors) {
|
||||||
if (mesh->mColors[i]) {
|
for (int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) {
|
||||||
animesh->mColors[i] = new aiColor4D[animesh->mNumVertices];
|
if (mesh->mColors[i]) {
|
||||||
std::memcpy(animesh->mColors[i], mesh->mColors[i], mesh->mNumVertices * sizeof(aiColor4D));
|
animesh->mColors[i] = new aiColor4D[animesh->mNumVertices];
|
||||||
} else {
|
std::memcpy(animesh->mColors[i], mesh->mColors[i], mesh->mNumVertices * sizeof(aiColor4D));
|
||||||
animesh->mColors[i] = nullptr;
|
} else {
|
||||||
|
animesh->mColors[i] = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
if (needTexCoords) {
|
||||||
if (mesh->mTextureCoords[i]) {
|
for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
||||||
animesh->mTextureCoords[i] = new aiVector3D[animesh->mNumVertices];
|
if (mesh->mTextureCoords[i]) {
|
||||||
std::memcpy(animesh->mTextureCoords[i], mesh->mTextureCoords[i], mesh->mNumVertices * sizeof(aiVector3D));
|
animesh->mTextureCoords[i] = new aiVector3D[animesh->mNumVertices];
|
||||||
} else {
|
std::memcpy(animesh->mTextureCoords[i], mesh->mTextureCoords[i], mesh->mNumVertices * sizeof(aiVector3D));
|
||||||
animesh->mTextureCoords[i] = nullptr;
|
} else {
|
||||||
|
animesh->mTextureCoords[i] = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return animesh;
|
return animesh;
|
||||||
|
|
|
@ -57,10 +57,20 @@ namespace Assimp {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create aiAnimMesh from aiMesh.
|
* Create aiAnimMesh from aiMesh.
|
||||||
* @param mesh The input mesh to create an animated mesh from.
|
* @param mesh The input mesh to create an animated mesh from.
|
||||||
|
* @param needPositions If true, positions will be copied from.
|
||||||
|
* @param needNormals If true, normals will be copied from.
|
||||||
|
* @param needTangents If true, tangents and bitangents will be copied from.
|
||||||
|
* @param needColors If true, colors will be copied from.
|
||||||
|
* @param needTexCoords If true, texCoords will be copied from.
|
||||||
* @return The new created animated mesh.
|
* @return The new created animated mesh.
|
||||||
*/
|
*/
|
||||||
ASSIMP_API aiAnimMesh *aiCreateAnimMesh(const aiMesh *mesh);
|
ASSIMP_API aiAnimMesh *aiCreateAnimMesh(const aiMesh *mesh,
|
||||||
|
bool needPositions = true,
|
||||||
|
bool needNormals = true,
|
||||||
|
bool needTangents = true,
|
||||||
|
bool needColors = true,
|
||||||
|
bool needTexCoords = true);
|
||||||
|
|
||||||
} // end of namespace Assimp
|
} // end of namespace Assimp
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue