Fix crash in mesh LOD generator if generated mesh has more indices
This commit is contained in:
@@ -1465,6 +1465,7 @@ bool ModelTool::ImportModel(const String& path, ModelData& data, Options& option
|
|||||||
baseLodTriangleCount += mesh->Indices.Count() / 3;
|
baseLodTriangleCount += mesh->Indices.Count() / 3;
|
||||||
baseLodVertexCount += mesh->Positions.Count();
|
baseLodVertexCount += mesh->Positions.Count();
|
||||||
}
|
}
|
||||||
|
Array<unsigned int> indices;
|
||||||
for (int32 lodIndex = Math::Clamp(baseLOD + 1, 1, lodCount - 1); lodIndex < lodCount; lodIndex++)
|
for (int32 lodIndex = Math::Clamp(baseLOD + 1, 1, lodCount - 1); lodIndex < lodCount; lodIndex++)
|
||||||
{
|
{
|
||||||
auto& dstLod = data.LODs[lodIndex];
|
auto& dstLod = data.LODs[lodIndex];
|
||||||
@@ -1486,16 +1487,18 @@ bool ModelTool::ImportModel(const String& path, ModelData& data, Options& option
|
|||||||
int32 srcMeshIndexCount = srcMesh->Indices.Count();
|
int32 srcMeshIndexCount = srcMesh->Indices.Count();
|
||||||
int32 srcMeshVertexCount = srcMesh->Positions.Count();
|
int32 srcMeshVertexCount = srcMesh->Positions.Count();
|
||||||
int32 dstMeshIndexCountTarget = int32(srcMeshIndexCount * triangleReduction) / 3 * 3;
|
int32 dstMeshIndexCountTarget = int32(srcMeshIndexCount * triangleReduction) / 3 * 3;
|
||||||
Array<unsigned int> indices;
|
if (dstMeshIndexCountTarget < 3 || dstMeshIndexCountTarget >= srcMeshIndexCount)
|
||||||
indices.Resize(dstMeshIndexCountTarget);
|
continue;
|
||||||
|
indices.Clear();
|
||||||
|
indices.Resize(srcMeshIndexCount);
|
||||||
int32 dstMeshIndexCount = {};
|
int32 dstMeshIndexCount = {};
|
||||||
if (options.SloppyOptimization)
|
if (options.SloppyOptimization)
|
||||||
dstMeshIndexCount = (int32)meshopt_simplifySloppy(indices.Get(), srcMesh->Indices.Get(), srcMeshIndexCount, (const float*)srcMesh->Positions.Get(), srcMeshVertexCount, sizeof(Float3), dstMeshIndexCountTarget);
|
dstMeshIndexCount = (int32)meshopt_simplifySloppy(indices.Get(), srcMesh->Indices.Get(), srcMeshIndexCount, (const float*)srcMesh->Positions.Get(), srcMeshVertexCount, sizeof(Float3), dstMeshIndexCountTarget, options.LODTargetError);
|
||||||
else
|
else
|
||||||
dstMeshIndexCount = (int32)meshopt_simplify(indices.Get(), srcMesh->Indices.Get(), srcMeshIndexCount, (const float*)srcMesh->Positions.Get(), srcMeshVertexCount, sizeof(Float3), dstMeshIndexCountTarget, options.LODTargetError);
|
dstMeshIndexCount = (int32)meshopt_simplify(indices.Get(), srcMesh->Indices.Get(), srcMeshIndexCount, (const float*)srcMesh->Positions.Get(), srcMeshVertexCount, sizeof(Float3), dstMeshIndexCountTarget, options.LODTargetError);
|
||||||
indices.Resize(dstMeshIndexCount);
|
if (dstMeshIndexCount <= 0 || dstMeshIndexCount > indices.Count())
|
||||||
if (dstMeshIndexCount == 0)
|
|
||||||
continue;
|
continue;
|
||||||
|
indices.Resize(dstMeshIndexCount);
|
||||||
|
|
||||||
// Generate simplified vertex buffer remapping table (use only vertices from LOD index buffer)
|
// Generate simplified vertex buffer remapping table (use only vertices from LOD index buffer)
|
||||||
Array<unsigned int> remap;
|
Array<unsigned int> remap;
|
||||||
|
|||||||
Reference in New Issue
Block a user