OvrAvatarPacket.cs
7.12 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;
public class OvrAvatarPacket
{
// Used with SDK driven packet flow
public IntPtr ovrNativePacket = IntPtr.Zero;
// ===============================================================
// All code below used for unity only pose blending option.
// ===============================================================
List<float> frameTimes = new List<float>();
List<OvrAvatarDriver.PoseFrame> frames = new List<OvrAvatarDriver.PoseFrame>();
List<byte[]> encodedAudioPackets = new List<byte[]>();
public float Duration { get { return frameTimes[frameTimes.Count - 1]; } }
public OvrAvatarDriver.PoseFrame FinalFrame { get { return frames[frames.Count - 1]; } }
public OvrAvatarPacket()
{
}
public OvrAvatarPacket(OvrAvatarDriver.PoseFrame initialPose)
{
frameTimes.Add(0.0f);
frames.Add(initialPose);
}
OvrAvatarPacket(List<float> frameTimes, List<OvrAvatarDriver.PoseFrame> frames, List<byte[]> audioPackets)
{
this.frameTimes = frameTimes;
this.frames = frames;
}
public void AddFrame(OvrAvatarDriver.PoseFrame frame, float deltaSeconds)
{
frameTimes.Add(Duration + deltaSeconds);
frames.Add(frame);
}
public OvrAvatarDriver.PoseFrame GetPoseFrame(float seconds)
{
if (frames.Count == 1)
{
return frames[0];
}
// This can be replaced with a more efficient binary search
int tailIndex = 1;
while (tailIndex < frameTimes.Count && frameTimes[tailIndex] < seconds)
{
++tailIndex;
}
OvrAvatarDriver.PoseFrame a = frames[tailIndex - 1];
OvrAvatarDriver.PoseFrame b = frames[tailIndex];
float aTime = frameTimes[tailIndex - 1];
float bTime = frameTimes[tailIndex];
float t = (seconds - aTime) / (bTime - aTime);
return OvrAvatarDriver.PoseFrame.Interpolate(a, b, t);
}
public static OvrAvatarPacket Read(Stream stream)
{
BinaryReader reader = new BinaryReader(stream);
// Todo: bounds check frame count
int frameCount = reader.ReadInt32();
List<float> frameTimes = new List<float>(frameCount);
for (int i = 0; i < frameCount; ++i)
{
frameTimes.Add(reader.ReadSingle());
}
List<OvrAvatarDriver.PoseFrame> frames = new List<OvrAvatarDriver.PoseFrame>(frameCount);
for (int i = 0; i < frameCount; ++i)
{
frames.Add(reader.ReadPoseFrame());
}
// Todo: bounds check audio packet count
int audioPacketCount = reader.ReadInt32();
List<byte[]> audioPackets = new List<byte[]>(audioPacketCount);
for (int i = 0; i < audioPacketCount; ++i)
{
int audioPacketSize = reader.ReadInt32();
byte[] audioPacket = reader.ReadBytes(audioPacketSize);
audioPackets.Add(audioPacket);
}
return new OvrAvatarPacket(frameTimes, frames, audioPackets);
}
public void Write(Stream stream)
{
BinaryWriter writer = new BinaryWriter(stream);
// Write all of the frames
int frameCount = frameTimes.Count;
writer.Write(frameCount);
for (int i = 0; i < frameCount; ++i)
{
writer.Write(frameTimes[i]);
}
for (int i = 0; i < frameCount; ++i)
{
OvrAvatarDriver.PoseFrame frame = frames[i];
writer.Write(frame);
}
// Write all of the encoded audio packets
int audioPacketCount = encodedAudioPackets.Count;
writer.Write(audioPacketCount);
for (int i = 0; i < audioPacketCount; ++i)
{
byte[] packet = encodedAudioPackets[i];
writer.Write(packet.Length);
writer.Write(packet);
}
}
}
static class BinaryWriterExtensions
{
public static void Write(this BinaryWriter writer, OvrAvatarDriver.PoseFrame frame)
{
writer.Write(frame.headPosition);
writer.Write(frame.headRotation);
writer.Write(frame.handLeftPosition);
writer.Write(frame.handLeftRotation);
writer.Write(frame.handRightPosition);
writer.Write(frame.handRightRotation);
writer.Write(frame.voiceAmplitude);
writer.Write(frame.controllerLeftPose);
writer.Write(frame.controllerRightPose);
}
public static void Write(this BinaryWriter writer, Vector3 vec3)
{
writer.Write(vec3.x);
writer.Write(vec3.y);
writer.Write(vec3.z);
}
public static void Write(this BinaryWriter writer, Vector2 vec2)
{
writer.Write(vec2.x);
writer.Write(vec2.y);
}
public static void Write(this BinaryWriter writer, Quaternion quat)
{
writer.Write(quat.x);
writer.Write(quat.y);
writer.Write(quat.z);
writer.Write(quat.w);
}
public static void Write(this BinaryWriter writer, OvrAvatarDriver.ControllerPose pose)
{
writer.Write((uint)pose.buttons);
writer.Write((uint)pose.touches);
writer.Write(pose.joystickPosition);
writer.Write(pose.indexTrigger);
writer.Write(pose.handTrigger);
writer.Write(pose.isActive);
}
}
static class BinaryReaderExtensions
{
public static OvrAvatarDriver.PoseFrame ReadPoseFrame(this BinaryReader reader)
{
return new OvrAvatarDriver.PoseFrame
{
headPosition = reader.ReadVector3(),
headRotation = reader.ReadQuaternion(),
handLeftPosition = reader.ReadVector3(),
handLeftRotation = reader.ReadQuaternion(),
handRightPosition = reader.ReadVector3(),
handRightRotation = reader.ReadQuaternion(),
voiceAmplitude = reader.ReadSingle(),
controllerLeftPose = reader.ReadControllerPose(),
controllerRightPose = reader.ReadControllerPose(),
};
}
public static Vector2 ReadVector2(this BinaryReader reader)
{
return new Vector2
{
x = reader.ReadSingle(),
y = reader.ReadSingle()
};
}
public static Vector3 ReadVector3(this BinaryReader reader)
{
return new Vector3
{
x = reader.ReadSingle(),
y = reader.ReadSingle(),
z = reader.ReadSingle()
};
}
public static Quaternion ReadQuaternion(this BinaryReader reader)
{
return new Quaternion
{
x = reader.ReadSingle(),
y = reader.ReadSingle(),
z = reader.ReadSingle(),
w = reader.ReadSingle(),
};
}
public static OvrAvatarDriver.ControllerPose ReadControllerPose(this BinaryReader reader)
{
return new OvrAvatarDriver.ControllerPose
{
buttons = (ovrAvatarButton)reader.ReadUInt32(),
touches = (ovrAvatarTouch)reader.ReadUInt32(),
joystickPosition = reader.ReadVector2(),
indexTrigger = reader.ReadSingle(),
handTrigger = reader.ReadSingle(),
isActive = reader.ReadBoolean(),
};
}
}