Skip to content

Commit

Permalink
Allow root position of continuous surface set with JointFollower
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-shariff committed Jan 8, 2024
1 parent 4995de3 commit 26a2b13
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Runtime/Interaction/DeformableSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down
14 changes: 2 additions & 12 deletions Runtime/Interaction/DeformableSurfaceCollidersManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class DeformableSurfaceCollidersManager: MonoBehaviour
private SkinnedMeshRenderer skinnedMeshRenderer;
private Mesh tempMesh;

private List<GameObject> collidersCache;
private Vector3 scaleFactor;
private int maxY, maxX;
private Vector2 surfaceBounds;
Expand Down Expand Up @@ -58,7 +57,7 @@ private void Update()
/// <summary>
/// Setup and return colliders. A collider will be placed on each vertice of the <see cref="SkinnedMeshRenderer"/>.
/// </summary>
public List<Collider> SetupColliders()
public List<Collider> SetupColliders(Transform collidersRootTransform)
{
generatedColliders = false;
if (skinnedMeshRenderer == null)
Expand Down Expand Up @@ -92,7 +91,7 @@ public List<Collider> SetupColliders()
maxY = vertices_native.Length - continuousInteractable.x_divisions;
maxX = continuousInteractable.x_divisions;

List<Collider> colliders = GenerateColliders(vertices, normals, transform, continuousInteractable.x_divisions);
List<Collider> colliders = GenerateColliders(vertices, normals, collidersRootTransform, continuousInteractable.x_divisions);

surfaceBounds = new Vector2(gridSize * (continuousInteractable.x_divisions - 1), gridSize * (continuousInteractable.y_divisions - 1));

Expand Down Expand Up @@ -125,15 +124,6 @@ private List<Collider> GenerateColliders(List<Vector3> positions, List<Vector3>
GameObject colliderObj;
scaleFactor = Vector3.zero;

if (collidersCache == null)
{
collidersCache = new List<GameObject>();
}

foreach(GameObject obj in collidersCache)
{
Destroy(obj);
}
Transform[] colliderTransforms = new Transform[positions.Count];
List<Collider> colliders = new List<Collider>();

Expand Down
31 changes: 24 additions & 7 deletions Runtime/Interaction/HPUIContinuousInteractable.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,6 +30,8 @@ public class HPUIContinuousInteractable: HPUIBaseInteractable
public List<XRHandJointID> 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();
Expand All @@ -46,8 +47,8 @@ public HPUIContinuousSurfaceEvent ContinuousSurfaceEvent

public int x_divisions { get; private set; }
private List<Transform> keypointsCache;
private MeshFilter filter;
private DeformableSurfaceCollidersManager surfaceCollidersManager;
private GameObject collidersRoot;

/// <summary>
/// See <see cref="MonoBehaviour"/>.
Expand Down Expand Up @@ -96,11 +97,13 @@ private List<Transform> SetupKeypoints()
keypointTransforms.Add(keypoint);
}

GameObject surfaceRoot = keypointTransforms[0].gameObject;
filter = surfaceRoot.GetComponent<MeshFilter>();
if (filter == null)
{
filter = surfaceRoot.AddComponent<MeshFilter>();
GameObject skinNode = new GameObject("SkinNode");
skinNode.transform.parent = this.transform;
filter = skinNode.AddComponent<MeshFilter>();
skinNode.transform.localPosition = Vector3.zero;
skinNode.transform.localRotation = Quaternion.identity;
}

return keypointTransforms;
Expand All @@ -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();
Expand All @@ -136,6 +142,11 @@ private IEnumerator DelayedExecuteCalibration(float x_size, float y_size, List<T
Destroy(filter.mesh);
}

if (collidersRoot != null)
{
Destroy(collidersRoot);
}

float step_size = y_size / y_divisions;
x_divisions = (int)(x_size / step_size);

Expand All @@ -146,15 +157,21 @@ private IEnumerator DelayedExecuteCalibration(float x_size, float y_size, List<T
filter.GetComponent<Renderer>().material = defaultMaterial;
}

collidersRoot = new GameObject("CollidersRoot");
collidersRoot.transform.parent = this.transform;
collidersRoot.transform.localPosition = Vector3.zero;
collidersRoot.transform.localRotation = Quaternion.identity;

surfaceCollidersManager = filter.GetComponent<DeformableSurfaceCollidersManager>();
if (surfaceCollidersManager == null)
{
surfaceCollidersManager = filter.gameObject.AddComponent<DeformableSurfaceCollidersManager>();
}

List<Collider> generatedColliders = surfaceCollidersManager.SetupColliders();
List<Collider> generatedColliders = surfaceCollidersManager.SetupColliders(collidersRoot.transform);

colliders.AddRange(generatedColliders);

// Forcing regsitration of interactable to run
OnDisable();
OnEnable();
Expand Down

0 comments on commit 26a2b13

Please sign in to comment.