Whinarn / UnityMeshSimplifier

Mesh simplification for Unity.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Values need to be initialized in SimplificationOptions to prevent silent failure/exit

yosun opened this issue · comments

commented

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:
Use the sample script provided in https://github.com/Whinarn/UnityMeshSimplifier/wiki/Mesh-Simplifier-API
Add this after meshSimplifier.Initialize(sourceMesh);
meshSimplifier.SimplificationOptions = so;
where so is exposed in editor (needs to have default min values set) - However, all values are 0 and false, which leads to silent exit

Expected behavior
Mesh Simplification should not silent exit

Hi @yosun,

The reason for this happening is because SimplificationOptions is a struct, and there is no way to override the default constructor for it. Which means that the struct will be initialized with everything zeroed essentially. And while most of the values can be zero, MaxIterationCount and Agressiveness can't. Which essentially leads to a "silent exit" as you describe it.

This is why there is SimplificationOptions.Default. This one includes all the default values of the simplifier and allows you to override only some values.

So if you are exposing the SimplificationOptions in a component, you need to make sure the default is set to SimplificationOptions.Default instead of default(SimplificationOptions).

You can find an example in one of the components included:

private SimplificationOptions simplificationOptions = SimplificationOptions.Default;

So this isn't really a bug. It's just related to C# itself, and how structs work.

I hope that this helps!

commented

You can also check for the case where Aggressiveness or MaxIterationCount are 0's, to assume the Default value, to avoid the silent exit.

I think that it would be better to throw an exception in that case. Personally I believe that it's better to let the programmer know that they have made a mistake, rather than trying to assume what they meant.

I'll see if I can get some time to take a look at it.

Hi @yosun,

I have release version 3.1.0 just now that includes validation of the simplification options struct as I described above. So if anything is invalid, it will throw an exception.

If you need to validate the options yourself prior to assigning it to a mesh simplifier you can use the new static method for this:
MeshSimplifier.ValidateOptions(SimplificationOptions options)
It will throw a new exception type ValidateSimplificationOptionsException that can be caught with a property name available for the problematic property.

In addition to this, the LOD Generator has been updated to pre-validate the options. Which causes a fast exit when generating LODs through the built-in components.

I hope that this helps!

I will close the issue, but feel free to re-open it if you have any further issues related to this.