diff --git a/Editor/Tools/LightmapTransferTool.cs b/Editor/Tools/LightmapTransferTool.cs index 4e680229..392449b4 100644 --- a/Editor/Tools/LightmapTransferTool.cs +++ b/Editor/Tools/LightmapTransferTool.cs @@ -4125,6 +4125,8 @@ void GenerateLods() var lods = ctx.LodGroup.GetLODs(); var newLods = new List(lods); + LodGenerationTool.NormalizeSingleLodTransitionForGeneration( + newLods, ctx.SourceLodIndex + 1); UvProgress.Begin("Generate LODs", cancelable: true); try diff --git a/Editor/Tools/LodGenerationTool.cs b/Editor/Tools/LodGenerationTool.cs index 1ae9252f..63f5d323 100644 --- a/Editor/Tools/LodGenerationTool.cs +++ b/Editor/Tools/LodGenerationTool.cs @@ -344,6 +344,11 @@ void ExecGenerateLods(int startLod) var lods = ctx.LodGroup.GetLODs(); var newLods = new List(lods); + // A group created from unlabelled renderers uses a low value so its only + // LOD is not culled early. Restore the normal LOD0 transition before + // appending generated levels; otherwise they start at 0.005 and below. + NormalizeSingleLodTransitionForGeneration(newLods, startLod); + UvProgress.Begin($"Generate LODs ({generateLodCount} levels)", cancelable: true); try { @@ -599,6 +604,15 @@ internal static LODGroup CreateLodGroupStatic(List<(GameObject go, int lodIndex) return lodGroup; } + internal static void NormalizeSingleLodTransitionForGeneration(List lods, int startLod) + { + if (startLod != 1 || lods.Count != 1 || + !Mathf.Approximately(lods[0].screenRelativeTransitionHeight, 0.01f)) + return; + + lods[0] = new LOD(0.5f, lods[0].renderers); + } + internal static LODGroup CreateLodGroupFromRenderers(GameObject root) { var renderers = root.GetComponentsInChildren(); diff --git a/Tests/Editor/LodGenerationToolTests.cs b/Tests/Editor/LodGenerationToolTests.cs new file mode 100644 index 00000000..319bb332 --- /dev/null +++ b/Tests/Editor/LodGenerationToolTests.cs @@ -0,0 +1,39 @@ +using System.Collections.Generic; +using NUnit.Framework; +using UnityEngine; + +namespace SashaRX.UnityMeshLab.Tests +{ + public class LodGenerationToolTests + { + [Test] + public void NormalizeSingleLodTransitionForGeneration_AutoCreatedGroup_RestoresLod0Transition() + { + var lods = new List { new LOD(0.01f, new Renderer[0]) }; + + LodGenerationTool.NormalizeSingleLodTransitionForGeneration(lods, 1); + + Assert.AreEqual(0.5f, lods[0].screenRelativeTransitionHeight); + } + + [Test] + public void NormalizeSingleLodTransitionForGeneration_ExistingTransition_PreservesValue() + { + var lods = new List { new LOD(0.25f, new Renderer[0]) }; + + LodGenerationTool.NormalizeSingleLodTransitionForGeneration(lods, 1); + + Assert.AreEqual(0.25f, lods[0].screenRelativeTransitionHeight); + } + + [Test] + public void NormalizeSingleLodTransitionForGeneration_NotAppendingAfterLod0_PreservesValue() + { + var lods = new List { new LOD(0.01f, new Renderer[0]) }; + + LodGenerationTool.NormalizeSingleLodTransitionForGeneration(lods, 2); + + Assert.AreEqual(0.01f, lods[0].screenRelativeTransitionHeight); + } + } +} diff --git a/Tests/Editor/LodGenerationToolTests.cs.meta b/Tests/Editor/LodGenerationToolTests.cs.meta new file mode 100644 index 00000000..70f76ac0 --- /dev/null +++ b/Tests/Editor/LodGenerationToolTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2dc349cf3bf14ef7a607f290bc27a765 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: