| 1 | using UnityEngine; |
| 2 | using System.Collections; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using Oculus.Avatar; |
| 6 | |
| 7 | public class OvrAvatarRemoteDriver : OvrAvatarDriver |
| 8 | { |
| 9 | Queue<OvrAvatarPacket> packetQueue = new Queue<OvrAvatarPacket>(); |
| 10 | |
| 11 | IntPtr CurrentSDKPacket = IntPtr.Zero; |
| 12 | float CurrentPacketTime = 0f; |
| 13 | |
| 14 | const int MinPacketQueue = 1; |
| 15 | const int MaxPacketQueue = 4; |
| 16 | |
| 17 | int CurrentSequence = -1; |
| 18 | |
| 19 | // Used for legacy Unity only packet blending |
| 20 | bool isStreaming = false; |
| 21 | OvrAvatarPacket currentPacket = null; |
| 22 | |
| 23 | public void QueuePacket(int sequence, OvrAvatarPacket packet) |
| 24 | { |
| 25 | if (sequence > CurrentSequence) |
| 26 | { |
| 27 | CurrentSequence = sequence; |
| 28 | packetQueue.Enqueue(packet); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public override void UpdateTransforms(IntPtr sdkAvatar) |
| 33 | { |
| 34 | switch(Mode) |
| 35 | { |
| 36 | case PacketMode.SDK: |
| 37 | UpdateFromSDKPacket(sdkAvatar); |
| 38 | break; |
| 39 | case PacketMode.Unity: |
| 40 | UpdateFromUnityPacket(sdkAvatar); |
| 41 | break; |
| 42 | default: |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private void UpdateFromSDKPacket(IntPtr sdkAvatar) |
| 48 | { |
| 49 | |
| 50 | if (CurrentSDKPacket == IntPtr.Zero && packetQueue.Count >= MinPacketQueue) |
| 51 | { |
| 52 | CurrentSDKPacket = packetQueue.Dequeue().ovrNativePacket; |
| 53 | } |
| 54 | |
| 55 | if (CurrentSDKPacket != IntPtr.Zero) |
| 56 | { |
| 57 | float PacketDuration = CAPI.ovrAvatarPacket_GetDurationSeconds(CurrentSDKPacket); |
| 58 | CAPI.ovrAvatar_UpdatePoseFromPacket(sdkAvatar, CurrentSDKPacket, Mathf.Min(PacketDuration, CurrentPacketTime)); |
| 59 | CurrentPacketTime += Time.deltaTime; |
| 60 | |
| 61 | if (CurrentPacketTime > PacketDuration) |
| 62 | { |
| 63 | CAPI.ovrAvatarPacket_Free(CurrentSDKPacket); |
| 64 | CurrentSDKPacket = IntPtr.Zero; |
| 65 | CurrentPacketTime = CurrentPacketTime - PacketDuration; |
| 66 | |
| 67 | //Throw away packets deemed too old. |
| 68 | while (packetQueue.Count > MaxPacketQueue) |
| 69 | { |
| 70 | packetQueue.Dequeue(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private void UpdateFromUnityPacket(IntPtr sdkAvatar) |
| 77 | { |
| 78 | // If we're not currently streaming, check to see if we've buffered enough |
| 79 | if (!isStreaming && packetQueue.Count > MinPacketQueue) |
| 80 | { |
| 81 | currentPacket = packetQueue.Dequeue(); |
| 82 | isStreaming = true; |
| 83 | } |
| 84 | |
| 85 | // If we are streaming, update our pose |
| 86 | if (isStreaming) |
| 87 | { |
| 88 | CurrentPacketTime += Time.deltaTime; |
| 89 | |
| 90 | // If we've elapsed past our current packet, advance |
| 91 | while (CurrentPacketTime > currentPacket.Duration) |
| 92 | { |
| 93 | |
| 94 | // If we're out of packets, stop streaming and |
| 95 | // lock to the final frame |
| 96 | if (packetQueue.Count == 0) |
| 97 | { |
| 98 | CurrentPose = currentPacket.FinalFrame; |
| 99 | CurrentPacketTime = 0.0f; |
| 100 | currentPacket = null; |
| 101 | isStreaming = false; |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | while (packetQueue.Count > MaxPacketQueue) |
| 106 | { |
| 107 | packetQueue.Dequeue(); |
| 108 | } |
| 109 | |
| 110 | // Otherwise, dequeue the next packet |
| 111 | CurrentPacketTime -= currentPacket.Duration; |
| 112 | currentPacket = packetQueue.Dequeue(); |
| 113 | } |
| 114 | |
| 115 | // Compute the pose based on our current time offset in the packet |
| 116 | CurrentPose = currentPacket.GetPoseFrame(CurrentPacketTime); |
| 117 | |
| 118 | UpdateTransformsFromPose(sdkAvatar); |
| 119 | } |
| 120 | } |
| 121 | } |