Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance Improvement #523

Merged
merged 4 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Significant Performance Improvements with small code changes `#523`

### Changed

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog].
- In this section, there are for debugging GC Objects `#464`
- Avoid Name Conflict in MergeBone `#467`
- Full EditMode Preview of RemoveMesh Components `#500`
- Significant Performance Improvements with small code changes `#523`

### Changed
- Improved 'Remove Unused Objects' `#401`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ public override void Process(OptimizerSession session, MeshInfo2 target)
var meaningfulBlendShapes = new HashSet<string>();

foreach (var vertex in target.Vertices)
foreach (var kvp in vertex.BlendShapes.Where(kvp => kvp.Value != default))
meaningfulBlendShapes.Add(kvp.Key);
meaningfulBlendShapes.UnionWith(vertex.BlendShapes.Keys);

var freezeBlendShape = Target.GetComponent<FreezeBlendShape>();
var serialized = new SerializedObject(freezeBlendShape);
var editorUtil = PrefabSafeSet.EditorUtil<string>.Create(
serialized.FindProperty(nameof(FreezeBlendShape.shapeKeysSet)),
0, p => p.stringValue, (p, v) => p.stringValue = v);
foreach (var (meaningLess, _) in target.BlendShapes.Where(x => !meaningfulBlendShapes.Contains(x.name)))
editorUtil.GetElementOf(meaningLess).EnsureAdded();
serialized.ApplyModifiedPropertiesWithoutUndo();
var set = freezeBlendShape.shapeKeysSet.GetAsSet();
set.UnionWith(target.BlendShapes.Where(x => !meaningfulBlendShapes.Contains(x.name)).Select(x => x.name));
freezeBlendShape.shapeKeysSet.SetValueNonPrefab(set);
}

// nothing to do
Expand Down
23 changes: 21 additions & 2 deletions Editor/Processors/SkinnedMeshes/MeshInfo2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,27 @@ public void ReadSkinnedMesh([NotNull] Mesh mesh)

BlendShapes.Add((shapeName, 0.0f));

var frames = Vertices.Select(v => v.BlendShapes[shapeName] = new List<Vertex.BlendShapeFrame>()).ToArray();
var shapes = new List<Vertex.BlendShapeFrame>[Vertices.Count];

for (int frame = 0; frame < mesh.GetBlendShapeFrameCount(i); frame++)
{
mesh.GetBlendShapeFrameVertices(i, frame, deltaVertices, deltaNormals, deltaTangents);
var weight = mesh.GetBlendShapeFrameWeight(i, frame);

for (var vertex = 0; vertex < deltaNormals.Length; vertex++)
frames[vertex].Add(new Vertex.BlendShapeFrame(weight, deltaVertices[vertex], deltaNormals[vertex], deltaTangents[vertex]));
{
if (deltaVertices[vertex] == Vector3.zero && deltaNormals[vertex] == Vector3.zero && deltaTangents[vertex] == Vector3.zero)
continue;
if (shapes[vertex] == null)
shapes[vertex] = new List<Vertex.BlendShapeFrame>();
shapes[vertex].Add(new Vertex.BlendShapeFrame(weight, deltaVertices[vertex],
deltaNormals[vertex], deltaTangents[vertex]));
}
}

for (var vertex = 0; vertex < shapes.Length; vertex++)
if (shapes[vertex] is List<Vertex.BlendShapeFrame> shapeFrames)
Vertices[vertex].BlendShapes[shapeName] = shapeFrames;
}
}

Expand Down Expand Up @@ -601,6 +612,14 @@ public bool TryGetBlendShape(string name, float weight, out Vector3 position, ou
return false;
}

if (frames.Count == 0)
{
position = default;
normal = default;
tangent = default;
return false;
}

if (Mathf.Abs(weight) <= 0.0001f && ZeroForWeightZero())
{
position = Vector3.zero;
Expand Down
11 changes: 11 additions & 0 deletions Internal/PrefabSafeSet/Runtime/PrefabSafeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ protected PrefabSafeSet(Object outerObject)
#endif
}

public void SetValueNonPrefab(IEnumerable<T> values)
{
#if UNITY_EDITOR
if (OuterObject && UnityEditor.PrefabUtility.IsPartOfPrefabInstance(OuterObject)
&& UnityEditor.PrefabUtility.IsPartOfAnyPrefab(OuterObject))
throw new InvalidOperationException("You cannot set value to Prefab Instance or Prefab");
Debug.Assert(prefabLayers.Length == 0);
#endif
mainSet = values.ToArray();
}

public HashSet<T> GetAsSet()
{
var result = new HashSet<T>(mainSet.Where(x => x.IsNotNull()));
Expand Down