2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2021-07-29 11:28:51 +00:00
|
|
|
/**
|
2018-10-08 19:06:48 +00:00
|
|
|
* @file Implementation of the SplitLargeMeshes postprocessing step
|
|
|
|
*/
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// internal headers of the post-processing framework
|
|
|
|
#include "SplitLargeMeshes.h"
|
|
|
|
#include "ProcessHelper.h"
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2018-10-08 19:06:48 +00:00
|
|
|
SplitLargeMeshesProcess_Triangle::SplitLargeMeshesProcess_Triangle() {
|
2015-05-19 03:57:13 +00:00
|
|
|
LIMIT = AI_SLM_DEFAULT_MAX_TRIANGLES;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2018-10-08 19:06:48 +00:00
|
|
|
SplitLargeMeshesProcess_Triangle::~SplitLargeMeshesProcess_Triangle() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to do here
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
2018-10-08 19:06:48 +00:00
|
|
|
bool SplitLargeMeshesProcess_Triangle::IsActive( unsigned int pFlags) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return (pFlags & aiProcess_SplitLargeMeshes) != 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
2018-10-08 19:06:48 +00:00
|
|
|
void SplitLargeMeshesProcess_Triangle::Execute( aiScene* pScene) {
|
2018-10-09 09:56:30 +00:00
|
|
|
if (0xffffffff == this->LIMIT || nullptr == pScene ) {
|
2018-10-08 19:06:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Triangle begin");
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector<std::pair<aiMesh*, unsigned int> > avList;
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
for( unsigned int a = 0; a < pScene->mNumMeshes; ++a) {
|
2015-05-19 03:57:13 +00:00
|
|
|
this->SplitMesh(a, pScene->mMeshes[a],avList);
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
if (avList.size() != pScene->mNumMeshes) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// it seems something has been split. rebuild the mesh list
|
|
|
|
delete[] pScene->mMeshes;
|
|
|
|
pScene->mNumMeshes = (unsigned int)avList.size();
|
|
|
|
pScene->mMeshes = new aiMesh*[avList.size()];
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int i = 0; i < avList.size();++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mMeshes[i] = avList[i].first;
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now we need to update all nodes
|
|
|
|
this->UpdateNode(pScene->mRootNode,avList);
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_INFO("SplitLargeMeshesProcess_Triangle finished. Meshes have been split");
|
2018-10-08 19:06:48 +00:00
|
|
|
} else {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Triangle finished. There was nothing to do");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Setup properties
|
2018-10-08 19:06:48 +00:00
|
|
|
void SplitLargeMeshesProcess_Triangle::SetupProperties( const Importer* pImp) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// get the current value of the split property
|
2015-05-19 03:57:13 +00:00
|
|
|
this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Update a node after some meshes have been split
|
|
|
|
void SplitLargeMeshesProcess_Triangle::UpdateNode(aiNode* pcNode,
|
2018-10-08 19:06:48 +00:00
|
|
|
const std::vector<std::pair<aiMesh*, unsigned int> >& avList) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// for every index in out list build a new entry
|
|
|
|
std::vector<unsigned int> aiEntries;
|
|
|
|
aiEntries.reserve(pcNode->mNumMeshes + 1);
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int i = 0; i < pcNode->mNumMeshes;++i) {
|
|
|
|
for (unsigned int a = 0; a < avList.size();++a) {
|
|
|
|
if (avList[a].second == pcNode->mMeshes[i]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiEntries.push_back(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now build the new list
|
2016-01-26 21:47:50 +00:00
|
|
|
delete[] pcNode->mMeshes;
|
2015-05-19 03:57:13 +00:00
|
|
|
pcNode->mNumMeshes = (unsigned int)aiEntries.size();
|
|
|
|
pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int b = 0; b < pcNode->mNumMeshes;++b) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcNode->mMeshes[b] = aiEntries[b];
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// recusively update all other nodes
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
UpdateNode ( pcNode->mChildren[i], avList );
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
|
|
|
void SplitLargeMeshesProcess_Triangle::SplitMesh(
|
2018-10-08 19:06:48 +00:00
|
|
|
unsigned int a,
|
|
|
|
aiMesh* pMesh,
|
|
|
|
std::vector<std::pair<aiMesh*, unsigned int> >& avList) {
|
|
|
|
if (pMesh->mNumFaces > SplitLargeMeshesProcess_Triangle::LIMIT) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_INFO("Mesh exceeds the triangle limit. It will be split ...");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// we need to split this mesh into sub meshes
|
|
|
|
// determine the size of a submesh
|
|
|
|
const unsigned int iSubMeshes = (pMesh->mNumFaces / LIMIT) + 1;
|
|
|
|
|
|
|
|
const unsigned int iOutFaceNum = pMesh->mNumFaces / iSubMeshes;
|
|
|
|
const unsigned int iOutVertexNum = iOutFaceNum * 3;
|
|
|
|
|
|
|
|
// now generate all submeshes
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int i = 0; i < iSubMeshes;++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMesh* pcMesh = new aiMesh;
|
|
|
|
pcMesh->mNumFaces = iOutFaceNum;
|
|
|
|
pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
|
|
|
|
|
|
|
|
// the name carries the adjacency information between the meshes
|
|
|
|
pcMesh->mName = pMesh->mName;
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
if (i == iSubMeshes-1) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mNumFaces = iOutFaceNum + (
|
|
|
|
pMesh->mNumFaces - iOutFaceNum * iSubMeshes);
|
|
|
|
}
|
|
|
|
// copy the list of faces
|
|
|
|
pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
|
|
|
|
|
|
|
|
const unsigned int iBase = iOutFaceNum * i;
|
|
|
|
|
|
|
|
// get the total number of indices
|
|
|
|
unsigned int iCnt = 0;
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int p = iBase; p < pcMesh->mNumFaces + iBase;++p) {
|
2015-05-19 03:57:13 +00:00
|
|
|
iCnt += pMesh->mFaces[p].mNumIndices;
|
|
|
|
}
|
|
|
|
pcMesh->mNumVertices = iCnt;
|
|
|
|
|
|
|
|
// allocate storage
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->mVertices != nullptr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mVertices = new aiVector3D[iCnt];
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasNormals()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mNormals = new aiVector3D[iCnt];
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasTangentsAndBitangents()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mTangents = new aiVector3D[iCnt];
|
|
|
|
pcMesh->mBitangents = new aiVector3D[iCnt];
|
|
|
|
}
|
|
|
|
|
|
|
|
// texture coordinates
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasTextureCoords( c)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mTextureCoords[c] = new aiVector3D[iCnt];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertex colors
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
|
|
|
|
if (pMesh->HasVertexColors( c)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mColors[c] = new aiColor4D[iCnt];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasBones()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// assume the number of bones won't change in most cases
|
|
|
|
pcMesh->mBones = new aiBone*[pMesh->mNumBones];
|
|
|
|
|
|
|
|
// iterate through all bones of the mesh and find those which
|
|
|
|
// need to be copied to the split mesh
|
|
|
|
std::vector<aiVertexWeight> avTempWeights;
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int p = 0; p < pcMesh->mNumBones;++p) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiBone* const bone = pcMesh->mBones[p];
|
|
|
|
avTempWeights.clear();
|
|
|
|
avTempWeights.reserve(bone->mNumWeights / iSubMeshes);
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int q = 0; q < bone->mNumWeights;++q) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiVertexWeight& weight = bone->mWeights[q];
|
2018-10-08 19:06:48 +00:00
|
|
|
if(weight.mVertexId >= iBase && weight.mVertexId < iBase + iOutVertexNum) {
|
2015-05-19 03:57:13 +00:00
|
|
|
avTempWeights.push_back(weight);
|
|
|
|
weight = avTempWeights.back();
|
|
|
|
weight.mVertexId -= iBase;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
if (!avTempWeights.empty()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// we'll need this bone. Copy it ...
|
|
|
|
aiBone* pc = new aiBone();
|
|
|
|
pcMesh->mBones[pcMesh->mNumBones++] = pc;
|
|
|
|
pc->mName = aiString(bone->mName);
|
|
|
|
pc->mNumWeights = (unsigned int)avTempWeights.size();
|
|
|
|
pc->mOffsetMatrix = bone->mOffsetMatrix;
|
|
|
|
|
|
|
|
// no need to reallocate the array for the last submesh.
|
|
|
|
// Here we can reuse the (large) source array, although
|
|
|
|
// we'll waste some memory
|
2018-10-08 19:06:48 +00:00
|
|
|
if (iSubMeshes-1 == i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pc->mWeights = bone->mWeights;
|
2018-10-08 19:06:48 +00:00
|
|
|
bone->mWeights = nullptr;
|
|
|
|
} else {
|
|
|
|
pc->mWeights = new aiVertexWeight[pc->mNumWeights];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy the weights
|
|
|
|
::memcpy(pc->mWeights,&avTempWeights[0],sizeof(aiVertexWeight)*pc->mNumWeights);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// (we will also need to copy the array of indices)
|
|
|
|
unsigned int iCurrent = 0;
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int p = 0; p < pcMesh->mNumFaces;++p) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mFaces[p].mNumIndices = 3;
|
|
|
|
// allocate a new array
|
|
|
|
const unsigned int iTemp = p + iBase;
|
|
|
|
const unsigned int iNumIndices = pMesh->mFaces[iTemp].mNumIndices;
|
|
|
|
|
|
|
|
// setup face type and number of indices
|
|
|
|
pcMesh->mFaces[p].mNumIndices = iNumIndices;
|
|
|
|
unsigned int* pi = pMesh->mFaces[iTemp].mIndices;
|
|
|
|
unsigned int* piOut = pcMesh->mFaces[p].mIndices = new unsigned int[iNumIndices];
|
|
|
|
|
|
|
|
// need to update the output primitive types
|
2018-10-08 19:06:48 +00:00
|
|
|
switch (iNumIndices) {
|
2015-05-19 03:57:13 +00:00
|
|
|
case 1:
|
|
|
|
pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and copy the contents of the old array, offset by current base
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int v = 0; v < iNumIndices;++v) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int iIndex = pi[v];
|
|
|
|
unsigned int iIndexOut = iCurrent++;
|
|
|
|
piOut[v] = iIndexOut;
|
|
|
|
|
|
|
|
// copy positions
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->mVertices != nullptr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mVertices[iIndexOut] = pMesh->mVertices[iIndex];
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy normals
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasNormals()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mNormals[iIndexOut] = pMesh->mNormals[iIndex];
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// copy tangents/bitangents
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasTangentsAndBitangents()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mTangents[iIndexOut] = pMesh->mTangents[iIndex];
|
|
|
|
pcMesh->mBitangents[iIndexOut] = pMesh->mBitangents[iIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// texture coordinates
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
|
|
|
|
if (pMesh->HasTextureCoords( c ) ) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mTextureCoords[c][iIndexOut] = pMesh->mTextureCoords[c][iIndex];
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
// vertex colors
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
|
|
|
|
if (pMesh->HasVertexColors( c)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mColors[c][iIndexOut] = pMesh->mColors[c][iIndex];
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the newly created mesh to the list
|
|
|
|
avList.push_back(std::pair<aiMesh*, unsigned int>(pcMesh,a));
|
|
|
|
}
|
|
|
|
|
|
|
|
// now delete the old mesh data
|
|
|
|
delete pMesh;
|
2018-10-08 19:06:48 +00:00
|
|
|
} else {
|
|
|
|
avList.push_back(std::pair<aiMesh*, unsigned int>(pMesh,a));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2018-10-08 19:06:48 +00:00
|
|
|
SplitLargeMeshesProcess_Vertex::SplitLargeMeshesProcess_Vertex() {
|
2015-05-19 03:57:13 +00:00
|
|
|
LIMIT = AI_SLM_DEFAULT_MAX_VERTICES;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2018-10-08 19:06:48 +00:00
|
|
|
SplitLargeMeshesProcess_Vertex::~SplitLargeMeshesProcess_Vertex() {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to do here
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
2018-10-08 19:06:48 +00:00
|
|
|
bool SplitLargeMeshesProcess_Vertex::IsActive( unsigned int pFlags) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return (pFlags & aiProcess_SplitLargeMeshes) != 0;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
2018-10-08 19:06:48 +00:00
|
|
|
void SplitLargeMeshesProcess_Vertex::Execute( aiScene* pScene) {
|
|
|
|
if (0xffffffff == this->LIMIT || nullptr == pScene ) {
|
2018-09-20 08:31:32 +00:00
|
|
|
return;
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Vertex begin");
|
2018-09-20 08:31:32 +00:00
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
std::vector<std::pair<aiMesh*, unsigned int> > avList;
|
|
|
|
|
2021-07-29 11:28:51 +00:00
|
|
|
//Check for point cloud first,
|
2018-09-20 08:31:32 +00:00
|
|
|
//Do not process point cloud, splitMesh works only with faces data
|
|
|
|
for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
|
2018-10-09 13:34:44 +00:00
|
|
|
if ( pScene->mMeshes[a]->mPrimitiveTypes == aiPrimitiveType_POINT ) {
|
2018-09-20 08:31:32 +00:00
|
|
|
return;
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2018-09-20 08:31:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
for( unsigned int a = 0; a < pScene->mNumMeshes; ++a ) {
|
2018-09-20 08:31:32 +00:00
|
|
|
this->SplitMesh(a, pScene->mMeshes[a], avList);
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-09-20 08:31:32 +00:00
|
|
|
if (avList.size() != pScene->mNumMeshes) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// it seems something has been split. rebuild the mesh list
|
|
|
|
delete[] pScene->mMeshes;
|
|
|
|
pScene->mNumMeshes = (unsigned int)avList.size();
|
|
|
|
pScene->mMeshes = new aiMesh*[avList.size()];
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int i = 0; i < avList.size();++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mMeshes[i] = avList[i].first;
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now we need to update all nodes
|
|
|
|
SplitLargeMeshesProcess_Triangle::UpdateNode(pScene->mRootNode,avList);
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_INFO("SplitLargeMeshesProcess_Vertex finished. Meshes have been split");
|
|
|
|
} else {
|
|
|
|
ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Vertex finished. There was nothing to do");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Setup properties
|
2018-10-08 19:06:48 +00:00
|
|
|
void SplitLargeMeshesProcess_Vertex::SetupProperties( const Importer* pImp) {
|
2015-05-19 03:57:13 +00:00
|
|
|
this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
|
|
|
void SplitLargeMeshesProcess_Vertex::SplitMesh(
|
2018-10-08 19:06:48 +00:00
|
|
|
unsigned int a,
|
|
|
|
aiMesh* pMesh,
|
|
|
|
std::vector<std::pair<aiMesh*, unsigned int> >& avList) {
|
|
|
|
if (pMesh->mNumVertices > SplitLargeMeshesProcess_Vertex::LIMIT) {
|
2015-05-19 03:57:13 +00:00
|
|
|
typedef std::vector< std::pair<unsigned int,float> > VertexWeightTable;
|
|
|
|
|
|
|
|
// build a per-vertex weight list if necessary
|
|
|
|
VertexWeightTable* avPerVertexWeights = ComputeVertexBoneWeightTable(pMesh);
|
|
|
|
|
|
|
|
// we need to split this mesh into sub meshes
|
|
|
|
// determine the estimated size of a submesh
|
|
|
|
// (this could be too large. Max waste is a single digit percentage)
|
|
|
|
const unsigned int iSubMeshes = (pMesh->mNumVertices / SplitLargeMeshesProcess_Vertex::LIMIT) + 1;
|
|
|
|
|
|
|
|
// create a std::vector<unsigned int> to indicate which vertices
|
|
|
|
// have already been copied
|
|
|
|
std::vector<unsigned int> avWasCopied;
|
|
|
|
avWasCopied.resize(pMesh->mNumVertices,0xFFFFFFFF);
|
|
|
|
|
|
|
|
// try to find a good estimate for the number of output faces
|
|
|
|
// per mesh. Add 12.5% as buffer
|
|
|
|
unsigned int iEstimatedSize = pMesh->mNumFaces / iSubMeshes;
|
|
|
|
iEstimatedSize += iEstimatedSize >> 3;
|
|
|
|
|
|
|
|
// now generate all submeshes
|
2018-10-08 19:06:48 +00:00
|
|
|
unsigned int iBase( 0 );
|
|
|
|
while (true) {
|
2015-05-19 03:57:13 +00:00
|
|
|
const unsigned int iOutVertexNum = SplitLargeMeshesProcess_Vertex::LIMIT;
|
|
|
|
aiMesh* pcMesh = new aiMesh;
|
|
|
|
pcMesh->mNumVertices = 0;
|
|
|
|
pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
|
|
|
|
|
|
|
|
// the name carries the adjacency information between the meshes
|
|
|
|
pcMesh->mName = pMesh->mName;
|
|
|
|
|
|
|
|
typedef std::vector<aiVertexWeight> BoneWeightList;
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasBones()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mBones = new aiBone*[pMesh->mNumBones];
|
|
|
|
::memset(pcMesh->mBones,0,sizeof(void*)*pMesh->mNumBones);
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear the temporary helper array
|
2018-10-08 19:06:48 +00:00
|
|
|
if (iBase) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// we can't use memset here we unsigned int needn' be 32 bits
|
2018-10-08 19:06:48 +00:00
|
|
|
for (auto &elem : avWasCopied) {
|
2016-05-22 10:55:47 +00:00
|
|
|
elem = 0xffffffff;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// output vectors
|
|
|
|
std::vector<aiFace> vFaces;
|
|
|
|
|
|
|
|
// reserve enough storage for most cases
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasPositions()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mVertices = new aiVector3D[iOutVertexNum];
|
|
|
|
}
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasNormals()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mNormals = new aiVector3D[iOutVertexNum];
|
|
|
|
}
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasTangentsAndBitangents()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mTangents = new aiVector3D[iOutVertexNum];
|
|
|
|
pcMesh->mBitangents = new aiVector3D[iOutVertexNum];
|
|
|
|
}
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int c = 0; pMesh->HasVertexColors(c);++c) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mColors[c] = new aiColor4D[iOutVertexNum];
|
|
|
|
}
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int c = 0; pMesh->HasTextureCoords(c);++c) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
|
|
|
|
pcMesh->mTextureCoords[c] = new aiVector3D[iOutVertexNum];
|
|
|
|
}
|
|
|
|
vFaces.reserve(iEstimatedSize);
|
|
|
|
|
|
|
|
// (we will also need to copy the array of indices)
|
2018-10-08 19:06:48 +00:00
|
|
|
while (iBase < pMesh->mNumFaces) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// allocate a new array
|
|
|
|
const unsigned int iNumIndices = pMesh->mFaces[iBase].mNumIndices;
|
|
|
|
|
|
|
|
// doesn't catch degenerates but is quite fast
|
|
|
|
unsigned int iNeed = 0;
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int v = 0; v < iNumIndices;++v) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
|
|
|
|
|
|
|
|
// check whether we do already have this vertex
|
2018-10-08 19:06:48 +00:00
|
|
|
if (0xFFFFFFFF == avWasCopied[iIndex]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
iNeed++;
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pcMesh->mNumVertices + iNeed > iOutVertexNum) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// don't use this face
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
vFaces.push_back(aiFace());
|
|
|
|
aiFace& rFace = vFaces.back();
|
|
|
|
|
|
|
|
// setup face type and number of indices
|
|
|
|
rFace.mNumIndices = iNumIndices;
|
|
|
|
rFace.mIndices = new unsigned int[iNumIndices];
|
|
|
|
|
|
|
|
// need to update the output primitive types
|
2018-10-08 19:06:48 +00:00
|
|
|
switch (rFace.mNumIndices) {
|
2015-05-19 03:57:13 +00:00
|
|
|
case 1:
|
|
|
|
pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and copy the contents of the old array, offset by current base
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int v = 0; v < iNumIndices;++v) {
|
2015-05-19 03:57:13 +00:00
|
|
|
unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
|
|
|
|
|
|
|
|
// check whether we do already have this vertex
|
2018-10-08 19:06:48 +00:00
|
|
|
if (0xFFFFFFFF != avWasCopied[iIndex]) {
|
2015-05-19 03:57:13 +00:00
|
|
|
rFace.mIndices[v] = avWasCopied[iIndex];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy positions
|
|
|
|
pcMesh->mVertices[pcMesh->mNumVertices] = (pMesh->mVertices[iIndex]);
|
|
|
|
|
|
|
|
// copy normals
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasNormals()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mNormals[pcMesh->mNumVertices] = (pMesh->mNormals[iIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy tangents/bitangents
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasTangentsAndBitangents()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mTangents[pcMesh->mNumVertices] = (pMesh->mTangents[iIndex]);
|
|
|
|
pcMesh->mBitangents[pcMesh->mNumVertices] = (pMesh->mBitangents[iIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// texture coordinates
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
|
|
|
|
if (pMesh->HasTextureCoords( c)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mTextureCoords[c][pcMesh->mNumVertices] = pMesh->mTextureCoords[c][iIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// vertex colors
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
|
|
|
|
if (pMesh->HasVertexColors( c)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mColors[c][pcMesh->mNumVertices] = pMesh->mColors[c][iIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check whether we have bone weights assigned to this vertex
|
|
|
|
rFace.mIndices[v] = pcMesh->mNumVertices;
|
2018-10-08 19:06:48 +00:00
|
|
|
if (avPerVertexWeights) {
|
2015-05-19 03:57:13 +00:00
|
|
|
VertexWeightTable& table = avPerVertexWeights[ pcMesh->mNumVertices ];
|
2018-10-08 19:06:48 +00:00
|
|
|
if( !table.empty() ) {
|
|
|
|
for (VertexWeightTable::const_iterator iter = table.begin();
|
|
|
|
iter != table.end();++iter) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// allocate the bone weight array if necessary
|
|
|
|
BoneWeightList* pcWeightList = (BoneWeightList*)pcMesh->mBones[(*iter).first];
|
2018-10-08 19:06:48 +00:00
|
|
|
if (nullptr == pcWeightList) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mBones[(*iter).first] = (aiBone*)(pcWeightList = new BoneWeightList());
|
|
|
|
}
|
|
|
|
pcWeightList->push_back(aiVertexWeight(pcMesh->mNumVertices,(*iter).second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
avWasCopied[iIndex] = pcMesh->mNumVertices;
|
|
|
|
pcMesh->mNumVertices++;
|
|
|
|
}
|
2018-10-08 19:06:48 +00:00
|
|
|
++iBase;
|
|
|
|
if(pcMesh->mNumVertices == iOutVertexNum) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// break here. The face is only added if it was complete
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check which bones we'll need to create for this submesh
|
2018-10-08 19:06:48 +00:00
|
|
|
if (pMesh->HasBones()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiBone** ppCurrent = pcMesh->mBones;
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int k = 0; k < pMesh->mNumBones;++k) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// check whether the bone is existing
|
|
|
|
BoneWeightList* pcWeightList;
|
2020-02-18 13:41:19 +00:00
|
|
|
pcWeightList = (BoneWeightList *)pcMesh->mBones[k];
|
|
|
|
if (nullptr != pcWeightList) {
|
|
|
|
aiBone *pcOldBone = pMesh->mBones[k];
|
2018-10-08 19:06:48 +00:00
|
|
|
aiBone* pcOut( nullptr );
|
2015-05-19 03:57:13 +00:00
|
|
|
*ppCurrent++ = pcOut = new aiBone();
|
|
|
|
pcOut->mName = aiString(pcOldBone->mName);
|
|
|
|
pcOut->mOffsetMatrix = pcOldBone->mOffsetMatrix;
|
|
|
|
pcOut->mNumWeights = (unsigned int)pcWeightList->size();
|
|
|
|
pcOut->mWeights = new aiVertexWeight[pcOut->mNumWeights];
|
|
|
|
|
|
|
|
// copy the vertex weights
|
|
|
|
::memcpy(pcOut->mWeights,&pcWeightList->operator[](0),
|
|
|
|
pcOut->mNumWeights * sizeof(aiVertexWeight));
|
|
|
|
|
|
|
|
// delete the temporary bone weight list
|
|
|
|
delete pcWeightList;
|
|
|
|
pcMesh->mNumBones++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy the face list to the mesh
|
|
|
|
pcMesh->mFaces = new aiFace[vFaces.size()];
|
|
|
|
pcMesh->mNumFaces = (unsigned int)vFaces.size();
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
for (unsigned int p = 0; p < pcMesh->mNumFaces;++p) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pcMesh->mFaces[p] = vFaces[p];
|
2018-10-08 19:06:48 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// add the newly created mesh to the list
|
|
|
|
avList.push_back(std::pair<aiMesh*, unsigned int>(pcMesh,a));
|
|
|
|
|
2018-10-08 19:06:48 +00:00
|
|
|
if (iBase == pMesh->mNumFaces) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// have all faces ... finish the outer loop, too
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the per-vertex weight list again
|
|
|
|
delete[] avPerVertexWeights;
|
|
|
|
|
|
|
|
// now delete the old mesh data
|
|
|
|
delete pMesh;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
avList.push_back(std::pair<aiMesh*, unsigned int>(pMesh,a));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|