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)). 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; } 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: diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/MeshComponent.cs index d44fbe1ae..ec4a8ca8b 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,25 @@ public static void CreateMesh(GameObject gameObject, Mesh m, PbrMaterial materia } } + 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); + 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 size = max - min; + if (sizeZ > 0) { + middle.z = posZ + sizeZ / 2; + size.z = sizeZ; + } + + return new Bounds(middle, size + margin * new float3(1f, 1f, 1f)); + } + 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 2f71c1781..b186225e6 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) { diff --git a/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs b/VisualPinball.Unity/VisualPinball.Unity/VPT/MetalWireGuide/MetalWireGuideMeshComponent.cs index 7731d25da..41f282214 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,13 @@ 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(); + mr.localBounds = CalculateBounds(mwgComponent.DragPoints, 25f, mwgComponent._standheight); + } } } 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); + } } }