diff options
author | K. S. Ernest (iFire) Lee <ernest.lee@chibifire.com> | 2021-01-09 10:04:09 -0800 |
---|---|---|
committer | K. S. Ernest (iFire) Lee <ernest.lee@chibifire.com> | 2021-01-11 06:07:54 -0800 |
commit | 59b61a1f64c2d77655cef89575cfd172c1e36832 (patch) | |
tree | 0eddf2bdfc416b1c77d77b1549926408778fbf35 /editor | |
parent | a33dc4274cdc7b46c7586e832baded992038bea8 (diff) |
Scale error in mesh optimizer so it uses absolute scale.
Switch to simplify sloppy for another try.
Update to meshoptimizer e3f53f66e7a35b9b8764bee478589d79e34fa698.
Diffstat (limited to 'editor')
-rw-r--r-- | editor/import/scene_importer_mesh.cpp | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/editor/import/scene_importer_mesh.cpp b/editor/import/scene_importer_mesh.cpp index cd7d58c6f6..620437af0e 100644 --- a/editor/import/scene_importer_mesh.cpp +++ b/editor/import/scene_importer_mesh.cpp @@ -140,6 +140,12 @@ void EditorSceneImporterMesh::generate_lods() { if (!SurfaceTool::simplify_func) { return; } + if (!SurfaceTool::simplify_scale_func) { + return; + } + if (!SurfaceTool::simplify_sloppy_func) { + return; + } for (int i = 0; i < surfaces.size(); i++) { if (surfaces[i].primitive != Mesh::PRIMITIVE_TRIANGLES) { @@ -157,20 +163,52 @@ void EditorSceneImporterMesh::generate_lods() { int min_indices = 10; int index_target = indices.size() / 2; - print_line("total: " + itos(indices.size())); + print_line("Total indices: " + itos(indices.size())); + float mesh_scale = SurfaceTool::simplify_scale_func((const float *)vertices_ptr, vertex_count, sizeof(Vector3)); + const float target_error = 1e-3f; + float abs_target_error = target_error / mesh_scale; while (index_target > min_indices) { float error; Vector<int> new_indices; new_indices.resize(indices.size()); - size_t new_len = SurfaceTool::simplify_func((unsigned int *)new_indices.ptrw(), (const unsigned int *)indices.ptr(), indices.size(), (const float *)vertices_ptr, vertex_count, sizeof(Vector3), index_target, 1e20, &error); - print_line("shoot for " + itos(index_target) + ", got " + itos(new_len) + " distance " + rtos(error)); + size_t new_len = SurfaceTool::simplify_func((unsigned int *)new_indices.ptrw(), (const unsigned int *)indices.ptr(), indices.size(), (const float *)vertices_ptr, vertex_count, sizeof(Vector3), index_target, abs_target_error, &error); if ((int)new_len > (index_target * 120 / 100)) { + // Attribute discontinuities break normals. + bool is_sloppy = false; + if (is_sloppy) { + abs_target_error = target_error / mesh_scale; + index_target = new_len; + while (index_target > min_indices) { + Vector<int> sloppy_new_indices; + sloppy_new_indices.resize(indices.size()); + new_len = SurfaceTool::simplify_sloppy_func((unsigned int *)sloppy_new_indices.ptrw(), (const unsigned int *)indices.ptr(), indices.size(), (const float *)vertices_ptr, vertex_count, sizeof(Vector3), index_target, abs_target_error, &error); + if ((int)new_len > (index_target * 120 / 100)) { + break; // 20 percent tolerance + } + sloppy_new_indices.resize(new_len); + Surface::LOD lod; + lod.distance = error * mesh_scale; + abs_target_error = lod.distance; + if (Math::is_equal_approx(abs_target_error, 0.0f)) { + return; + } + lod.indices = sloppy_new_indices; + print_line("Lod " + itos(surfaces.write[i].lods.size()) + " shoot for " + itos(index_target / 3) + " triangles, got " + itos(new_len / 3) + " triangles. Distance " + rtos(lod.distance) + ". Use simplify sloppy."); + surfaces.write[i].lods.push_back(lod); + index_target /= 2; + } + } break; // 20 percent tolerance } new_indices.resize(new_len); Surface::LOD lod; - lod.distance = error; + lod.distance = error * mesh_scale; + abs_target_error = lod.distance; + if (Math::is_equal_approx(abs_target_error, 0.0f)) { + return; + } lod.indices = new_indices; + print_line("Lod " + itos(surfaces.write[i].lods.size()) + " shoot for " + itos(index_target / 3) + " triangles, got " + itos(new_len / 3) + " triangles. Distance " + rtos(lod.distance)); surfaces.write[i].lods.push_back(lod); index_target /= 2; } |