|
|
|
@ -90,7 +90,7 @@ void SplitByBoneCountProcess::Execute( aiScene* pScene)
|
|
|
|
|
|
|
|
|
|
// early out
|
|
|
|
|
bool isNecessary = false;
|
|
|
|
|
for( size_t a = 0; a < pScene->mNumMeshes; ++a)
|
|
|
|
|
for( unsigned int a = 0; a < pScene->mNumMeshes; ++a)
|
|
|
|
|
if( pScene->mMeshes[a]->mNumBones > mMaxBoneCount )
|
|
|
|
|
isNecessary = true;
|
|
|
|
|
|
|
|
|
@ -107,7 +107,7 @@ void SplitByBoneCountProcess::Execute( aiScene* pScene)
|
|
|
|
|
// build a new array of meshes for the scene
|
|
|
|
|
std::vector<aiMesh*> meshes;
|
|
|
|
|
|
|
|
|
|
for( size_t a = 0; a < pScene->mNumMeshes; ++a)
|
|
|
|
|
for( unsigned int a = 0; a < pScene->mNumMeshes; ++a)
|
|
|
|
|
{
|
|
|
|
|
aiMesh* srcMesh = pScene->mMeshes[a];
|
|
|
|
|
|
|
|
|
@ -118,9 +118,9 @@ void SplitByBoneCountProcess::Execute( aiScene* pScene)
|
|
|
|
|
if( !newMeshes.empty() )
|
|
|
|
|
{
|
|
|
|
|
// store new meshes and indices of the new meshes
|
|
|
|
|
for( size_t b = 0; b < newMeshes.size(); ++b)
|
|
|
|
|
for( unsigned int b = 0; b < newMeshes.size(); ++b)
|
|
|
|
|
{
|
|
|
|
|
mSubMeshIndices[a].push_back( meshes.size());
|
|
|
|
|
mSubMeshIndices[a].push_back( static_cast<unsigned int>(meshes.size()));
|
|
|
|
|
meshes.push_back( newMeshes[b]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -130,13 +130,13 @@ void SplitByBoneCountProcess::Execute( aiScene* pScene)
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Mesh is kept unchanged - store it's new place in the mesh array
|
|
|
|
|
mSubMeshIndices[a].push_back( meshes.size());
|
|
|
|
|
mSubMeshIndices[a].push_back( static_cast<unsigned int>(meshes.size()));
|
|
|
|
|
meshes.push_back( srcMesh);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// rebuild the scene's mesh array
|
|
|
|
|
pScene->mNumMeshes = meshes.size();
|
|
|
|
|
pScene->mNumMeshes = static_cast<unsigned int>(meshes.size());
|
|
|
|
|
delete [] pScene->mMeshes;
|
|
|
|
|
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
|
|
|
|
|
std::copy( meshes.begin(), meshes.end(), pScene->mMeshes);
|
|
|
|
@ -157,33 +157,33 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
|
|
|
|
|
// necessary optimisation: build a list of all affecting bones for each vertex
|
|
|
|
|
// TODO: (thom) maybe add a custom allocator here to avoid allocating tens of thousands of small arrays
|
|
|
|
|
typedef std::pair<size_t, float> BoneWeight;
|
|
|
|
|
typedef std::pair<unsigned int, float> BoneWeight;
|
|
|
|
|
std::vector< std::vector<BoneWeight> > vertexBones( pMesh->mNumVertices);
|
|
|
|
|
for( size_t a = 0; a < pMesh->mNumBones; ++a)
|
|
|
|
|
for( unsigned int a = 0; a < pMesh->mNumBones; ++a)
|
|
|
|
|
{
|
|
|
|
|
const aiBone* bone = pMesh->mBones[a];
|
|
|
|
|
for( size_t b = 0; b < bone->mNumWeights; ++b)
|
|
|
|
|
for( unsigned int b = 0; b < bone->mNumWeights; ++b)
|
|
|
|
|
vertexBones[ bone->mWeights[b].mVertexId ].push_back( BoneWeight( a, bone->mWeights[b].mWeight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t numFacesHandled = 0;
|
|
|
|
|
unsigned int numFacesHandled = 0;
|
|
|
|
|
std::vector<bool> isFaceHandled( pMesh->mNumFaces, false);
|
|
|
|
|
while( numFacesHandled < pMesh->mNumFaces )
|
|
|
|
|
{
|
|
|
|
|
// which bones are used in the current submesh
|
|
|
|
|
size_t numBones = 0;
|
|
|
|
|
unsigned int numBones = 0;
|
|
|
|
|
std::vector<bool> isBoneUsed( pMesh->mNumBones, false);
|
|
|
|
|
// indices of the faces which are going to go into this submesh
|
|
|
|
|
std::vector<size_t> subMeshFaces;
|
|
|
|
|
std::vector<unsigned int> subMeshFaces;
|
|
|
|
|
subMeshFaces.reserve( pMesh->mNumFaces);
|
|
|
|
|
// accumulated vertex count of all the faces in this submesh
|
|
|
|
|
size_t numSubMeshVertices = 0;
|
|
|
|
|
unsigned int numSubMeshVertices = 0;
|
|
|
|
|
// a small local array of new bones for the current face. State of all used bones for that face
|
|
|
|
|
// can only be updated AFTER the face is completely analysed. Thanks to imre for the fix.
|
|
|
|
|
std::vector<size_t> newBonesAtCurrentFace;
|
|
|
|
|
std::vector<unsigned int> newBonesAtCurrentFace;
|
|
|
|
|
|
|
|
|
|
// add faces to the new submesh as long as all bones affecting the faces' vertices fit in the limit
|
|
|
|
|
for( size_t a = 0; a < pMesh->mNumFaces; ++a)
|
|
|
|
|
for( unsigned int a = 0; a < pMesh->mNumFaces; ++a)
|
|
|
|
|
{
|
|
|
|
|
// skip if the face is already stored in a submesh
|
|
|
|
|
if( isFaceHandled[a] )
|
|
|
|
@ -191,12 +191,12 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
|
|
|
|
|
const aiFace& face = pMesh->mFaces[a];
|
|
|
|
|
// check every vertex if its bones would still fit into the current submesh
|
|
|
|
|
for( size_t b = 0; b < face.mNumIndices; ++b )
|
|
|
|
|
for( unsigned int b = 0; b < face.mNumIndices; ++b )
|
|
|
|
|
{
|
|
|
|
|
const std::vector<BoneWeight>& vb = vertexBones[face.mIndices[b]];
|
|
|
|
|
for( size_t c = 0; c < vb.size(); ++c)
|
|
|
|
|
for( unsigned int c = 0; c < vb.size(); ++c)
|
|
|
|
|
{
|
|
|
|
|
size_t boneIndex = vb[c].first;
|
|
|
|
|
unsigned int boneIndex = vb[c].first;
|
|
|
|
|
// if the bone is already used in this submesh, it's ok
|
|
|
|
|
if( isBoneUsed[boneIndex] )
|
|
|
|
|
continue;
|
|
|
|
@ -214,7 +214,7 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
// mark all new bones as necessary
|
|
|
|
|
while( !newBonesAtCurrentFace.empty() )
|
|
|
|
|
{
|
|
|
|
|
size_t newIndex = newBonesAtCurrentFace.back();
|
|
|
|
|
unsigned int newIndex = newBonesAtCurrentFace.back();
|
|
|
|
|
newBonesAtCurrentFace.pop_back(); // this also avoids the deallocation which comes with a clear()
|
|
|
|
|
if( isBoneUsed[newIndex] )
|
|
|
|
|
continue;
|
|
|
|
@ -242,7 +242,7 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
|
|
|
|
|
// create all the arrays for this mesh if the old mesh contained them
|
|
|
|
|
newMesh->mNumVertices = numSubMeshVertices;
|
|
|
|
|
newMesh->mNumFaces = subMeshFaces.size();
|
|
|
|
|
newMesh->mNumFaces = static_cast<unsigned int>(subMeshFaces.size());
|
|
|
|
|
newMesh->mVertices = new aiVector3D[newMesh->mNumVertices];
|
|
|
|
|
if( pMesh->HasNormals() )
|
|
|
|
|
newMesh->mNormals = new aiVector3D[newMesh->mNumVertices];
|
|
|
|
@ -251,13 +251,13 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
newMesh->mTangents = new aiVector3D[newMesh->mNumVertices];
|
|
|
|
|
newMesh->mBitangents = new aiVector3D[newMesh->mNumVertices];
|
|
|
|
|
}
|
|
|
|
|
for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
|
|
|
|
|
for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
|
|
|
|
|
{
|
|
|
|
|
if( pMesh->HasTextureCoords( a) )
|
|
|
|
|
newMesh->mTextureCoords[a] = new aiVector3D[newMesh->mNumVertices];
|
|
|
|
|
newMesh->mNumUVComponents[a] = pMesh->mNumUVComponents[a];
|
|
|
|
|
}
|
|
|
|
|
for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
|
|
|
|
|
for( unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
|
|
|
|
|
{
|
|
|
|
|
if( pMesh->HasVertexColors( a) )
|
|
|
|
|
newMesh->mColors[a] = new aiColor4D[newMesh->mNumVertices];
|
|
|
|
@ -265,9 +265,9 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
|
|
|
|
|
// and copy over the data, generating faces with linear indices along the way
|
|
|
|
|
newMesh->mFaces = new aiFace[subMeshFaces.size()];
|
|
|
|
|
size_t nvi = 0; // next vertex index
|
|
|
|
|
std::vector<size_t> previousVertexIndices( numSubMeshVertices, std::numeric_limits<size_t>::max()); // per new vertex: its index in the source mesh
|
|
|
|
|
for( size_t a = 0; a < subMeshFaces.size(); ++a )
|
|
|
|
|
unsigned int nvi = 0; // next vertex index
|
|
|
|
|
std::vector<unsigned int> previousVertexIndices( numSubMeshVertices, std::numeric_limits<unsigned int>::max()); // per new vertex: its index in the source mesh
|
|
|
|
|
for( unsigned int a = 0; a < subMeshFaces.size(); ++a )
|
|
|
|
|
{
|
|
|
|
|
const aiFace& srcFace = pMesh->mFaces[subMeshFaces[a]];
|
|
|
|
|
aiFace& dstFace = newMesh->mFaces[a];
|
|
|
|
@ -275,9 +275,9 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
dstFace.mIndices = new unsigned int[dstFace.mNumIndices];
|
|
|
|
|
|
|
|
|
|
// accumulate linearly all the vertices of the source face
|
|
|
|
|
for( size_t b = 0; b < dstFace.mNumIndices; ++b )
|
|
|
|
|
for( unsigned int b = 0; b < dstFace.mNumIndices; ++b )
|
|
|
|
|
{
|
|
|
|
|
size_t srcIndex = srcFace.mIndices[b];
|
|
|
|
|
unsigned int srcIndex = srcFace.mIndices[b];
|
|
|
|
|
dstFace.mIndices[b] = nvi;
|
|
|
|
|
previousVertexIndices[nvi] = srcIndex;
|
|
|
|
|
|
|
|
|
@ -289,12 +289,12 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
newMesh->mTangents[nvi] = pMesh->mTangents[srcIndex];
|
|
|
|
|
newMesh->mBitangents[nvi] = pMesh->mBitangents[srcIndex];
|
|
|
|
|
}
|
|
|
|
|
for( size_t c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++c )
|
|
|
|
|
for( unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++c )
|
|
|
|
|
{
|
|
|
|
|
if( pMesh->HasTextureCoords( c) )
|
|
|
|
|
newMesh->mTextureCoords[c][nvi] = pMesh->mTextureCoords[c][srcIndex];
|
|
|
|
|
}
|
|
|
|
|
for( size_t c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS; ++c )
|
|
|
|
|
for( unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS; ++c )
|
|
|
|
|
{
|
|
|
|
|
if( pMesh->HasVertexColors( c) )
|
|
|
|
|
newMesh->mColors[c][nvi] = pMesh->mColors[c][srcIndex];
|
|
|
|
@ -310,8 +310,8 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
newMesh->mNumBones = 0;
|
|
|
|
|
newMesh->mBones = new aiBone*[numBones];
|
|
|
|
|
|
|
|
|
|
std::vector<size_t> mappedBoneIndex( pMesh->mNumBones, std::numeric_limits<size_t>::max());
|
|
|
|
|
for( size_t a = 0; a < pMesh->mNumBones; ++a )
|
|
|
|
|
std::vector<unsigned int> mappedBoneIndex( pMesh->mNumBones, std::numeric_limits<unsigned int>::max());
|
|
|
|
|
for( unsigned int a = 0; a < pMesh->mNumBones; ++a )
|
|
|
|
|
{
|
|
|
|
|
if( !isBoneUsed[a] )
|
|
|
|
|
continue;
|
|
|
|
@ -329,21 +329,21 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
ai_assert( newMesh->mNumBones == numBones );
|
|
|
|
|
|
|
|
|
|
// iterate over all new vertices and count which bones affected its old vertex in the source mesh
|
|
|
|
|
for( size_t a = 0; a < numSubMeshVertices; ++a )
|
|
|
|
|
for( unsigned int a = 0; a < numSubMeshVertices; ++a )
|
|
|
|
|
{
|
|
|
|
|
size_t oldIndex = previousVertexIndices[a];
|
|
|
|
|
unsigned int oldIndex = previousVertexIndices[a];
|
|
|
|
|
const std::vector<BoneWeight>& bonesOnThisVertex = vertexBones[oldIndex];
|
|
|
|
|
|
|
|
|
|
for( size_t b = 0; b < bonesOnThisVertex.size(); ++b )
|
|
|
|
|
for( unsigned int b = 0; b < bonesOnThisVertex.size(); ++b )
|
|
|
|
|
{
|
|
|
|
|
size_t newBoneIndex = mappedBoneIndex[ bonesOnThisVertex[b].first ];
|
|
|
|
|
if( newBoneIndex != std::numeric_limits<size_t>::max() )
|
|
|
|
|
unsigned int newBoneIndex = mappedBoneIndex[ bonesOnThisVertex[b].first ];
|
|
|
|
|
if( newBoneIndex != std::numeric_limits<unsigned int>::max() )
|
|
|
|
|
newMesh->mBones[newBoneIndex]->mNumWeights++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// allocate all bone weight arrays accordingly
|
|
|
|
|
for( size_t a = 0; a < newMesh->mNumBones; ++a )
|
|
|
|
|
for( unsigned int a = 0; a < newMesh->mNumBones; ++a )
|
|
|
|
|
{
|
|
|
|
|
aiBone* bone = newMesh->mBones[a];
|
|
|
|
|
ai_assert( bone->mNumWeights > 0 );
|
|
|
|
@ -352,18 +352,18 @@ void SplitByBoneCountProcess::SplitMesh( const aiMesh* pMesh, std::vector<aiMesh
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// now copy all the bone vertex weights for all the vertices which made it into the new submesh
|
|
|
|
|
for( size_t a = 0; a < numSubMeshVertices; ++a)
|
|
|
|
|
for( unsigned int a = 0; a < numSubMeshVertices; ++a)
|
|
|
|
|
{
|
|
|
|
|
// find the source vertex for it in the source mesh
|
|
|
|
|
size_t previousIndex = previousVertexIndices[a];
|
|
|
|
|
unsigned int previousIndex = previousVertexIndices[a];
|
|
|
|
|
// these bones were affecting it
|
|
|
|
|
const std::vector<BoneWeight>& bonesOnThisVertex = vertexBones[previousIndex];
|
|
|
|
|
// all of the bones affecting it should be present in the new submesh, or else
|
|
|
|
|
// the face it comprises shouldn't be present
|
|
|
|
|
for( size_t b = 0; b < bonesOnThisVertex.size(); ++b)
|
|
|
|
|
for( unsigned int b = 0; b < bonesOnThisVertex.size(); ++b)
|
|
|
|
|
{
|
|
|
|
|
size_t newBoneIndex = mappedBoneIndex[ bonesOnThisVertex[b].first ];
|
|
|
|
|
ai_assert( newBoneIndex != std::numeric_limits<size_t>::max() );
|
|
|
|
|
unsigned int newBoneIndex = mappedBoneIndex[ bonesOnThisVertex[b].first ];
|
|
|
|
|
ai_assert( newBoneIndex != std::numeric_limits<unsigned int>::max() );
|
|
|
|
|
aiVertexWeight* dstWeight = newMesh->mBones[newBoneIndex]->mWeights + newMesh->mBones[newBoneIndex]->mNumWeights;
|
|
|
|
|
newMesh->mBones[newBoneIndex]->mNumWeights++;
|
|
|
|
|
|
|
|
|
@ -383,22 +383,22 @@ void SplitByBoneCountProcess::UpdateNode( aiNode* pNode) const
|
|
|
|
|
// rebuild the node's mesh index list
|
|
|
|
|
if( pNode->mNumMeshes > 0 )
|
|
|
|
|
{
|
|
|
|
|
std::vector<size_t> newMeshList;
|
|
|
|
|
for( size_t a = 0; a < pNode->mNumMeshes; ++a)
|
|
|
|
|
std::vector<unsigned int> newMeshList;
|
|
|
|
|
for( unsigned int a = 0; a < pNode->mNumMeshes; ++a)
|
|
|
|
|
{
|
|
|
|
|
size_t srcIndex = pNode->mMeshes[a];
|
|
|
|
|
const std::vector<size_t>& replaceMeshes = mSubMeshIndices[srcIndex];
|
|
|
|
|
unsigned int srcIndex = pNode->mMeshes[a];
|
|
|
|
|
const std::vector<unsigned int>& replaceMeshes = mSubMeshIndices[srcIndex];
|
|
|
|
|
newMeshList.insert( newMeshList.end(), replaceMeshes.begin(), replaceMeshes.end());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete pNode->mMeshes;
|
|
|
|
|
pNode->mNumMeshes = newMeshList.size();
|
|
|
|
|
pNode->mNumMeshes = static_cast<unsigned int>(newMeshList.size());
|
|
|
|
|
pNode->mMeshes = new unsigned int[pNode->mNumMeshes];
|
|
|
|
|
std::copy( newMeshList.begin(), newMeshList.end(), pNode->mMeshes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// do that also recursively for all children
|
|
|
|
|
for( size_t a = 0; a < pNode->mNumChildren; ++a )
|
|
|
|
|
for( unsigned int a = 0; a < pNode->mNumChildren; ++a )
|
|
|
|
|
{
|
|
|
|
|
UpdateNode( pNode->mChildren[a]);
|
|
|
|
|
}
|
|
|
|
|