BlueGit

blue-star ggj-2020

Files

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

OvrAvatarLocalDriver.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OvrAvatarLocalDriver.cs 5.0 KiB Download Raw
1using UnityEngine;
2using System.Collections;
3using System;
4using System.Collections.Generic;
5using Oculus.Avatar;
6
7public class OvrAvatarLocalDriver : OvrAvatarDriver {
8
9 Vector3 centerEyePosition = Vector3.zero;
10 Quaternion centerEyeRotation = Quaternion.identity;
11
12 ControllerPose GetMalibuControllerPose(OVRInput.Controller controller)
13 {
14 ovrAvatarButton buttons = 0;
15 if (OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger, controller)) buttons |= ovrAvatarButton.One;
16
17 return new ControllerPose
18 {
19 buttons = buttons,
20 touches = OVRInput.Get(OVRInput.Touch.PrimaryTouchpad) ? ovrAvatarTouch.One : 0,
21 joystickPosition = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad, controller),
22 indexTrigger = 0f,
23 handTrigger = 0f,
24 isActive = (OVRInput.GetActiveController() & controller) != 0,
25 };
26 }
27
28 float voiceAmplitude = 0.0f;
29 ControllerPose GetControllerPose(OVRInput.Controller controller)
30 {
31 ovrAvatarButton buttons = 0;
32 if (OVRInput.Get(OVRInput.Button.One, controller)) buttons |= ovrAvatarButton.One;
33 if (OVRInput.Get(OVRInput.Button.Two, controller)) buttons |= ovrAvatarButton.Two;
34 if (OVRInput.Get(OVRInput.Button.Start, controller)) buttons |= ovrAvatarButton.Three;
35 if (OVRInput.Get(OVRInput.Button.PrimaryThumbstick, controller)) buttons |= ovrAvatarButton.Joystick;
36
37 ovrAvatarTouch touches = 0;
38 if (OVRInput.Get(OVRInput.Touch.One, controller)) touches |= ovrAvatarTouch.One;
39 if (OVRInput.Get(OVRInput.Touch.Two, controller)) touches |= ovrAvatarTouch.Two;
40 if (OVRInput.Get(OVRInput.Touch.PrimaryThumbstick, controller)) touches |= ovrAvatarTouch.Joystick;
41 if (OVRInput.Get(OVRInput.Touch.PrimaryThumbRest, controller)) touches |= ovrAvatarTouch.ThumbRest;
42 if (OVRInput.Get(OVRInput.Touch.PrimaryIndexTrigger, controller)) touches |= ovrAvatarTouch.Index;
43 if (!OVRInput.Get(OVRInput.NearTouch.PrimaryIndexTrigger, controller)) touches |= ovrAvatarTouch.Pointing;
44 if (!OVRInput.Get(OVRInput.NearTouch.PrimaryThumbButtons, controller)) touches |= ovrAvatarTouch.ThumbUp;
45
46 return new ControllerPose
47 {
48 buttons = buttons,
49 touches = touches,
50 joystickPosition = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick, controller),
51 indexTrigger = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, controller),
52 handTrigger = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, controller),
53 isActive = (OVRInput.GetActiveController() & controller) != 0,
54 };
55 }
56
57 private void CalculateCurrentPose()
58 {
59 // Platform and device agnostic calls to return center eye pose, used to pass in head pose to sdk
60 OVRNodeStateProperties.GetNodeStatePropertyVector3(UnityEngine.XR.XRNode.CenterEye, NodeStatePropertyType.Position,
61 OVRPlugin.Node.EyeCenter, OVRPlugin.Step.Render, out centerEyePosition);
62 OVRNodeStateProperties.GetNodeStatePropertyQuaternion(UnityEngine.XR.XRNode.CenterEye, NodeStatePropertyType.Orientation,
63 OVRPlugin.Node.EyeCenter, OVRPlugin.Step.Render, out centerEyeRotation);
64
65 if (GetIsTrackedRemote())
66 {
67 CurrentPose = new PoseFrame
68 {
69 voiceAmplitude = voiceAmplitude,
70 headPosition = centerEyePosition,
71 headRotation = centerEyeRotation,
72 handLeftPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTrackedRemote),
73 handLeftRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTrackedRemote),
74 handRightPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTrackedRemote),
75 handRightRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTrackedRemote),
76 controllerLeftPose = GetMalibuControllerPose(OVRInput.Controller.LTrackedRemote),
77 controllerRightPose = GetMalibuControllerPose(OVRInput.Controller.RTrackedRemote),
78 };
79 }
80 else
81 {
82 CurrentPose = new PoseFrame
83 {
84 voiceAmplitude = voiceAmplitude,
85 headPosition = centerEyePosition,
86 headRotation = centerEyeRotation,
87 handLeftPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch),
88 handLeftRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch),
89 handRightPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch),
90 handRightRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch),
91 controllerLeftPose = GetControllerPose(OVRInput.Controller.LTouch),
92 controllerRightPose = GetControllerPose(OVRInput.Controller.RTouch),
93 };
94 }
95 }
96
97 public override void UpdateTransforms(IntPtr sdkAvatar)
98 {
99 CalculateCurrentPose();
100 UpdateTransformsFromPose(sdkAvatar);
101 }
102}