three-gamepad-controls 0.3.1 → 0.4.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-fly-controls.d.ts +91 -0
- package/dist/gamepad-fly-controls.js +83 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,5 +38,6 @@ bun add three-gamepad-controls
|
|
|
38
38
|
|
|
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
|
+
- [GamepadFlyControls](./docs/gamepad-fly-controls.md) — Gamepad support for `FlyControls`.
|
|
41
42
|
- [GamepadOrbitControls](./docs/gamepad-orbit-controls.md) — Gamepad support for `OrbitControls`.
|
|
42
43
|
- [GamepadPointerLockControls](./docs/gamepad-pointer-lock-controls.md) — Gamepad support for `PointerLockControls`.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { GamepadControls } from "./gamepad-controls.js";
|
|
2
|
+
import { FlyControls } from "three/addons/controls/FlyControls.js";
|
|
3
|
+
|
|
4
|
+
//#region src/gamepad-fly-controls.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for {@link GamepadFlyControls}.
|
|
7
|
+
*
|
|
8
|
+
* Every property has a sensible default, so you only need to pass the properties you want to override.
|
|
9
|
+
*/
|
|
10
|
+
type GamepadFlyControlsOptions = {
|
|
11
|
+
/**
|
|
12
|
+
* Multiplier on `FlyControls.movementSpeed` for translation.
|
|
13
|
+
* @default 1.0
|
|
14
|
+
*/
|
|
15
|
+
moveSpeed: number;
|
|
16
|
+
/**
|
|
17
|
+
* Multiplier on `FlyControls.rollSpeed` for rotation.
|
|
18
|
+
* @default 1.0
|
|
19
|
+
*/
|
|
20
|
+
rotateSpeed: 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
|
+
* Button index for **rolling left**.
|
|
48
|
+
* @default 4 — Left shoulder
|
|
49
|
+
*/
|
|
50
|
+
buttonRollLeft: number;
|
|
51
|
+
/**
|
|
52
|
+
* Button index for **rolling right**.
|
|
53
|
+
* @default 5 — Right shoulder
|
|
54
|
+
*/
|
|
55
|
+
buttonRollRight: number;
|
|
56
|
+
/**
|
|
57
|
+
* Button index for **moving up** (analog trigger value used for proportional speed).
|
|
58
|
+
* @default 6 — Left trigger
|
|
59
|
+
*/
|
|
60
|
+
buttonMoveUp: number;
|
|
61
|
+
/**
|
|
62
|
+
* Button index for **moving down** (analog trigger value used for proportional speed).
|
|
63
|
+
* @default 7 — Right trigger
|
|
64
|
+
*/
|
|
65
|
+
buttonMoveDown: number;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Adds full 6DOF gamepad support to Three.js `FlyControls`.
|
|
69
|
+
*
|
|
70
|
+
* Gamepad input is additive with keyboard/mouse input — call both
|
|
71
|
+
* `gamepadControls.update(delta)` and `controls.update(delta)` each frame.
|
|
72
|
+
* Bindings and speeds are configurable via {@link GamepadFlyControlsOptions}.
|
|
73
|
+
*/
|
|
74
|
+
declare class GamepadFlyControls extends GamepadControls {
|
|
75
|
+
#private;
|
|
76
|
+
/**
|
|
77
|
+
* @param controls - A Three.js `FlyControls` instance.
|
|
78
|
+
* @param options - Optional overrides for the default behavior.
|
|
79
|
+
* Any property not provided falls back to its default value.
|
|
80
|
+
*/
|
|
81
|
+
constructor(controls: FlyControls, options?: Partial<GamepadFlyControlsOptions>);
|
|
82
|
+
/**
|
|
83
|
+
* Maps the current gamepad state to `FlyControls` translation and rotation.
|
|
84
|
+
*
|
|
85
|
+
* @param deltaTime - Seconds since the last frame.
|
|
86
|
+
* @param gamepad - Fresh gamepad snapshot provided by the base class.
|
|
87
|
+
*/
|
|
88
|
+
protected onUpdate(deltaTime: number, gamepad: Gamepad): void;
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
export { GamepadFlyControls, GamepadFlyControlsOptions };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
|
|
2
|
+
import { GamepadControls } from "./gamepad-controls.js";
|
|
3
|
+
import { Quaternion } from "three";
|
|
4
|
+
//#region src/gamepad-fly-controls.ts
|
|
5
|
+
/**
|
|
6
|
+
* Default options merged in the constructor when no explicit configuration is provided.
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_FLY_OPTIONS = {
|
|
9
|
+
moveSpeed: 1,
|
|
10
|
+
rotateSpeed: 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
|
+
buttonRollLeft: GAMEPAD_BUTTON.LeftShoulder,
|
|
17
|
+
buttonRollRight: GAMEPAD_BUTTON.RightShoulder,
|
|
18
|
+
buttonMoveUp: GAMEPAD_BUTTON.LeftTrigger,
|
|
19
|
+
buttonMoveDown: GAMEPAD_BUTTON.RightTrigger
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Adds full 6DOF gamepad support to Three.js `FlyControls`.
|
|
23
|
+
*
|
|
24
|
+
* Gamepad input is additive with keyboard/mouse input — call both
|
|
25
|
+
* `gamepadControls.update(delta)` and `controls.update(delta)` each frame.
|
|
26
|
+
* Bindings and speeds are configurable via {@link GamepadFlyControlsOptions}.
|
|
27
|
+
*/
|
|
28
|
+
var GamepadFlyControls = class extends GamepadControls {
|
|
29
|
+
#controls;
|
|
30
|
+
#options;
|
|
31
|
+
#tmpQuaternion;
|
|
32
|
+
/**
|
|
33
|
+
* @param controls - A Three.js `FlyControls` instance.
|
|
34
|
+
* @param options - Optional overrides for the default behavior.
|
|
35
|
+
* Any property not provided falls back to its default value.
|
|
36
|
+
*/
|
|
37
|
+
constructor(controls, options) {
|
|
38
|
+
super();
|
|
39
|
+
this.#controls = controls;
|
|
40
|
+
this.#options = {
|
|
41
|
+
...DEFAULT_FLY_OPTIONS,
|
|
42
|
+
...options
|
|
43
|
+
};
|
|
44
|
+
this.#tmpQuaternion = new Quaternion();
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Maps the current gamepad state to `FlyControls` translation and rotation.
|
|
48
|
+
*
|
|
49
|
+
* @param deltaTime - Seconds since the last frame.
|
|
50
|
+
* @param gamepad - Fresh gamepad snapshot provided by the base class.
|
|
51
|
+
*/
|
|
52
|
+
onUpdate(deltaTime, gamepad) {
|
|
53
|
+
const { moveSpeed, rotateSpeed, deadzone, axisMoveForward, axisMoveRight, axisLookX, axisLookY, buttonRollLeft, buttonRollRight, buttonMoveUp, buttonMoveDown } = this.#options;
|
|
54
|
+
const moveMult = deltaTime * this.#controls.movementSpeed * moveSpeed;
|
|
55
|
+
const fwd = this.#applyDeadzone(gamepad.axes[axisMoveForward] ?? 0, deadzone);
|
|
56
|
+
if (fwd !== 0) this.#controls.object.translateZ(fwd * moveMult);
|
|
57
|
+
const strafe = this.#applyDeadzone(gamepad.axes[axisMoveRight] ?? 0, deadzone);
|
|
58
|
+
if (strafe !== 0) this.#controls.object.translateX(strafe * moveMult);
|
|
59
|
+
const up = gamepad.buttons[buttonMoveUp]?.value ?? 0;
|
|
60
|
+
const down = gamepad.buttons[buttonMoveDown]?.value ?? 0;
|
|
61
|
+
if (up > deadzone) this.#controls.object.translateY(up * moveMult);
|
|
62
|
+
if (down > deadzone) this.#controls.object.translateY(-down * moveMult);
|
|
63
|
+
const rotMult = deltaTime * this.#controls.rollSpeed * rotateSpeed;
|
|
64
|
+
const pitch = -this.#applyDeadzone(gamepad.axes[axisLookY] ?? 0, deadzone);
|
|
65
|
+
const yaw = -this.#applyDeadzone(gamepad.axes[axisLookX] ?? 0, deadzone);
|
|
66
|
+
const roll = (gamepad.buttons[buttonRollLeft]?.pressed ? 1 : 0) - (gamepad.buttons[buttonRollRight]?.pressed ? 1 : 0);
|
|
67
|
+
if (pitch !== 0 || yaw !== 0 || roll !== 0) {
|
|
68
|
+
this.#tmpQuaternion.set(pitch * rotMult, yaw * rotMult, roll * rotMult, 1).normalize();
|
|
69
|
+
this.#controls.object.quaternion.multiply(this.#tmpQuaternion);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Returns `value` unchanged, or `0` if below the dead zone `threshold`.
|
|
74
|
+
*
|
|
75
|
+
* @param value - Raw axis or trigger value, typically in `[-1, 1]`.
|
|
76
|
+
* @param threshold - Dead zone size; values below this magnitude are zeroed.
|
|
77
|
+
*/
|
|
78
|
+
#applyDeadzone(value, threshold) {
|
|
79
|
+
return Math.abs(value) < threshold ? 0 : value;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
//#endregion
|
|
83
|
+
export { GamepadFlyControls };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue } from "./core.js";
|
|
2
2
|
import { GamepadControls, GamepadControlsEventMap } from "./gamepad-controls.js";
|
|
3
|
+
import { GamepadFlyControls, GamepadFlyControlsOptions } from "./gamepad-fly-controls.js";
|
|
3
4
|
import { GamepadOrbitControls, GamepadOrbitControlsOptions } from "./gamepad-orbit-controls.js";
|
|
4
5
|
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 };
|
|
6
|
+
export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadAxisKey, GamepadAxisValue, GamepadButtonKey, GamepadButtonValue, GamepadControls, GamepadControlsEventMap, GamepadFlyControls, GamepadFlyControlsOptions, GamepadOrbitControls, GamepadOrbitControlsOptions, GamepadPointerLockControls, GamepadPointerLockControlsOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { GAMEPAD_AXIS, GAMEPAD_BUTTON } from "./core.js";
|
|
2
2
|
import { GamepadControls } from "./gamepad-controls.js";
|
|
3
|
+
import { GamepadFlyControls } from "./gamepad-fly-controls.js";
|
|
3
4
|
import { GamepadOrbitControls } from "./gamepad-orbit-controls.js";
|
|
4
5
|
import { GamepadPointerLockControls } from "./gamepad-pointer-lock-controls.js";
|
|
5
|
-
export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadControls, GamepadOrbitControls, GamepadPointerLockControls };
|
|
6
|
+
export { GAMEPAD_AXIS, GAMEPAD_BUTTON, GamepadControls, GamepadFlyControls, GamepadOrbitControls, GamepadPointerLockControls };
|
package/package.json
CHANGED