@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
144 lines • 8.41 kB
JavaScript
import { __esDecorate, __runInitializers } from "../../tslib.es6.js";
import { serialize } from "../../Misc/decorators.js";
import { CameraInputTypes } from "../../Cameras/cameraInputsManager.js";
import { Matrix, Vector3, Vector2 } from "../../Maths/math.vector.pure.js";
import { Gamepad } from "../../Gamepads/gamepad.js";
/**
* Manage the gamepad inputs to control a free camera.
* @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
*/
let FreeCameraGamepadInput = (() => {
var _a;
let _gamepadAngularSensibility_decorators;
let _gamepadAngularSensibility_initializers = [];
let _gamepadAngularSensibility_extraInitializers = [];
let _gamepadMoveSensibility_decorators;
let _gamepadMoveSensibility_initializers = [];
let _gamepadMoveSensibility_extraInitializers = [];
return _a = class FreeCameraGamepadInput {
constructor() {
/**
* Defines the gamepad rotation sensibility.
* This is the threshold from when rotation starts to be accounted for to prevent jittering.
*/
this.gamepadAngularSensibility = __runInitializers(this, _gamepadAngularSensibility_initializers, 200);
/**
* Defines the gamepad move sensibility.
* This is the threshold from when moving starts to be accounted for for to prevent jittering.
*/
this.gamepadMoveSensibility = (__runInitializers(this, _gamepadAngularSensibility_extraInitializers), __runInitializers(this, _gamepadMoveSensibility_initializers, 40));
/**
* Defines the minimum value at which any analog stick input is ignored.
* Note: This value should only be a value between 0 and 1.
*/
this.deadzoneDelta = (__runInitializers(this, _gamepadMoveSensibility_extraInitializers), 0.1);
this._yAxisScale = 1.0;
this._cameraTransform = Matrix.Identity();
this._deltaTransform = Vector3.Zero();
this._vector3 = Vector3.Zero();
this._vector2 = Vector2.Zero();
}
/**
* Gets or sets a boolean indicating that Yaxis (for right stick) should be inverted
*/
get invertYAxis() {
return this._yAxisScale !== 1.0;
}
set invertYAxis(value) {
this._yAxisScale = value ? -1.0 : 1.0;
}
/**
* Attach the input controls to a specific dom element to get the input from.
*/
attachControl() {
const manager = this.camera.getScene().gamepadManager;
this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
if (gamepad.type !== Gamepad.POSE_ENABLED) {
// prioritize XBOX gamepads.
if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
this.gamepad = gamepad;
}
}
});
this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
if (this.gamepad === gamepad) {
this.gamepad = null;
}
});
// check if there are already other controllers connected
this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
// if no xbox controller was found, but there are gamepad controllers, take the first one
if (!this.gamepad && manager.gamepads.length) {
this.gamepad = manager.gamepads[0];
}
}
/**
* Detach the current controls from the specified dom element.
*/
detachControl() {
this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
this.gamepad = null;
}
/**
* Update the current camera state depending on the inputs that have been used this frame.
* This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
*/
checkInputs() {
if (this.gamepad && this.gamepad.leftStick) {
const camera = this.camera;
const lsValues = this.gamepad.leftStick;
if (this.gamepadMoveSensibility !== 0) {
lsValues.x = Math.abs(lsValues.x) > this.deadzoneDelta ? lsValues.x / this.gamepadMoveSensibility : 0;
lsValues.y = Math.abs(lsValues.y) > this.deadzoneDelta ? lsValues.y / this.gamepadMoveSensibility : 0;
}
let rsValues = this.gamepad.rightStick;
if (rsValues && this.gamepadAngularSensibility !== 0) {
rsValues.x = Math.abs(rsValues.x) > this.deadzoneDelta ? rsValues.x / this.gamepadAngularSensibility : 0;
rsValues.y = (Math.abs(rsValues.y) > this.deadzoneDelta ? rsValues.y / this.gamepadAngularSensibility : 0) * this._yAxisScale;
}
else {
rsValues = { x: 0, y: 0 };
}
if (!camera.rotationQuaternion) {
Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, this._cameraTransform);
}
else {
camera.rotationQuaternion.toRotationMatrix(this._cameraTransform);
}
const speed = camera._computeLocalCameraSpeed() * 50.0;
this._vector3.copyFromFloats(lsValues.x * speed, 0, -lsValues.y * speed);
Vector3.TransformCoordinatesToRef(this._vector3, this._cameraTransform, this._deltaTransform);
camera.cameraDirection.addInPlace(this._deltaTransform);
this._vector2.copyFromFloats(rsValues.y, rsValues.x);
camera.cameraRotation.addInPlace(this._vector2);
}
}
/**
* Gets the class name of the current input.
* @returns the class name
*/
getClassName() {
return "FreeCameraGamepadInput";
}
/**
* Get the friendly name associated with the input class.
* @returns the input friendly name
*/
getSimpleName() {
return "gamepad";
}
},
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
_gamepadAngularSensibility_decorators = [serialize()];
_gamepadMoveSensibility_decorators = [serialize()];
__esDecorate(null, null, _gamepadAngularSensibility_decorators, { kind: "field", name: "gamepadAngularSensibility", static: false, private: false, access: { has: obj => "gamepadAngularSensibility" in obj, get: obj => obj.gamepadAngularSensibility, set: (obj, value) => { obj.gamepadAngularSensibility = value; } }, metadata: _metadata }, _gamepadAngularSensibility_initializers, _gamepadAngularSensibility_extraInitializers);
__esDecorate(null, null, _gamepadMoveSensibility_decorators, { kind: "field", name: "gamepadMoveSensibility", static: false, private: false, access: { has: obj => "gamepadMoveSensibility" in obj, get: obj => obj.gamepadMoveSensibility, set: (obj, value) => { obj.gamepadMoveSensibility = value; } }, metadata: _metadata }, _gamepadMoveSensibility_initializers, _gamepadMoveSensibility_extraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
})(),
_a;
})();
export { FreeCameraGamepadInput };
CameraInputTypes["FreeCameraGamepadInput"] = FreeCameraGamepadInput;
//# sourceMappingURL=freeCameraGamepadInput.js.map