-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from anatawa12/global-meshinfo2-holder
Share MeshInfo2 globally
- Loading branch information
Showing
15 changed files
with
86 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Anatawa12.AvatarOptimizer.ErrorReporting; | ||
using Anatawa12.AvatarOptimizer.Processors.SkinnedMeshes; | ||
using UnityEngine; | ||
|
||
namespace Anatawa12.AvatarOptimizer.Processors | ||
{ | ||
internal class MeshInfo2Holder | ||
{ | ||
private readonly Dictionary<SkinnedMeshRenderer, MeshInfo2> _skinnedCache = | ||
new Dictionary<SkinnedMeshRenderer, MeshInfo2>(); | ||
|
||
private readonly Dictionary<MeshRenderer, MeshInfo2> _staticCache = new Dictionary<MeshRenderer, MeshInfo2>(); | ||
|
||
public MeshInfo2 GetMeshInfoFor(SkinnedMeshRenderer renderer) => | ||
_skinnedCache.TryGetValue(renderer, out var cached) | ||
? cached | ||
: _skinnedCache[renderer] = new MeshInfo2(renderer); | ||
|
||
|
||
public MeshInfo2 GetMeshInfoFor(MeshRenderer renderer) => | ||
_staticCache.TryGetValue(renderer, out var cached) | ||
? cached | ||
: _staticCache[renderer] = new MeshInfo2(renderer); | ||
|
||
public void SaveToMesh(OptimizerSession session) | ||
{ | ||
foreach (var keyValuePair in _skinnedCache) | ||
{ | ||
var targetRenderer = keyValuePair.Key; | ||
if (!targetRenderer) continue; | ||
|
||
keyValuePair.Value.WriteToSkinnedMeshRenderer(targetRenderer, session); | ||
} | ||
|
||
foreach (var keyValuePair in _staticCache) | ||
{ | ||
var targetRenderer = keyValuePair.Key; | ||
if (!targetRenderer) continue; | ||
var meshInfo = keyValuePair.Value; | ||
var meshFilter = targetRenderer.GetComponent<MeshFilter>(); | ||
|
||
BuildReport.ReportingObject(targetRenderer, () => | ||
{ | ||
var mesh = meshFilter.sharedMesh | ||
? session.MayCreate(meshFilter.sharedMesh) | ||
: session.AddToAsset(new Mesh()); | ||
meshInfo.WriteToMesh(mesh); | ||
meshFilter.sharedMesh = mesh; | ||
targetRenderer.sharedMaterials = meshInfo.SubMeshes.Select(x => x.SharedMaterial).ToArray(); | ||
}); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters