Skip to content

Commit fbca446

Browse files
committed
Added missing controller inputs. Changed controller action names to enums.
1 parent 9bdb7f6 commit fbca446

File tree

10 files changed

+74
-139
lines changed

10 files changed

+74
-139
lines changed

Packages/webxr-interactions/Runtime/Scripts/ControllerInteraction.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ void Update()
2727
{
2828
controller.TryUpdateButtons();
2929

30-
float normalizedTime = controller.GetButton("Trigger") ? 1 : controller.GetAxis("Grip");
31-
32-
if (controller.GetButtonDown("Trigger") || controller.GetButtonDown("Grip"))
30+
// Get button A(0 or 1), or Axis Trigger/Grip (0 to 1), the larger between them all, by that order
31+
float normalizedTime = controller.GetButton(WebXRController.ButtonTypes.ButtonA) ? 1 :
32+
Mathf.Max(controller.GetAxis(WebXRController.AxisTypes.Trigger),
33+
controller.GetAxis(WebXRController.AxisTypes.Grip));
34+
35+
if (controller.GetButtonDown(WebXRController.ButtonTypes.Trigger)
36+
|| controller.GetButtonDown(WebXRController.ButtonTypes.Grip)
37+
|| controller.GetButtonDown(WebXRController.ButtonTypes.ButtonA))
38+
{
3339
Pickup();
40+
}
3441

35-
if (controller.GetButtonUp("Trigger") || controller.GetButtonUp("Grip"))
42+
if (controller.GetButtonUp(WebXRController.ButtonTypes.Trigger)
43+
|| controller.GetButtonUp(WebXRController.ButtonTypes.Grip)
44+
|| controller.GetButtonUp(WebXRController.ButtonTypes.ButtonA))
45+
{
3646
Drop();
47+
}
3748

3849
// Use the controller button or axis position to manipulate the playback time for hand model.
3950
anim.Play("Take", -1, normalizedTime);

Packages/webxr-interactions/Samples~/Desert/ControllersMap.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Packages/webxr-interactions/Samples~/Desert/ControllersMap/LeftControllerMap.asset

Lines changed: 0 additions & 19 deletions
This file was deleted.

Packages/webxr-interactions/Samples~/Desert/ControllersMap/LeftControllerMap.asset.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Packages/webxr-interactions/Samples~/Desert/ControllersMap/RightControllerMap.asset

Lines changed: 0 additions & 19 deletions
This file was deleted.

Packages/webxr-interactions/Samples~/Desert/ControllersMap/RightControllerMap.asset.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Packages/webxr-interactions/Samples~/Desert/Prefabs/WebXRCameraSet.prefab

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,7 +1373,6 @@ MonoBehaviour:
13731373
m_Name:
13741374
m_EditorClassIdentifier:
13751375
hand: 1
1376-
inputMap: {fileID: 11400000, guid: f2b0e62590098b246823680b10d75b46, type: 2}
13771376
simulate3dof: 0
13781377
eyesToElbow: {x: 0.1, y: -0.4, z: 0.15}
13791378
elbowHand: {x: 0, y: 0, z: 0.25}
@@ -1864,7 +1863,6 @@ MonoBehaviour:
18641863
m_Name:
18651864
m_EditorClassIdentifier:
18661865
hand: 2
1867-
inputMap: {fileID: 11400000, guid: a37529c06e00da94091bc81a0193c815, type: 2}
18681866
simulate3dof: 0
18691867
eyesToElbow: {x: 0.1, y: -0.4, z: 0.15}
18701868
elbowHand: {x: 0, y: 0, z: 0.25}

Packages/webxr/Runtime/Scripts/WebXRController.cs

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,30 @@ namespace WebXR
1010

1111
public class WebXRController : MonoBehaviour
1212
{
13+
public enum ButtonTypes
14+
{
15+
Trigger = 0,
16+
Grip = 1,
17+
Thumbstick = 2,
18+
Touchpad = 3,
19+
ButtonA = 4,
20+
ButtonB = 5
21+
}
22+
23+
public enum AxisTypes
24+
{
25+
Trigger,
26+
Grip
27+
}
28+
29+
public enum Axis2DTypes
30+
{
31+
Thumbstick, // primary2DAxis
32+
Touchpad // secondary2DAxis
33+
}
34+
1335
[Tooltip("Controller hand to use.")]
1436
public WebXRControllerHand hand = WebXRControllerHand.NONE;
15-
[Tooltip("Controller input settings.")]
16-
public WebXRControllerInputMap inputMap;
1737
[Tooltip("Simulate 3dof controller")]
1838
public bool simulate3dof = false;
1939
[Tooltip("Vector from head to elbow")]
@@ -41,7 +61,7 @@ public class WebXRController : MonoBehaviour
4161

4262
private Quaternion headRotation;
4363
private Vector3 headPosition;
44-
private Dictionary<string, WebXRControllerButton> buttonStates = new Dictionary<string, WebXRControllerButton>();
64+
private Dictionary<ButtonTypes, WebXRControllerButton> buttonStates = new Dictionary<ButtonTypes, WebXRControllerButton>();
4565

4666
private Dictionary<int, Transform> handJoints = new Dictionary<int, Transform>();
4767
private bool handJointsVisible = false;
@@ -107,12 +127,12 @@ public void TryUpdateButtons()
107127
}
108128

109129
WebXRControllerButton[] buttons = new WebXRControllerButton[6];
110-
buttons[0] = new WebXRControllerButton(trigger == 1, trigger);
111-
buttons[1] = new WebXRControllerButton(squeeze == 1, squeeze);
112-
buttons[2] = new WebXRControllerButton(thumbstick == 1, thumbstick);
113-
buttons[3] = new WebXRControllerButton(touchpad == 1, touchpad);
114-
buttons[4] = new WebXRControllerButton(buttonA == 1, buttonA);
115-
buttons[5] = new WebXRControllerButton(buttonB == 1, buttonB);
130+
buttons[(int)ButtonTypes.Trigger] = new WebXRControllerButton(trigger == 1, trigger);
131+
buttons[(int)ButtonTypes.Grip] = new WebXRControllerButton(squeeze == 1, squeeze);
132+
buttons[(int)ButtonTypes.Thumbstick] = new WebXRControllerButton(thumbstick == 1, thumbstick);
133+
buttons[(int)ButtonTypes.Touchpad] = new WebXRControllerButton(touchpad == 1, touchpad);
134+
buttons[(int)ButtonTypes.ButtonA] = new WebXRControllerButton(buttonA == 1, buttonA);
135+
buttons[(int)ButtonTypes.ButtonB] = new WebXRControllerButton(buttonB == 1, buttonB);
116136
UpdateButtons(buttons);
117137
}
118138
#endif
@@ -124,27 +144,35 @@ private void UpdateButtons(WebXRControllerButton[] buttons)
124144
for (int i = 0; i < buttons.Length; i++)
125145
{
126146
WebXRControllerButton button = buttons[i];
127-
foreach (WebXRControllerInput input in inputMap.inputs)
128-
{
129-
if (input.gamepadId == i)
130-
SetButtonState(input.actionName, button.pressed, button.value);
131-
}
147+
SetButtonState((ButtonTypes)i, button.pressed, button.value);
132148
}
133149
}
134150

135-
public float GetAxis(string action)
151+
public float GetAxis(AxisTypes action)
136152
{
137153
switch (action)
138154
{
139-
case "Grip":
155+
case AxisTypes.Grip:
140156
return squeeze;
141-
case "Trigger":
157+
case AxisTypes.Trigger:
142158
return trigger;
143159
}
144160
return 0;
145161
}
146162

147-
public bool GetButton(string action)
163+
public Vector2 GetAxis2D(Axis2DTypes action)
164+
{
165+
switch (action)
166+
{
167+
case Axis2DTypes.Thumbstick:
168+
return new Vector2(thumbstickX, thumbstickY);
169+
case Axis2DTypes.Touchpad:
170+
return new Vector2(touchpadX, touchpadY);
171+
}
172+
return Vector2.zero;
173+
}
174+
175+
public bool GetButton(ButtonTypes action)
148176
{
149177
if (!buttonStates.ContainsKey(action))
150178
{
@@ -153,14 +181,14 @@ public bool GetButton(string action)
153181
return buttonStates[action].pressed;
154182
}
155183

156-
private bool GetPastButtonState(string action)
184+
private bool GetPastButtonState(ButtonTypes action)
157185
{
158186
if (!buttonStates.ContainsKey(action))
159187
return false;
160188
return buttonStates[action].prevPressedState;
161189
}
162190

163-
private void SetButtonState(string action, bool isPressed, float value)
191+
private void SetButtonState(ButtonTypes action, bool isPressed, float value)
164192
{
165193
if (buttonStates.ContainsKey(action))
166194
{
@@ -171,14 +199,14 @@ private void SetButtonState(string action, bool isPressed, float value)
171199
buttonStates.Add(action, new WebXRControllerButton(isPressed, value));
172200
}
173201

174-
private void SetPastButtonState(string action, bool isPressed)
202+
private void SetPastButtonState(ButtonTypes action, bool isPressed)
175203
{
176204
if (!buttonStates.ContainsKey(action))
177205
return;
178206
buttonStates[action].prevPressedState = isPressed;
179207
}
180208

181-
public bool GetButtonDown(string action)
209+
public bool GetButtonDown(ButtonTypes action)
182210
{
183211
if (GetButton(action) && !GetPastButtonState(action))
184212
{
@@ -188,7 +216,7 @@ public bool GetButtonDown(string action)
188216
return false;
189217
}
190218

191-
public bool GetButtonUp(string action)
219+
public bool GetButtonUp(ButtonTypes action)
192220
{
193221
if (!GetButton(action) && GetPastButtonState(action))
194222
{
@@ -236,12 +264,12 @@ private void OnControllerUpdate(WebXRControllerData controllerData)
236264
buttonB = controllerData.buttonB;
237265

238266
WebXRControllerButton[] buttons = new WebXRControllerButton[6];
239-
buttons[0] = new WebXRControllerButton(trigger == 1, trigger);
240-
buttons[1] = new WebXRControllerButton(squeeze == 1, squeeze);
241-
buttons[2] = new WebXRControllerButton(thumbstick == 1, thumbstick);
242-
buttons[3] = new WebXRControllerButton(touchpad == 1, touchpad);
243-
buttons[4] = new WebXRControllerButton(buttonA == 1, buttonA);
244-
buttons[5] = new WebXRControllerButton(buttonB == 1, buttonB);
267+
buttons[(int)ButtonTypes.Trigger] = new WebXRControllerButton(trigger == 1, trigger);
268+
buttons[(int)ButtonTypes.Grip] = new WebXRControllerButton(squeeze == 1, squeeze);
269+
buttons[(int)ButtonTypes.Thumbstick] = new WebXRControllerButton(thumbstick == 1, thumbstick);
270+
buttons[(int)ButtonTypes.Touchpad] = new WebXRControllerButton(touchpad == 1, touchpad);
271+
buttons[(int)ButtonTypes.ButtonA] = new WebXRControllerButton(buttonA == 1, buttonA);
272+
buttons[(int)ButtonTypes.ButtonB] = new WebXRControllerButton(buttonB == 1, buttonB);
245273
UpdateButtons(buttons);
246274
}
247275
}
@@ -295,8 +323,8 @@ private void OnHandUpdate(WebXRHandData handData)
295323
squeeze = handData.squeeze;
296324

297325
WebXRControllerButton[] buttons = new WebXRControllerButton[2];
298-
buttons[0] = new WebXRControllerButton(trigger == 1, trigger);
299-
buttons[1] = new WebXRControllerButton(squeeze == 1, squeeze);
326+
buttons[(int)ButtonTypes.Trigger] = new WebXRControllerButton(trigger == 1, trigger);
327+
buttons[(int)ButtonTypes.Grip] = new WebXRControllerButton(squeeze == 1, squeeze);
300328
UpdateButtons(buttons);
301329
}
302330
}
@@ -360,11 +388,6 @@ public void Pulse(float intensity, float duration)
360388

361389
void OnEnable()
362390
{
363-
if (inputMap == null)
364-
{
365-
Debug.LogError("A Input Map must be assigned to WebXRController!");
366-
return;
367-
}
368391
WebXRManager.OnControllerUpdate += OnControllerUpdate;
369392
WebXRManager.OnHandUpdate += OnHandUpdate;
370393
WebXRManager.OnHeadsetUpdate += onHeadsetUpdate;

Packages/webxr/Runtime/Scripts/WebXRControllerInputMap.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

Packages/webxr/Runtime/Scripts/WebXRControllerInputMap.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)