From 26a2b13ccba0f4cf1b5415c3fd018d232f8d0577 Mon Sep 17 00:00:00 2001 From: Ahmed Shariff Date: Sun, 7 Jan 2024 23:31:46 -0800 Subject: [PATCH] Allow root position of continuous surface set with JointFollower --- Runtime/Interaction/DeformableSurface.cs | 2 +- .../DeformableSurfaceCollidersManager.cs | 14 ++------- .../Interaction/HPUIContinuousInteractable.cs | 31 ++++++++++++++----- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Runtime/Interaction/DeformableSurface.cs b/Runtime/Interaction/DeformableSurface.cs index 993fc07..7ffe780 100644 --- a/Runtime/Interaction/DeformableSurface.cs +++ b/Runtime/Interaction/DeformableSurface.cs @@ -130,7 +130,7 @@ public static void GenerateMeshBottomMiddleOrigin(float x_size, float y_size, fl for (int i = 0; i < x_divisions; i++) { vertices.Add(new Vector3(x_size * ((i - ((float)x_divisions / 2.0f)) / (float)x_divisions), surfaceOffset, y_size * (k / (float)y_divisions))); - normals.Add(Vector3.forward); + normals.Add(Vector3.down); uvs.Add(new Vector2(1 - k / (float)(y_divisions - 1), i / (float)(x_divisions - 1))); } diff --git a/Runtime/Interaction/DeformableSurfaceCollidersManager.cs b/Runtime/Interaction/DeformableSurfaceCollidersManager.cs index 4d5aaeb..34cb356 100644 --- a/Runtime/Interaction/DeformableSurfaceCollidersManager.cs +++ b/Runtime/Interaction/DeformableSurfaceCollidersManager.cs @@ -19,7 +19,6 @@ public class DeformableSurfaceCollidersManager: MonoBehaviour private SkinnedMeshRenderer skinnedMeshRenderer; private Mesh tempMesh; - private List collidersCache; private Vector3 scaleFactor; private int maxY, maxX; private Vector2 surfaceBounds; @@ -58,7 +57,7 @@ private void Update() /// /// Setup and return colliders. A collider will be placed on each vertice of the . /// - public List SetupColliders() + public List SetupColliders(Transform collidersRootTransform) { generatedColliders = false; if (skinnedMeshRenderer == null) @@ -92,7 +91,7 @@ public List SetupColliders() maxY = vertices_native.Length - continuousInteractable.x_divisions; maxX = continuousInteractable.x_divisions; - List colliders = GenerateColliders(vertices, normals, transform, continuousInteractable.x_divisions); + List colliders = GenerateColliders(vertices, normals, collidersRootTransform, continuousInteractable.x_divisions); surfaceBounds = new Vector2(gridSize * (continuousInteractable.x_divisions - 1), gridSize * (continuousInteractable.y_divisions - 1)); @@ -125,15 +124,6 @@ private List GenerateColliders(List positions, List GameObject colliderObj; scaleFactor = Vector3.zero; - if (collidersCache == null) - { - collidersCache = new List(); - } - - foreach(GameObject obj in collidersCache) - { - Destroy(obj); - } Transform[] colliderTransforms = new Transform[positions.Count]; List colliders = new List(); diff --git a/Runtime/Interaction/HPUIContinuousInteractable.cs b/Runtime/Interaction/HPUIContinuousInteractable.cs index 8b417f8..74f5e03 100644 --- a/Runtime/Interaction/HPUIContinuousInteractable.cs +++ b/Runtime/Interaction/HPUIContinuousInteractable.cs @@ -1,7 +1,6 @@ using System.Collections; using System.Collections.Generic; using ubco.ovilab.HPUI.Tracking; -using ubco.ovilab.HPUI.Utils; using UnityEngine; using UnityEngine.XR.Hands; using UnityEngine.XR.Interaction.Toolkit; @@ -31,6 +30,8 @@ public class HPUIContinuousInteractable: HPUIBaseInteractable public List keypointJoints; [Tooltip("(Optional) The default material to use for the surface.")] public Material defaultMaterial; + [Tooltip("(Optional) the MeshFilter of the corresponding SkinnedMeshRenderer. If not set, will create a child object with the MeshFilter and SkinnedMeshRenderer.")] + public MeshFilter filter; [SerializeField] private HPUIContinuousSurfaceEvent continuousSurfaceCreatedEvent = new HPUIContinuousSurfaceEvent(); @@ -46,8 +47,8 @@ public HPUIContinuousSurfaceEvent ContinuousSurfaceEvent public int x_divisions { get; private set; } private List keypointsCache; - private MeshFilter filter; private DeformableSurfaceCollidersManager surfaceCollidersManager; + private GameObject collidersRoot; /// /// See . @@ -96,11 +97,13 @@ private List SetupKeypoints() keypointTransforms.Add(keypoint); } - GameObject surfaceRoot = keypointTransforms[0].gameObject; - filter = surfaceRoot.GetComponent(); if (filter == null) { - filter = surfaceRoot.AddComponent(); + GameObject skinNode = new GameObject("SkinNode"); + skinNode.transform.parent = this.transform; + filter = skinNode.AddComponent(); + skinNode.transform.localPosition = Vector3.zero; + skinNode.transform.localRotation = Quaternion.identity; } return keypointTransforms; @@ -117,7 +120,10 @@ public void Configure() { for (int i = 0; i < keypointsCache.Count; ++i) { - Destroy(keypointsCache[i].gameObject); + if (keypointsCache[i] != transform) + { + Destroy(keypointsCache[i].gameObject); + } } } keypointsCache = SetupKeypoints(); @@ -136,6 +142,11 @@ private IEnumerator DelayedExecuteCalibration(float x_size, float y_size, List().material = defaultMaterial; } + collidersRoot = new GameObject("CollidersRoot"); + collidersRoot.transform.parent = this.transform; + collidersRoot.transform.localPosition = Vector3.zero; + collidersRoot.transform.localRotation = Quaternion.identity; + surfaceCollidersManager = filter.GetComponent(); if (surfaceCollidersManager == null) { surfaceCollidersManager = filter.gameObject.AddComponent(); } - List generatedColliders = surfaceCollidersManager.SetupColliders(); + List generatedColliders = surfaceCollidersManager.SetupColliders(collidersRoot.transform); colliders.AddRange(generatedColliders); + // Forcing regsitration of interactable to run OnDisable(); OnEnable();