Skip to content

Commit

Permalink
feat: add BuildContext.SetEnableUVDistributionRecalculation (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore authored Nov 18, 2024
1 parent 2fab4f7 commit 50966fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- [#472] Added the `DependsOnContext` attribute, for declaring dependencies between extension contexts.
- Added `BuildContext.SetEnableUVDistributionRecalculation` to allow opting out from the automatic call to
`Mesh.RecalculateUVDistributionMetrics` on generated meshes.

### Fixed

Expand Down
37 changes: 35 additions & 2 deletions Editor/API/BuildContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,51 @@ internal void Finish()
}
}

private readonly HashSet<Mesh> _meshesExcludedFromRecalculation = new();

/// <summary>
/// NDMF will automatically invoke RecalculateUVDistributionMetrics on all temporary asset meshes to ensure
/// they work properly with streaming mipmaps. If you don't want this to happen for a specific mesh (eg if you
/// invoked RecalculateUVDistributionMetric with a specific UV channel), invoke this function with enabled=false
/// to opt out.
/// </summary>
/// <param name="mesh">The mesh to control</param>
/// <param name="enabled">False to disable UV distribution recalculation</param>
public void SetEnableUVDistributionRecalculation(Mesh mesh, bool enabled)
{
if (enabled)
{
_meshesExcludedFromRecalculation.Remove(mesh);
}
else
{
_meshesExcludedFromRecalculation.Add(mesh);
}
}

private void RecalculateAllMeshes()
{
foreach (var mesh in EnumerateMeshes())
{
if (!_meshesExcludedFromRecalculation.Contains(mesh))
{
mesh.RecalculateUVDistributionMetrics();
}
}
}

private IEnumerable<Mesh> EnumerateMeshes()
{
foreach (var meshFilter in _avatarRootObject.GetComponentsInChildren<MeshFilter>())
{
var mesh = meshFilter.sharedMesh;
if (mesh != null && IsTemporaryAsset(mesh)) mesh.RecalculateUVDistributionMetrics();
if (mesh != null && IsTemporaryAsset(mesh)) yield return mesh;
}

foreach (var skinnedMeshRenderer in _avatarRootObject.GetComponentsInChildren<SkinnedMeshRenderer>())
{
var mesh = skinnedMeshRenderer.sharedMesh;
if (mesh != null && IsTemporaryAsset(mesh)) mesh.RecalculateUVDistributionMetrics();
if (mesh != null && IsTemporaryAsset(mesh)) yield return mesh;
}
}
}
Expand Down

0 comments on commit 50966fb

Please sign in to comment.