Skip to content

Commit b9e5188

Browse files
committed
[geometry] fixed a bug where the target triangle count wasn't correct
1 parent 3adcf58 commit b9e5188

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

runtime/Core/GeometryProcessing.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ namespace spartan::geometry_processing
299299
indices->push_back(baseIndex + i);
300300
}
301301

302-
// Build bottom cap
302+
// build bottom cap
303303
baseIndex = (int)vertices->size();
304304
y = -0.5f * height;
305305

@@ -310,13 +310,13 @@ namespace spartan::geometry_processing
310310
const float u = x / height + 0.5f;
311311
const float v = z / height + 0.5f;
312312

313-
normal = Vector3(0, -1, 0);
314-
tangent = Vector3(1, 0, 0);
313+
normal = Vector3(0, -1, 0);
314+
tangent = Vector3(1, 0, 0);
315315
vertices->emplace_back(Vector3(x, y, z), Vector2(u, v), normal, tangent);
316316
}
317317

318-
normal = Vector3(0, -1, 0);
319-
tangent = Vector3(1, 0, 0);
318+
normal = Vector3(0, -1, 0);
319+
tangent = Vector3(1, 0, 0);
320320
vertices->emplace_back(Vector3(0, y, 0), Vector2(0.5f, 0.5f), normal, tangent);
321321

322322
centerIndex = (int)vertices->size() - 1;
@@ -339,9 +339,13 @@ namespace spartan::geometry_processing
339339
float error = 0.1f;
340340
size_t index_count = indices.size();
341341
size_t current_triangle_count = indices.size() / 3;
342-
342+
343+
if (current_triangle_count >= triangle_target)
344+
return;
345+
343346
// loop until the current triangle count is less than or equal to the target triangle count
344347
std::vector<uint32_t> indices_simplified(index_count);
348+
uint32_t iteration_count = 0;
345349
while (current_triangle_count > triangle_target)
346350
{
347351
float threshold = 1.0f - reduction;
@@ -359,9 +363,14 @@ namespace spartan::geometry_processing
359363
);
360364

361365
indices.assign(indices_simplified.begin(), indices_simplified.begin() + index_count);
362-
current_triangle_count = index_count / 3;
363-
reduction += 0.1f;
364-
error += 0.1f;
366+
current_triangle_count = index_count / 3;
367+
reduction = fmodf(reduction + 0.1f, 1.0f);
368+
error = fmodf(error + 0.1f, 1.0f);
369+
370+
// break if meshopt_simplify gives up
371+
iteration_count++;
372+
if (iteration_count > 10)
373+
break;
365374
}
366375
}
367376

@@ -392,7 +401,7 @@ namespace spartan::geometry_processing
392401

393402
// optimization #4: create a simplified version of the model
394403
{
395-
auto get_vertex_target = [](size_t vertex_count)
404+
auto get_triangle_target = [](size_t triangle_count)
396405
{
397406
std::tuple<float, size_t> agressivness_table[] =
398407
{
@@ -402,17 +411,17 @@ namespace spartan::geometry_processing
402411
{ 0.9f, 2000 } // gentle
403412
};
404413

405-
for (const auto& [reduction_percentage, vertex_threshold] : agressivness_table)
414+
for (const auto& [reduction_percentage, triangle_threshold] : agressivness_table)
406415
{
407-
if (vertex_count > vertex_threshold)
416+
if (triangle_count > triangle_threshold)
408417
{
409-
return static_cast<size_t>(vertex_count * reduction_percentage);
418+
return static_cast<size_t>(triangle_count * reduction_percentage);
410419
}
411420
}
412-
return vertex_count; // native
421+
return triangle_count; // native
413422
};
414423

415-
simplify(indices, vertices, get_vertex_target(vertex_count));
424+
simplify(indices, vertices, get_triangle_target(indices.size() / 3));
416425
}
417426
}
418427
}

0 commit comments

Comments
 (0)