-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Geospatial Camera Keyboard Inputs and PinchToZoom #17466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
georginahalpern
merged 5 commits into
BabylonJS:master
from
georginahalpern:geoCamKeyboard
Nov 18, 2025
+260
−8
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
238 changes: 238 additions & 0 deletions
238
packages/dev/core/src/Cameras/Inputs/geospatialCameraKeyboardInput.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| import type { Nullable } from "../../types"; | ||
| import { serialize } from "../../Misc/decorators"; | ||
| import type { Observer } from "../../Misc/observable"; | ||
| import type { Scene } from "../../scene"; | ||
| import type { GeospatialCamera } from "../geospatialCamera"; | ||
| import type { ICameraInput } from "../cameraInputsManager"; | ||
| import { CameraInputTypes } from "../cameraInputsManager"; | ||
| import type { KeyboardInfo } from "../../Events/keyboardEvents"; | ||
| import { KeyboardEventTypes } from "../../Events/keyboardEvents"; | ||
| import { Tools } from "../../Misc/tools"; | ||
| import type { AbstractEngine } from "../../Engines/abstractEngine"; | ||
|
|
||
| /** | ||
| * Manage the keyboard inputs to control the movement of a geospatial camera. | ||
| * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs | ||
| */ | ||
| export class GeospatialCameraKeyboardInput implements ICameraInput<GeospatialCamera> { | ||
| /** | ||
| * Defines the camera the input is attached to. | ||
| */ | ||
| public camera: GeospatialCamera; | ||
|
|
||
| /** | ||
| * Defines the list of key codes associated with the up action (pan up) | ||
| */ | ||
| @serialize() | ||
| public keysUp = [38]; | ||
|
|
||
| /** | ||
| * Defines the list of key codes associated with the down action (pan down) | ||
| */ | ||
| @serialize() | ||
| public keysDown = [40]; | ||
|
|
||
| /** | ||
| * Defines the list of key codes associated with the left action (pan left) | ||
| */ | ||
| @serialize() | ||
| public keysLeft = [37]; | ||
|
|
||
| /** | ||
| * Defines the list of key codes associated with the right action (pan right) | ||
| */ | ||
| @serialize() | ||
| public keysRight = [39]; | ||
|
|
||
| /** | ||
| * Defines the list of key codes associated with zoom in (+ or =) | ||
| */ | ||
| @serialize() | ||
| public keysZoomIn = [187, 107]; // 187 = + key, 107 = numpad + | ||
|
|
||
| /** | ||
| * Defines the list of key codes associated with zoom out (-) | ||
| */ | ||
| @serialize() | ||
| public keysZoomOut = [189, 109]; // 189 = - key, 109 = numpad - | ||
|
|
||
| /** | ||
| * Defines the rotation sensitivity of the inputs. | ||
| * (How many pixels of pointer input to apply per keypress, before rotation speed factor is applied by movement class) | ||
| */ | ||
| @serialize() | ||
| public rotationSensitivity = 1.0; | ||
|
|
||
| /** | ||
| * Defines the panning sensitivity of the inputs. | ||
| * (How many pixels of pointer input to apply per keypress, before pan speed factor is applied by movement class) | ||
| */ | ||
| @serialize() | ||
| public panSensitivity: number = 1.0; | ||
|
|
||
| /** | ||
| * Defines the zooming sensitivity of the inputs. | ||
| * (How many pixels of pointer input to apply per keypress, before zoom speed factor is applied by movement class) | ||
| */ | ||
| @serialize() | ||
| public zoomSensitivity: number = 1.0; | ||
|
|
||
| private _keys = new Array<number>(); | ||
| private _ctrlPressed: boolean; | ||
| private _onCanvasBlurObserver: Nullable<Observer<AbstractEngine>>; | ||
| private _onKeyboardObserver: Nullable<Observer<KeyboardInfo>>; | ||
| private _engine: AbstractEngine; | ||
| private _scene: Scene; | ||
|
|
||
| /** | ||
| * 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) | ||
| */ | ||
| public attachControl(noPreventDefault?: boolean): void { | ||
| // was there a second variable defined? | ||
| noPreventDefault = Tools.BackCompatCameraNoPreventDefault(arguments); | ||
|
|
||
| if (this._onCanvasBlurObserver) { | ||
| return; | ||
| } | ||
|
|
||
| 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) { | ||
| this._ctrlPressed = evt.ctrlKey; | ||
|
|
||
| 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.keysZoomIn.indexOf(evt.keyCode) !== -1 || | ||
| this.keysZoomOut.indexOf(evt.keyCode) !== -1 | ||
| ) { | ||
| const index = this._keys.indexOf(evt.keyCode); | ||
|
|
||
| if (index === -1) { | ||
| this._keys.push(evt.keyCode); | ||
| } | ||
|
|
||
| if (evt.preventDefault) { | ||
| 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.keysZoomIn.indexOf(evt.keyCode) !== -1 || | ||
| this.keysZoomOut.indexOf(evt.keyCode) !== -1 | ||
| ) { | ||
| const index = this._keys.indexOf(evt.keyCode); | ||
|
|
||
| if (index >= 0) { | ||
| this._keys.splice(index, 1); | ||
| } | ||
|
|
||
| if (evt.preventDefault) { | ||
| if (!noPreventDefault) { | ||
| evt.preventDefault(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Detach the current controls from the specified dom element. | ||
| */ | ||
| public detachControl(): void { | ||
| 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. | ||
| */ | ||
| public checkInputs(): void { | ||
| if (this._onKeyboardObserver) { | ||
| const camera = this.camera; | ||
|
|
||
| for (let index = 0; index < this._keys.length; index++) { | ||
| const keyCode = this._keys[index]; | ||
| if (this._ctrlPressed) { | ||
| // Rotation | ||
| if (this.keysLeft.indexOf(keyCode) !== -1) { | ||
georginahalpern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| camera.movement.rotationAccumulatedPixels.y -= this.rotationSensitivity; | ||
| } else if (this.keysRight.indexOf(keyCode) !== -1) { | ||
| camera.movement.rotationAccumulatedPixels.y += this.rotationSensitivity; | ||
| } else if (this.keysUp.indexOf(keyCode) !== -1) { | ||
| camera.movement.rotationAccumulatedPixels.x -= this.rotationSensitivity; | ||
| } else if (this.keysDown.indexOf(keyCode) !== -1) { | ||
| camera.movement.rotationAccumulatedPixels.x += this.rotationSensitivity; | ||
| } | ||
| } else { | ||
| // Zoom | ||
| if (this.keysZoomIn.indexOf(keyCode) !== -1) { | ||
| camera.movement.zoomAccumulatedPixels += this.zoomSensitivity; | ||
| } else if (this.keysZoomOut.indexOf(keyCode) !== -1) { | ||
| camera.movement.zoomAccumulatedPixels -= this.zoomSensitivity; | ||
| } else { | ||
| // Call into movement class handleDrag so that behavior matches that of pointer input, simulating drag from center of screen | ||
| const centerX = this._engine.getRenderWidth() / 2; | ||
| const centerY = this._engine.getRenderHeight() / 2; | ||
| camera.movement.startDrag(centerX, centerY); | ||
| if (this.keysLeft.indexOf(keyCode) !== -1) { | ||
| camera.movement.handleDrag(centerX - this.panSensitivity, centerY); | ||
| } else if (this.keysRight.indexOf(keyCode) !== -1) { | ||
| camera.movement.handleDrag(centerX + this.panSensitivity, centerY); | ||
| } else if (this.keysUp.indexOf(keyCode) !== -1) { | ||
| camera.movement.handleDrag(centerX, centerY + this.panSensitivity); | ||
| } else if (this.keysDown.indexOf(keyCode) !== -1) { | ||
| camera.movement.handleDrag(centerX, centerY - this.panSensitivity); | ||
| } | ||
| camera.movement.stopDrag(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the class name of the current input. | ||
| * @returns the class name | ||
| */ | ||
| public getClassName(): string { | ||
| return "GeospatialCameraKeyboardInput"; | ||
| } | ||
|
|
||
| /** | ||
| * Get the friendly name associated with the input class. | ||
| * @returns the input friendly name | ||
| */ | ||
| public getSimpleName(): string { | ||
| return "keyboard"; | ||
| } | ||
| } | ||
|
|
||
| (<any>CameraInputTypes)["GeospatialCameraKeyboardInput"] = GeospatialCameraKeyboardInput; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.