Skip to content

Commit

Permalink
fix(simplifier): options for uv component count
Browse files Browse the repository at this point in the history
  • Loading branch information
Whinarn committed Mar 26, 2021
1 parent 6fcb6da commit 7f7b773
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 56 deletions.
17 changes: 2 additions & 15 deletions Runtime/LODGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,22 +607,9 @@ private static void CollectChildRenderersForLOD(Transform transform, List<Render

private static Mesh SimplifyMesh(Mesh mesh, float quality, SimplificationOptions options)
{
int uvComponentCount = -1;
if (options.ManualUVComponentCount)
{
uvComponentCount = options.UVComponentCount;
}

var meshSimplifier = new MeshSimplifier();
meshSimplifier.PreserveBorderEdges = options.PreserveBorderEdges;
meshSimplifier.PreserveUVSeamEdges = options.PreserveUVSeamEdges;
meshSimplifier.PreserveUVFoldoverEdges = options.PreserveUVFoldoverEdges;
meshSimplifier.PreserveSurfaceCurvature = options.PreserveSurfaceCurvature;
meshSimplifier.EnableSmartLink = options.EnableSmartLink;
meshSimplifier.VertexLinkDistance = options.VertexLinkDistance;
meshSimplifier.MaxIterationCount = options.MaxIterationCount;
meshSimplifier.Agressiveness = options.Agressiveness;
meshSimplifier.Initialize(mesh, uvComponentCount);
meshSimplifier.SimplificationOptions = options;
meshSimplifier.Initialize(mesh);
meshSimplifier.SimplifyMesh(quality);

var simplifiedMesh = meshSimplifier.ToMesh();
Expand Down
76 changes: 35 additions & 41 deletions Runtime/MeshSimplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1992,27 +1992,19 @@ public void AddBlendShapes(BlendShape[] blendShapes)
#region Initialize
/// <summary>
/// Initializes the algorithm with the original mesh.
/// Will automatically detect the count of UV components used on the mesh.
/// </summary>
/// <param name="mesh">The mesh.</param>
public void Initialize(Mesh mesh)
{
Initialize(mesh, -1);
}

/// <summary>
/// Initializes the algorithm with the original mesh.
/// </summary>
/// <param name="mesh">The mesh.</param>
/// <param name="uvComponentCount">The count of UV components that are used on the mesh.
/// -1 means that it will be automatically detected.
/// Note that all UV channels would use the same UV component count.</param>
public void Initialize(Mesh mesh, int uvComponentCount)
{
if (mesh == null)
throw new ArgumentNullException(nameof(mesh));
if (uvComponentCount < -1 || uvComponentCount > 4)
throw new ArgumentOutOfRangeException(nameof(uvComponentCount));

int uvComponentCount = simplificationOptions.UVComponentCount;
if (simplificationOptions.ManualUVComponentCount)
{
if (uvComponentCount < 0 || uvComponentCount > 4)
throw new InvalidOperationException("The UV component count cannot be below 0 or above 4.");
}

this.Vertices = mesh.vertices;
this.Normals = mesh.normals;
Expand All @@ -2024,33 +2016,35 @@ public void Initialize(Mesh mesh, int uvComponentCount)

for (int channel = 0; channel < UVChannelCount; channel++)
{
switch (uvComponentCount)
if (simplificationOptions.ManualUVComponentCount)
{
case -1:
{
var uvs = MeshUtils.GetMeshUVs(mesh, channel);
SetUVsAuto(channel, uvs);
break;
}
case 1:
case 2:
{
var uvs = MeshUtils.GetMeshUVs2D(mesh, channel);
SetUVs(channel, uvs);
break;
}
case 3:
{
var uvs = MeshUtils.GetMeshUVs3D(mesh, channel);
SetUVs(channel, uvs);
break;
}
case 4:
{
var uvs = MeshUtils.GetMeshUVs(mesh, channel);
SetUVs(channel, uvs);
break;
}
switch (uvComponentCount)
{
case 1:
case 2:
{
var uvs = MeshUtils.GetMeshUVs2D(mesh, channel);
SetUVs(channel, uvs);
break;
}
case 3:
{
var uvs = MeshUtils.GetMeshUVs3D(mesh, channel);
SetUVs(channel, uvs);
break;
}
case 4:
{
var uvs = MeshUtils.GetMeshUVs(mesh, channel);
SetUVs(channel, uvs);
break;
}
}
}
else
{
var uvs = MeshUtils.GetMeshUVs(mesh, channel);
SetUVsAuto(channel, uvs);
}
}

Expand Down

0 comments on commit 7f7b773

Please sign in to comment.