Tanoda
QuaternionCurves.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;
10
11namespace Leap.Unity.Recording {
12
14 [System.Serializable]
15 public struct QuaternionCurves {
16
17 [SerializeField]
18 private AnimationCurve _xCurve;
19 public AnimationCurve xCurve {
20 get { if (_xCurve == null) { _xCurve = new AnimationCurve(); } return _xCurve; }
21 set { _xCurve = value; }
22 }
23
24 [SerializeField]
25 private AnimationCurve _yCurve;
26 public AnimationCurve yCurve {
27 get { if (_yCurve == null) { _yCurve = new AnimationCurve(); } return _yCurve; }
28 set { _yCurve = value; }
29 }
30
31 [SerializeField]
32 private AnimationCurve _zCurve;
33 public AnimationCurve zCurve {
34 get { if (_zCurve == null) { _zCurve = new AnimationCurve(); } return _zCurve; }
35 set { _zCurve = value; }
36 }
37
38 [SerializeField]
39 private AnimationCurve _wCurve;
40 public AnimationCurve wCurve {
41 get { if (_wCurve == null) { _wCurve = new AnimationCurve(); } return _wCurve; }
42 set { _wCurve = value; }
43 }
44
45 public void AddKeyframes(float time, Quaternion value) {
46 // Normalize the quaternion.
47 value = Quaternion.Lerp(value, value, 1.0f);
48 // Make sure the quaternion always stays on the same hemisphere of the hypersphere.
49 if (value.w < 0f) {
50 value = new Quaternion(-value.x, -value.y, -value.z, -value.w);
51 }
52 // Normalize again!
53 value = Quaternion.Lerp(value, value, 1.0f);
54
55 xCurve.AddKey(time, value.x);
56 yCurve.AddKey(time, value.y);
57 zCurve.AddKey(time, value.z);
58 wCurve.AddKey(time, value.w);
59 }
60
61 public Quaternion Evaluate(float time) {
62 var x = xCurve.Evaluate(time);
63 var y = yCurve.Evaluate(time);
64 var z = zCurve.Evaluate(time);
65 var w = wCurve.Evaluate(time);
66
67 Quaternion evaluated = new Quaternion(x, y, z, w);
68 return evaluated.ToNormalized();
69 }
70
71#if UNITY_EDITOR
76 public void Compress(float maxAngleError = 1f) {
77 AnimationCurve outXCurve, outYCurve, outZCurve, outWCurve;
78 AnimationCurveUtil.CompressRotations(xCurve, yCurve, zCurve, wCurve,
79 out outXCurve, out outYCurve,
80 out outZCurve, out outWCurve,
81 maxAngleError: maxAngleError);
82 xCurve = outXCurve;
83 yCurve = outYCurve;
84 zCurve = outZCurve;
85 wCurve = outWCurve;
86 }
87#endif
88 }
89
90}
AnimationCurve data for an animated Quaternion.
void AddKeyframes(float time, Quaternion value)