BlueGit

blue-star ggj-2020

Files

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

OvrAvatarDriver.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OvrAvatarDriver.cs 4.9 KiB Download Raw
1using UnityEngine;
2using System.Collections;
3using System;
4using Oculus.Avatar;
5
6public abstract class OvrAvatarDriver : MonoBehaviour {
7
8 public enum PacketMode
9 {
10 SDK,
11 Unity
12 };
13
14 public PacketMode Mode;
15 protected PoseFrame CurrentPose;
16 public PoseFrame GetCurrentPose() { return CurrentPose; }
17 public abstract void UpdateTransforms(IntPtr sdkAvatar);
18
19 private ovrAvatarControllerType ControllerType = ovrAvatarControllerType.Quest;
20 public struct ControllerPose
21 {
22 public ovrAvatarButton buttons;
23 public ovrAvatarTouch touches;
24 public Vector2 joystickPosition;
25 public float indexTrigger;
26 public float handTrigger;
27 public bool isActive;
28
29 public static ControllerPose Interpolate(ControllerPose a, ControllerPose b, float t)
30 {
31 return new ControllerPose
32 {
33 buttons = t < 0.5f ? a.buttons : b.buttons,
34 touches = t < 0.5f ? a.touches : b.touches,
35 joystickPosition = Vector2.Lerp(a.joystickPosition, b.joystickPosition, t),
36 indexTrigger = Mathf.Lerp(a.indexTrigger, b.indexTrigger, t),
37 handTrigger = Mathf.Lerp(a.handTrigger, b.handTrigger, t),
38 isActive = t < 0.5f ? a.isActive : b.isActive,
39 };
40 }
41 }
42
43 public struct PoseFrame
44 {
45 public Vector3 headPosition;
46 public Quaternion headRotation;
47 public Vector3 handLeftPosition;
48 public Quaternion handLeftRotation;
49 public Vector3 handRightPosition;
50 public Quaternion handRightRotation;
51 public float voiceAmplitude;
52
53 public ControllerPose controllerLeftPose;
54 public ControllerPose controllerRightPose;
55
56 public static PoseFrame Interpolate(PoseFrame a, PoseFrame b, float t)
57 {
58 return new PoseFrame
59 {
60 headPosition = Vector3.Lerp(a.headPosition, b.headPosition, t),
61 headRotation = Quaternion.Slerp(a.headRotation, b.headRotation, t),
62 handLeftPosition = Vector3.Lerp(a.handLeftPosition, b.handLeftPosition, t),
63 handLeftRotation = Quaternion.Slerp(a.handLeftRotation, b.handLeftRotation, t),
64 handRightPosition = Vector3.Lerp(a.handRightPosition, b.handRightPosition, t),
65 handRightRotation = Quaternion.Slerp(a.handRightRotation, b.handRightRotation, t),
66 voiceAmplitude = Mathf.Lerp(a.voiceAmplitude, b.voiceAmplitude, t),
67 controllerLeftPose = ControllerPose.Interpolate(a.controllerLeftPose, b.controllerLeftPose, t),
68 controllerRightPose = ControllerPose.Interpolate(a.controllerRightPose, b.controllerRightPose, t),
69 };
70 }
71 };
72
73 void Start()
74 {
75 var headsetType = OVRPlugin.GetSystemHeadsetType();
76 switch (headsetType)
77 {
78 case OVRPlugin.SystemHeadset.GearVR_R320:
79 case OVRPlugin.SystemHeadset.GearVR_R321:
80 case OVRPlugin.SystemHeadset.GearVR_R322:
81 case OVRPlugin.SystemHeadset.GearVR_R323:
82 case OVRPlugin.SystemHeadset.GearVR_R324:
83 case OVRPlugin.SystemHeadset.GearVR_R325:
84 ControllerType = ovrAvatarControllerType.Malibu;
85 break;
86 case OVRPlugin.SystemHeadset.Oculus_Go:
87 ControllerType = ovrAvatarControllerType.Go;
88 break;
89 case OVRPlugin.SystemHeadset.Oculus_Quest:
90 case OVRPlugin.SystemHeadset.Rift_S:
91 ControllerType = ovrAvatarControllerType.Quest;
92 break;
93 case OVRPlugin.SystemHeadset.Rift_DK1:
94 case OVRPlugin.SystemHeadset.Rift_DK2:
95 case OVRPlugin.SystemHeadset.Rift_CV1:
96 default:
97 ControllerType = ovrAvatarControllerType.Touch;
98 break;
99 }
100 }
101
102 protected void UpdateTransformsFromPose(IntPtr sdkAvatar)
103 {
104 if (sdkAvatar != IntPtr.Zero)
105 {
106 ovrAvatarTransform bodyTransform = OvrAvatar.CreateOvrAvatarTransform(CurrentPose.headPosition, CurrentPose.headRotation);
107 ovrAvatarHandInputState inputStateLeft = OvrAvatar.CreateInputState(OvrAvatar.CreateOvrAvatarTransform(CurrentPose.handLeftPosition, CurrentPose.handLeftRotation), CurrentPose.controllerLeftPose);
108 ovrAvatarHandInputState inputStateRight = OvrAvatar.CreateInputState(OvrAvatar.CreateOvrAvatarTransform(CurrentPose.handRightPosition, CurrentPose.handRightRotation), CurrentPose.controllerRightPose);
109
110 CAPI.ovrAvatarPose_UpdateBody(sdkAvatar, bodyTransform);
111 CAPI.ovrAvatarPose_UpdateHandsWithType(sdkAvatar, inputStateLeft, inputStateRight, ControllerType);
112 }
113 }
114
115 public static bool GetIsTrackedRemote()
116 {
117 return OVRInput.IsControllerConnected(OVRInput.Controller.RTrackedRemote) || OVRInput.IsControllerConnected(OVRInput.Controller.LTrackedRemote);
118 }
119}