three-gamepad-controls 0.2.0 → 0.3.1

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.
package/README.md CHANGED
@@ -39,3 +39,4 @@ bun add three-gamepad-controls
39
39
  - [Core](./docs/core.md) — The fundamental building blocks.
40
40
  - [GamepadControls](./docs/gamepad-controls.md) — Abstract base class for custom gamepad controls.
41
41
  - [GamepadOrbitControls](./docs/gamepad-orbit-controls.md) — Gamepad support for `OrbitControls`.
42
+ - [GamepadPointerLockControls](./docs/gamepad-pointer-lock-controls.md) — Gamepad support for `PointerLockControls`.
@@ -5,8 +5,7 @@ import { OrbitControls } from "three/addons/controls/OrbitControls.js";
5
5
  /**
6
6
  * Configuration for {@link GamepadOrbitControls}.
7
7
  *
8
- * Every property has a sensible default (see {@link DEFAULT_ORBIT_OPTIONS}),
9
- * so you only need to pass the properties you want to override.
8
+ * Every property has a sensible default, so you only need to pass the properties you want to override.
10
9
  */
11
10
  type GamepadOrbitControlsOptions = {
12
11
  /**
@@ -0,0 +1,71 @@
1
+ import { GamepadControls } from "./gamepad-controls.js";
2
+ import { PointerLockControls } from "three/addons/controls/PointerLockControls.js";
3
+
4
+ //#region src/gamepad-pointer-lock-controls.d.ts
5
+ /**
6
+ * Configuration for {@link GamepadPointerLockControls}.
7
+ *
8
+ * Every property has a sensible default, so you only need to pass the properties you want to override.
9
+ */
10
+ type GamepadPointerLockControlsOptions = {
11
+ /**
12
+ * Camera movement speed in world units per second at full stick deflection.
13
+ * @default 5.0
14
+ */
15
+ moveSpeed: number;
16
+ /**
17
+ * Multiplier on look rotation speed (combined with `PointerLockControls.pointerSpeed`).
18
+ * @default 1.0
19
+ */
20
+ lookSpeed: number;
21
+ /**
22
+ * Axis dead zone threshold in the range `[0, 1]`.
23
+ * @default 0.1
24
+ */
25
+ deadzone: number;
26
+ /**
27
+ * Axis index for **forward / backward** movement.
28
+ * @default 1 — Left stick Y
29
+ */
30
+ axisMoveForward: number;
31
+ /**
32
+ * Axis index for **right / left** strafe movement.
33
+ * @default 0 — Left stick X
34
+ */
35
+ axisMoveRight: number;
36
+ /**
37
+ * Axis index for **horizontal** camera look (yaw).
38
+ * @default 2 — Right stick X
39
+ */
40
+ axisLookX: number;
41
+ /**
42
+ * Axis index for **vertical** camera look (pitch).
43
+ * @default 3 — Right stick Y
44
+ */
45
+ axisLookY: number;
46
+ };
47
+ /**
48
+ * Adds gamepad support to Three.js `PointerLockControls`.
49
+ *
50
+ * Gamepad input is fully independent of pointer lock state. When the pointer
51
+ * IS locked, mouse and gamepad look inputs are additive.
52
+ * Bindings and speeds are configurable via {@link GamepadPointerLockControlsOptions}.
53
+ */
54
+ declare class GamepadPointerLockControls extends GamepadControls {
55
+ #private;
56
+ /**
57
+ * @param controls - A Three.js `PointerLockControls` instance.
58
+ * @param options - Optional overrides for the default behavior.
59
+ * Any property not provided falls back to its default value.
60
+ */
61
+ constructor(controls: PointerLockControls, options?: Partial<GamepadPointerLockControlsOptions>);
62
+ /**
63
+ * Maps the current gamepad state to `PointerLockControls` movement and look.
64
+ *
65
+ * @param deltaTime - Seconds since the last frame.
66
+ * @param gamepad - Fresh gamepad snapshot provided by the base class.
67
+ */
68
+ protected onUpdate(deltaTime: number, gamepad: Gamepad): void;
69
+ }
70
+ //#endregion
71
+ export { GamepadPointerLockControls, GamepadPointerLockControlsOptions };
@@ -0,0 +1,77 @@
1
+ import { GAMEPAD_AXIS } from "./core.js";
2
+ import { GamepadControls } from "./gamepad-controls.js";
3
+ import { Euler } from "three";
4
+ //#region src/gamepad-pointer-lock-controls.ts
5
+ /**
6
+ * Default options merged in the constructor when no explicit configuration is provided.
7
+ */
8
+ const DEFAULT_POINTER_LOCK_OPTIONS = {
9
+ moveSpeed: 5,
10
+ lookSpeed: 1,
11
+ deadzone: .1,
12
+ axisMoveForward: GAMEPAD_AXIS.LeftY,
13
+ axisMoveRight: GAMEPAD_AXIS.LeftX,
14
+ axisLookX: GAMEPAD_AXIS.RightX,
15
+ axisLookY: GAMEPAD_AXIS.RightY
16
+ };
17
+ /**
18
+ * Adds gamepad support to Three.js `PointerLockControls`.
19
+ *
20
+ * Gamepad input is fully independent of pointer lock state. When the pointer
21
+ * IS locked, mouse and gamepad look inputs are additive.
22
+ * Bindings and speeds are configurable via {@link GamepadPointerLockControlsOptions}.
23
+ */
24
+ var GamepadPointerLockControls = class extends GamepadControls {
25
+ #controls;
26
+ #options;
27
+ #euler;
28
+ /**
29
+ * @param controls - A Three.js `PointerLockControls` instance.
30
+ * @param options - Optional overrides for the default behavior.
31
+ * Any property not provided falls back to its default value.
32
+ */
33
+ constructor(controls, options) {
34
+ super();
35
+ this.#controls = controls;
36
+ this.#options = {
37
+ ...DEFAULT_POINTER_LOCK_OPTIONS,
38
+ ...options
39
+ };
40
+ this.#euler = new Euler(0, 0, 0, "YXZ");
41
+ }
42
+ /**
43
+ * Maps the current gamepad state to `PointerLockControls` movement and look.
44
+ *
45
+ * @param deltaTime - Seconds since the last frame.
46
+ * @param gamepad - Fresh gamepad snapshot provided by the base class.
47
+ */
48
+ onUpdate(deltaTime, gamepad) {
49
+ const { moveSpeed, lookSpeed, deadzone, axisMoveForward, axisMoveRight, axisLookX, axisLookY } = this.#options;
50
+ const fwd = this.#applyDeadzone(gamepad.axes[axisMoveForward] ?? 0, deadzone);
51
+ const strafe = this.#applyDeadzone(gamepad.axes[axisMoveRight] ?? 0, deadzone);
52
+ if (fwd !== 0) this.#controls.moveForward(-fwd * moveSpeed * deltaTime);
53
+ if (strafe !== 0) this.#controls.moveRight(strafe * moveSpeed * deltaTime);
54
+ const lookX = this.#applyDeadzone(gamepad.axes[axisLookX] ?? 0, deadzone);
55
+ const lookY = this.#applyDeadzone(gamepad.axes[axisLookY] ?? 0, deadzone);
56
+ if (lookX !== 0 || lookY !== 0) {
57
+ const camera = this.#controls.object;
58
+ const scale = lookSpeed * this.#controls.pointerSpeed * deltaTime * Math.PI;
59
+ this.#euler.setFromQuaternion(camera.quaternion);
60
+ this.#euler.y -= lookX * scale;
61
+ this.#euler.x -= lookY * scale;
62
+ this.#euler.x = Math.max(Math.PI / 2 - this.#controls.maxPolarAngle, Math.min(Math.PI / 2 - this.#controls.minPolarAngle, this.#euler.x));
63
+ camera.quaternion.setFromEuler(this.#euler);
64
+ }
65
+ }
66
+ /**
67
+ * Returns `value` unchanged, or `0` if below the dead zone `threshold`.
68
+ *
69
+ * @param value - Raw axis value, typically in `[-1, 1]`.
70
+ * @param threshold - Dead zone size; values below this magnitude are zeroed.
71
+ */
72
+ #applyDeadzone(value, threshold) {
73
+ return Math.abs(value) < threshold ? 0 : value;
74
+ }
75
+ };
76
+ //#endregion
77
+ export { GamepadPointerLockControls };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue } from "./core.js";
2
2
  import { GamepadControls, GamepadControlsEventMap } from "./gamepad-controls.js";
3
3
  import { GamepadOrbitControls, GamepadOrbitControlsOptions } from "./gamepad-orbit-controls.js";
4
- export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, GamepadControls, GamepadControlsEventMap, GamepadOrbitControls, GamepadOrbitControlsOptions };
4
+ import { GamepadPointerLockControls, GamepadPointerLockControlsOptions } from "./gamepad-pointer-lock-controls.js";
5
+ export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, GamepadControls, GamepadControlsEventMap, GamepadOrbitControls, GamepadOrbitControlsOptions, GamepadPointerLockControls, GamepadPointerLockControlsOptions };
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
2
2
  import { GamepadControls } from "./gamepad-controls.js";
3
3
  import { GamepadOrbitControls } from "./gamepad-orbit-controls.js";
4
- export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadControls, GamepadOrbitControls };
4
+ import { GamepadPointerLockControls } from "./gamepad-pointer-lock-controls.js";
5
+ export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadControls, GamepadOrbitControls, GamepadPointerLockControls };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "three-gamepad-controls",
3
3
  "description": "Gamepad support for Three.js controls.",
4
4
  "author": "Kasnix",
5
- "version": "0.2.0",
5
+ "version": "0.3.1",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/luckasnix/three-gamepad-controls.git"
@@ -35,15 +35,15 @@
35
35
  "node": ">=24.0.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@biomejs/biome": "2.4.15",
39
- "@commitlint/cli": "21.0.1",
40
- "@commitlint/config-conventional": "21.0.1",
38
+ "@biomejs/biome": "2.4.16",
39
+ "@commitlint/cli": "21.0.2",
40
+ "@commitlint/config-conventional": "21.0.2",
41
41
  "@commitlint/types": "21.0.1",
42
42
  "@types/node": "24.12.4",
43
43
  "@types/three": "0.184.1",
44
44
  "husky": "9.1.7",
45
45
  "three": "0.184.0",
46
- "tsdown": "0.22.0",
46
+ "tsdown": "0.22.1",
47
47
  "typescript": "6.0.3"
48
48
  },
49
49
  "peerDependencies": {