From 89ffa5ab26f0ffa95f68d543ad807fec6f2e0bc2 Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 4 Sep 2022 14:11:38 +0200 Subject: [PATCH 1/7] fix: Bounding box for metal wire guides. --- .../MetalWireGuide/MetalWireGuideComponent.cs | 31 ++++++++++++++++++- .../MetalWireGuideMeshComponent.cs | 13 +++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideComponent.cs index 2f71c1781..7042069d6 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideComponent.cs @@ -24,6 +24,7 @@ using System; using System.Collections.Generic; using Unity.Entities; +using Unity.Mathematics; using UnityEngine; using VisualPinball.Engine.Math; using VisualPinball.Engine.VPT; @@ -195,7 +196,7 @@ public override MetalWireGuideData CopyDataTo(MetalWireGuideData data, string[] #region Editor Tooling - private Vector3 DragPointCenter { + internal Vector3 DragPointCenter { get { var sum = Vertex3D.Zero; foreach (var t in DragPoints) { @@ -206,6 +207,34 @@ private Vector3 DragPointCenter { } } + internal Vector3 DragPointMiddle { + get { + var min = new float3(float.MaxValue, float.MaxValue, float.MaxValue); + var max = new float3(float.MinValue, float.MinValue, float.MinValue); + foreach (var t in DragPoints) { + var p = (float3)t.Center.ToUnityVector3(); + min = math.min(min, p); + max = math.max(max, p); + } + var xy = min + (max - min) / 2; + return new Vector3(xy.x, xy.y, 0); + } + } + + internal Vector3 DragPointSize { + get { + var min = new float3(float.MaxValue, float.MaxValue, float.MaxValue); + var max = new float3(float.MinValue, float.MinValue, float.MinValue); + foreach (var t in DragPoints) { + var p = (float3)t.Center.ToUnityVector3(); + min = math.min(min, p); + max = math.max(max, p); + } + var xy = max - min; + return new Vector3(xy.x, xy.y, _standheight); + } + } + public override ItemDataTransformType EditorPositionType => ItemDataTransformType.ThreeD; public override Vector3 GetEditorPosition() { diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs index 7731d25da..81dc05cac 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -using System; using UnityEngine; using VisualPinball.Engine.VPT; using VisualPinball.Engine.VPT.MetalWireGuide; @@ -32,5 +31,17 @@ protected override Mesh GetMesh(MetalWireGuideData data) protected override PbrMaterial GetMaterial(MetalWireGuideData data, Table table) => new MetalWireGuideMeshGenerator(MainComponent).GetMaterial(table, data); + + public override void RebuildMeshes() + { + base.RebuildMeshes(); + var mwgComponent = GetComponentInParent(); + var mr = GetComponent(); + + var bounds = mr.localBounds; + bounds.center = mwgComponent.DragPointMiddle; + bounds.size = mwgComponent.DragPointSize + 25f * Vector3.one; + mr.localBounds = bounds; + } } } From bd5ee15c7865c391e850636b3e0f844f3325c1fb Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 4 Sep 2022 14:11:58 +0200 Subject: [PATCH 2/7] chore: Use Logger instead of Debug.Log. --- .../AssetBrowser/AssetBrowser.cs | 8 ++++++-- .../VisualPinball.Unity.Editor/AssetBrowser/AssetQuery.cs | 6 ++++-- .../PropertyDrawers/TypeRestrictionPropertyDrawer.cs | 8 ++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/VisualPinball.Unity/VisualPinball.Unity.Editor/AssetBrowser/AssetBrowser.cs b/VisualPinball.Unity/VisualPinball.Unity.Editor/AssetBrowser/AssetBrowser.cs index 639b762ac..52aac9cac 100644 --- a/VisualPinball.Unity/VisualPinball.Unity.Editor/AssetBrowser/AssetBrowser.cs +++ b/VisualPinball.Unity/VisualPinball.Unity.Editor/AssetBrowser/AssetBrowser.cs @@ -20,9 +20,11 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using NLog; using UnityEditor; using UnityEngine; using UnityEngine.UIElements; +using Logger = NLog.Logger; namespace VisualPinball.Unity.Editor { @@ -59,6 +61,8 @@ private AssetResult LastSelectedAsset { private readonly Dictionary _elementByAsset = new(); private readonly Dictionary _assetsByElement = new(); + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + [MenuItem("Visual Pinball/Asset Browser")] public static void ShowWindow() { @@ -297,10 +301,10 @@ public void AddAssets(IEnumerable paths, Func> _attributes = new(); private readonly HashSet _tags = new(); + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + public AssetQuery(List libraries) { _libraries = libraries; @@ -135,7 +137,7 @@ private void Run() }); } catch (Exception e) { - Debug.LogError($"Error reading assets from {lib.Name}, maybe corruption? ({e.Message})\n{e.StackTrace}"); + Logger.Error($"Error reading assets from {lib.Name}, maybe corruption? ({e.Message})\n{e.StackTrace}"); // old data or whatever, just don't crash here. return Array.Empty(); } diff --git a/VisualPinball.Unity/VisualPinball.Unity.Editor/Utils/PropertyDrawers/TypeRestrictionPropertyDrawer.cs b/VisualPinball.Unity/VisualPinball.Unity.Editor/Utils/PropertyDrawers/TypeRestrictionPropertyDrawer.cs index 38b92655c..b7b35869d 100644 --- a/VisualPinball.Unity/VisualPinball.Unity.Editor/Utils/PropertyDrawers/TypeRestrictionPropertyDrawer.cs +++ b/VisualPinball.Unity/VisualPinball.Unity.Editor/Utils/PropertyDrawers/TypeRestrictionPropertyDrawer.cs @@ -16,9 +16,11 @@ using System; using System.Linq; +using NLog; using UnityEditor; using UnityEditor.IMGUI.Controls; using UnityEngine; +using Logger = NLog.Logger; namespace VisualPinball.Unity.Editor { @@ -28,6 +30,8 @@ public class TypeRestrictionPropertyDrawer : PropertyDrawer private MonoBehaviour _component; private AdvancedDropdownState _itemPickDropdownState; + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + public override float GetPropertyHeight(SerializedProperty property, GUIContent label) => EditorGUIUtility.singleLineHeight + 2; public override void OnGUI(Rect pos, SerializedProperty property, GUIContent label) @@ -36,7 +40,7 @@ public override void OnGUI(Rect pos, SerializedProperty property, GUIContent lab #region Sanity Checks if (property.propertyType != SerializedPropertyType.ObjectReference) { - Debug.LogError("[TypeRestriction] attribute must be on an object reference."); + Logger.Error("[TypeRestriction] attribute must be on an object reference."); return; } @@ -46,7 +50,7 @@ public override void OnGUI(Rect pos, SerializedProperty property, GUIContent lab var comp = property.serializedObject.targetObject as Component; if (comp == null) { - Debug.LogError($"Cannot find component of {property.serializedObject.targetObject.name}."); + Logger.Error($"Cannot find component of {property.serializedObject.targetObject.name}."); return; } From 6fd9aa18d2fed6e8c33caa9337aafd75a7e74d1f Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 4 Sep 2022 22:58:16 +0200 Subject: [PATCH 3/7] mesh: Make bounds computation reusable. --- .../VisualPinball.Unity/VPT/MeshComponent.cs | 19 +++++++++++++ .../MetalWireGuide/MetalWireGuideComponent.cs | 28 ------------------- .../MetalWireGuideMeshComponent.cs | 6 +--- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs index d44fbe1ae..837ea5c51 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs @@ -15,7 +15,10 @@ // along with this program. If not, see . using System; +using System.Collections.Generic; +using Unity.Mathematics; using UnityEngine; +using VisualPinball.Engine.Math; using VisualPinball.Engine.VPT; using VisualPinball.Engine.VPT.Table; using Mesh = VisualPinball.Engine.VPT.Mesh; @@ -98,6 +101,22 @@ public static void CreateMesh(GameObject gameObject, Mesh m, PbrMaterial materia } } + protected static Bounds CalculateBounds(IEnumerable dragPoints, float margin = 0, float sizeZ = 0) + { + var min = new float3(float.MaxValue, float.MaxValue, float.MaxValue); + var max = new float3(float.MinValue, float.MinValue, float.MinValue); + foreach (var t in dragPoints) { + var p = (float3)t.Center.ToUnityVector3(); + min = math.min(min, p); + max = math.max(max, p); + } + var middle = min + (max - min) / 2; + var sizeXy = max - min; + var size = new float3(sizeXy.x, sizeXy.y, sizeZ > 0 ? sizeZ : sizeXy.z) + margin * new float3(1f, 1f, 1f); + + return new Bounds(middle, size); + } + private void UpdateMesh() { var data = MainComponent.InstantiateData(); diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideComponent.cs index 7042069d6..b186225e6 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideComponent.cs @@ -207,34 +207,6 @@ internal Vector3 DragPointCenter { } } - internal Vector3 DragPointMiddle { - get { - var min = new float3(float.MaxValue, float.MaxValue, float.MaxValue); - var max = new float3(float.MinValue, float.MinValue, float.MinValue); - foreach (var t in DragPoints) { - var p = (float3)t.Center.ToUnityVector3(); - min = math.min(min, p); - max = math.max(max, p); - } - var xy = min + (max - min) / 2; - return new Vector3(xy.x, xy.y, 0); - } - } - - internal Vector3 DragPointSize { - get { - var min = new float3(float.MaxValue, float.MaxValue, float.MaxValue); - var max = new float3(float.MinValue, float.MinValue, float.MinValue); - foreach (var t in DragPoints) { - var p = (float3)t.Center.ToUnityVector3(); - min = math.min(min, p); - max = math.max(max, p); - } - var xy = max - min; - return new Vector3(xy.x, xy.y, _standheight); - } - } - public override ItemDataTransformType EditorPositionType => ItemDataTransformType.ThreeD; public override Vector3 GetEditorPosition() { diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs index 81dc05cac..41f282214 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs @@ -37,11 +37,7 @@ public override void RebuildMeshes() base.RebuildMeshes(); var mwgComponent = GetComponentInParent(); var mr = GetComponent(); - - var bounds = mr.localBounds; - bounds.center = mwgComponent.DragPointMiddle; - bounds.size = mwgComponent.DragPointSize + 25f * Vector3.one; - mr.localBounds = bounds; + mr.localBounds = CalculateBounds(mwgComponent.DragPoints, 25f, mwgComponent._standheight); } } } From 6b6c8497da0127ad7f5471fb93c211def24a64a3 Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 4 Sep 2022 23:24:30 +0200 Subject: [PATCH 4/7] mesh: Make bounds calculation more readable. --- .../VisualPinball.Unity/VPT/MeshComponent.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs index 837ea5c51..ec4a8ca8b 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs @@ -101,7 +101,7 @@ public static void CreateMesh(GameObject gameObject, Mesh m, PbrMaterial materia } } - protected static Bounds CalculateBounds(IEnumerable dragPoints, float margin = 0, float sizeZ = 0) + protected static Bounds CalculateBounds(IEnumerable dragPoints, float margin = 0, float sizeZ = 0, float posZ = 0) { var min = new float3(float.MaxValue, float.MaxValue, float.MaxValue); var max = new float3(float.MinValue, float.MinValue, float.MinValue); @@ -111,10 +111,13 @@ protected static Bounds CalculateBounds(IEnumerable dragPoints, f max = math.max(max, p); } var middle = min + (max - min) / 2; - var sizeXy = max - min; - var size = new float3(sizeXy.x, sizeXy.y, sizeZ > 0 ? sizeZ : sizeXy.z) + margin * new float3(1f, 1f, 1f); + var size = max - min; + if (sizeZ > 0) { + middle.z = posZ + sizeZ / 2; + size.z = sizeZ; + } - return new Bounds(middle, size); + return new Bounds(middle, size + margin * new float3(1f, 1f, 1f)); } private void UpdateMesh() From ec39847bef22302ab81f28c37c58eb45eaacf91b Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 4 Sep 2022 23:25:24 +0200 Subject: [PATCH 5/7] mesh: Fix bounds for surfaces. --- .../VPT/Surface/SurfaceSideMeshComponent.cs | 8 ++++++++ .../VPT/Surface/SurfaceTopMeshComponent.cs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/Surface/SurfaceSideMeshComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/Surface/SurfaceSideMeshComponent.cs index fd075e116..5c5e23a57 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/VPT/Surface/SurfaceSideMeshComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/VPT/Surface/SurfaceSideMeshComponent.cs @@ -35,5 +35,13 @@ protected override Mesh GetMesh(SurfaceData data) protected override PbrMaterial GetMaterial(SurfaceData data, Table table) => new SurfaceMeshGenerator(data).GetMaterial(SurfaceMeshGenerator.Side, table, data); + + public override void RebuildMeshes() + { + base.RebuildMeshes(); + var sc = GetComponentInParent(); + var mr = GetComponent(); + mr.localBounds = CalculateBounds(sc.DragPoints, 0, sc.HeightTop - sc.HeightBottom, sc.HeightBottom); + } } } diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/Surface/SurfaceTopMeshComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/Surface/SurfaceTopMeshComponent.cs index fa4577b0e..e2442e4da 100644 --- a/VisualPinball.Unity/VisualPinball.Unity/VPT/Surface/SurfaceTopMeshComponent.cs +++ b/VisualPinball.Unity/VisualPinball.Unity/VPT/Surface/SurfaceTopMeshComponent.cs @@ -35,5 +35,13 @@ protected override Mesh GetMesh(SurfaceData data) protected override PbrMaterial GetMaterial(SurfaceData data, Table table) => new SurfaceMeshGenerator(data).GetMaterial(SurfaceMeshGenerator.Top, table, data); + + public override void RebuildMeshes() + { + base.RebuildMeshes(); + var sc = GetComponentInParent(); + var mr = GetComponent(); + mr.localBounds = CalculateBounds(sc.DragPoints, 0, 2f, sc.HeightTop - 1f); + } } } From 40925518ecb7348a89e0634c024eadb4402966b3 Mon Sep 17 00:00:00 2001 From: freezy Date: Sun, 4 Sep 2022 23:56:15 +0200 Subject: [PATCH 6/7] debug: Add bounding box component. --- .../Common/BoundingBoxComponent.cs | 42 +++++++++++++++++++ .../Common/BoundingBoxComponent.cs.meta | 11 +++++ 2 files changed, 53 insertions(+) create mode 100644 VisualPinball.Unity/VisualPinball.Unity/Common/BoundingBoxComponent.cs create mode 100644 VisualPinball.Unity/VisualPinball.Unity/Common/BoundingBoxComponent.cs.meta diff --git a/VisualPinball.Unity/VisualPinball.Unity/Common/BoundingBoxComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/Common/BoundingBoxComponent.cs new file mode 100644 index 000000000..1c601ede8 --- /dev/null +++ b/VisualPinball.Unity/VisualPinball.Unity/Common/BoundingBoxComponent.cs @@ -0,0 +1,42 @@ +// Visual Pinball Engine +// Copyright (C) 2022 freezy and VPE Team +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using UnityEngine; + +namespace VisualPinball.Unity.Editor +{ + public class BoundingBoxComponent : MonoBehaviour + { + + void OnDrawGizmosSelected() + { + Gizmos.color = Color.yellow; + var r = GetComponent(); + if (r != null) { + var b = r.bounds; + Gizmos.DrawSphere(b.center, 0.001f); //center sphere + Gizmos.DrawWireCube(b.center, b.size); + } else { + var rs = GetComponentsInChildren(); + foreach (var r2 in rs) { + var b = r2.bounds; + Gizmos.DrawSphere(b.center, 0.001f); //center sphere + Gizmos.DrawWireCube(b.center, b.size); + } + } + } + } +} diff --git a/VisualPinball.Unity/VisualPinball.Unity/Common/BoundingBoxComponent.cs.meta b/VisualPinball.Unity/VisualPinball.Unity/Common/BoundingBoxComponent.cs.meta new file mode 100644 index 000000000..379ad1512 --- /dev/null +++ b/VisualPinball.Unity/VisualPinball.Unity/Common/BoundingBoxComponent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5aa9d03717239ed4e872ccba5e14b6e5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 52fc0213ebe598d037c2b5c14f838d0ef2574c29 Mon Sep 17 00:00:00 2001 From: freezy Date: Mon, 5 Sep 2022 00:18:23 +0200 Subject: [PATCH 7/7] doc: Update changelog. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bb6f860e..87bc1da82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,7 @@ Built with Unity 2021.3.0 - Put game-, mesh-, collision- animation data into separate components ([#227](https://github.com/freezy/VisualPinball.Engine/pull/227), [Documentation](https://docs.visualpinball.org/creators-guide/editor/unity-components.html)). ### Fixed +- Disappearing objects due to wrong bounding box ([#441](https://github.com/freezy/VisualPinball.Engine/pull/441)). - Default table import ([#434](https://github.com/freezy/VisualPinball.Engine/pull/434)) - Remaining ball spinning issue should now be solved ([#397](https://github.com/freezy/VisualPinball.Engine/pull/397)). - Physics error when the ball would stop rotate ([#393](https://github.com/freezy/VisualPinball.Engine/pull/393)).