12using System.Collections;
16 [Tooltip(
"The current Leap Data Provider for the scene.")]
18 [Tooltip(
"The diameter of the visual sphere cursor.")]
20 [Tooltip(
"The diameter of the collider that checks for pinchable objects.")]
22 [Tooltip(
"The amount of motion amplification for cursor movement.")]
24 [Tooltip(
"The amount that the cursor is lerped to its previous position per frame.")]
27 [Tooltip(
"The springiness of the spring joint.")]
29 [Tooltip(
"The dampening of the spring joint.")]
31 [Tooltip(
"The drag applied to the object while it is being dragged.")]
33 [Tooltip(
"The angular drag applied to the object while it is being dragged.")]
35 [Tooltip(
"The amount of dead-zone in the spring joint.")]
38 private Quaternion CurrentRotation;
40 #pragma warning disable 0649
42 private Mesh _sphereMesh;
44 private Material _sphereMaterial;
45 #pragma warning restore 0649
47 private GameObject[] Cursors;
48 private SpringJoint[,] SpringJoints;
49 private bool[] prevPinching;
56 Debug.LogError(
"Cannot use LeapImageRetriever if there is no LeapProvider!");
62 Cursors =
new GameObject[2];
64 for (
int i = 0; i < Cursors.Length; i++) {
65 Cursors[i] =
new GameObject(
"Cursor " + i);
66 Cursors[i].AddComponent<MeshFilter>().mesh = _sphereMesh;
67 Cursors[i].AddComponent<MeshRenderer>().sharedMaterial = _sphereMaterial;
68 Cursors[i].AddComponent<Rigidbody>().isKinematic =
true;
69 Cursors[i].transform.parent = transform;
73 prevPinching =
new bool[2];
74 SpringJoints =
new SpringJoint[2, 10];
83 Quaternion HeadYaw = Quaternion.Euler(0f, XRSupportUtil.GetXRNodeHeadLocalRotation().eulerAngles.y, 0f);
84 CurrentRotation = Quaternion.Slerp(CurrentRotation, HeadYaw, 0.1f);
86 for (
int whichHand = 0; whichHand < 2; whichHand++) {
87 if (whichHand > curFrame.
Hands.Count - 1) {
88 if (Cursors[whichHand].activeInHierarchy) {
89 Cursors[whichHand].SetActive(
false);
93 if (!Cursors[whichHand].activeInHierarchy) {
94 Cursors[whichHand].SetActive(
true);
99 switch (curFrame.
Hands[whichHand].IsRight) {
101 ProjectionOrigin = XRSupportUtil.GetXRNodeHeadLocalPosition() + CurrentRotation *
new Vector3(0.15f, -0.13f, 0.1f);
104 ProjectionOrigin = XRSupportUtil.GetXRNodeHeadLocalPosition() + CurrentRotation *
new Vector3(-0.15f, -0.13f, 0.1f);
108 Vector3 Offset = curFrame.
Hands[whichHand].Fingers[1].Bone(
Bone.
BoneType.TYPE_METACARPAL).Center.ToVector3() - ProjectionOrigin;
112 if (curFrame.
Hands[whichHand].PinchDistance < 30f) {
113 if (!prevPinching[whichHand]) {
114 prevPinching[whichHand] =
true;
115 Cursors[whichHand].GetComponent<MeshRenderer>().material.color =
Color.green;
119 for (
int i = 0; i < Mathf.Min(10, Colliders.Length); i++) {
121 if (!Colliders[i].attachedRigidbody || Colliders[i].attachedRigidbody.isKinematic) {
125 if (!SpringJoints[whichHand, i]) {
126 SpringJoints[whichHand, i] = Cursors[whichHand].AddComponent<SpringJoint>();
127 Cursors[whichHand].GetComponent<Rigidbody>().isKinematic =
true;
130 SpringJoints[whichHand, i].transform.position = Cursors[whichHand].transform.position;
131 SpringJoints[whichHand, i].anchor =
Vector3.zero;
133 SpringJoints[whichHand, i].spring =
Spring;
134 SpringJoints[whichHand, i].damper =
Damper;
135 SpringJoints[whichHand, i].maxDistance =
Distance;
136 SpringJoints[whichHand, i].connectedBody = Colliders[i].attachedRigidbody;
138 StartCoroutine(DragObject(whichHand, i));
141 }
else if (curFrame.
Hands[whichHand].PinchDistance > 40f) {
142 if (prevPinching[whichHand]) {
143 prevPinching[whichHand] =
false;
144 Cursors[whichHand].GetComponent<MeshRenderer>().material.color =
Color.white;
152 private IEnumerator DragObject(
int whichHand,
int i) {
153 float oldDrag = SpringJoints[whichHand, i].connectedBody.drag;
154 float oldAngularDrag = SpringJoints[whichHand, i].connectedBody.angularDrag;
155 SpringJoints[whichHand, i].connectedBody.drag =
Drag;
156 SpringJoints[whichHand, i].connectedBody.angularDrag =
AngularDrag;
158 while (prevPinching[whichHand]) {
159 SpringJoints[whichHand, i].transform.position = Cursors[whichHand].transform.position;
163 if (SpringJoints[whichHand, i].connectedBody) {
164 SpringJoints[whichHand, i].connectedBody.drag = oldDrag;
165 SpringJoints[whichHand, i].connectedBody.angularDrag = oldAngularDrag;
166 SpringJoints[whichHand, i].connectedBody =
null;
167 Destroy(SpringJoints[whichHand, i]);
The Bone class represents a tracked bone.
BoneType
Enumerates the type of bones.
The Frame class represents a set of hand and finger tracking data detected in a single frame.
List< Hand > Hands
The list of Hand objects detected in this frame, given in arbitrary order. The list can be empty if n...
Provides Frame object data to the Unity application by firing events as soon as Frame data is availab...
abstract Frame CurrentFrame
The current frame for this update cycle, in world space.