three-gamepad-controls 0.6.0 → 0.7.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 +1 -0
- package/dist/gamepad-trackball-controls.d.ts +85 -0
- package/dist/gamepad-trackball-controls.js +102 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,3 +43,4 @@ bun add three-gamepad-controls
|
|
|
43
43
|
- [GamepadMapControls](./docs/gamepad-map-controls.md) — Gamepad support for `MapControls`.
|
|
44
44
|
- [GamepadOrbitControls](./docs/gamepad-orbit-controls.md) — Gamepad support for `OrbitControls`.
|
|
45
45
|
- [GamepadPointerLockControls](./docs/gamepad-pointer-lock-controls.md) — Gamepad support for `PointerLockControls`.
|
|
46
|
+
- [GamepadTrackballControls](./docs/gamepad-trackball-controls.md) — Gamepad support for `TrackballControls`.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { GamepadControls } from "./gamepad-controls.js";
|
|
2
|
+
import { TrackballControls } from "three/addons/controls/TrackballControls.js";
|
|
3
|
+
|
|
4
|
+
//#region src/gamepad-trackball-controls.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for {@link GamepadTrackballControls}.
|
|
7
|
+
*
|
|
8
|
+
* Every property has a sensible default, so you only need to pass the properties you want to override.
|
|
9
|
+
*/
|
|
10
|
+
type GamepadTrackballControlsOptions = {
|
|
11
|
+
/**
|
|
12
|
+
* Multiplier on `TrackballControls.rotateSpeed` for rotation.
|
|
13
|
+
* @default 1.0
|
|
14
|
+
*/
|
|
15
|
+
rotateSpeed: number;
|
|
16
|
+
/**
|
|
17
|
+
* Multiplier on `TrackballControls.panSpeed` for panning.
|
|
18
|
+
* @default 1.0
|
|
19
|
+
*/
|
|
20
|
+
panSpeed: number;
|
|
21
|
+
/**
|
|
22
|
+
* Multiplier on `TrackballControls.zoomSpeed` for zooming.
|
|
23
|
+
* @default 1.0
|
|
24
|
+
*/
|
|
25
|
+
zoomSpeed: number;
|
|
26
|
+
/**
|
|
27
|
+
* Axis dead zone threshold in the range `[0, 1]`.
|
|
28
|
+
* @default 0.1
|
|
29
|
+
*/
|
|
30
|
+
deadzone: number;
|
|
31
|
+
/**
|
|
32
|
+
* Axis index for **horizontal** trackball rotation.
|
|
33
|
+
* @default 0 - Left stick X
|
|
34
|
+
*/
|
|
35
|
+
axisRotateX: number;
|
|
36
|
+
/**
|
|
37
|
+
* Axis index for **vertical** trackball rotation.
|
|
38
|
+
* @default 1 - Left stick Y
|
|
39
|
+
*/
|
|
40
|
+
axisRotateY: number;
|
|
41
|
+
/**
|
|
42
|
+
* Axis index for **horizontal** panning.
|
|
43
|
+
* @default 2 - Right stick X
|
|
44
|
+
*/
|
|
45
|
+
axisPanX: number;
|
|
46
|
+
/**
|
|
47
|
+
* Axis index for **vertical** panning.
|
|
48
|
+
* @default 3 - Right stick Y
|
|
49
|
+
*/
|
|
50
|
+
axisPanY: number;
|
|
51
|
+
/**
|
|
52
|
+
* Button index for zooming **in** (analog trigger value used for proportional zoom).
|
|
53
|
+
* @default 6 - Left trigger
|
|
54
|
+
*/
|
|
55
|
+
buttonZoomIn: number;
|
|
56
|
+
/**
|
|
57
|
+
* Button index for zooming **out** (analog trigger value used for proportional zoom).
|
|
58
|
+
* @default 7 - Right trigger
|
|
59
|
+
*/
|
|
60
|
+
buttonZoomOut: number;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Adds gamepad support to Three.js `TrackballControls`.
|
|
64
|
+
*
|
|
65
|
+
* Call `update()` inside the render loop **before** `TrackballControls.update()`.
|
|
66
|
+
* Bindings and speed multipliers are configurable via {@link GamepadTrackballControlsOptions}.
|
|
67
|
+
*/
|
|
68
|
+
declare class GamepadTrackballControls extends GamepadControls {
|
|
69
|
+
#private;
|
|
70
|
+
/**
|
|
71
|
+
* @param controls - A Three.js `TrackballControls` instance.
|
|
72
|
+
* @param options - Optional overrides for the default behavior.
|
|
73
|
+
* Any property not provided falls back to its default value.
|
|
74
|
+
*/
|
|
75
|
+
constructor(controls: TrackballControls, options?: Partial<GamepadTrackballControlsOptions>);
|
|
76
|
+
/**
|
|
77
|
+
* Maps the current gamepad state to `TrackballControls` rotation, pan, and zoom.
|
|
78
|
+
*
|
|
79
|
+
* @param deltaTime - Seconds since the last frame.
|
|
80
|
+
* @param gamepad - Fresh gamepad snapshot provided by the base class.
|
|
81
|
+
*/
|
|
82
|
+
protected onUpdate(deltaTime: number, gamepad: Gamepad): void;
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
export { GamepadTrackballControls, GamepadTrackballControlsOptions };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
|
|
2
|
+
import { GamepadControls } from "./gamepad-controls.js";
|
|
3
|
+
//#region src/gamepad-trackball-controls.ts
|
|
4
|
+
/**
|
|
5
|
+
* Default options merged in the constructor when no explicit configuration is provided.
|
|
6
|
+
*/
|
|
7
|
+
const DEFAULT_TRACKBALL_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
|
+
buttonZoomIn: GAMEPAD_BUTTON.LeftTrigger,
|
|
17
|
+
buttonZoomOut: GAMEPAD_BUTTON.RightTrigger
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Adds gamepad support to Three.js `TrackballControls`.
|
|
21
|
+
*
|
|
22
|
+
* Call `update()` inside the render loop **before** `TrackballControls.update()`.
|
|
23
|
+
* Bindings and speed multipliers are configurable via {@link GamepadTrackballControlsOptions}.
|
|
24
|
+
*/
|
|
25
|
+
var GamepadTrackballControls = class extends GamepadControls {
|
|
26
|
+
#controls;
|
|
27
|
+
#options;
|
|
28
|
+
/**
|
|
29
|
+
* @param controls - A Three.js `TrackballControls` 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_TRACKBALL_OPTIONS,
|
|
38
|
+
...options
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Maps the current gamepad state to `TrackballControls` rotation, pan, and zoom.
|
|
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, buttonZoomIn, buttonZoomOut } = this.#options;
|
|
49
|
+
this.#queueRotation(deltaTime, gamepad, rotateSpeed, deadzone, axisRotateX, axisRotateY);
|
|
50
|
+
this.#queuePan(deltaTime, gamepad, panSpeed, deadzone, axisPanX, axisPanY);
|
|
51
|
+
this.#queueZoom(deltaTime, gamepad, zoomSpeed, deadzone, buttonZoomIn, buttonZoomOut);
|
|
52
|
+
}
|
|
53
|
+
#queueRotation(deltaTime, gamepad, rotateSpeed, deadzone, axisRotateX, axisRotateY) {
|
|
54
|
+
const controls = this.#controls;
|
|
55
|
+
if (controls.noRotate) {
|
|
56
|
+
controls._movePrev.copy(controls._moveCurr);
|
|
57
|
+
controls._lastAngle = 0;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const rotX = this.#applyDeadzone(gamepad.axes[axisRotateX] ?? 0, deadzone);
|
|
61
|
+
const rotY = this.#applyDeadzone(gamepad.axes[axisRotateY] ?? 0, deadzone);
|
|
62
|
+
if (rotX === 0 && rotY === 0) return;
|
|
63
|
+
const scale = rotateSpeed * deltaTime * Math.PI;
|
|
64
|
+
controls._moveCurr.x += rotX * scale;
|
|
65
|
+
controls._moveCurr.y += -rotY * scale;
|
|
66
|
+
}
|
|
67
|
+
#queuePan(deltaTime, gamepad, panSpeed, deadzone, axisPanX, axisPanY) {
|
|
68
|
+
const controls = this.#controls;
|
|
69
|
+
if (controls.noPan) {
|
|
70
|
+
controls._panStart.copy(controls._panEnd);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const panX = this.#applyDeadzone(gamepad.axes[axisPanX] ?? 0, deadzone);
|
|
74
|
+
const panY = this.#applyDeadzone(gamepad.axes[axisPanY] ?? 0, deadzone);
|
|
75
|
+
if (panX === 0 && panY === 0) return;
|
|
76
|
+
const scale = panSpeed * deltaTime;
|
|
77
|
+
controls._panEnd.x += panX * scale;
|
|
78
|
+
controls._panEnd.y += panY * scale;
|
|
79
|
+
}
|
|
80
|
+
#queueZoom(deltaTime, gamepad, zoomSpeed, deadzone, buttonZoomIn, buttonZoomOut) {
|
|
81
|
+
const controls = this.#controls;
|
|
82
|
+
if (controls.noZoom) {
|
|
83
|
+
controls._zoomStart.copy(controls._zoomEnd);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const triggerIn = gamepad.buttons[buttonZoomIn]?.value ?? 0;
|
|
87
|
+
const triggerOut = gamepad.buttons[buttonZoomOut]?.value ?? 0;
|
|
88
|
+
if (triggerIn <= deadzone && triggerOut <= deadzone) return;
|
|
89
|
+
controls._zoomEnd.y += (triggerOut - triggerIn) * zoomSpeed * deltaTime;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Returns `value` unchanged, or `0` if below the dead zone `threshold`.
|
|
93
|
+
*
|
|
94
|
+
* @param value - Raw axis or trigger value, typically in `[-1, 1]`.
|
|
95
|
+
* @param threshold - Dead zone size; values below this magnitude are zeroed.
|
|
96
|
+
*/
|
|
97
|
+
#applyDeadzone(value, threshold) {
|
|
98
|
+
return Math.abs(value) < threshold ? 0 : value;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
102
|
+
export { GamepadTrackballControls };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ import { GamepadFlyControls, GamepadFlyControlsOptions } from "./gamepad-fly-con
|
|
|
5
5
|
import { GamepadOrbitControls, GamepadOrbitControlsOptions } from "./gamepad-orbit-controls.js";
|
|
6
6
|
import { GamepadMapControls } from "./gamepad-map-controls.js";
|
|
7
7
|
import { GamepadPointerLockControls, GamepadPointerLockControlsOptions } from "./gamepad-pointer-lock-controls.js";
|
|
8
|
-
|
|
8
|
+
import { GamepadTrackballControls, GamepadTrackballControlsOptions } from "./gamepad-trackball-controls.js";
|
|
9
|
+
export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, GamepadControls, GamepadControlsEventMap, GamepadFirstPersonControls, GamepadFirstPersonControlsOptions, GamepadFlyControls, GamepadFlyControlsOptions, GamepadMapControls, GamepadOrbitControls, GamepadOrbitControlsOptions, GamepadPointerLockControls, GamepadPointerLockControlsOptions, GamepadTrackballControls, GamepadTrackballControlsOptions };
|
package/dist/index.js
CHANGED
|
@@ -5,4 +5,5 @@ import { GamepadFlyControls } from "./gamepad-fly-controls.js";
|
|
|
5
5
|
import { GamepadOrbitControls } from "./gamepad-orbit-controls.js";
|
|
6
6
|
import { GamepadMapControls } from "./gamepad-map-controls.js";
|
|
7
7
|
import { GamepadPointerLockControls } from "./gamepad-pointer-lock-controls.js";
|
|
8
|
-
|
|
8
|
+
import { GamepadTrackballControls } from "./gamepad-trackball-controls.js";
|
|
9
|
+
export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadControls, GamepadFirstPersonControls, GamepadFlyControls, GamepadMapControls, GamepadOrbitControls, GamepadPointerLockControls, GamepadTrackballControls };
|
package/package.json
CHANGED