Version of the simplifier that outputs a triangle mapping
I'm reading glTF and building meshlet clusters that span multiple primitives, so I need to know the material of each triangle.
For now I work around this with the experimental demo/clusterlod.h header and meshoptimizer, by taking a majority vote of the three vertex materials. That's only an approximation.
Unreal Engine instead carries a per-triangle material array through simplification, so it gets the real material, not an approximation. I wanted to do the same, but the API only returns the output triangles and doesn't tell me which input triangle each one came from.
Then I noticed that when triangles are removed, the order is not shuffled - they are just compacted forward. So I modified the code to carry a parallel array of triangle ids along with it.
Unlike #1073, the hash table workaround doesn't work on this path. simplify changes the vertices themselves, so I can't build the key. And simplify doesn't create new triangles either.
I've attached the change below. Would you be willing to take a look?
The core of it is two lines in remapIndexBuffer and two in pruneComponents; the rest
is the extra parameter and the wrappers.
thanks
diff (against master)diff --git a/src/meshoptimizer.h b/src/meshoptimizer.h
index 312a7575..6e78d33c 100644
--- a/src/meshoptimizer.h
+++ b/src/meshoptimizer.h
@@ -540,6 +540,20 @@ MESHOPTIMIZER_API size_t meshopt_simplify(unsigned int* destination, const unsig
*/
MESHOPTIMIZER_API size_t meshopt_simplifyWithAttributes(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* result_error);
+/**
+ * Experimental: Mesh simplifier with triangle provenance (same behavior as meshopt_simplifyWithAttributes)
+ *
+ * Edge collapse never creates new triangles: it moves vertices and deletes triangles that become
+ * degenerate. Every output triangle is therefore one of the input triangles, and this variant reports
+ * which one, so callers can carry per-triangle data (material ids, face tags, ...) through simplification
+ * exactly instead of re-deriving it from vertex attributes.
+ *
+ * triangle_remap must contain index_count/3 elements; on return, triangle_remap[i] is the index of the
+ * input triangle that output triangle i came from (strictly increasing). Can be NULL, which makes this
+ * function identical to meshopt_simplifyWithAttributes.
+ */
+MESHOPTIMIZER_EXPERIMENTAL size_t meshopt_simplifyWithAttributesTracked(unsigned int* destination, unsigned int* triangle_remap, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* result_error);
+
/**
* Mesh simplifier with position/attribute update
* Reduces the number of triangles in the mesh, attempting to preserve mesh appearance as much as possible.
diff --git a/src/simplifier.cpp b/src/simplifier.cpp
index 85c50252..8e997cf8 100644
--- a/src/simplifier.cpp
+++ b/src/simplifier.cpp
@@ -1829,7 +1829,7 @@ static void solveAttributes(Vector3* vertex_positions, float* vertex_attributes,
}
}
-static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const unsigned int* collapse_remap, const unsigned int* remap)
+static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const unsigned int* collapse_remap, const unsigned int* remap, unsigned int* triangle_remap)
{
size_t write = 0;
@@ -1856,6 +1856,9 @@ static size_t remapIndexBuffer(unsigned int* indices, size_t index_count, const
indices[write + 0] = v0;
indices[write + 1] = v1;
indices[write + 2] = v2;
+ // triangle provenance travels with the triangle; write <= i so this is safe in place
+ if (triangle_remap)
+ triangle_remap[write / 3] = triangle_remap[i / 3];
write += 3;
}
}
@@ -2001,7 +2004,7 @@ static void measureComponents(float* component_errors, size_t component_count, c
component_errors[i] = component_errors[i * 4 + 3];
}
-static size_t pruneComponents(unsigned int* indices, size_t index_count, const unsigned int* components, const float* component_errors, size_t component_count, float error_cutoff, float& nexterror)
+static size_t pruneComponents(unsigned int* indices, size_t index_count, const unsigned int* components, const float* component_errors, size_t component_count, float error_cutoff, float& nexterror, unsigned int* triangle_remap)
{
(void)component_count;
@@ -2021,6 +2024,8 @@ static size_t pruneComponents(unsigned int* indices, size_t index_count, const u
indices[write + 0] = v0;
indices[write + 1] = v1;
indices[write + 2] = v2;
+ if (triangle_remap)
+ triangle_remap[write / 3] = triangle_remap[i / 3];
write += 3;
}
}
@@ -2337,7 +2342,7 @@ enum
meshopt_SimplifyInternalDebug = 1 << 30
};
-size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
+static size_t simplifyEdgeTracked(unsigned int* destination, unsigned int* triangle_remap, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
{
using namespace meshopt;
@@ -2358,6 +2363,11 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
if (result != indices)
memcpy(result, indices, index_count * sizeof(unsigned int));
+ // triangle provenance starts as identity; the two compaction passes below carry it along
+ if (triangle_remap)
+ for (size_t i = 0; i < index_count / 3; ++i)
+ triangle_remap[i] = unsigned(i);
+
// build an index remap and update indices/vertex_count to minimize the subsequent work
// note: as a consequence, errors will be computed relative to the subset extent
unsigned int* sparse_remap = NULL;
@@ -2533,10 +2543,10 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
remapEdgeLoops(loop, vertex_count, collapse_remap);
remapEdgeLoops(loopback, vertex_count, collapse_remap);
- result_count = remapIndexBuffer(result, result_count, collapse_remap, remap);
+ result_count = remapIndexBuffer(result, result_count, collapse_remap, remap, triangle_remap);
if ((options & meshopt_SimplifyPrune) && result_count > target_index_count && component_nexterror <= vertex_error)
- result_count = pruneComponents(result, result_count, components, component_errors, component_count, vertex_error, component_nexterror);
+ result_count = pruneComponents(result, result_count, components, component_errors, component_count, vertex_error, component_nexterror, triangle_remap);
}
// at this point, component_nexterror might be stale: component it references may have been removed through a series of edge collapses
@@ -2557,7 +2567,7 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
if (component_errors[i] > component_maxerror && component_errors[i] <= component_cutoff)
component_maxerror = component_errors[i];
- size_t new_count = pruneComponents(result, result_count, components, component_errors, component_count, component_cutoff, component_nexterror);
+ size_t new_count = pruneComponents(result, result_count, components, component_errors, component_count, component_cutoff, component_nexterror, triangle_remap);
if (new_count == result_count && !component_nextstale)
break;
@@ -2624,6 +2634,18 @@ size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indic
return result_count;
}
+size_t meshopt_simplifyEdge(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
+{
+ return simplifyEdgeTracked(destination, NULL, indices, index_count, vertex_positions_data, vertex_count, vertex_positions_stride, vertex_attributes_data, vertex_attributes_stride, attribute_weights, attribute_count, vertex_lock, target_index_count, target_error, options, out_result_error);
+}
+
+size_t meshopt_simplifyWithAttributesTracked(unsigned int* destination, unsigned int* triangle_remap, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes_data, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
+{
+ assert((options & meshopt_SimplifyInternalSolve) == 0); // use meshopt_simplifyWithUpdate instead
+
+ return simplifyEdgeTracked(destination, triangle_remap, indices, index_count, vertex_positions_data, vertex_count, vertex_positions_stride, vertex_attributes_data, vertex_attributes_stride, attribute_weights, attribute_count, vertex_lock, target_index_count, target_error, options, out_result_error);
+}
+
size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions_data, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, unsigned int options, float* out_result_error)
{
assert((options & meshopt_SimplifyInternalSolve) == 0); // use meshopt_simplifyWithUpdate instead
@@ -2805,7 +2827,7 @@ size_t meshopt_simplifyPrune(unsigned int* destination, const unsigned int* indi
measureComponents(component_errors, component_count, components, vertex_positions, vertex_count);
float component_nexterror = 0;
- size_t result_count = pruneComponents(result, index_count, components, component_errors, component_count, target_error * target_error, component_nexterror);
+ size_t result_count = pruneComponents(result, index_count, components, component_errors, component_count, target_error * target_error, component_nexterror, NULL);
return result_count;
}Source: zeux/meshoptimizer