Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
7.1 KiB

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