Skip to content

Commit

Permalink
Use ESM and latest three.js 0.169.0 (#4888)
Browse files Browse the repository at this point in the history
* Use ESM and latest three.js 0.169.0

* don't use window.map

* wording
  • Loading branch information
birkskyum authored Oct 24, 2024
1 parent 7e759ad commit 491ac04
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 39 deletions.
29 changes: 19 additions & 10 deletions test/examples/add-3d-model-threejs-with-shadow.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<html lang="en">

<head>
<title>Add a 3D model with shadow with three.js</title>
<meta property="og:description" content="Use a custom style layer with three.js to add a 3D model with Shadow to the map." />
<title>Add a 3D model with shadow using three.js</title>
<meta property="og:description"
content="Use a custom style layer with three.js to add a 3D model with shadow to the map." />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='../../dist/maplibre-gl.css' />
Expand All @@ -20,22 +21,32 @@
height: 100%;
}
</style>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
}
}
</script>
</head>

<body>
<script src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<script src="https://unpkg.com/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<div id="map"></div>
<script>
const map = (window.map = new maplibregl.Map({

<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

const map = new maplibregl.Map({
container: 'map',
style:
'https://api.maptiler.com/maps/basic/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
zoom: 18,
center: [148.9819, -35.3981],
pitch: 60,
antialias: true
}));
});

const modelOrigin = [148.9819, -35.39847];
const modelAltitude = 0;
Expand All @@ -56,8 +67,6 @@
scale: modelAsMercatorCoordinate.meterInMercatorCoordinateUnits()
};

const THREE = window.THREE;

const customLayer = {
id: '3d-model',
type: 'custom',
Expand Down Expand Up @@ -89,7 +98,7 @@
ground.receiveShadow = true;
this.scene.add(ground);

const loader = new THREE.GLTFLoader();
const loader = new GLTFLoader();
loader.load(
'https://maplibre.org/maplibre-gl-js/docs/assets/34M_17/34M_17.gltf',
(gltf) => {
Expand Down
27 changes: 17 additions & 10 deletions test/examples/add-3d-model-with-terrain.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">

<head>
<title>Adding 3D models with three.js on terrain</title>
<title>Adding 3D models using three.js on terrain</title>
<meta property="og:description" content="Use a custom style layer with three.js to add 3D models to a map with 3d terrain." />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand All @@ -20,13 +20,22 @@
height: 100%;
}
</style>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
}
}
</script>
</head>

<body>
<script src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<script src="https://unpkg.com/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<div id="map"></div>
<script>
<div id="map"></div>

<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

/**
* Objective:
* Given two known world-locations `model1Location` and `model2Location`,
Expand All @@ -36,8 +45,6 @@

async function main() {

const THREE = window.THREE;

const map = new maplibregl.Map({
container: 'map',
center: [11.5257, 47.668],
Expand Down Expand Up @@ -100,7 +107,7 @@
}

async function loadModel() {
const loader = new THREE.GLTFLoader();
const loader = new GLTFLoader();
const gltf = await loader.loadAsync('https://maplibre.org/maplibre-gl-js/docs/assets/34M_17/34M_17.gltf');
const model = gltf.scene;
return model;
Expand Down Expand Up @@ -210,4 +217,4 @@
</script>
</body>

</html>
</html>
27 changes: 18 additions & 9 deletions test/examples/add-3d-model.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Add a 3D model with three.js</title>
<title>Add a 3D model using three.js</title>
<meta property="og:description" content="Use a custom style layer with three.js to add a 3D model to the map." />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand All @@ -13,19 +13,29 @@
</style>
</head>
<body>
<script src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<script src="https://unpkg.com/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
}
}
</script>
<div id="map"></div>
<script>
const map = (window.map = new maplibregl.Map({

<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

const map = new maplibregl.Map({
container: 'map',
style:
'https://api.maptiler.com/maps/basic/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
zoom: 18,
center: [148.9819, -35.3981],
pitch: 60,
antialias: true // create the gl context with MSAA antialiasing, so custom layers are antialiased
}));
});

// parameters to ensure the model is georeferenced correctly on the map
const modelOrigin = [148.9819, -35.39847];
Expand All @@ -51,7 +61,6 @@
scale: modelAsMercatorCoordinate.meterInMercatorCoordinateUnits()
};

const THREE = window.THREE;

// configuration of the custom layer for a 3D model per the CustomLayerInterface
const customLayer = {
Expand All @@ -72,7 +81,7 @@
this.scene.add(directionalLight2);

// use the three.js GLTF loader to add the 3D model to the three.js scene
const loader = new THREE.GLTFLoader();
const loader = new GLTFLoader();
loader.load(
'https://maplibre.org/maplibre-gl-js/docs/assets/34M_17/34M_17.gltf',
(gltf) => {
Expand Down Expand Up @@ -142,4 +151,4 @@
});
</script>
</body>
</html>
</html>
29 changes: 19 additions & 10 deletions test/examples/globe-3d-model.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">

<head>
<title>Add a 3D model to globe with three.js</title>
<title>Add a 3D model to globe using three.js</title>
<meta property="og:description" content="Use a custom style layer with three.js to add a 3D model to a globe." />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down Expand Up @@ -39,21 +39,32 @@
</head>

<body>
<script src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<script src="https://unpkg.com/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
}
}
</script>

<div id="map"></div>
<br />
<button id="project">Toggle projection</button>
<script>
const map = (window.map = new maplibregl.Map({
<script type="module">

import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/basic/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
zoom: 5.5,
center: [150.16546137527212, -35.017179237129994],
maxPitch: 80,
pitch: 70,
antialias: true, // create the gl context with MSAA antialiasing, so custom layers are antialiased
}));
});

map.on('style.load', () => {
map.setProjection({
Expand All @@ -71,8 +82,6 @@
});
});

const THREE = window.THREE;

// configuration of the custom layer for a 3D model per the CustomLayerInterface
const customLayer = {
id: '3d-model',
Expand All @@ -92,7 +101,7 @@
this.scene.add(directionalLight2);

// use the three.js GLTF loader to add the 3D model to the three.js scene
const loader = new THREE.GLTFLoader();
const loader = new GLTFLoader();
loader.load(
'https://maplibre.org/maplibre-gl-js/docs/assets/34M_17/34M_17.gltf',
(gltf) => {
Expand Down Expand Up @@ -144,4 +153,4 @@
</script>
</body>

</html>
</html>

0 comments on commit 491ac04

Please sign in to comment.