Tanoda
LeapUnityExtensions.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using System.Collections;
11using LeapInternal;
12
13namespace Leap.Unity {
14
18 public static class UnityVectorExtension {
19
27 public static Vector3 ToVector3(this Vector vector) {
28 return new Vector3(vector.x, vector.y, vector.z);
29 }
30
31 public static Vector3 ToVector3(this LEAP_VECTOR vector) {
32 return new Vector3(vector.x, vector.y, vector.z);
33 }
34
42 public static Vector4 ToVector4(this Vector vector) {
43 return new Vector4(vector.x, vector.y, vector.z, 0.0f);
44 }
45
53 public static Vector ToVector(this Vector3 vector) {
54 return new Vector(vector.x, vector.y, vector.z);
55 }
56
57 public static LEAP_VECTOR ToCVector(this Vector3 vector) {
58 LEAP_VECTOR cVector = new LEAP_VECTOR();
59 cVector.x = vector.x;
60 cVector.y = vector.y;
61 cVector.z = vector.z;
62 return cVector;
63 }
64 }
65
69 public static class UnityQuaternionExtension {
75 public static Quaternion ToQuaternion(this LeapQuaternion q) {
76 return new Quaternion(q.x, q.y, q.z, q.w);
77 }
78
79 public static Quaternion ToQuaternion(this LEAP_QUATERNION q) {
80 return new Quaternion(q.x, q.y, q.z, q.w);
81 }
82
88 public static LeapQuaternion ToLeapQuaternion(this Quaternion q) {
89 return new LeapQuaternion(q.x, q.y, q.z, q.w);
90 }
91
92 public static LEAP_QUATERNION ToCQuaternion(this Quaternion q) {
93 LEAP_QUATERNION cQuaternion = new LEAP_QUATERNION();
94 cQuaternion.x = q.x;
95 cQuaternion.y = q.y;
96 cQuaternion.z = q.z;
97 cQuaternion.w = q.w;
98 return cQuaternion;
99 }
100 }
101
105 public static class UnityMatrixExtension {
107 public static readonly Vector LEAP_UP = new Vector(0, 1, 0);
109 public static readonly Vector LEAP_FORWARD = new Vector(0, 0, -1);
111 public static readonly Vector LEAP_ORIGIN = new Vector(0, 0, 0);
113 public static readonly float MM_TO_M = 1e-3f;
114
123 public static Quaternion CalculateRotation(this LeapTransform trs) {
124 Vector3 up = trs.yBasis.ToVector3();
125 Vector3 forward = -trs.zBasis.ToVector3();
126 return Quaternion.LookRotation(forward, up);
127 }
128
140 public static LeapTransform GetLeapMatrix(this Transform t) {
141 Vector scale = new Vector(t.lossyScale.x * MM_TO_M, t.lossyScale.y * MM_TO_M, t.lossyScale.z * MM_TO_M);
142 LeapTransform transform = new LeapTransform(t.position.ToVector(), t.rotation.ToLeapQuaternion(), scale);
143 transform.MirrorZ(); // Unity is left handed.
144 return transform;
145 }
146
150 public static void Transform(this Transform thisTransform, Matrix4x4 transform) {
151 Matrix4x4 newTransform = transform * thisTransform.localToWorldMatrix;
152 thisTransform.position = newTransform.GetVector3();
153 thisTransform.rotation = newTransform.GetQuaternion();
154 thisTransform.localScale = Vector3.Scale(thisTransform.localScale,
155 transform.lossyScale);
156 }
157 }
158}
The LeapQuaternion struct represents a rotation in three-dimensional space.
The LeapTransform class represents a transform in three dimensional space.
Vector zBasis
The z-basis of the transform.
LeapQuaternion rotation
The rotational component of the transform.
void MirrorZ()
Mirrors this transform's rotation and scale across the z-axis. Translation is not affected.
Vector yBasis
The y-basis of the transform.
The Vector struct represents a three-component mathematical vector or point such as a direction or po...
Definition: Vector.cs:36
float y
Definition: Vector.cs:219
float x
Definition: Vector.cs:218
float z
Definition: Vector.cs:220