Skip to content

Add LOD cross-fade support, null safety, and consistent LODGroup initialization#4

Open
Copilot wants to merge 2 commits into
masterfrom
copilot/improve-unity-lod-integration
Open

Add LOD cross-fade support, null safety, and consistent LODGroup initialization#4
Copilot wants to merge 2 commits into
masterfrom
copilot/improve-unity-lod-integration

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 11, 2026

AutoLOD creates LODGroup components across three code paths but never configures fade mode, uses inconsistent initialization patterns, and lacks null guards on renderer arrays.

LOD fade mode

  • New LODFadeMode and animateCrossFading settings in AutoLODSettingsData, LODImportSettings, and the Preferences UI
  • Applied to all three LODGroup creation sites: AutoLOD.GenerateLODs, ModelImporterLODGenerator.CreateLODGroup, LODVolume_Editor.GenerateHLOD/GenerateLODs
  • Per-model override via LODDataEditor inspector syncs fade settings

Consistent LODGroup initialization

ModelImporterLODGenerator.CreateLODGroup now uses the ForceLOD(0) / SetLODs / RecalculateBounds / ForceLOD(-1) pattern already used in AutoLOD.cs, ensuring deterministic initial LOD state.

Null safety

  • ModelImporterLODGenerator.CreateLODGroup: filters null renderers from LOD arrays before SetLODs
  • Extensions.HasLODChain: guards lod.renderers for null before .Length
  • LODGroupExtensions.SetRenderersEnabled: early-returns on null lods, skips null renderers

LODGroupHelper.InvalidateCache()

New public method to flush all cached derived state (m_LODs, m_ReferencePoint, m_WorldSpaceSize, m_MaxLOD). The setter already does this on reassignment, but external callers modifying the underlying LODGroup directly had no way to reset stale cache.

// Configure fade mode on created LODGroups
lodGroup.fadeMode = autoLODSettingsData.FadeMode;
lodGroup.animateCrossFading = autoLODSettingsData.AnimateCrossFading;

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…Cache()

Co-authored-by: YvesAlbuquerque <38794389+YvesAlbuquerque@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve Unity LOD integration Add LOD cross-fade support, null safety, and consistent LODGroup initialization Mar 11, 2026
@YvesAlbuquerque YvesAlbuquerque marked this pull request as ready for review May 7, 2026 09:16
Copilot AI review requested due to automatic review settings May 7, 2026 09:16
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds configurable LOD cross-fade behavior across AutoLOD’s LODGroup creation paths, improves null-safety when working with renderer arrays, and introduces a cache invalidation API for LODGroupHelper to prevent stale derived data.

Changes:

  • Introduces LODFadeMode and animateCrossFading settings in project preferences and import settings, and applies them when creating/configuring LODGroups.
  • Adds null-safety around LOD renderer arrays (filtering null renderers before SetLODs, and guarding null arrays during iteration).
  • Adds LODGroupHelper.InvalidateCache() to allow external callers to flush cached LODGroup-derived state.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Runtime/LODVolume_Editor.cs Applies fade mode + cross-fade animation settings when generating scene/volume LODGroups.
Runtime/LODImportSettings.cs Adds fade mode + animate cross-fading fields to per-asset import settings.
Runtime/LODGroupHelper.cs Adds InvalidateCache() for flushing cached LODGroup-derived values.
Runtime/Extensions/LODGroupExtensions.cs Adds null guards when enabling/disabling renderers via LOD arrays.
Runtime/AutoLODSettingsData.cs Stores fade mode + cross-fade animation settings and exposes them via properties.
Editor/ModelImporterLODGenerator.cs Filters null renderers, uses consistent ForceLOD init pattern, and applies fade settings on import-created LODGroups.
Editor/LODDataEditor.cs Syncs fade settings into per-model import settings when toggling defaults/overrides.
Editor/Extensions.cs Adds null guard for lod.renderers in HasLODChain().
Editor/AutoLODSettings.cs Adds Preferences UI for selecting LOD fade mode and cross-fade animation.
Editor/AutoLOD.cs Applies fade settings when generating LODGroups from the menu workflow.
CHANGELOG.md Documents the new fade settings, cache invalidation, and null-safety fixes.
Comments suppressed due to low confidence (1)

Editor/LODDataEditor.cs:50

  • LODImportSettings now contains fadeMode and animateCrossFading, but the custom LODImportSettingsDrawer currently does not render these fields. As a result, when Override Defaults is enabled, the per-model import settings UI won’t allow editing the new fade settings (and the PR description’s “per-model override” isn’t actually achievable). Update the drawer to display these properties (and ideally only show animateCrossFading when fade mode != None, matching Preferences).
                m_ImportSettings.FindPropertyRelative("fadeMode").enumValueIndex = (int)autoLODSettingsData.FadeMode;
                m_ImportSettings.FindPropertyRelative("animateCrossFading").boolValue = autoLODSettingsData.AnimateCrossFading;
            }

            if (settingsOverridden)
            {
                EditorGUILayout.PropertyField(m_ImportSettings, new GUIContent("Import Settings"), true);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -1,4 +1,5 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;
Comment thread Runtime/LODGroupHelper.cs
Comment on lines +69 to +79
/// <summary>
/// Invalidates all cached LODGroup data, forcing it to be re-fetched on next access.
/// Call this after modifying the LODGroup's LODs, transform, or size externally.
/// </summary>
public void InvalidateCache()
{
m_LODs = null;
m_ReferencePoint = null;
m_WorldSpaceSize = null;
m_MaxLOD = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants