Skip to content
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

Toggle to allow Camera Below the Surface #5028

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### ✨ Features and improvements
- Support Vertical Perspective projection ([#5023](https://github.com/maplibre/maplibre-gl-js/pull/5023))
- Toggle for Terrain Collision ([#5028](https://github.com/maplibre/maplibre-gl-js/pull/5028))
- _...Add new stuff here..._

### 🐞 Bug fixes
Expand Down
32 changes: 32 additions & 0 deletions src/ui/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ export abstract class Camera extends Evented {
*/
_centerClampedToGround: boolean;

/**
* @internal
* Controls whether the camera should avoid going below terrain surface.
* When false, the camera can go under the terrain.
*/
_terrainCollision?: boolean;

abstract _requestRenderFrame(a: () => void): TaskID;
abstract _cancelRenderFrame(_: TaskID): void;

Expand All @@ -333,6 +340,7 @@ export abstract class Camera extends Evented {
this.transform = transform;
this._bearingSnap = options.bearingSnap;
this.cameraHelper = cameraHelper;
this._terrainCollision = true;

this.on('moveend', () => {
delete this._requestedCameraState;
Expand Down Expand Up @@ -423,6 +431,26 @@ export abstract class Camera extends Evented {
this._centerClampedToGround = centerClampedToGround;
}

/**
* Gets whether terrain collision detection is enabled.
*
* When true (default), camera will avoid going below terrain surface.
* When false, camera can go under the terrain.
*/
getTerrainCollision(): boolean {
return this._terrainCollision;
}

/**
* Sets whether terrain collision detection is enabled.
*
* When true (default), camera will avoid going below terrain surface.
* When false, camera can go under the terrain.
*/
setTerrainCollision(terrainCollision: boolean): void {
this._terrainCollision = terrainCollision;
}

/**
* Pans the map by the specified offset.
*
Expand Down Expand Up @@ -1239,6 +1267,10 @@ export abstract class Camera extends Evented {
* @param tr - The transform to check.
*/
_elevateCameraIfInsideTerrain(tr: ITransform) : { pitch?: number; zoom?: number } {
if (!this._terrainCollision) {
return {};
}

if (!this.terrain && tr.elevation >= 0 && tr.pitch <= 90) {
return {};
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"version": 8,
"metadata": {
"test": {
"height": 256,
"width": 256,
"maxPitch": 180,
"operations": [
["setTerrainCollision", false],
["setCenterElevation", 10],
["wait"]
]
}
},
"center": [11.52517, 47.34487],
"zoom": 13,
"pitch": 100,
"sources": {
"terrain": {
"type": "raster-dem",
"tiles": ["local://tiles/terrain-shading/{z}-{x}-{y}.terrain.png"],
"maxzoom": 12,
"tileSize": 256
},
"osm": {
"type": "raster",
"tiles": ["local://tiles/number/{z}.png"],
"maxzoom": 17,
"tileSize": 256
}
},
"layers": [
{
"id": "background",
"type": "background",
"paint": {
"background-color": "white"
}
},
{
"id": "raster",
"type": "raster",
"source": "osm",
"paint": {
"raster-opacity": 1.0
}
}
],
"terrain": {
"source": "terrain",
"exaggeration": 2
}
}
Loading