BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Avatar / Scripts / OvrAvatarPacket.cs

OvrAvatarPacket.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OvrAvatarPacket.cs 7.1 KiB Download Raw
1using UnityEngine;
2using System.Collections;
3using System.IO;
4using System.Collections.Generic;
5using System;
6
7public class OvrAvatarPacket
8{
9 // Used with SDK driven packet flow
10 public IntPtr ovrNativePacket = IntPtr.Zero;
11
12 // ===============================================================
13 // All code below used for unity only pose blending option.
14 // ===============================================================
15 List<float> frameTimes = new List<float>();
16 List<OvrAvatarDriver.PoseFrame> frames = new List<OvrAvatarDriver.PoseFrame>();
17 List<byte[]> encodedAudioPackets = new List<byte[]>();
18
19 public float Duration { get { return frameTimes[frameTimes.Count - 1]; } }
20 public OvrAvatarDriver.PoseFrame FinalFrame { get { return frames[frames.Count - 1]; } }
21
22 public OvrAvatarPacket()
23 {
24 }
25
26 public OvrAvatarPacket(OvrAvatarDriver.PoseFrame initialPose)
27 {
28 frameTimes.Add(0.0f);
29 frames.Add(initialPose);
30 }
31
32 OvrAvatarPacket(List<float> frameTimes, List<OvrAvatarDriver.PoseFrame> frames, List<byte[]> audioPackets)
33 {
34 this.frameTimes = frameTimes;
35 this.frames = frames;
36 }
37
38 public void AddFrame(OvrAvatarDriver.PoseFrame frame, float deltaSeconds)
39 {
40 frameTimes.Add(Duration + deltaSeconds);
41 frames.Add(frame);
42 }
43
44 public OvrAvatarDriver.PoseFrame GetPoseFrame(float seconds)
45 {
46 if (frames.Count == 1)
47 {
48 return frames[0];
49 }
50
51 // This can be replaced with a more efficient binary search
52 int tailIndex = 1;
53 while (tailIndex < frameTimes.Count && frameTimes[tailIndex] < seconds)
54 {
55 ++tailIndex;
56 }
57 OvrAvatarDriver.PoseFrame a = frames[tailIndex - 1];
58 OvrAvatarDriver.PoseFrame b = frames[tailIndex];
59 float aTime = frameTimes[tailIndex - 1];
60 float bTime = frameTimes[tailIndex];
61 float t = (seconds - aTime) / (bTime - aTime);
62 return OvrAvatarDriver.PoseFrame.Interpolate(a, b, t);
63 }
64
65 public static OvrAvatarPacket Read(Stream stream)
66 {
67 BinaryReader reader = new BinaryReader(stream);
68
69 // Todo: bounds check frame count
70 int frameCount = reader.ReadInt32();
71 List<float> frameTimes = new List<float>(frameCount);
72 for (int i = 0; i < frameCount; ++i)
73 {
74 frameTimes.Add(reader.ReadSingle());
75 }
76 List<OvrAvatarDriver.PoseFrame> frames = new List<OvrAvatarDriver.PoseFrame>(frameCount);
77 for (int i = 0; i < frameCount; ++i)
78 {
79 frames.Add(reader.ReadPoseFrame());
80 }
81
82 // Todo: bounds check audio packet count
83 int audioPacketCount = reader.ReadInt32();
84 List<byte[]> audioPackets = new List<byte[]>(audioPacketCount);
85 for (int i = 0; i < audioPacketCount; ++i)
86 {
87 int audioPacketSize = reader.ReadInt32();
88 byte[] audioPacket = reader.ReadBytes(audioPacketSize);
89 audioPackets.Add(audioPacket);
90 }
91
92 return new OvrAvatarPacket(frameTimes, frames, audioPackets);
93 }
94
95 public void Write(Stream stream)
96 {
97 BinaryWriter writer = new BinaryWriter(stream);
98
99 // Write all of the frames
100 int frameCount = frameTimes.Count;
101 writer.Write(frameCount);
102 for (int i = 0; i < frameCount; ++i)
103 {
104 writer.Write(frameTimes[i]);
105 }
106 for (int i = 0; i < frameCount; ++i)
107 {
108 OvrAvatarDriver.PoseFrame frame = frames[i];
109 writer.Write(frame);
110 }
111
112 // Write all of the encoded audio packets
113 int audioPacketCount = encodedAudioPackets.Count;
114 writer.Write(audioPacketCount);
115 for (int i = 0; i < audioPacketCount; ++i)
116 {
117 byte[] packet = encodedAudioPackets[i];
118 writer.Write(packet.Length);
119 writer.Write(packet);
120 }
121 }
122}
123
124static class BinaryWriterExtensions
125{
126 public static void Write(this BinaryWriter writer, OvrAvatarDriver.PoseFrame frame)
127 {
128 writer.Write(frame.headPosition);
129 writer.Write(frame.headRotation);
130 writer.Write(frame.handLeftPosition);
131 writer.Write(frame.handLeftRotation);
132 writer.Write(frame.handRightPosition);
133 writer.Write(frame.handRightRotation);
134 writer.Write(frame.voiceAmplitude);
135
136 writer.Write(frame.controllerLeftPose);
137 writer.Write(frame.controllerRightPose);
138 }
139
140 public static void Write(this BinaryWriter writer, Vector3 vec3)
141 {
142 writer.Write(vec3.x);
143 writer.Write(vec3.y);
144 writer.Write(vec3.z);
145 }
146
147 public static void Write(this BinaryWriter writer, Vector2 vec2)
148 {
149 writer.Write(vec2.x);
150 writer.Write(vec2.y);
151 }
152
153 public static void Write(this BinaryWriter writer, Quaternion quat)
154 {
155 writer.Write(quat.x);
156 writer.Write(quat.y);
157 writer.Write(quat.z);
158 writer.Write(quat.w);
159 }
160 public static void Write(this BinaryWriter writer, OvrAvatarDriver.ControllerPose pose)
161 {
162 writer.Write((uint)pose.buttons);
163 writer.Write((uint)pose.touches);
164 writer.Write(pose.joystickPosition);
165 writer.Write(pose.indexTrigger);
166 writer.Write(pose.handTrigger);
167 writer.Write(pose.isActive);
168 }
169}
170
171static class BinaryReaderExtensions
172{
173 public static OvrAvatarDriver.PoseFrame ReadPoseFrame(this BinaryReader reader)
174 {
175 return new OvrAvatarDriver.PoseFrame
176 {
177 headPosition = reader.ReadVector3(),
178 headRotation = reader.ReadQuaternion(),
179 handLeftPosition = reader.ReadVector3(),
180 handLeftRotation = reader.ReadQuaternion(),
181 handRightPosition = reader.ReadVector3(),
182 handRightRotation = reader.ReadQuaternion(),
183 voiceAmplitude = reader.ReadSingle(),
184
185 controllerLeftPose = reader.ReadControllerPose(),
186 controllerRightPose = reader.ReadControllerPose(),
187 };
188 }
189
190 public static Vector2 ReadVector2(this BinaryReader reader)
191 {
192 return new Vector2
193 {
194 x = reader.ReadSingle(),
195 y = reader.ReadSingle()
196 };
197 }
198
199 public static Vector3 ReadVector3(this BinaryReader reader)
200 {
201 return new Vector3
202 {
203 x = reader.ReadSingle(),
204 y = reader.ReadSingle(),
205 z = reader.ReadSingle()
206 };
207 }
208
209 public static Quaternion ReadQuaternion(this BinaryReader reader)
210 {
211 return new Quaternion
212 {
213 x = reader.ReadSingle(),
214 y = reader.ReadSingle(),
215 z = reader.ReadSingle(),
216 w = reader.ReadSingle(),
217 };
218 }
219 public static OvrAvatarDriver.ControllerPose ReadControllerPose(this BinaryReader reader)
220 {
221 return new OvrAvatarDriver.ControllerPose
222 {
223 buttons = (ovrAvatarButton)reader.ReadUInt32(),
224 touches = (ovrAvatarTouch)reader.ReadUInt32(),
225 joystickPosition = reader.ReadVector2(),
226 indexTrigger = reader.ReadSingle(),
227 handTrigger = reader.ReadSingle(),
228 isActive = reader.ReadBoolean(),
229 };
230 }
231}