-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVRPlayer.cs
120 lines (107 loc) · 2.89 KB
/
VRPlayer.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
using System.Collections.Generic;
using Newtonsoft.Json;
#if UNITY
using UnityEngine;
#else
using System.Numerics;
#endif
namespace EchoVRAPI
{
public class VRPlayer
{
public List<float> vr_left { get; set; }
public List<float> vr_position { get; set; }
public List<float> vr_forward { get; set; }
public List<float> vr_up { get; set; }
[JsonIgnore]
public Vector3 Position
{
get => vr_position?.ToVector3() ?? UniversalUnityExtensions.UniversalVector3Zero();
set => vr_position = value.ToFloatList();
}
[JsonIgnore] internal Quaternion? rot;
[JsonIgnore]
public Quaternion Rotation
{
get
{
if (rot != null) return (Quaternion)rot;
rot = Math2.QuaternionLookRotation(vr_forward.ToVector3(), vr_up.ToVector3());
return (Quaternion)rot;
}
set
{
rot = value;
vr_forward = value.Forward().ToFloatList();
vr_up = value.Up().ToFloatList();
vr_left = value.Left().ToFloatList();
}
}
public static VRPlayer operator +(VRPlayer t1, VRPlayer t2)
{
if (t2 == null) return t1;
VRPlayer ret = new VRPlayer
{
Position = t1.Position + t2.Position,
// ret.Rotation = Quaternion.Multiply(t1.Rotation, t2.Rotation);
Rotation = t1.Rotation
};
return ret;
}
public static VRPlayer operator -(VRPlayer t1, VRPlayer t2)
{
if (t2 == null) return t1;
VRPlayer ret = new VRPlayer
{
Position = t1.Position - t2.Position,
// ret.Rotation = Quaternion.Multiply(t1.Rotation, Quaternion.Inverse(t2.Rotation));
Rotation = t1.Rotation
};
return ret;
}
public Transform ToTransform()
{
return new Transform
{
Position = Position,
Rotation = Rotation
};
}
/// <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 VRPlayer Lerp(VRPlayer from, VRPlayer to, float t)
{
t = Math2.Clamp01(t);
VRPlayer newTransform = new VRPlayer()
{
vr_position = from.vr_position,
vr_left = from.vr_left,
vr_forward = from.vr_forward,
vr_up = from.vr_up,
};
newTransform.Position = Vector3.Lerp(from.Position, to.Position, t);
newTransform.Rotation = Quaternion.Lerp(from.Rotation, to.Rotation, t);
return newTransform;
}
/// <summary>
/// Creates a completely empty player, but initializes arrays and stuff to avoid null checking
/// </summary>
/// <returns>A VRPlayer object</returns>
public static VRPlayer CreateEmpty()
{
return new VRPlayer
{
vr_position = new List<float> { 0, 0, 0 },
vr_left = new List<float> { 0, 0, 0 },
vr_forward = new List<float> { 0, 0, 0 },
vr_up = new List<float> { 0, 0, 0 },
};
}
}
}