@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
863 lines (859 loc) • 139 kB
JavaScript
import { l as __runInitializers, aY as Tools, cd as KeyboardEventTypes, V as Vector3, q as __esDecorate, s as serialize, ce as CameraInputTypes, O as Observable, by as PointerEventTypes, cf as EventConstants, M as Matrix, T as TmpVectors, cg as CameraInputsManager, aE as _MissingSideEffect, A as AbstractEngine, b3 as Vector2, ch as TargetCamera, r as serializeAsVector3, a as EngineStore, o as SerializationHelper, G as GetClass, a7 as VertexBuffer, m as Texture, C as Constants, Q as SmartArray, y as Logger } from './index-MZPybX0H.esm.js';
/**
* Manage the keyboard inputs to control the movement of a free camera.
* @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
*/
let FreeCameraKeyboardMoveInput = (() => {
var _a;
let _keysUp_decorators;
let _keysUp_initializers = [];
let _keysUp_extraInitializers = [];
let _keysUpward_decorators;
let _keysUpward_initializers = [];
let _keysUpward_extraInitializers = [];
let _keysDown_decorators;
let _keysDown_initializers = [];
let _keysDown_extraInitializers = [];
let _keysDownward_decorators;
let _keysDownward_initializers = [];
let _keysDownward_extraInitializers = [];
let _keysLeft_decorators;
let _keysLeft_initializers = [];
let _keysLeft_extraInitializers = [];
let _keysRight_decorators;
let _keysRight_initializers = [];
let _keysRight_extraInitializers = [];
let _rotationSpeed_decorators;
let _rotationSpeed_initializers = [];
let _rotationSpeed_extraInitializers = [];
let _keysRotateLeft_decorators;
let _keysRotateLeft_initializers = [];
let _keysRotateLeft_extraInitializers = [];
let _keysRotateRight_decorators;
let _keysRotateRight_initializers = [];
let _keysRotateRight_extraInitializers = [];
let _keysRotateUp_decorators;
let _keysRotateUp_initializers = [];
let _keysRotateUp_extraInitializers = [];
let _keysRotateDown_decorators;
let _keysRotateDown_initializers = [];
let _keysRotateDown_extraInitializers = [];
return _a = class FreeCameraKeyboardMoveInput {
constructor() {
/**
* Gets or Set the list of keyboard keys used to control the forward move of the camera.
*/
this.keysUp = __runInitializers(this, _keysUp_initializers, [38]);
/**
* Gets or Set the list of keyboard keys used to control the upward move of the camera.
*/
this.keysUpward = (__runInitializers(this, _keysUp_extraInitializers), __runInitializers(this, _keysUpward_initializers, [33]));
/**
* Gets or Set the list of keyboard keys used to control the backward move of the camera.
*/
this.keysDown = (__runInitializers(this, _keysUpward_extraInitializers), __runInitializers(this, _keysDown_initializers, [40]));
/**
* Gets or Set the list of keyboard keys used to control the downward move of the camera.
*/
this.keysDownward = (__runInitializers(this, _keysDown_extraInitializers), __runInitializers(this, _keysDownward_initializers, [34]));
/**
* Gets or Set the list of keyboard keys used to control the left strafe move of the camera.
*/
this.keysLeft = (__runInitializers(this, _keysDownward_extraInitializers), __runInitializers(this, _keysLeft_initializers, [37]));
/**
* Gets or Set the list of keyboard keys used to control the right strafe move of the camera.
*/
this.keysRight = (__runInitializers(this, _keysLeft_extraInitializers), __runInitializers(this, _keysRight_initializers, [39]));
/**
* Defines the pointer angular sensibility along the X and Y axis or how fast is the camera rotating.
*/
this.rotationSpeed = (__runInitializers(this, _keysRight_extraInitializers), __runInitializers(this, _rotationSpeed_initializers, 0.5));
/**
* Gets or Set the list of keyboard keys used to control the left rotation move of the camera.
*/
this.keysRotateLeft = (__runInitializers(this, _rotationSpeed_extraInitializers), __runInitializers(this, _keysRotateLeft_initializers, []));
/**
* Gets or Set the list of keyboard keys used to control the right rotation move of the camera.
*/
this.keysRotateRight = (__runInitializers(this, _keysRotateLeft_extraInitializers), __runInitializers(this, _keysRotateRight_initializers, []));
/**
* Gets or Set the list of keyboard keys used to control the up rotation move of the camera.
*/
this.keysRotateUp = (__runInitializers(this, _keysRotateRight_extraInitializers), __runInitializers(this, _keysRotateUp_initializers, []));
/**
* Gets or Set the list of keyboard keys used to control the down rotation move of the camera.
*/
this.keysRotateDown = (__runInitializers(this, _keysRotateUp_extraInitializers), __runInitializers(this, _keysRotateDown_initializers, []));
this._keys = (__runInitializers(this, _keysRotateDown_extraInitializers), new Array());
}
/**
* Attach the input controls to a specific dom element to get the input from.
* @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
*/
attachControl(noPreventDefault) {
noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
if (this._onCanvasBlurObserver) {
return;
}
this._scene = this.camera.getScene();
this._engine = this._scene.getEngine();
this._onCanvasBlurObserver = this._engine.onCanvasBlurObservable.add(() => {
this._keys.length = 0;
});
this._onKeyboardObserver = this._scene.onKeyboardObservable.add((info) => {
const evt = info.event;
if (!evt.metaKey) {
if (info.type === KeyboardEventTypes.KEYDOWN) {
if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
this.keysDown.indexOf(evt.keyCode) !== -1 ||
this.keysLeft.indexOf(evt.keyCode) !== -1 ||
this.keysRight.indexOf(evt.keyCode) !== -1 ||
this.keysUpward.indexOf(evt.keyCode) !== -1 ||
this.keysDownward.indexOf(evt.keyCode) !== -1 ||
this.keysRotateLeft.indexOf(evt.keyCode) !== -1 ||
this.keysRotateRight.indexOf(evt.keyCode) !== -1 ||
this.keysRotateUp.indexOf(evt.keyCode) !== -1 ||
this.keysRotateDown.indexOf(evt.keyCode) !== -1) {
const index = this._keys.indexOf(evt.keyCode);
if (index === -1) {
this._keys.push(evt.keyCode);
}
if (!noPreventDefault) {
evt.preventDefault();
}
}
}
else {
if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
this.keysDown.indexOf(evt.keyCode) !== -1 ||
this.keysLeft.indexOf(evt.keyCode) !== -1 ||
this.keysRight.indexOf(evt.keyCode) !== -1 ||
this.keysUpward.indexOf(evt.keyCode) !== -1 ||
this.keysDownward.indexOf(evt.keyCode) !== -1 ||
this.keysRotateLeft.indexOf(evt.keyCode) !== -1 ||
this.keysRotateRight.indexOf(evt.keyCode) !== -1 ||
this.keysRotateUp.indexOf(evt.keyCode) !== -1 ||
this.keysRotateDown.indexOf(evt.keyCode) !== -1) {
const index = this._keys.indexOf(evt.keyCode);
if (index >= 0) {
this._keys.splice(index, 1);
}
if (!noPreventDefault) {
evt.preventDefault();
}
}
}
}
});
}
/**
* Detach the current controls from the specified dom element.
*/
detachControl() {
if (this._scene) {
if (this._onKeyboardObserver) {
this._scene.onKeyboardObservable.remove(this._onKeyboardObserver);
}
if (this._onCanvasBlurObserver) {
this._engine.onCanvasBlurObservable.remove(this._onCanvasBlurObserver);
}
this._onKeyboardObserver = null;
this._onCanvasBlurObserver = null;
}
this._keys.length = 0;
}
/**
* 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._onKeyboardObserver) {
const camera = this.camera;
// Movement keys are gated on the keyboard→translate mapping and rotation keys on
// keyboard→rotate. Removing the corresponding entry disables that family of keys.
// An entry's optional `sensitivity` acts as a gain (default 1) over the legacy scaling.
const input = camera.movement.input;
const translateEntry = input.getEntry("keyboard", "translate");
const rotateEntry = input.getEntry("keyboard", "rotate");
const rotateGain = rotateEntry?.sensitivity ?? 1;
// Movement keys are accumulated into a single local direction and applied once below, so that
// holding two directions at once (e.g. forward + left) moves along a normalized diagonal at the
// same speed as a single direction, instead of the ~1.41x boost (sqrt(2)) that results from
// applying each movement key independently. Rotation keys act on separate angular axes and are
// applied directly as before.
const localDirection = camera._localDirection.copyFromFloats(0, 0, 0);
// Keyboard
for (let index = 0; index < this._keys.length; index++) {
const keyCode = this._keys[index];
if (this.keysLeft.indexOf(keyCode) !== -1) {
localDirection.x -= 1;
}
else if (this.keysUp.indexOf(keyCode) !== -1) {
localDirection.z += 1;
}
else if (this.keysRight.indexOf(keyCode) !== -1) {
localDirection.x += 1;
}
else if (this.keysDown.indexOf(keyCode) !== -1) {
localDirection.z -= 1;
}
else if (this.keysUpward.indexOf(keyCode) !== -1) {
localDirection.y += 1;
}
else if (this.keysDownward.indexOf(keyCode) !== -1) {
localDirection.y -= 1;
}
else if (this.keysRotateLeft.indexOf(keyCode) !== -1) {
if (rotateEntry) {
camera.cameraRotation.y -= this._getLocalRotation() * rotateGain;
}
}
else if (this.keysRotateRight.indexOf(keyCode) !== -1) {
if (rotateEntry) {
camera.cameraRotation.y += this._getLocalRotation() * rotateGain;
}
}
else if (this.keysRotateUp.indexOf(keyCode) !== -1) {
if (rotateEntry) {
camera.cameraRotation.x -= this._getLocalRotation() * rotateGain;
}
}
else if (this.keysRotateDown.indexOf(keyCode) !== -1) {
if (rotateEntry) {
camera.cameraRotation.x += this._getLocalRotation() * rotateGain;
}
}
}
// Apply a single, normalized movement once all keys for this frame have been accumulated.
// Translation is suppressed when the keyboard→translate mapping is removed.
if (translateEntry && (localDirection.x !== 0 || localDirection.y !== 0 || localDirection.z !== 0)) {
const speed = camera._computeLocalCameraSpeed() * (translateEntry.sensitivity ?? 1);
// Normalize so a diagonal isn't faster than an axis-aligned move, then scale to the per-frame speed.
localDirection.normalize().scaleInPlace(speed);
if (camera.getScene().useRightHandedSystem) {
localDirection.z *= -1;
}
camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
Vector3.TransformNormalToRef(localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
camera.cameraDirection.addInPlace(camera._transformedDirection);
}
}
}
/**
* Gets the class name of the current input.
* @returns the class name
*/
getClassName() {
return "FreeCameraKeyboardMoveInput";
}
/** @internal */
_onLostFocus() {
this._keys.length = 0;
}
/**
* Get the friendly name associated with the input class.
* @returns the input friendly name
*/
getSimpleName() {
return "keyboard";
}
_getLocalRotation() {
const handednessMultiplier = this.camera._calculateHandednessMultiplier();
const rotation = ((this.rotationSpeed * this._engine.getDeltaTime()) / 1000) * handednessMultiplier;
return rotation;
}
},
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
_keysUp_decorators = [serialize()];
_keysUpward_decorators = [serialize()];
_keysDown_decorators = [serialize()];
_keysDownward_decorators = [serialize()];
_keysLeft_decorators = [serialize()];
_keysRight_decorators = [serialize()];
_rotationSpeed_decorators = [serialize()];
_keysRotateLeft_decorators = [serialize()];
_keysRotateRight_decorators = [serialize()];
_keysRotateUp_decorators = [serialize()];
_keysRotateDown_decorators = [serialize()];
__esDecorate(null, null, _keysUp_decorators, { kind: "field", name: "keysUp", static: false, private: false, access: { has: obj => "keysUp" in obj, get: obj => obj.keysUp, set: (obj, value) => { obj.keysUp = value; } }, metadata: _metadata }, _keysUp_initializers, _keysUp_extraInitializers);
__esDecorate(null, null, _keysUpward_decorators, { kind: "field", name: "keysUpward", static: false, private: false, access: { has: obj => "keysUpward" in obj, get: obj => obj.keysUpward, set: (obj, value) => { obj.keysUpward = value; } }, metadata: _metadata }, _keysUpward_initializers, _keysUpward_extraInitializers);
__esDecorate(null, null, _keysDown_decorators, { kind: "field", name: "keysDown", static: false, private: false, access: { has: obj => "keysDown" in obj, get: obj => obj.keysDown, set: (obj, value) => { obj.keysDown = value; } }, metadata: _metadata }, _keysDown_initializers, _keysDown_extraInitializers);
__esDecorate(null, null, _keysDownward_decorators, { kind: "field", name: "keysDownward", static: false, private: false, access: { has: obj => "keysDownward" in obj, get: obj => obj.keysDownward, set: (obj, value) => { obj.keysDownward = value; } }, metadata: _metadata }, _keysDownward_initializers, _keysDownward_extraInitializers);
__esDecorate(null, null, _keysLeft_decorators, { kind: "field", name: "keysLeft", static: false, private: false, access: { has: obj => "keysLeft" in obj, get: obj => obj.keysLeft, set: (obj, value) => { obj.keysLeft = value; } }, metadata: _metadata }, _keysLeft_initializers, _keysLeft_extraInitializers);
__esDecorate(null, null, _keysRight_decorators, { kind: "field", name: "keysRight", static: false, private: false, access: { has: obj => "keysRight" in obj, get: obj => obj.keysRight, set: (obj, value) => { obj.keysRight = value; } }, metadata: _metadata }, _keysRight_initializers, _keysRight_extraInitializers);
__esDecorate(null, null, _rotationSpeed_decorators, { kind: "field", name: "rotationSpeed", static: false, private: false, access: { has: obj => "rotationSpeed" in obj, get: obj => obj.rotationSpeed, set: (obj, value) => { obj.rotationSpeed = value; } }, metadata: _metadata }, _rotationSpeed_initializers, _rotationSpeed_extraInitializers);
__esDecorate(null, null, _keysRotateLeft_decorators, { kind: "field", name: "keysRotateLeft", static: false, private: false, access: { has: obj => "keysRotateLeft" in obj, get: obj => obj.keysRotateLeft, set: (obj, value) => { obj.keysRotateLeft = value; } }, metadata: _metadata }, _keysRotateLeft_initializers, _keysRotateLeft_extraInitializers);
__esDecorate(null, null, _keysRotateRight_decorators, { kind: "field", name: "keysRotateRight", static: false, private: false, access: { has: obj => "keysRotateRight" in obj, get: obj => obj.keysRotateRight, set: (obj, value) => { obj.keysRotateRight = value; } }, metadata: _metadata }, _keysRotateRight_initializers, _keysRotateRight_extraInitializers);
__esDecorate(null, null, _keysRotateUp_decorators, { kind: "field", name: "keysRotateUp", static: false, private: false, access: { has: obj => "keysRotateUp" in obj, get: obj => obj.keysRotateUp, set: (obj, value) => { obj.keysRotateUp = value; } }, metadata: _metadata }, _keysRotateUp_initializers, _keysRotateUp_extraInitializers);
__esDecorate(null, null, _keysRotateDown_decorators, { kind: "field", name: "keysRotateDown", static: false, private: false, access: { has: obj => "keysRotateDown" in obj, get: obj => obj.keysRotateDown, set: (obj, value) => { obj.keysRotateDown = value; } }, metadata: _metadata }, _keysRotateDown_initializers, _keysRotateDown_extraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
})(),
_a;
})();
CameraInputTypes["FreeCameraKeyboardMoveInput"] = FreeCameraKeyboardMoveInput;
/**
* Manage the mouse inputs to control the movement of a free camera.
* @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
*/
let FreeCameraMouseInput = (() => {
var _a;
let _buttons_decorators;
let _buttons_initializers = [];
let _buttons_extraInitializers = [];
let _angularSensibility_decorators;
let _angularSensibility_initializers = [];
let _angularSensibility_extraInitializers = [];
return _a = class FreeCameraMouseInput {
/**
* Applies a pointer-drag delta as camera rotation, but only if the camera's configurable
* input map resolves the current pointer interaction to "rotate". The map is consulted with
* the currently active mouse button so consumers can remap or disable pointer-driven rotation.
* The applied scale comes from the resolved entry's `sensitivity`/`sensitivityX`/`sensitivityY`,
* falling back to the legacy `angularSensibility` for backward compatibility. The rotation is
* still written to `camera.cameraRotation` (not the movement accumulators) so existing code that
* reads `cameraRotation` immediately after a pointer event keeps working.
* @param offsetX Horizontal pointer delta (already handedness-adjusted).
* @param offsetY Vertical pointer delta (already handedness-adjusted).
*/
_applyPointerRotation(offsetX, offsetY) {
this._pointerConditions.button = this._currentActiveButton;
const entry = this.camera.movement.input.resolveInteraction("pointer", this._pointerConditions);
if (!entry || entry.interaction !== "rotate") {
return;
}
const sensitivityX = entry.sensitivityX ?? entry.sensitivity ?? 1 / this.angularSensibility;
const sensitivityY = entry.sensitivityY ?? entry.sensitivity ?? 1 / this.angularSensibility;
this.camera.cameraRotation.y += offsetX * sensitivityX;
this.camera.cameraRotation.x += offsetY * sensitivityY;
}
/**
* Manage the mouse inputs to control the movement of a free camera.
* @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
* @param touchEnabled Defines if touch is enabled or not
*/
constructor(
/**
* [true] Define if touch is enabled in the mouse input
*/
touchEnabled = true) {
this.touchEnabled = touchEnabled;
/**
* Defines the buttons associated with the input to handle camera move.
*/
this.buttons = __runInitializers(this, _buttons_initializers, [0, 1, 2]);
/**
* Defines the pointer angular sensibility along the X and Y axis or how fast is the camera rotating.
*/
this.angularSensibility = (__runInitializers(this, _buttons_extraInitializers), __runInitializers(this, _angularSensibility_initializers, 2000.0));
this._pointerInput = __runInitializers(this, _angularSensibility_extraInitializers);
this._previousPosition = null;
/**
* Observable for when a pointer move event occurs containing the move offset
*/
this.onPointerMovedObservable = new Observable();
/**
* @internal
* If the camera should be rotated automatically based on pointer movement
*/
this._allowCameraRotation = true;
this._currentActiveButton = -1;
this._activePointerId = -1;
/** Reused conditions object for `resolveInteraction` to avoid per-move allocations. */
this._pointerConditions = {};
}
/**
* Attach the input controls to a specific dom element to get the input from.
* @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
*/
attachControl(noPreventDefault) {
noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
const engine = this.camera.getEngine();
const element = engine.getInputElement();
if (!this._pointerInput) {
this._pointerInput = (p) => {
const evt = p.event;
const isTouch = evt.pointerType === "touch";
if (!this.touchEnabled && isTouch) {
return;
}
if (p.type !== PointerEventTypes.POINTERMOVE && this.buttons.indexOf(evt.button) === -1) {
return;
}
const srcElement = evt.target;
if (p.type === PointerEventTypes.POINTERDOWN) {
// If the input is touch with more than one touch OR if the input is mouse and there is already an active button, return
if ((isTouch && this._activePointerId !== -1) || (!isTouch && this._currentActiveButton !== -1)) {
return;
}
this._activePointerId = evt.pointerId;
try {
srcElement?.setPointerCapture(evt.pointerId);
}
catch (e) {
//Nothing to do with the error. Execution will continue.
}
if (this._currentActiveButton === -1) {
this._currentActiveButton = evt.button;
}
this._previousPosition = {
x: evt.clientX,
y: evt.clientY,
};
if (!noPreventDefault) {
evt.preventDefault();
if (element) {
element.focus();
}
}
// This is required to move while pointer button is down
if (engine.isPointerLock && this._onMouseMove) {
this._onMouseMove(p.event);
}
}
else if (p.type === PointerEventTypes.POINTERUP) {
// If input is touch with a different touch id OR if input is mouse with a different button, return
if ((isTouch && this._activePointerId !== evt.pointerId) || (!isTouch && this._currentActiveButton !== evt.button)) {
return;
}
try {
srcElement?.releasePointerCapture(evt.pointerId);
}
catch (e) {
//Nothing to do with the error.
}
this._currentActiveButton = -1;
this._previousPosition = null;
if (!noPreventDefault) {
evt.preventDefault();
}
this._activePointerId = -1;
}
else if (p.type === PointerEventTypes.POINTERMOVE && (this._activePointerId === evt.pointerId || !isTouch)) {
if (engine.isPointerLock && this._onMouseMove) {
this._onMouseMove(p.event);
}
else if (this._previousPosition) {
const handednessMultiplier = this.camera._calculateHandednessMultiplier();
const offsetX = (evt.clientX - this._previousPosition.x) * handednessMultiplier;
const offsetY = (evt.clientY - this._previousPosition.y) * handednessMultiplier;
if (this._allowCameraRotation) {
this._applyPointerRotation(offsetX, offsetY);
}
this.onPointerMovedObservable.notifyObservers({ offsetX: offsetX, offsetY: offsetY });
this._previousPosition = {
x: evt.clientX,
y: evt.clientY,
};
if (!noPreventDefault) {
evt.preventDefault();
}
}
}
};
}
this._onMouseMove = (evt) => {
if (!engine.isPointerLock) {
return;
}
const handednessMultiplier = this.camera._calculateHandednessMultiplier();
this._applyPointerRotation(evt.movementX * handednessMultiplier, evt.movementY * handednessMultiplier);
this._previousPosition = null;
if (!noPreventDefault) {
evt.preventDefault();
}
};
this._observer = this.camera
.getScene()
._inputManager._addCameraPointerObserver(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);
if (element) {
this._contextMenuBind = (evt) => this.onContextMenu(evt);
element.addEventListener("contextmenu", this._contextMenuBind, false); // TODO: We need to figure out how to handle this for Native
}
}
/**
* Called on JS contextmenu event.
* Override this method to provide functionality.
* @param evt the context menu event
*/
onContextMenu(evt) {
evt.preventDefault();
}
/**
* Detach the current controls from the specified dom element.
*/
detachControl() {
if (this._observer) {
this.camera.getScene()._inputManager._removeCameraPointerObserver(this._observer);
if (this._contextMenuBind) {
const engine = this.camera.getEngine();
const element = engine.getInputElement();
if (element) {
element.removeEventListener("contextmenu", this._contextMenuBind);
}
}
if (this.onPointerMovedObservable) {
this.onPointerMovedObservable.clear();
}
this._observer = null;
this._onMouseMove = null;
this._previousPosition = null;
}
this._activePointerId = -1;
this._currentActiveButton = -1;
}
/**
* Gets the class name of the current input.
* @returns the class name
*/
getClassName() {
return "FreeCameraMouseInput";
}
/**
* Get the friendly name associated with the input class.
* @returns the input friendly name
*/
getSimpleName() {
return "mouse";
}
},
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
_buttons_decorators = [serialize()];
_angularSensibility_decorators = [serialize()];
__esDecorate(null, null, _buttons_decorators, { kind: "field", name: "buttons", static: false, private: false, access: { has: obj => "buttons" in obj, get: obj => obj.buttons, set: (obj, value) => { obj.buttons = value; } }, metadata: _metadata }, _buttons_initializers, _buttons_extraInitializers);
__esDecorate(null, null, _angularSensibility_decorators, { kind: "field", name: "angularSensibility", static: false, private: false, access: { has: obj => "angularSensibility" in obj, get: obj => obj.angularSensibility, set: (obj, value) => { obj.angularSensibility = value; } }, metadata: _metadata }, _angularSensibility_initializers, _angularSensibility_extraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
})(),
_a;
})();
CameraInputTypes["FreeCameraMouseInput"] = FreeCameraMouseInput;
/**
* Base class for mouse wheel input..
* See FollowCameraMouseWheelInput in src/Cameras/Inputs/freeCameraMouseWheelInput.ts
* for example usage.
*/
let BaseCameraMouseWheelInput = (() => {
var _a;
let _wheelPrecisionX_decorators;
let _wheelPrecisionX_initializers = [];
let _wheelPrecisionX_extraInitializers = [];
let _wheelPrecisionY_decorators;
let _wheelPrecisionY_initializers = [];
let _wheelPrecisionY_extraInitializers = [];
let _wheelPrecisionZ_decorators;
let _wheelPrecisionZ_initializers = [];
let _wheelPrecisionZ_extraInitializers = [];
return _a = class BaseCameraMouseWheelInput {
constructor() {
/**
* How fast is the camera moves in relation to X axis mouseWheel events.
* Use negative value to reverse direction.
*/
this.wheelPrecisionX = __runInitializers(this, _wheelPrecisionX_initializers, 3.0);
/**
* How fast is the camera moves in relation to Y axis mouseWheel events.
* Use negative value to reverse direction.
*/
this.wheelPrecisionY = (__runInitializers(this, _wheelPrecisionX_extraInitializers), __runInitializers(this, _wheelPrecisionY_initializers, 3.0));
/**
* How fast is the camera moves in relation to Z axis mouseWheel events.
* Use negative value to reverse direction.
*/
this.wheelPrecisionZ = (__runInitializers(this, _wheelPrecisionY_extraInitializers), __runInitializers(this, _wheelPrecisionZ_initializers, 3.0));
/**
* Observable for when a mouse wheel move event occurs.
*/
this.onChangedObservable = (__runInitializers(this, _wheelPrecisionZ_extraInitializers), new Observable());
/**
* Incremental value of multiple mouse wheel movements of the X axis.
* Should be zero-ed when read.
*/
this._wheelDeltaX = 0;
/**
* Incremental value of multiple mouse wheel movements of the Y axis.
* Should be zero-ed when read.
*/
this._wheelDeltaY = 0;
/**
* Incremental value of multiple mouse wheel movements of the Z axis.
* Should be zero-ed when read.
*/
this._wheelDeltaZ = 0;
/**
* Firefox uses a different scheme to report scroll distances to other
* browsers. Rather than use complicated methods to calculate the exact
* multiple we need to apply, let's just cheat and use a constant.
* https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaMode
* https://stackoverflow.com/questions/20110224/what-is-the-height-of-a-line-in-a-wheel-event-deltamode-dom-delta-line
*/
this._ffMultiplier = 12;
/**
* Different event attributes for wheel data fall into a few set ranges.
* Some relevant but dated date here:
* https://stackoverflow.com/questions/5527601/normalizing-mousewheel-speed-across-browsers
*/
this._normalize = 120;
}
/**
* Attach the input controls to a specific dom element to get the input from.
* @param noPreventDefault Defines whether event caught by the controls
* should call preventdefault().
* (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
*/
attachControl(noPreventDefault) {
noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments);
this._wheel = (pointer) => {
// sanity check - this should be a PointerWheel event.
if (pointer.type !== PointerEventTypes.POINTERWHEEL) {
return;
}
const event = pointer.event;
const platformScale = event.deltaMode === EventConstants.DOM_DELTA_LINE ? this._ffMultiplier : 1; // If this happens to be set to DOM_DELTA_LINE, adjust accordingly
this._wheelDeltaX += (this.wheelPrecisionX * platformScale * event.deltaX) / this._normalize;
this._wheelDeltaY -= (this.wheelPrecisionY * platformScale * event.deltaY) / this._normalize;
this._wheelDeltaZ += (this.wheelPrecisionZ * platformScale * event.deltaZ) / this._normalize;
if (event.preventDefault) {
if (!noPreventDefault) {
event.preventDefault();
}
}
};
this._observer = this.camera.getScene()._inputManager._addCameraPointerObserver(this._wheel, PointerEventTypes.POINTERWHEEL);
}
/**
* Detach the current controls from the specified dom element.
*/
detachControl() {
if (this._observer) {
this.camera.getScene()._inputManager._removeCameraPointerObserver(this._observer);
this._observer = null;
this._wheel = null;
}
if (this.onChangedObservable) {
this.onChangedObservable.clear();
}
}
/**
* Called for each rendered frame.
*/
checkInputs() {
this.onChangedObservable.notifyObservers({
wheelDeltaX: this._wheelDeltaX,
wheelDeltaY: this._wheelDeltaY,
wheelDeltaZ: this._wheelDeltaZ,
});
// Clear deltas.
this._wheelDeltaX = 0;
this._wheelDeltaY = 0;
this._wheelDeltaZ = 0;
}
/**
* Gets the class name of the current input.
* @returns the class name
*/
getClassName() {
return "BaseCameraMouseWheelInput";
}
/**
* Get the friendly name associated with the input class.
* @returns the input friendly name
*/
getSimpleName() {
return "mousewheel";
}
},
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
_wheelPrecisionX_decorators = [serialize()];
_wheelPrecisionY_decorators = [serialize()];
_wheelPrecisionZ_decorators = [serialize()];
__esDecorate(null, null, _wheelPrecisionX_decorators, { kind: "field", name: "wheelPrecisionX", static: false, private: false, access: { has: obj => "wheelPrecisionX" in obj, get: obj => obj.wheelPrecisionX, set: (obj, value) => { obj.wheelPrecisionX = value; } }, metadata: _metadata }, _wheelPrecisionX_initializers, _wheelPrecisionX_extraInitializers);
__esDecorate(null, null, _wheelPrecisionY_decorators, { kind: "field", name: "wheelPrecisionY", static: false, private: false, access: { has: obj => "wheelPrecisionY" in obj, get: obj => obj.wheelPrecisionY, set: (obj, value) => { obj.wheelPrecisionY = value; } }, metadata: _metadata }, _wheelPrecisionY_initializers, _wheelPrecisionY_extraInitializers);
__esDecorate(null, null, _wheelPrecisionZ_decorators, { kind: "field", name: "wheelPrecisionZ", static: false, private: false, access: { has: obj => "wheelPrecisionZ" in obj, get: obj => obj.wheelPrecisionZ, set: (obj, value) => { obj.wheelPrecisionZ = value; } }, metadata: _metadata }, _wheelPrecisionZ_initializers, _wheelPrecisionZ_extraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
})(),
_a;
})();
// eslint-disable-next-line @typescript-eslint/naming-convention
var _CameraProperty;
(function (_CameraProperty) {
_CameraProperty[_CameraProperty["MoveRelative"] = 0] = "MoveRelative";
_CameraProperty[_CameraProperty["RotateRelative"] = 1] = "RotateRelative";
_CameraProperty[_CameraProperty["MoveScene"] = 2] = "MoveScene";
})(_CameraProperty || (_CameraProperty = {}));
/**
* Manage the mouse wheel inputs to control a free camera.
* @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
*/
let FreeCameraMouseWheelInput = (() => {
var _a;
let _classSuper = BaseCameraMouseWheelInput;
let _instanceExtraInitializers = [];
let _set_wheelXMoveRelative_decorators;
let _set_wheelYMoveRelative_decorators;
let _set_wheelZMoveRelative_decorators;
let _set_wheelXRotateRelative_decorators;
let _set_wheelYRotateRelative_decorators;
let _set_wheelZRotateRelative_decorators;
let _set_wheelXMoveScene_decorators;
let _set_wheelYMoveScene_decorators;
let _set_wheelZMoveScene_decorators;
return _a = class FreeCameraMouseWheelInput extends _classSuper {
constructor() {
super(...arguments);
/**
* Defines the camera the input is attached to.
*/
this.camera = __runInitializers(this, _instanceExtraInitializers);
this._moveRelative = Vector3.Zero();
this._rotateRelative = Vector3.Zero();
this._moveScene = Vector3.Zero();
/**
* These are set to the desired default behaviour.
*/
this._wheelXAction = _CameraProperty.MoveRelative;
this._wheelXActionCoordinate = 0 /* Coordinate.X */;
this._wheelYAction = _CameraProperty.MoveRelative;
this._wheelYActionCoordinate = 2 /* Coordinate.Z */;
this._wheelZAction = null;
this._wheelZActionCoordinate = null;
}
/**
* Gets the class name of the current input.
* @returns the class name
*/
getClassName() {
return "FreeCameraMouseWheelInput";
}
/**
* Set which movement axis (relative to camera's orientation) the mouse
* wheel's X axis controls.
* @param axis The axis to be moved. Set null to clear.
*/
set wheelXMoveRelative(axis) {
if (axis === null && this._wheelXAction !== _CameraProperty.MoveRelative) {
// Attempting to clear different _wheelXAction.
return;
}
this._wheelXAction = _CameraProperty.MoveRelative;
this._wheelXActionCoordinate = axis;
}
/**
* Get the configured movement axis (relative to camera's orientation) the
* mouse wheel's X axis controls.
* @returns The configured axis or null if none.
*/
get wheelXMoveRelative() {
if (this._wheelXAction !== _CameraProperty.MoveRelative) {
return null;
}
return this._wheelXActionCoordinate;
}
/**
* Set which movement axis (relative to camera's orientation) the mouse
* wheel's Y axis controls.
* @param axis The axis to be moved. Set null to clear.
*/
set wheelYMoveRelative(axis) {
if (axis === null && this._wheelYAction !== _CameraProperty.MoveRelative) {
// Attempting to clear different _wheelYAction.
return;
}
this._wheelYAction = _CameraProperty.MoveRelative;
this._wheelYActionCoordinate = axis;
}
/**
* Get the configured movement axis (relative to camera's orientation) the
* mouse wheel's Y axis controls.
* @returns The configured axis or null if none.
*/
get wheelYMoveRelative() {
if (this._wheelYAction !== _CameraProperty.MoveRelative) {
return null;
}
return this._wheelYActionCoordinate;
}
/**
* Set which movement axis (relative to camera's orientation) the mouse
* wheel's Z axis controls.
* @param axis The axis to be moved. Set null to clear.
*/
set wheelZMoveRelative(axis) {
if (axis === null && this._wheelZAction !== _CameraProperty.MoveRelative) {
// Attempting to clear different _wheelZAction.
return;
}
this._wheelZAction = _CameraProperty.MoveRelative;
this._wheelZActionCoordinate = axis;
}
/**
* Get the configured movement axis (relative to camera's orientation) the
* mouse wheel's Z axis controls.
* @returns The configured axis or null if none.
*/
get wheelZMoveRelative() {
if (this._wheelZAction !== _CameraProperty.MoveRelative) {
return null;
}
return this._wheelZActionCoordinate;
}
/**
* Set which rotation axis (relative to camera's orientation) the mouse
* wheel's X axis controls.
* @param axis The axis to be moved. Set null to clear.
*/
set wheelXRotateRelative(axis) {
if (axis === null && this._wheelXAction !== _CameraProperty.RotateRelative) {
// Attempting to clear different _wheelXAction.
return;
}
this._wheelXAction = _CameraProperty.RotateRelative;
this._wheelXActionCoordinate = axis;
}
/**
* Get the configured rotation axis (relative to camera's orientation) the
* mouse wheel's X axis controls.
* @returns The configured axis or null if none.
*/
get wheelXRotateRelative() {
if (this._wheelXAction !== _CameraProperty.RotateRelative) {
return null;
}
return this._wheelXActionCoordinate;
}
/**
* Set which rotation axis (relative to camera's orientation) the mouse
* wheel's Y axis controls.
* @param axis The axis to be moved. Set null to clear.
*/
set wheelYRotateRelative(axis) {
if (axis === null && this._wheelYAction !== _CameraProperty.RotateRelative) {
// Attempting to clear different _wheelYAction.
return;
}
this._wheelYAction = _CameraProperty.RotateRelative;
this._wheelYActionCoordinate = axis;
}
/**
* Get the configured rotation axis (relative to camera's orientation) the
* mouse wheel's Y axis controls.
* @returns The configured axis or null if none.
*/
get wheelYRotateRelative() {
if (this._wheelYAction !== _CameraProperty.RotateRelative) {
return null;
}
return this._wheelYActionCoordinate;
}
/**
* Set which rotation axis (relative to camera's orientation) the mouse
* wheel's Z axis controls.
* @param axis The axis to be moved. Set null to clear.
*/
set wheelZRotateRelative(axis) {
if (axis === null && this._wheelZAction !== _CameraProperty.RotateRelative) {
// Attempting to clear different _wheelZAction.
return;
}
this._wheelZ