three-gamepad-controls 0.1.0 → 0.2.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.
package/README.md CHANGED
@@ -2,7 +2,40 @@
2
2
 
3
3
  Gamepad support for [Three.js](https://threejs.org) controls, built on top of [Web Gamepad API](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API).
4
4
 
5
+ ## Installation
6
+
7
+ npm:
8
+
9
+ ```bash
10
+ npm i three-gamepad-controls
11
+ ```
12
+
13
+ pnpm:
14
+
15
+ ```bash
16
+ pnpm add three-gamepad-controls
17
+ ```
18
+
19
+ Yarn:
20
+
21
+ ```bash
22
+ yarn add three-gamepad-controls
23
+ ```
24
+
25
+ Deno:
26
+
27
+ ```bash
28
+ deno add npm:three-gamepad-controls
29
+ ```
30
+
31
+ Bun:
32
+
33
+ ```bash
34
+ bun add three-gamepad-controls
35
+ ```
36
+
5
37
  ## Documentation
6
38
 
7
39
  - [Core](./docs/core.md) — The fundamental building blocks.
8
40
  - [GamepadControls](./docs/gamepad-controls.md) — Abstract base class for custom gamepad controls.
41
+ - [GamepadOrbitControls](./docs/gamepad-orbit-controls.md) — Gamepad support for `OrbitControls`.
@@ -0,0 +1,86 @@
1
+ import { GamepadControls } from "./gamepad-controls.js";
2
+ import { OrbitControls } from "three/addons/controls/OrbitControls.js";
3
+
4
+ //#region src/gamepad-orbit-controls.d.ts
5
+ /**
6
+ * Configuration for {@link GamepadOrbitControls}.
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.
10
+ */
11
+ type GamepadOrbitControlsOptions = {
12
+ /**
13
+ * Multiplier on orbit rotation speed.
14
+ * @default 1.0
15
+ */
16
+ rotateSpeed: number;
17
+ /**
18
+ * Multiplier on pan speed.
19
+ * @default 1.0
20
+ */
21
+ panSpeed: number;
22
+ /**
23
+ * Multiplier on zoom (dolly) speed.
24
+ * @default 1.0
25
+ */
26
+ zoomSpeed: number;
27
+ /**
28
+ * Axis dead zone threshold in the range `[0, 1]`.
29
+ * @default 0.1
30
+ */
31
+ deadzone: number;
32
+ /**
33
+ * Axis index for **horizontal** orbit rotation.
34
+ * @default 0 — Left stick X
35
+ */
36
+ axisRotateX: number;
37
+ /**
38
+ * Axis index for **vertical** orbit rotation.
39
+ * @default 1 — Left stick Y
40
+ */
41
+ axisRotateY: number;
42
+ /**
43
+ * Axis index for **horizontal** panning.
44
+ * @default 2 — Right stick X
45
+ */
46
+ axisPanX: number;
47
+ /**
48
+ * Axis index for **vertical** panning.
49
+ * @default 3 — Right stick Y
50
+ */
51
+ axisPanY: number;
52
+ /**
53
+ * Button index for zooming **in** (analog trigger value used for proportional zoom).
54
+ * @default 6 — Left trigger
55
+ */
56
+ buttonDollyIn: number;
57
+ /**
58
+ * Button index for zooming **out** (analog trigger value used for proportional zoom).
59
+ * @default 7 — Right trigger
60
+ */
61
+ buttonDollyOut: number;
62
+ };
63
+ /**
64
+ * Adds gamepad support to Three.js `OrbitControls`.
65
+ *
66
+ * Call `update()` inside the render loop **before** `OrbitControls.update()`.
67
+ * Bindings and speeds are configurable via {@link GamepadOrbitControlsOptions}.
68
+ */
69
+ declare class GamepadOrbitControls extends GamepadControls {
70
+ #private;
71
+ /**
72
+ * @param controls - A Three.js `OrbitControls` instance.
73
+ * @param options - Optional overrides for the default behavior.
74
+ * Any property not provided falls back to its default value.
75
+ */
76
+ constructor(controls: OrbitControls, options?: Partial<GamepadOrbitControlsOptions>);
77
+ /**
78
+ * Maps the current gamepad state to `OrbitControls` rotation, pan, and dolly.
79
+ *
80
+ * @param deltaTime - Seconds since the last frame.
81
+ * @param gamepad - Fresh gamepad snapshot provided by the base class.
82
+ */
83
+ protected onUpdate(deltaTime: number, gamepad: Gamepad): void;
84
+ }
85
+ //#endregion
86
+ export { GamepadOrbitControls, GamepadOrbitControlsOptions };
@@ -0,0 +1,72 @@
1
+ import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
2
+ import { GamepadControls } from "./gamepad-controls.js";
3
+ //#region src/gamepad-orbit-controls.ts
4
+ /**
5
+ * Default options merged in the constructor when no explicit configuration is provided.
6
+ */
7
+ const DEFAULT_ORBIT_OPTIONS = {
8
+ rotateSpeed: 1,
9
+ panSpeed: 1,
10
+ zoomSpeed: 1,
11
+ deadzone: .1,
12
+ axisRotateX: GAMEPAD_AXIS.LeftX,
13
+ axisRotateY: GAMEPAD_AXIS.LeftY,
14
+ axisPanX: GAMEPAD_AXIS.RightX,
15
+ axisPanY: GAMEPAD_AXIS.RightY,
16
+ buttonDollyIn: GAMEPAD_BUTTON.LeftTrigger,
17
+ buttonDollyOut: GAMEPAD_BUTTON.RightTrigger
18
+ };
19
+ /**
20
+ * Adds gamepad support to Three.js `OrbitControls`.
21
+ *
22
+ * Call `update()` inside the render loop **before** `OrbitControls.update()`.
23
+ * Bindings and speeds are configurable via {@link GamepadOrbitControlsOptions}.
24
+ */
25
+ var GamepadOrbitControls = class extends GamepadControls {
26
+ #controls;
27
+ #options;
28
+ /**
29
+ * @param controls - A Three.js `OrbitControls` 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_ORBIT_OPTIONS,
38
+ ...options
39
+ };
40
+ }
41
+ /**
42
+ * Maps the current gamepad state to `OrbitControls` rotation, pan, and dolly.
43
+ *
44
+ * @param deltaTime - Seconds since the last frame.
45
+ * @param gamepad - Fresh gamepad snapshot provided by the base class.
46
+ */
47
+ onUpdate(deltaTime, gamepad) {
48
+ const { rotateSpeed, panSpeed, zoomSpeed, deadzone, axisRotateX, axisRotateY, axisPanX, axisPanY, buttonDollyIn, buttonDollyOut } = this.#options;
49
+ const rotX = this.#applyDeadzone(gamepad.axes[axisRotateX] ?? 0, deadzone);
50
+ const rotY = this.#applyDeadzone(gamepad.axes[axisRotateY] ?? 0, deadzone);
51
+ if (rotX !== 0) this.#controls.rotateLeft(rotX * rotateSpeed * deltaTime * Math.PI);
52
+ if (rotY !== 0) this.#controls.rotateUp(rotY * rotateSpeed * deltaTime * Math.PI);
53
+ const panX = this.#applyDeadzone(gamepad.axes[axisPanX] ?? 0, deadzone);
54
+ const panY = this.#applyDeadzone(gamepad.axes[axisPanY] ?? 0, deadzone);
55
+ if (panX !== 0 || panY !== 0) this.#controls.pan(panX * panSpeed * deltaTime * 500, panY * panSpeed * deltaTime * 500);
56
+ const triggerIn = gamepad.buttons[buttonDollyIn]?.value ?? 0;
57
+ const triggerOut = gamepad.buttons[buttonDollyOut]?.value ?? 0;
58
+ if (triggerIn > deadzone) this.#controls.dollyIn(1 + zoomSpeed * triggerIn * deltaTime);
59
+ if (triggerOut > deadzone) this.#controls.dollyOut(1 + zoomSpeed * triggerOut * deltaTime);
60
+ }
61
+ /**
62
+ * Returns `value` unchanged, or `0` if below the dead zone `threshold`.
63
+ *
64
+ * @param value - Raw axis or trigger value, typically in `[-1, 1]`.
65
+ * @param threshold - Dead zone size; values below this magnitude are zeroed.
66
+ */
67
+ #applyDeadzone(value, threshold) {
68
+ return Math.abs(value) < threshold ? 0 : value;
69
+ }
70
+ };
71
+ //#endregion
72
+ export { GamepadOrbitControls };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue } from "./core.js";
2
2
  import { GamepadControls, GamepadControlsEventMap } from "./gamepad-controls.js";
3
- export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, GamepadControls, GamepadControlsEventMap };
3
+ import { GamepadOrbitControls, GamepadOrbitControlsOptions } from "./gamepad-orbit-controls.js";
4
+ export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, GamepadControls, GamepadControlsEventMap, GamepadOrbitControls, GamepadOrbitControlsOptions };
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
2
2
  import { GamepadControls } from "./gamepad-controls.js";
3
- export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadControls };
3
+ import { GamepadOrbitControls } from "./gamepad-orbit-controls.js";
4
+ export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadControls, GamepadOrbitControls };
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.1.0",
5
+ "version": "0.2.0",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "git+https://github.com/luckasnix/three-gamepad-controls.git"