Skip to content

Commit

Permalink
- #146 Update to Mapbox 2.2
Browse files Browse the repository at this point in the history
  - #225 Mapbox 2.2: Update Depth calculation keeping compatibility with previous versions
  - #226 Mapbox 2.2: Update all the examples (14-buildingshadow & 17-azuremaps not updated)

#### 📝 Documentation
- Updated [documentation](/examples/readme.md) (Added a note on the examples updated to Mapbox 2.2.0)
  • Loading branch information
jscastro76 committed Apr 7, 2021
1 parent e1f169b commit 6ecf9a9
Show file tree
Hide file tree
Showing 24 changed files with 265 additions and 151 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ Minor version by [@jscastro76](https://github.com/jscastro76), some enhancements
#### :sparkles: Enhancements
- #224 Ignore worker_threads
- #223 Can't resolve worker threads

- #146 Update to Mapbox 2.2
- #225 Mapbox 2.2: Update Depth calculation keeping compatibility with previous versions
- #226 Mapbox 2.2: Update all the examples (14-buildingshadow & 17-azuremaps not updated)

#### :pencil: Documentation
- Updated [documentation](/examples/readme.md) (Added a note on the examples updated to Mapbox 2.2.0)

- - -

## 2.2.1
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![NPM license](http://img.shields.io/npm/l/threebox-plugin.svg?style=flat-square)](https://www.npmjs.org/package/threebox-plugin)
![npm](https://img.shields.io/npm/dt/threebox-plugin?style=social)

A **[*Three.js*](https://threejs.org/) (r127)** plugin for **[*Mapbox GL JS*](https://docs.mapbox.com/mapbox-gl-js/examples/)** and **[*Azure Maps*](https://azure.microsoft.com/en-us/services/azure-maps/)** using the [`CustomLayerInterface`](https://docs.mapbox.com/mapbox-gl-js/api/properties/#customlayerinterface) feature. Provides convenient methods to manage objects in lnglat coordinates, and to synchronize the map and scene cameras.
A **[*Three.js*](https://threejs.org/)** plugin for **[*Mapbox GL JS*](https://docs.mapbox.com/mapbox-gl-js/examples/)** and **[*Azure Maps*](https://azure.microsoft.com/en-us/services/azure-maps/)** using the [`CustomLayerInterface`](https://docs.mapbox.com/mapbox-gl-js/api/properties/#customlayerinterface) feature. Provides convenient methods to manage objects in lnglat coordinates, and to synchronize the map and scene cameras.
<img alt="threebox" src="docs/gallery.jpg">

<br>
Expand Down Expand Up @@ -52,8 +52,8 @@ npm i threebox-plugin

Only in this fork, there is a list of new features implemented on top of the amazing work from [@peterqliu](https://github.com/peterqliu/threebox/):
- Updated to **Three.js r127**.
- Updated to Mapbox-gl-js v1.11.1.
- Updated to Azure Maps v2.0.31.
- Updated to **Mapbox-gl-js v2.2.0**.
- Updated to **Azure Maps v2.0.31**.
- [+20 examples](https://github.com/jscastro76/threebox/tree/master/examples) with all the new features.
- Support for multiple 3D format objects (FBX, GLTF/GLB, Collada, OBJ/MTL).
- Support for 3D extruded shapes from [GeoJson](https://geojson.org/) features or points array.
Expand Down Expand Up @@ -99,9 +99,9 @@ All the [**Threebox Documentation**](/docs/Threebox.md) has been completely upda

## Compatibility/Dependencies

- **Three.js r127** (already bundled into the Threebox build). If desired, other versions can be swapped in and rebuilt [here](https://github.com/jscastro76/threebox/blob/master/src/three.js), though compatibility is not guaranteed.
- Mapbox-gl-js v1.11.1.
- Azure Maps v2.0.31.
- **Three.js r127**. (already bundled into the Threebox build). If desired, other versions can be swapped in and rebuilt [here](https://github.com/jscastro76/threebox/blob/master/src/three.js), though compatibility is not guaranteed. **(WARNING: v118.3 breaks compatibility in some cases)**
- **Mapbox-gl-js v1.11.1. or v.2.0.1**
- **Azure Maps v2.0.31.**

<br>

Expand Down
61 changes: 53 additions & 8 deletions dist/threebox.js
Original file line number Diff line number Diff line change
Expand Up @@ -1863,19 +1863,46 @@ CameraSync.prototype = {
return;
}

// Furthest distance optimized by @jscastro76
const t = this.map.transform;
let farZ = 0;
let furthestDistance = 0;
this.state.fov = t._fov;
this.halfFov = this.state.fov / 2;
const groundAngle = Math.PI / 2 + t._pitch;
this.cameraToCenterDistance = 0.5 / Math.tan(this.halfFov) * t.height;
this.state.cameraTranslateZ = new THREE.Matrix4().makeTranslation(0, 0, this.cameraToCenterDistance);
const topHalfSurfaceDistance = Math.sin(this.halfFov) * this.state.cameraToCenterDistance / Math.sin(Math.PI - groundAngle - this.halfFov);
const pitchAngle = Math.cos((Math.PI / 2) - t._pitch); //pitch seems to influence heavily the depth calculation and cannot be more than 60 = PI/3
this.cameraToCenterDistance = 0.5 / Math.tan(this.halfFov) * t.height;

if (window.mapboxgl && parseFloat(window.mapboxgl.version) >= 2.0) {
// mapbox version >= 2.0
const worldSize = this.worldSize(t);
const pixelsPerMeter = this.mercatorZfromAltitude(1, t.center.lat) * worldSize;
const fovAboveCenter = this.fovAboveCenter(t);

// Adjust distance to MSL by the minimum possible elevation visible on screen,
// this way the far plane is pushed further in the case of negative elevation.
const minElevationInPixels = 0; // TODO test with elevation exageration this.elevation ? this.elevation.getMinElevationBelowMSL() * pixelsPerMeter : 0;
const cameraToSeaLevelDistance = ((t._camera.position[2] * worldSize) - minElevationInPixels) / Math.cos(t._pitch);
const topHalfSurfaceDistance = Math.sin(fovAboveCenter) * cameraToSeaLevelDistance / Math.sin(utils.clamp(Math.PI - groundAngle - fovAboveCenter, 0.01, Math.PI - 0.01));

// Calculate z distance of the farthest fragment that should be rendered.
furthestDistance = pitchAngle * topHalfSurfaceDistance + cameraToSeaLevelDistance;

// Add a bit extra to avoid precision problems when a fragment's distance is exactly `furthestDistance`
const horizonDistance = cameraToSeaLevelDistance * (1 / t._horizonShift);
farZ = Math.min(furthestDistance * 1.01, horizonDistance);
} else {
// mapbox version < 2.0 or azure maps
// Furthest distance optimized by @jscastro76
const topHalfSurfaceDistance = Math.sin(this.halfFov) * this.state.cameraToCenterDistance / Math.sin(Math.PI - groundAngle - this.halfFov);

// Calculate z distance of the farthest fragment that should be rendered.
furthestDistance = pitchAngle * topHalfSurfaceDistance + this.state.cameraToCenterDistance;

// Calculate z distance of the farthest fragment that should be rendered.
const furthestDistance = pitchAngle * topHalfSurfaceDistance + this.state.cameraToCenterDistance;
// Add a bit extra to avoid precision problems when a fragment's distance is exactly `furthestDistance`
farZ = furthestDistance * 1.01;

// Add a bit extra to avoid precision problems when a fragment's distance is exactly `furthestDistance`
const farZ = furthestDistance * 1.01;
}
this.state.cameraTranslateZ = new THREE.Matrix4().makeTranslation(0, 0, this.cameraToCenterDistance);

// someday @ansis set further near plane to fix precision for deckgl,so we should fix it to use mapbox-gl v1.3+ correctly
// https://github.com/mapbox/mapbox-gl-js/commit/5cf6e5f523611bea61dae155db19a7cb19eb825c#diff-5dddfe9d7b5b4413ee54284bc1f7966d
Expand Down Expand Up @@ -1913,8 +1940,26 @@ CameraSync.prototype = {
.premultiply(scale)
.premultiply(translateMap)

// utils.prettyPrintMatrix(this.camera.projectionMatrix.elements);
this.map.fire('CameraSynced', { detail: { nearZ: nearZ, farZ: farZ, pitch: t._pitch, angle: t.angle, furthestDistance: furthestDistance, maxFurthestDistance: this.state.maxFurthestDistance, cameraToCenterDistance: this.cameraToCenterDistance, t: this.map.transform, tbProjMatrix: this.camera.projectionMatrix.elements, tbWorldMatrix: this.world.matrix.elements, cameraSyn: CameraSync } });

},

worldSize(transform) {
return transform.tileSize * transform.scale;
},

fovAboveCenter(transform) {
return transform._fov * (0.5 + transform.centerOffset.y / transform.height);
},

mercatorZfromAltitude(altitude, lat) {
return altitude / this.circumferenceAtLatitude(lat);
},

circumferenceAtLatitude(latitude) {
return ThreeboxConstants.EARTH_CIRCUMFERENCE * Math.cos(latitude * Math.PI / 180);
},

calcCameraMatrix(pitch, angle, trz) {
const t = this.map.transform;
Expand Down
2 changes: 1 addition & 1 deletion dist/threebox.min.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/01-basic.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!doctype html>
<head>
<title>Sphere Example</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<script src="../dist/threebox.min.js" type="text/javascript"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" type="text/css" />
<script src="config.js"></script>
<style>
Expand Down
4 changes: 2 additions & 2 deletions examples/02-line.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<style>
body, html {
width: 100%;
Expand Down
4 changes: 2 additions & 2 deletions examples/03-tube.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!doctype html>
<head>
<title>Tube Example</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
Expand Down
7 changes: 4 additions & 3 deletions examples/04-mercator.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<style>
body, html {
width: 100%;
Expand Down Expand Up @@ -47,7 +47,8 @@
map,
map.getCanvas().getContext('webgl'),
{
defaultLights: true
defaultLights: true,
enableSelectingObjects: true
}
);

Expand Down
4 changes: 2 additions & 2 deletions examples/05-logistics.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body, html {
Expand Down
36 changes: 18 additions & 18 deletions examples/06-object3d.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<!doctype html>
<head>
<title>Threebox Object3D Example</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
}

#map {
width: 100%;
height: 100%;
}
</style>
<title>Threebox Object3D Example</title>
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
}

#map {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id='map' class='map'></div>
Expand Down
4 changes: 2 additions & 2 deletions examples/07-alignmentTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html>
<head>
<title>Threebox alignment Test</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions examples/08-3dbuildings.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html>
<head>
<title>Threebox display buildings in 3D with auto tooltips</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions examples/09-raycaster.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!doctype html>
<head>
<title>Threebox raycaster of Objects3D, 3D models and Fill-extrusions</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions examples/10-stylechange.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html>
<head>
<title>Threebox change map style for Eiffel Tower</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />

Expand Down
4 changes: 2 additions & 2 deletions examples/11-animation.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="plugins/jquery.min.js"></script>
<!-- Place your kit's code here -->
Expand Down
8 changes: 5 additions & 3 deletions examples/12-add3dmodel.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<!doctype html>
<head>
<head>
<meta charset="utf-8" />
<title>Add a 3D model with Threebox</title>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<style>
body, html {
width: 100%;
Expand All @@ -18,6 +18,7 @@
width: 100%;
height: 100%;
}

#time {
position: absolute;
left: 0;
Expand All @@ -27,6 +28,7 @@
margin-right: auto;
min-width: 90%;
}

#hour {
background: rgba(0, 0, 0, 0.5);
color: #fff;
Expand Down
4 changes: 2 additions & 2 deletions examples/13-eiffel.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<html>
<head>
<title>Threebox Statue of Liberty and Eiffel Tower</title>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />
<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="plugins/jquery.min.js" type="text/javascript"></script>
Expand Down
5 changes: 2 additions & 3 deletions examples/15-performance.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
<script src="../dist/threebox.js" type="text/javascript"></script>
<link href="../dist/threebox.css" rel="stylesheet" />
<script src="config.js"></script>
<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css" rel="stylesheet" />

<link href="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v2.2.0/mapbox-gl.js"></script>
<script src="plugins/jquery.min.js"></script>
<!-- Place your kit's code here -->
<script src="css/fontawesome.js"></script>
Expand Down
Loading

0 comments on commit 6ecf9a9

Please sign in to comment.