-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform.cs
130 lines (111 loc) · 2.97 KB
/
Transform.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
#if UNITY
using UnityEngine;
#else
using System.Numerics;
#endif
namespace EchoVRAPI
{
/// <summary>
/// Object for position and rotation
/// </summary>
public class Transform
{
/// <summary>
/// Don't get this value. Use Position property instead
/// </summary>
public List<float> pos { get; set; }
/// <summary>
/// Don't get this value. Use Position property instead
/// </summary>
public List<float> position { get; set; }
public List<float> forward { get; set; }
public List<float> left { get; set; }
public List<float> up { get; set; }
[JsonIgnore]
public Vector3 Position
{
get
{
if (pos != null) return pos.ToVector3();
if (position != null) return position.ToVector3();
throw new NullReferenceException("Neither pos nor position are set");
}
set
{
// only set the one with prior data
if (pos != null) pos = value.ToFloatList();
if (position != null) position = value.ToFloatList();
else position = value.ToFloatList();
}
}
[JsonIgnore]
internal Quaternion? rot;
[JsonIgnore]
public Quaternion Rotation
{
get
{
if (rot != null) return (Quaternion) rot;
rot = Math2.QuaternionLookRotation(forward.ToVector3(), up.ToVector3());
return (Quaternion) rot;
}
set
{
rot = value;
forward = value.Forward().ToFloatList();
up = value.Up().ToFloatList();
left = value.Left().ToFloatList();
}
}
public static Transform operator +(Transform t1, Transform t2)
{
if (t2 == null) return t1;
if (t1 == null) return t2;
Transform ret = new Transform
{
Position = t1.Position + t2.Position,
// ret.Rotation = Quaternion.Multiply(t1.Rotation, t2.Rotation);
Rotation = t1.Rotation
};
return ret;
}
public static Transform operator -(Transform t1, Transform t2)
{
if (t2 == null) return t1;
if (t1 == null) return t2;
Transform ret = new Transform
{
Position = t1.Position - t2.Position,
// ret.Rotation = Quaternion.Multiply(t1.Rotation, Quaternion.Inverse(t2.Rotation));
Rotation = t1.Rotation
};
return ret;
}
/// <summary>
/// ↔ Mixes the two states with a linear interpolation based on t
/// For binary or int values, the "from" state is preferred.
/// </summary>
/// <param name="from">The start state</param>
/// <param name="to">The next state</param>
/// <param name="t">Weighting of the two states</param>
/// <returns>A mix of the two states</returns>
internal static Transform Lerp(Transform from, Transform to, float t)
{
t = Math2.Clamp01(t);
Transform newTransform = new Transform()
{
pos = from.pos,
position = from.position,
left = from.left,
forward = from.forward,
up = from.up,
};
newTransform.Position = Vector3.Lerp(from.Position, to.Position, t);
newTransform.Rotation = Quaternion.Slerp(from.Rotation, to.Rotation, t);
return newTransform;
}
}
}