Skip to content

Commit

Permalink
Pos on interactable returns unity coords instead of normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-shariff committed Jan 11, 2024
1 parent f448a65 commit 8569c22
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
22 changes: 11 additions & 11 deletions Runtime/Interaction/DeformableSurfaceCollidersManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class DeformableSurfaceCollidersManager: MonoBehaviour

private Vector3 scaleFactor;
private int maxY, maxX;
private Vector2 surfaceBounds;
private float gridSize;
private float gridSize, offset_x, offset_y;
private TransformAccessArray colliderObjects;

private Dictionary<Collider, Vector2> colliderCoords;
Expand Down Expand Up @@ -91,18 +90,16 @@ public List<Collider> SetupColliders(Transform collidersRootTransform)
maxY = vertices_native.Length - continuousInteractable.x_divisions;
maxX = 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));
List<Collider> colliders = GenerateColliders(vertices, normals, collidersRootTransform, continuousInteractable.x_divisions, continuousInteractable.y_divisions);

generatedColliders = true;
return colliders;
}

/// <summary>
/// Return the (appoximate) point on the surface of where the collider is.
/// The returned Vector2 - (x, z) on the xz-plane.
/// (0, 0) would be the bounds min on the surface & (1, 1) the bounds max on the surface.
/// The returned Vector2 - (x, z) on the xz-plane. This is relative to the
/// center of the surface.
/// </summary>
public Vector2 GetSurfacePointForCollider(Collider collider)
{
Expand All @@ -111,14 +108,14 @@ public Vector2 GetSurfacePointForCollider(Collider collider)
throw new ArgumentException("Unknown {collider.name}");
}

return colliderCoords[collider]/surfaceBounds;
return colliderCoords[collider];
}

/// <summary>
/// Generate the colliders for a given set of vertices. The vertices are expected to be the order along x then along y.
/// The generated colliders will be parented to the rootTransform.
/// </summary>
private List<Collider> GenerateColliders(List<Vector3> positions, List<Vector3> _normals, Transform rootTransform, int x_divisions)
private List<Collider> GenerateColliders(List<Vector3> positions, List<Vector3> _normals, Transform rootTransform, int x_divisions, int y_divisions)
{
var right = positions[1] - positions[0];
GameObject colliderObj;
Expand Down Expand Up @@ -149,14 +146,17 @@ private List<Collider> GenerateColliders(List<Vector3> positions, List<Vector3>
scaleFactor.z = (gridSize / buttonSize.y) * 1.05f * rootTransform.lossyScale.y;
scaleFactor.y = 0.00001f;
gridSize = (positions[0] - positions[1]).magnitude;
}

offset_x = gridSize * x_divisions * 0.5f;
offset_y = gridSize * y_divisions * 0.5f;
}
colliderObj.transform.parent = rootTransform;
colliderObj.transform.localPosition = positions[i];
colliderObj.transform.localRotation = Quaternion.identity;
colliderObj.transform.localScale = scaleFactor;
colliderTransforms[i] = colliderObj.transform;

colliderCoords.Add(collider, new Vector2(gridSize * x, gridSize * y));
colliderCoords.Add(collider, new Vector2(gridSize * x - offset_x, gridSize * y - offset_y));
}

colliderObjects = new TransformAccessArray(colliderTransforms);
Expand Down
18 changes: 5 additions & 13 deletions Runtime/Interaction/HPUIBaseInteractable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public Handedness Handedness
/// </summary>
public HPUIGestureEvent GestureEvent { get => gestureEvent; set => gestureEvent = value; }

private Vector2 surfaceBounds, surfaceOrigin;

#region overrides
/// <inheritdoc />
protected override void Awake()
Expand Down Expand Up @@ -80,15 +78,6 @@ protected virtual void ComputeSurfaceBounds()

Bounds colliderBounds = boundsCollider.bounds;
Transform interactableTransform = GetAttachTransform(null);
surfaceOrigin = ComputeTargetPointOnInteractablePlane(colliderBounds.center
- interactableTransform.right.normalized * colliderBounds.extents.x
- interactableTransform.forward.normalized * colliderBounds.extents.z,
interactableTransform);

surfaceBounds = ComputeTargetPointOnInteractablePlane(colliderBounds.center
+ interactableTransform.right.normalized * colliderBounds.extents.x
+ interactableTransform.forward.normalized * colliderBounds.extents.z,
interactableTransform) - surfaceOrigin;
}

protected DistanceInfo GetDistanceOverride(IXRInteractable interactable, Vector3 position)
Expand All @@ -107,7 +96,10 @@ protected Vector2 ComputeTargetPointOnInteractablePlane(Vector3 targetPoint, Tra
Plane xzPlane = new Plane(interactableTransform.up, interactableTransform.position);

Vector3 pointOnXZPlane = xzPlane.ClosestPointOnPlane(targetPoint);
pointOnXZPlane = transform.InverseTransformPoint(pointOnXZPlane);

// InverseTransformPoint without taking scale.
Matrix4x4 worldToLocalMatrix = Matrix4x4.TRS(interactableTransform.position, interactableTransform.rotation, Vector3.one).inverse;
pointOnXZPlane = worldToLocalMatrix.MultiplyPoint3x4(pointOnXZPlane);
return new Vector2(pointOnXZPlane.x, pointOnXZPlane.z);
}

Expand All @@ -117,7 +109,7 @@ public virtual Vector2 ComputeInteractorPostion(IXRInteractor interactor)
{
Vector3 closestPointOnCollider = GetDistanceOverride(this, interactor.GetAttachTransform(this).position).point;
Vector2 pointOnPlane = ComputeTargetPointOnInteractablePlane(closestPointOnCollider, GetAttachTransform(interactor));
return (pointOnPlane - surfaceOrigin) / surfaceBounds;
return pointOnPlane;
}

/// <inheritdoc />
Expand Down
3 changes: 1 addition & 2 deletions Runtime/Interaction/IHPUIInteractable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public interface IHPUIInteractable : IXRInteractable, IXRSelectInteractable

/// <summary>
/// Get the projection of the interactors position on the xz plane of this interactable, normalized.
/// the returned Vector2 - (x, z) on the xz-plane.
/// (0, 0) would be the bounds min on the surface & (1, 1) the bounds max on the surface.
/// the returned Vector2 - (x, z) on the xz-plane, relative to the center of the interactable.
/// </summary>
Vector2 ComputeInteractorPostion(IXRInteractor interactor);

Expand Down

0 comments on commit 8569c22

Please sign in to comment.