summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Thronefall/NGS.MeshFusionPro/LODMeshFusionSource.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Thronefall_v1.0/Thronefall/NGS.MeshFusionPro/LODMeshFusionSource.cs')
-rw-r--r--Thronefall_v1.0/Thronefall/NGS.MeshFusionPro/LODMeshFusionSource.cs141
1 files changed, 141 insertions, 0 deletions
diff --git a/Thronefall_v1.0/Thronefall/NGS.MeshFusionPro/LODMeshFusionSource.cs b/Thronefall_v1.0/Thronefall/NGS.MeshFusionPro/LODMeshFusionSource.cs
new file mode 100644
index 0000000..4b941b9
--- /dev/null
+++ b/Thronefall_v1.0/Thronefall/NGS.MeshFusionPro/LODMeshFusionSource.cs
@@ -0,0 +1,141 @@
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace NGS.MeshFusionPro;
+
+public class LODMeshFusionSource : MeshFusionSource
+{
+ [SerializeField]
+ [HideInInspector]
+ private LODGroup _group;
+
+ private LODGroupCombineSource _source;
+
+ private CombinedLODGroupPart _part;
+
+ private Bounds? _savedBounds;
+
+ public override bool TryGetBounds(ref Bounds bounds)
+ {
+ if (_source != null)
+ {
+ bounds = _source.Bounds;
+ return true;
+ }
+ if (_part != null)
+ {
+ bounds = _part.Bounds;
+ return true;
+ }
+ if (_savedBounds.HasValue)
+ {
+ bounds = _savedBounds.Value;
+ return true;
+ }
+ if (base.IsIncompatible)
+ {
+ return false;
+ }
+ LOD[] lODs = _group.GetLODs();
+ bounds = new Bounds(base.transform.position, Vector3.zero);
+ for (int i = 0; i < lODs.Length; i++)
+ {
+ Renderer[] renderers = lODs[i].renderers;
+ for (int j = 0; j < renderers.Length; j++)
+ {
+ if (!(renderers[j] == null))
+ {
+ bounds.Encapsulate(renderers[j].bounds);
+ }
+ }
+ }
+ _savedBounds = bounds;
+ return true;
+ }
+
+ protected override void OnSourceCombinedInternal(ICombinedObject root, ICombinedObjectPart part)
+ {
+ _part = (CombinedLODGroupPart)part;
+ }
+
+ protected override bool CheckCompatibilityAndGetComponents(out string incompatibilityReason)
+ {
+ if (_group == null)
+ {
+ _group = GetComponent<LODGroup>();
+ }
+ if (_group == null)
+ {
+ incompatibilityReason = "LODGroup not found";
+ return false;
+ }
+ incompatibilityReason = "";
+ MeshFilter filter = null;
+ Mesh mesh = null;
+ LOD[] lODs = _group.GetLODs();
+ for (int i = 0; i < lODs.Length; i++)
+ {
+ Renderer[] renderers = lODs[i].renderers;
+ for (int j = 0; j < renderers.Length; j++)
+ {
+ MeshRenderer renderer = renderers[j] as MeshRenderer;
+ if (!(renderer == null) && !CanCreateCombineSource(renderer.gameObject, ref incompatibilityReason, ref renderer, ref filter, ref mesh))
+ {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ protected override void CreateSources()
+ {
+ _source = new LODGroupCombineSource(_group);
+ }
+
+ protected override IEnumerable<ICombineSource> GetCombineSources()
+ {
+ if (_source != null)
+ {
+ yield return _source;
+ }
+ }
+
+ protected override IEnumerable<ICombinedObjectPart> GetCombinedParts()
+ {
+ if (_part != null)
+ {
+ yield return _part;
+ }
+ }
+
+ protected override void ClearSources()
+ {
+ if (_source != null)
+ {
+ _source = null;
+ }
+ }
+
+ protected override void ClearParts()
+ {
+ if (_part != null)
+ {
+ _part = null;
+ }
+ }
+
+ protected override void ToggleComponents(bool enabled)
+ {
+ LOD[] lODs = _group.GetLODs();
+ for (int i = 0; i < lODs.Length; i++)
+ {
+ Renderer[] renderers = lODs[i].renderers;
+ for (int j = 0; j < renderers.Length; j++)
+ {
+ renderers[j].enabled = enabled;
+ }
+ }
+ _group.enabled = enabled;
+ }
+}