From ed4e43a04484d1ce5b1724522385358e5942c996 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 13 Jan 2025 16:49:28 -0800 Subject: [PATCH 1/3] delete duplicate box shape fixer --- NewHorizons/Builder/Props/QuantumBuilder.cs | 35 ------------------- .../Utility/Geometry/BoxShapeFinder.cs | 34 ++++++++---------- 2 files changed, 15 insertions(+), 54 deletions(-) diff --git a/NewHorizons/Builder/Props/QuantumBuilder.cs b/NewHorizons/Builder/Props/QuantumBuilder.cs index 90227e11..a6a475cf 100644 --- a/NewHorizons/Builder/Props/QuantumBuilder.cs +++ b/NewHorizons/Builder/Props/QuantumBuilder.cs @@ -295,39 +295,4 @@ namespace NewHorizons.Builder.Props return globalCorners.Select(globalCorner => relativeTo.transform.InverseTransformPoint(globalCorner)).ToArray(); } } - - /// - /// for some reason mesh bounds are wrong unless we wait a bit - /// so this script contiously checks everything until it is correct - /// - /// this actually only seems to be a problem with skinned renderers. normal ones work fine - /// TODO: at some point narrow this down to just skinned, instead of doing everything and checking every frame - /// - public class BoxShapeFixer : MonoBehaviour - { - public BoxShape shape; - public MeshFilter meshFilter; - public SkinnedMeshRenderer skinnedMeshRenderer; - - public void Update() - { - if (meshFilter == null && skinnedMeshRenderer == null) - { - NHLogger.LogVerbose("Useless BoxShapeFixer, destroying"); - DestroyImmediate(this); - } - - Mesh sharedMesh = null; - if (meshFilter != null) sharedMesh = meshFilter.sharedMesh; - if (skinnedMeshRenderer != null) sharedMesh = skinnedMeshRenderer.sharedMesh; - - if (sharedMesh == null) return; - if (sharedMesh.bounds.size == Vector3.zero) return; - - shape.size = sharedMesh.bounds.size; - shape.center = sharedMesh.bounds.center; - - DestroyImmediate(this); - } - } } diff --git a/NewHorizons/Utility/Geometry/BoxShapeFinder.cs b/NewHorizons/Utility/Geometry/BoxShapeFinder.cs index ebdfd9e4..40e5c37a 100644 --- a/NewHorizons/Utility/Geometry/BoxShapeFinder.cs +++ b/NewHorizons/Utility/Geometry/BoxShapeFinder.cs @@ -3,7 +3,14 @@ using UnityEngine; namespace NewHorizons.Utility.Geometry; -internal class BoxShapeFixer : MonoBehaviour +/// +/// for some reason mesh bounds are wrong unless we wait a bit +/// so this script contiously checks everything until it is correct +/// +/// this actually only seems to be a problem with skinned renderers. normal ones work fine +/// TODO: at some point narrow this down to just skinned, instead of doing everything and checking every frame +/// +public class BoxShapeFixer : MonoBehaviour { public BoxShape shape; public MeshFilter meshFilter; @@ -13,31 +20,20 @@ internal class BoxShapeFixer : MonoBehaviour { if (meshFilter == null && skinnedMeshRenderer == null) { - NHLogger.LogVerbose("Useless BoxShapeFixer, destroying"); DestroyImmediate(this); + NHLogger.LogVerbose("Useless BoxShapeFixer, destroying"); + DestroyImmediate(this); } Mesh sharedMesh = null; - if (meshFilter != null) - { - sharedMesh = meshFilter.sharedMesh; - } - if (skinnedMeshRenderer != null) - { - sharedMesh = skinnedMeshRenderer.sharedMesh; - } + if (meshFilter != null) sharedMesh = meshFilter.sharedMesh; + if (skinnedMeshRenderer != null) sharedMesh = skinnedMeshRenderer.sharedMesh; - if (sharedMesh == null) - { - return; - } - if (sharedMesh.bounds.size == Vector3.zero) - { - return; - } + if (sharedMesh == null) return; + if (sharedMesh.bounds.size == Vector3.zero) return; shape.size = sharedMesh.bounds.size; shape.center = sharedMesh.bounds.center; DestroyImmediate(this); } -} +} \ No newline at end of file From 06023b77544d13dde7961978edbe611bbccc94f2 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 13 Jan 2025 16:51:46 -0800 Subject: [PATCH 2/3] delete duplicate BoundsUtilities stuff --- NewHorizons/Builder/Props/QuantumBuilder.cs | 70 +------------------ .../Utility/Geometry/BoundsUtilities.cs | 17 ++--- 2 files changed, 10 insertions(+), 77 deletions(-) diff --git a/NewHorizons/Builder/Props/QuantumBuilder.cs b/NewHorizons/Builder/Props/QuantumBuilder.cs index a6a475cf..302270e8 100644 --- a/NewHorizons/Builder/Props/QuantumBuilder.cs +++ b/NewHorizons/Builder/Props/QuantumBuilder.cs @@ -223,76 +223,8 @@ namespace NewHorizons.Builder.Props shuffle._shuffledObjects = propsInGroup.Select(p => p.transform).ToArray(); shuffle.Awake(); // this doesn't get called on its own for some reason. what? how? - AddBoundsVisibility(shuffleParent); + BoundsUtilities.AddBoundsVisibility(shuffleParent); shuffleParent.SetActive(true); } - - - struct BoxShapeReciever - { - public MeshFilter f; - public SkinnedMeshRenderer s; - public GameObject gameObject; - } - - public static void AddBoundsVisibility(GameObject g) - { - var meshFilters = g.GetComponentsInChildren(); - var skinnedMeshRenderers = g.GetComponentsInChildren(); - - var boxShapeRecievers = meshFilters - .Select(f => new BoxShapeReciever() { f = f, gameObject = f.gameObject }) - .Concat( - skinnedMeshRenderers.Select(s => new BoxShapeReciever() { s = s, gameObject = s.gameObject }) - ) - .ToList(); - - foreach (var boxshapeReciever in boxShapeRecievers) - { - var box = boxshapeReciever.gameObject.AddComponent(); - boxshapeReciever.gameObject.AddComponent(); - boxshapeReciever.gameObject.AddComponent(); - - var fixer = boxshapeReciever.gameObject.AddComponent(); - fixer.shape = box; - fixer.meshFilter = boxshapeReciever.f; - fixer.skinnedMeshRenderer = boxshapeReciever.s; - } - } - - // BUG: ignores skinned guys. this coincidentally makes it work without BoxShapeFixer - public static Bounds GetBoundsOfSelfAndChildMeshes(GameObject g) - { - var meshFilters = g.GetComponentsInChildren(); - var corners = meshFilters.SelectMany(m => GetMeshCorners(m, g)).ToList(); - - Bounds b = new Bounds(corners[0], Vector3.zero); - corners.ForEach(corner => b.Encapsulate(corner)); - - return b; - } - - public static Vector3[] GetMeshCorners(MeshFilter m, GameObject relativeTo = null) - { - var bounds = m.mesh.bounds; - - var localCorners = new Vector3[] - { - bounds.min, - bounds.max, - new Vector3(bounds.min.x, bounds.min.y, bounds.max.z), - new Vector3(bounds.min.x, bounds.max.y, bounds.min.z), - new Vector3(bounds.max.x, bounds.min.y, bounds.min.z), - new Vector3(bounds.min.x, bounds.max.y, bounds.max.z), - new Vector3(bounds.max.x, bounds.min.y, bounds.max.z), - new Vector3(bounds.max.x, bounds.max.y, bounds.min.z), - }; - - var globalCorners = localCorners.Select(localCorner => m.transform.TransformPoint(localCorner)).ToArray(); - - if (relativeTo == null) return globalCorners; - - return globalCorners.Select(globalCorner => relativeTo.transform.InverseTransformPoint(globalCorner)).ToArray(); - } } } diff --git a/NewHorizons/Utility/Geometry/BoundsUtilities.cs b/NewHorizons/Utility/Geometry/BoundsUtilities.cs index b251a6d7..9f60ce0d 100644 --- a/NewHorizons/Utility/Geometry/BoundsUtilities.cs +++ b/NewHorizons/Utility/Geometry/BoundsUtilities.cs @@ -40,6 +40,7 @@ public static class BoundsUtilities } } + // BUG: ignores skinned guys. this coincidentally makes it work without BoxShapeFixer public static Bounds GetBoundsOfSelfAndChildMeshes(GameObject gameObject) { var meshFilters = gameObject.GetComponentsInChildren(); @@ -57,14 +58,14 @@ public static class BoundsUtilities var localCorners = new Vector3[] { - bounds.min, - bounds.max, - new Vector3(bounds.min.x, bounds.min.y, bounds.max.z), - new Vector3(bounds.min.x, bounds.max.y, bounds.min.z), - new Vector3(bounds.max.x, bounds.min.y, bounds.min.z), - new Vector3(bounds.min.x, bounds.max.y, bounds.max.z), - new Vector3(bounds.max.x, bounds.min.y, bounds.max.z), - new Vector3(bounds.max.x, bounds.max.y, bounds.min.z), + bounds.min, + bounds.max, + new Vector3(bounds.min.x, bounds.min.y, bounds.max.z), + new Vector3(bounds.min.x, bounds.max.y, bounds.min.z), + new Vector3(bounds.max.x, bounds.min.y, bounds.min.z), + new Vector3(bounds.min.x, bounds.max.y, bounds.max.z), + new Vector3(bounds.max.x, bounds.min.y, bounds.max.z), + new Vector3(bounds.max.x, bounds.max.y, bounds.min.z), }; var globalCorners = localCorners.Select(meshFilter.transform.TransformPoint).ToArray(); From 649883ab4e464fc6f9e3d139a07972e532a0fc91 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Mon, 13 Jan 2025 16:53:59 -0800 Subject: [PATCH 3/3] j --- NewHorizons/Utility/Geometry/BoxShapeFinder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NewHorizons/Utility/Geometry/BoxShapeFinder.cs b/NewHorizons/Utility/Geometry/BoxShapeFinder.cs index 40e5c37a..1c0173a8 100644 --- a/NewHorizons/Utility/Geometry/BoxShapeFinder.cs +++ b/NewHorizons/Utility/Geometry/BoxShapeFinder.cs @@ -36,4 +36,4 @@ public class BoxShapeFixer : MonoBehaviour DestroyImmediate(this); } -} \ No newline at end of file +}