maplibre-three-plugin is a bridge plugin that cleverly
connects MapLibre GL JS with Three.js, enabling
developers to implement 3D rendering and animation on maps.
npm install @dvt3d/maplibre-three-plugin
----------------------------------------
yarn add @dvt3d/maplibre-three-plugin
maplibre-three-plugin depends on three, please make sure three is installed before using it.
<div id="map-container"></div>
import maplibregl from 'maplibre-gl'
import * as THREE from 'three'
import { GLTFLoader } from 'three/addons'
import * as MTP from '@dvt3d/maplibre-three-plugin'
const map = new maplibregl.Map({
container: 'map-container', // container id
style: 'https://api.maptiler.com/maps/basic-v2/style.json?key=get_access_key',
zoom: 18,
center: [148.9819, -35.3981],
pitch: 60,
canvasContextAttributes: { antialias: true },
maxPitch: 85,
})
//init three scene
const mapScene = new MTP.MapScene(map)
//add light
mapScene.addLight(new THREE.AmbientLight())
// add model
const glTFLoader = new GLTFLoader()
glTFLoader.load('./assets/34M_17/34M_17.gltf', (gltf) => {
let rtcGroup = MTP.Creator.createRTCGroup([148.9819, -35.39847])
rtcGroup.add(gltf.scene)
mapScene.addObject(rtcGroup)
})
![]() | ![]() | ![]() | ![]() |
|---|---|---|---|
| model | sun-light | point | point-collection |
![]() | ![]() | ![]() | |
| billboard | div-icon | 3d-tiles | 3d-tiles-osgb |
![]() | ![]() | ![]() | ![]() |
| 3d-tiles-cesium-ion | 3d-gs-cesium-ion | 3d-gs-splat | heat-map |
const mapScene = new MapScene(map)
{Map} map : map instance{Object} options : config// config
Object({
scene: null, //THREE.Scene,if not passed in, the default scene will be used
camera: null, //THREE.Camera, if not passed in, the default camera will be used
renderer: null, //THREE.WebGLRenderer if not passed in, the default renderer will be used
preserveDrawingBuffer: false,
renderLoop: (ins) => {
} //Frame animation rendering function, if not passed in, the default function will be used,the params is an instance for MapScene
})
preReset : A hook that calls renderer.resetState before each animation framepostReset: A hook that calls renderer.resetState after each animation framepreRender: A hook that calls renderer.render before each animation framepostRender: A hook that calls renderer.render after each animation frame{maplibregl.Map} map : readonly{HTMLCanvasElement} canvas : readonly{THREE.Camera} camera : readonly{THREE.Sence} scene : readonly{THREE.Group} lights: readonly{THREE.Group} world : readonly{THREE.WebGLRenderer} renderer : readonlyaddLight(light)
Add light to the scene, support custom light objects, but the custom light objects need to support the delegate
property, and the delegate type is THREE.Object3D
{THREE.Object3D | Sun | CustomLight } light thisremoveLight(light)
Remove light from the scene
{THREE.Object3D | Sun | CustomLight } light thisaddObject(object)
Add an object to world,support custom object, but the custom object need to support the delegate property, and the
delegate type is THREE.Object3D
{THREE.Object3D | CustomObject} object thisremoveObject(object)
Remove an object from world
{THREE.Object3D | CustomObject} object thisflyTo(target,[completed],[duration])
Fly the map to the provided target over a period of time, the completion callback will be triggered when the flight is
complete, the target needs to contain the position property
{THREE.Object3D | CustomObject} target {Function} completed :{Number} duration :thiszoomTo(target,[completed])
Zoom the map to the provided target
{Ojbect} target {Function} completed :thison(type,callback)
{String} type {Function} callback :thisoff(type,callback)
{String} type {Function} callback :thisconst scale = new SceneTransform.projectedUnitsPerMeter(24)
projectedMercatorUnitsPerMeter()
{Number} valueprojectedUnitsPerMeter(lat)
{Number} lat {Number} valuelngLatToVector3(lng, [lat], [alt] )
{Array | Number} lng { Number} lat { Number} alt {THREE.Vector3} vvector3ToLngLat(v)
{THREE.Vector3} v{Array} valueconst sun = new Sun()
{THREE.Group} delegate : readonly{Boolean} castShadow {Date || String} currentTime {THREE.DirectionalLight} sunLight : readonly{THREE.HemisphereLight} hemiLight: readonly{Object} frameState:thisconst rtcGroup = Creator.createRTCGroup([-1000, 0, 0])
createRTCGroup(center, [rotation], [scale])
{Array} center{Array} rotation: default value is [0,0,0]{Array} scale: scale corresponding to the current latitude{THREE.Group} rtccreateMercatorRTCGroup(center, [rotation], [scale])
{Array} center{Array} rotation: default value is [0,0,0]{Array} scale: scale corresponding to the current latitude{THREE.Group} rtccreateShadowGround(center, [width], [height])
{THREE.Vector3} center{Number} width: default value is 100{Number} height : default value is 100{Object} rtc