three-gamepad-controls 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,6 +23,10 @@ type GamepadInputEventMap = {
23
23
  gamepad: Gamepad;
24
24
  };
25
25
  };
26
+ /**
27
+ * Dead zone processing mode for two-dimensional stick reads.
28
+ */
29
+ type GamepadDeadzoneMode = "axial" | "radial";
26
30
  /**
27
31
  * Configuration for {@link GamepadInput}.
28
32
  */
@@ -32,6 +36,16 @@ type GamepadInputOptions = {
32
36
  * @default 0.1
33
37
  */
34
38
  deadzone: number;
39
+ /**
40
+ * Default dead zone mode for stick reads.
41
+ * @default "axial"
42
+ */
43
+ deadzoneMode: GamepadDeadzoneMode;
44
+ /**
45
+ * Whether stick reads rescale values outside the dead zone by default.
46
+ * @default false
47
+ */
48
+ rescale: boolean;
35
49
  /**
36
50
  * Browser-assigned gamepad slot to use.
37
51
  *
@@ -44,7 +58,7 @@ type GamepadInputOptions = {
44
58
  gamepadIndex?: number;
45
59
  };
46
60
  /**
47
- * Options for axis and stick reads.
61
+ * Options for axis reads.
48
62
  */
49
63
  type GamepadAxisOptions = {
50
64
  /**
@@ -52,6 +66,24 @@ type GamepadAxisOptions = {
52
66
  */
53
67
  deadzone?: number;
54
68
  };
69
+ /**
70
+ * Options for two-dimensional stick reads.
71
+ */
72
+ type GamepadStickOptions = GamepadAxisOptions & {
73
+ /**
74
+ * Dead zone shape for this read.
75
+ *
76
+ * `"axial"` processes each axis independently, while `"radial"` processes
77
+ * the stick magnitude.
78
+ * @default "axial"
79
+ */
80
+ deadzoneMode?: GamepadDeadzoneMode;
81
+ /**
82
+ * Whether to remap values outside the dead zone to the full output range.
83
+ * @default false
84
+ */
85
+ rescale?: boolean;
86
+ };
55
87
  /**
56
88
  * Two-dimensional stick input after dead zone processing.
57
89
  */
@@ -166,10 +198,10 @@ declare class GamepadInput extends EventDispatcher<GamepadInputEventMap> {
166
198
  *
167
199
  * @param xAxis - Horizontal axis index.
168
200
  * @param yAxis - Vertical axis index.
169
- * @param options - Optional per-read axis options.
201
+ * @param options - Optional per-read stick options.
170
202
  * @returns Object containing processed `x` and `y` values.
171
203
  */
172
- stick(xAxis: number, yAxis: number, options?: GamepadAxisOptions): GamepadStick;
204
+ stick(xAxis: number, yAxis: number, options?: GamepadStickOptions): GamepadStick;
173
205
  /**
174
206
  * Plays an effect through the active gamepad's primary vibration actuator.
175
207
  *
@@ -193,4 +225,4 @@ declare class GamepadInput extends EventDispatcher<GamepadInputEventMap> {
193
225
  resetVibration(): Promise<GamepadHapticsResult | null>;
194
226
  }
195
227
  //#endregion
196
- export { GamepadAxisOptions, GamepadInput, GamepadInputEventMap, GamepadInputOptions, GamepadStick };
228
+ export { GamepadAxisOptions, GamepadDeadzoneMode, GamepadInput, GamepadInputEventMap, GamepadInputOptions, GamepadStick, GamepadStickOptions };
@@ -2,19 +2,68 @@ import { isGamepadVibrationSupported, playGamepadVibrationEffect, resetGamepadVi
2
2
  import { GamepadManager } from "./gamepad-manager.js";
3
3
  import { EventDispatcher } from "three";
4
4
  //#region src/gamepad-input.ts
5
- const DEFAULT_GAMEPAD_INPUT_OPTIONS = { deadzone: .1 };
5
+ const DEFAULT_GAMEPAD_INPUT_OPTIONS = {
6
+ deadzone: .1,
7
+ deadzoneMode: "axial",
8
+ rescale: false
9
+ };
6
10
  /**
7
- * Returns `value` unchanged, or `0` if below the dead zone `threshold`.
11
+ * Applies a dead zone and remaps the remaining magnitude to `[0, 1]`.
8
12
  *
9
- * Kept private to this module because `GamepadInput` is the only public API
10
- * that currently exposes processed axis values.
13
+ * @param magnitude - Non-negative input magnitude.
14
+ * @param threshold - Dead zone size.
15
+ * @returns Rescaled magnitude, or `0` when inside a fully closed dead zone.
16
+ */
17
+ const rescaleGamepadDeadzoneMagnitude = (magnitude, threshold) => {
18
+ if (magnitude <= threshold || threshold >= 1) return 0;
19
+ return Math.min((magnitude - threshold) / (1 - threshold), 1);
20
+ };
21
+ /**
22
+ * Applies an axial dead zone, optionally rescaling the remaining range.
11
23
  *
12
- * @param value - Raw axis or trigger value, typically in `[-1, 1]`.
13
- * @param threshold - Dead zone size; values below this magnitude are zeroed.
14
- * @returns The original value when outside the dead zone, otherwise `0`.
24
+ * @param value - Raw axis value.
25
+ * @param threshold - Dead zone size.
26
+ * @param rescale - Whether to remap the remaining magnitude to `[0, 1]`.
27
+ * @returns Processed signed axis value.
15
28
  */
16
- const applyGamepadDeadzone = (value, threshold) => {
17
- return Math.abs(value) < threshold ? 0 : value;
29
+ const applyGamepadAxialDeadzone = (value, threshold, rescale) => {
30
+ const magnitude = Math.abs(value);
31
+ if (magnitude < threshold) return 0;
32
+ if (!rescale) return value;
33
+ return Math.sign(value) * rescaleGamepadDeadzoneMagnitude(magnitude, threshold);
34
+ };
35
+ /**
36
+ * Returns the stick unchanged, or centered if its magnitude is below the dead
37
+ * zone `threshold`.
38
+ *
39
+ * @param x - Raw horizontal stick value.
40
+ * @param y - Raw vertical stick value.
41
+ * @param threshold - Radial dead zone size.
42
+ * @param rescale - Whether to remap the remaining magnitude to `[0, 1]`.
43
+ * @returns The processed stick, or `{ x: 0, y: 0 }` inside the dead zone.
44
+ */
45
+ const applyGamepadRadialDeadzone = (x, y, threshold, rescale) => {
46
+ const magnitude = Math.hypot(x, y);
47
+ if (magnitude < threshold) return {
48
+ x: 0,
49
+ y: 0
50
+ };
51
+ if (rescale) {
52
+ const rescaledMagnitude = rescaleGamepadDeadzoneMagnitude(magnitude, threshold);
53
+ if (rescaledMagnitude === 0 || magnitude === 0) return {
54
+ x: 0,
55
+ y: 0
56
+ };
57
+ const scale = rescaledMagnitude / magnitude;
58
+ return {
59
+ x: x * scale,
60
+ y: y * scale
61
+ };
62
+ }
63
+ return {
64
+ x,
65
+ y
66
+ };
18
67
  };
19
68
  /**
20
69
  * Returns whether a gamepad button is currently pressed.
@@ -225,20 +274,26 @@ var GamepadInput = class extends EventDispatcher {
225
274
  */
226
275
  axis(axis, options) {
227
276
  const value = this.#gamepad?.axes[axis] ?? 0;
228
- return applyGamepadDeadzone(value, this.#getDeadzone(options));
277
+ return applyGamepadAxialDeadzone(value, this.#getDeadzone(options), false);
229
278
  }
230
279
  /**
231
280
  * Returns a two-axis stick after dead zone processing.
232
281
  *
233
282
  * @param xAxis - Horizontal axis index.
234
283
  * @param yAxis - Vertical axis index.
235
- * @param options - Optional per-read axis options.
284
+ * @param options - Optional per-read stick options.
236
285
  * @returns Object containing processed `x` and `y` values.
237
286
  */
238
287
  stick(xAxis, yAxis, options) {
288
+ const deadzoneMode = options?.deadzoneMode ?? this.#options.deadzoneMode;
289
+ const rescale = options?.rescale ?? this.#options.rescale;
290
+ const threshold = this.#getDeadzone(options);
291
+ const x = this.#gamepad?.axes[xAxis] ?? 0;
292
+ const y = this.#gamepad?.axes[yAxis] ?? 0;
293
+ if (deadzoneMode === "radial") return applyGamepadRadialDeadzone(x, y, threshold, rescale);
239
294
  return {
240
- x: this.axis(xAxis, options),
241
- y: this.axis(yAxis, options)
295
+ x: applyGamepadAxialDeadzone(x, threshold, rescale),
296
+ y: applyGamepadAxialDeadzone(y, threshold, rescale)
242
297
  };
243
298
  }
244
299
  /**
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, MAX_GAMEPAD_INDEX, MIN_GAMEPAD_INDEX } from "./core.js";
2
- import { GamepadAxisOptions, GamepadInput, GamepadInputEventMap, GamepadInputOptions, GamepadStick } from "./gamepad-input.js";
2
+ import { GamepadAxisOptions, GamepadDeadzoneMode, GamepadInput, GamepadInputEventMap, GamepadInputOptions, GamepadStick, GamepadStickOptions } from "./gamepad-input.js";
3
3
  import { GamepadControls, GamepadControlsEventMap, GamepadControlsOptions } from "./gamepad-controls.js";
4
4
  import { GamepadArcballControls, GamepadArcballControlsOptions } from "./gamepad-arcball-controls.js";
5
5
  import { GamepadDragControls, GamepadDragControlsOptions } from "./gamepad-drag-controls.js";
@@ -10,4 +10,4 @@ import { GamepadMapControls } from "./gamepad-map-controls.js";
10
10
  import { GamepadPointerLockControls, GamepadPointerLockControlsOptions } from "./gamepad-pointer-lock-controls.js";
11
11
  import { GamepadTrackballControls, GamepadTrackballControlsOptions } from "./gamepad-trackball-controls.js";
12
12
  import { GamepadTransformControls, GamepadTransformControlsOptions } from "./gamepad-transform-controls.js";
13
- export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadArcballControls, GamepadArcballControlsOptions, GamepadAxisKey, GamepadAxisOptions, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, GamepadControls, GamepadControlsEventMap, GamepadControlsOptions, GamepadDragControls, GamepadDragControlsOptions, GamepadFirstPersonControls, GamepadFirstPersonControlsOptions, GamepadFlyControls, GamepadFlyControlsOptions, GamepadInput, GamepadInputEventMap, GamepadInputOptions, GamepadMapControls, GamepadOrbitControls, GamepadOrbitControlsOptions, GamepadPointerLockControls, GamepadPointerLockControlsOptions, GamepadStick, GamepadTrackballControls, GamepadTrackballControlsOptions, GamepadTransformControls, GamepadTransformControlsOptions, MAX_GAMEPAD_INDEX, MIN_GAMEPAD_INDEX };
13
+ export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadArcballControls, GamepadArcballControlsOptions, GamepadAxisKey, GamepadAxisOptions, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, GamepadControls, GamepadControlsEventMap, GamepadControlsOptions, GamepadDeadzoneMode, GamepadDragControls, GamepadDragControlsOptions, GamepadFirstPersonControls, GamepadFirstPersonControlsOptions, GamepadFlyControls, GamepadFlyControlsOptions, GamepadInput, GamepadInputEventMap, GamepadInputOptions, GamepadMapControls, GamepadOrbitControls, GamepadOrbitControlsOptions, GamepadPointerLockControls, GamepadPointerLockControlsOptions, GamepadStick, GamepadStickOptions, GamepadTrackballControls, GamepadTrackballControlsOptions, GamepadTransformControls, GamepadTransformControlsOptions, MAX_GAMEPAD_INDEX, MIN_GAMEPAD_INDEX };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "three-gamepad-controls",
3
3
  "description": "Gamepad support for Three.js controls.",
4
- "version": "0.15.0",
4
+ "version": "0.17.0",
5
5
  "homepage": "https://github.com/luckasnix/three-gamepad-controls#readme",
6
6
  "author": {
7
7
  "name": "Kasnix",