1
1
using UnityEngine ;
2
- using InputDevice = UnityEngine . XR . InputDevice ;
2
+ using UnityEngine . Events ;
3
3
#if UNITY_INPUT_SYSTEM_1_4_4_OR_NEWER
4
4
using UnityEngine . InputSystem ;
5
5
using UnityEngine . InputSystem . Layouts ;
6
+ using UnityEngine . InputSystem . LowLevel ;
6
7
#if XR_HANDS_1_1_OR_NEWER
7
8
using System . Collections ;
8
9
using System . Collections . Generic ;
@@ -19,15 +20,35 @@ namespace WebXR.InputSystem
19
20
#endif
20
21
public class WebXRInputSystem : MonoBehaviour
21
22
{
22
- public Transform [ ] leftHandTransforms ;
23
- public Transform [ ] rightHandTransforms ;
24
- private Pose [ ] leftPoses ;
25
- private Pose [ ] rightPoses ;
26
- public Vector3 rightPos ;
27
- public Vector3 leftPos ;
28
- public Vector3 headPos ;
29
- public int devicesType ;
23
+ [ SerializeField ]
24
+ private UnityEvent onLeftControllerProfiles ;
25
+ public UnityEvent OnLeftControllerProfiles
26
+ {
27
+ get => onLeftControllerProfiles ;
28
+ set => onLeftControllerProfiles = value ;
29
+ }
30
+
31
+ [ SerializeField ]
32
+ private UnityEvent onRightControllerProfiles ;
33
+ public UnityEvent OnRightControllerProfiles
34
+ {
35
+ get => onRightControllerProfiles ;
36
+ set => onRightControllerProfiles = value ;
37
+ }
38
+ private string [ ] leftProfiles = null ;
39
+ private string [ ] rightProfiles = null ;
40
+ private bool hasLeftProfiles = false ;
41
+ private bool hasRightProfiles = false ;
30
42
43
+ public string [ ] GetLeftProfiles ( )
44
+ {
45
+ return leftProfiles ;
46
+ }
47
+
48
+ public string [ ] GetRightProfiles ( )
49
+ {
50
+ return rightProfiles ;
51
+ }
31
52
#if UNITY_INPUT_SYSTEM_1_4_4_OR_NEWER
32
53
private static bool initialized = false ;
33
54
WebXRController left = null ;
@@ -65,19 +86,15 @@ private void Awake()
65
86
}
66
87
}
67
88
subsystemUpdater = new XRHandProviderUtility . SubsystemUpdater ( webXRHandsSubsystem ) ;
68
-
69
- leftPoses = new Pose [ leftHandTransforms . Length ] ;
70
- rightPoses = new Pose [ rightHandTransforms . Length ] ;
71
- for ( int i = 0 ; i < leftHandTransforms . Length ; i ++ )
72
- {
73
- leftPoses [ i ] = new Pose ( leftHandTransforms [ i ] . position , leftHandTransforms [ i ] . rotation ) ;
74
- rightPoses [ i ] = new Pose ( rightHandTransforms [ i ] . position , rightHandTransforms [ i ] . rotation ) ;
75
- }
76
89
#endif
77
90
}
78
91
79
92
private void OnEnable ( )
80
93
{
94
+ unsafe
95
+ {
96
+ InputSystem . onDeviceCommand += HandleOnDeviceCommand ;
97
+ }
81
98
WebXRManager . OnXRChange += OnXRChange ;
82
99
WebXRManager . OnHeadsetUpdate += OnHeadsetUpdate ;
83
100
WebXRManager . OnControllerUpdate += OnControllerUpdate ;
@@ -90,6 +107,10 @@ private void OnEnable()
90
107
91
108
private void OnDisable ( )
92
109
{
110
+ unsafe
111
+ {
112
+ InputSystem . onDeviceCommand -= HandleOnDeviceCommand ;
113
+ }
93
114
RemoveAllDevices ( ) ;
94
115
WebXRManager . OnXRChange -= OnXRChange ;
95
116
WebXRManager . OnHeadsetUpdate -= OnHeadsetUpdate ;
@@ -121,64 +142,26 @@ private void OnDestroy()
121
142
}
122
143
#endif
123
144
124
- /*
125
- private void Update1()
145
+ private unsafe long ? HandleOnDeviceCommand (
146
+ UnityEngine . InputSystem . InputDevice inputDevice ,
147
+ InputDeviceCommand * command )
126
148
{
127
- OnHeadsetUpdate(
128
- Matrix4x4.identity,
129
- Matrix4x4.identity,
130
- Quaternion.identity,
131
- Quaternion.identity,
132
- headPos,
133
- headPos
134
- );
135
- WebXRHandData handDataLeft = new();
136
- handDataLeft.frame = Time.frameCount;
137
- handDataLeft.enabled = devicesType == 2;
138
- handDataLeft.hand = 1;
139
- handDataLeft.trigger = 1;
140
- for (int i = 0; i < handDataLeft.joints.Length; i++)
149
+ if ( inputDevice != left && inputDevice != right )
141
150
{
142
- handDataLeft.joints[i].radius = 0.01f;
143
- handDataLeft.joints[i].position = leftPoses[i].position;
144
- handDataLeft.joints[i].rotation = leftPoses[i].rotation;
145
- handDataLeft.joints[i].position += leftPos;
151
+ return null ;
146
152
}
147
- OnHandUpdate(handDataLeft);
148
- WebXRHandData handDataRight = new();
149
- handDataRight.frame = Time.frameCount;
150
- handDataRight.enabled = devicesType == 2;
151
- handDataRight.hand = 2;
152
- handDataRight.trigger = 1;
153
- for (int i = 0; i < handDataRight.joints.Length; i++)
153
+ if ( command ->type != InternalSendHapticImpulseCommand . Type )
154
154
{
155
- handDataRight.joints[i].radius = 0.01f;
156
- handDataRight.joints[i].position = rightPoses[i].position;
157
- handDataRight.joints[i].rotation = rightPoses[i].rotation;
158
- handDataRight.joints[i].position += rightPos;
155
+ return null ;
159
156
}
160
- OnHandUpdate(handDataRight);
161
157
162
- WebXRControllerData controllerDataLeft = new WebXRControllerData();
163
- controllerDataLeft.frame = Time.frameCount;
164
- controllerDataLeft.enabled = devicesType == 1;
165
- controllerDataLeft.hand = 1;
166
- controllerDataLeft.position = leftPos;
167
- controllerDataLeft.rotation = Quaternion.identity;
168
- controllerDataLeft.gripPosition = leftPos;
169
- controllerDataLeft.gripRotation = Quaternion.identity;
170
- OnControllerUpdate(controllerDataLeft);
171
- WebXRControllerData controllerDataRight = new WebXRControllerData();
172
- controllerDataRight.frame = Time.frameCount;
173
- controllerDataRight.enabled = devicesType == 1;
174
- controllerDataRight.hand = 2;
175
- controllerDataRight.position = rightPos;
176
- controllerDataRight.rotation = Quaternion.identity;
177
- controllerDataRight.gripPosition = rightPos;
178
- controllerDataRight.gripRotation = Quaternion.identity;
179
- OnControllerUpdate(controllerDataRight);
158
+ var impulseCommand = * ( InternalSendHapticImpulseCommand * ) command ;
159
+ WebXRManager. Instance . HapticPulse (
160
+ inputDevice == left ? WebXRControllerHand . LEFT : WebXRControllerHand . RIGHT ,
161
+ impulseCommand . amplitude ,
162
+ impulseCommand . duration * 1000f ) ;
163
+ return 0 ;
180
164
}
181
- */
182
165
183
166
private void OnXRChange (
184
167
WebXRState state ,
@@ -232,9 +215,21 @@ private void OnControllerUpdate(WebXRControllerData controllerData)
232
215
{
233
216
case 1 :
234
217
UpdateController ( controllerData , ref left ) ;
218
+ if ( ! hasLeftProfiles && controllerData . profiles != null )
219
+ {
220
+ leftProfiles = controllerData . profiles ;
221
+ hasLeftProfiles = true ;
222
+ onLeftControllerProfiles ? . Invoke ( ) ;
223
+ }
235
224
break ;
236
225
case 2 :
237
226
UpdateController ( controllerData , ref right ) ;
227
+ if ( ! hasRightProfiles && controllerData . profiles != null )
228
+ {
229
+ rightProfiles = controllerData . profiles ;
230
+ hasRightProfiles = true ;
231
+ onRightControllerProfiles ? . Invoke ( ) ;
232
+ }
238
233
break ;
239
234
}
240
235
}
0 commit comments