In addition to the regular edge collapse, we now allow the simplifier to
remove small isolated components. Components that are removed are below
the error threshold in size and as such should not noticeably contribute
to the overall rendering of the object.
This helps simplify topologically complex but small parts of larger
meshes and more comfortably reach the LOD targets.
In some cases, pruning can cause the last LOD to shrink to 0 triangles
which may prevent a slightly larger LOD from being used at the maximum
distance; in this case we retry simplification without pruning once.
Instead of simplifying every LOD from the original down to an
increasing number of triangles, we simplify each LOD from the previous
LOD and stop when the simplification can't proceed further.
This has a few benefits:
- It's significantly faster; using sparse flag helps ensure that
subsequent simplifications after the first one are increasingly
cheaper.
- It results in higher quality attributes on generated LODs; attribute
quadrics reduce the quality of attribute preservation the more they
are accumulated, so recomputing them from intermediate geometry helps.
- It results in monotonic appearance: if a feature is reduced in a
higher LOD, it will stay reduced or get reduced more significantly in
lower LODs. This is not a significant problem right now, but can be
helpful to ensure if the number of LODs increases or some newer
features get enabled.
This significantly improves LOD quality for skinned objects, especially
if the skinned object geometry is very simple (e.g. planar or
cylindrical) in bind pose.
Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
- Revert meshoptimizer patch; the metric used for LOD selection now
incorporates attribute error
- Since LOD selection is now aware of attribute deviation, we don't
need to use a higher normal weight
- To prevent normal creases from creating input triangles with very
large normal deviation, reduce default normal merge threshold
- Since we now use combined metric to select LODs, we never need LODs
with error>1 as these should not be selected if the mesh is visible.
When importing meshes with vertex colors, we will now supply them to the
simplifier as attributes with weight=1 (in addition to normals). This will
guide the simplification to preserve regions where vertex colors change.
For this change to take full effect, it is also necessary to respect the
full error when selecting LODs; this change does not do that yet, so there
are going to still be cases where vertex colors change abruptly during LOD
switch in a visible manner.
While all the previous fixes to optimizeVertexCache invocation fixed the
vertex transform efficiency, the import code still was missing two
crucial recommendations from meshoptimizer documentation:
- All meshes should be optimized for vertex cache (this reorders
vertices for maximum fetch efficiency)
- When LODs are used with a shared vertex buffer, the vertex order
should be generated by doing a vertex fetch optimization on the
concatenated index buffer from coarse to fine LODs; this maximizes
fetch efficiency for coarse LODs
The last point is especially crucial for Mali GPUs; unlike other GPUs
where vertex order affects fetch efficiency but not shading, these GPUs
have various shading quirks (depending on the GPU generation) that
really require consecutive index ranges for each LOD, which requires the
second optimization mentioned above. However all of these also help
desktop GPUs and other mobile GPUs as well.
Because this optimization is "global" in the sense that it affects all
LODs and all vertex arrays in concert, I've taken this opportunity to
isolate all optimization code in this function and pull it out of
generate_lods and create_shadow_mesh; this doesn't change the vertex
cache efficiency, but makes the code cleaner. Consequently,
optimize_indices should be called after other functions like
create_shadow_mesh / generate_lods.
This required exposing meshopt_optimizeVertexFetchRemap; as a drive-by,
meshopt_simplifySloppy was never used so it's not exposed anymore - this
will simplify future meshopt upgrades if they end up changing the
function's interface.
"Raycast Normals" was introduced in 4.4 dev and defaulted to "false".
The limited testing results at the time suggested that raycasting
generally reduces normal quality compared to native simplifier results,
at the same time increasing vertex memory and import time.
To play it safe, we introduced a setting that defaulted to false, with
the goal of removing it later in 4.4 development cycle if no regressions
are noticed. Since we already had three dev snapshots and no reports,
this change removes the setting and associated code.
"Normal Split Angle" was only used when raycast normals were enabled;
this change removes it from the settings, but keeps it in the script
binding for compatibility.
Existing meshes import exactly the same after this change (unless they
chose to override raycasting which would be surprising).
split_normals helper was only used in this code path and is also removed
for simplicity; it is unlikely that this code will be useful as is, as
it can only regenerate normals without fixing tangents or updating
positions.
The Godot-specific patch is just a single line now; removing this patch
will likely require adjusting Godot importer code to handle error limits
better.
This also adds new SIMPLIFY_ options; Godot is currently not using any
of these but might use SIMPLIFY_PRUNE and SIMPLIFY_SPARSE in the future.
optimize_vertex_cache_func assumes the input is a triangle mesh; when an
imported mesh contains points/lines/triangle strips, this code should be
disabled.
Previously, vertex cache optimization was ran for the LOD meshes, but
was never ran for the base mesh or for the shadow meshes, including
shadow LOD chain (shadow LOD chain would sometimes get implicitly
optimized for vertex cache as a byproduct of base LOD optimization, but
not always). This could significantly affect the rendering performance
of geometry heavy scenes, especially for depth or shadow passes where
the fragment load is light.
These properties look like they were intended to be internal but they were missing the `PROPERTY_USAGE_INTERNAL` flag.
- `PackedScene::_bundled`
- `PortableCompressedTexture2D::_data`
- `ImporterMesh::_data`
When UVs are mirrored in a mesh, collapsing vertices across the
mirroring seam can significantly reduce quality in a way that is not
apparent to the simplifier. Even if simplifier was given access to UV
data, the coordinates would need to be weighted very highly to prevent
these collapses, which would penalize overall quality of reasonable
models.
Normally, well behaved models with mirrored UVs have tangent data that
is correctly mirrored, which results in duplicate vertices along the
seam. The simplifier automatically recognizes that seam and preserves
its structure; typically models have few edge loops where UV winding is
flipped so this does not affect simplification quality much.
However, pre-processing for LOD data welded vertices when UVs and
normals were close, which welds these seams and breaks simplification,
creating triangles with distorted UVs.
We now take tangent frame sign into account when the input model has
tangent data, and only weld vertices when the sign is the same.
Normal raycaster makes LOD generation process >2x slower and often
generates normals that look significantly worse compared to what the
simplifier comes up with by default. This was likely different before
last meshoptimizer upgrade, as the attribute metric was not functioning
properly, but now it looks like it's doing more harm than good.
This change makes it disabled by default but keeps an easy option to
re-enable it per mesh using LOD parameters for now until we get more
confidence and can remove the code outright.
Because the long term plan would be to disable this feature entirely,
the scripting API isn't changed, and it's just off-by-default there with
no way to re-enable.