
Security News
Critical Security Vulnerability in React Server Components
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.
@maptiler/3d
Advanced tools
Add 3D objects and lighting to your MapTiler SDK JS maps
With this MapTiler SDK module, you can add 3D objects to your basemap with plenty of customizations from glTF/glb files! Those can be meshes, groups of meshes, point clouds and a mix of all these.
Here are some examples:
Add an airplane 3D model to your map using the MapTiler 3D JS Module
Add multiple 3D models to the map with the MapTiler 3D JS Module
Display a building model based on point cloud data on a map with the MapTiler 3D JS Module
Display a 3D building model generated with photogrammetry software with the MapTiler 3D JS Module
Display a 3D building model generated with photogrammetry software with the MapTiler 3D JS Module
Display a point cloud 3D building model on a map with the MapTiler 3D JS Module
Import and play GLTF animations from GLTF files
Listen for mouse events on 3D objects
Apply declarative UI 'states' to 3D objects
From NPM and using the ES module, in a terminal, in your project:
npm install @maptiler/3d
Then to import:
import { Layer3D } from "@maptiler/3d";
// or
import * as maptiler3d from "@maptiler/3d";
From CDN and using the UMD bundle, in the <head></head> section of your HTML file:
<script src="https://cdn.maptiler.com/maptiler-3d/v1.0.0/maptiler-3d.umd.js"></script>
To generate the typedoc documentation and serve them locally:
npm run doc && npx http-server docs
With the UMD bundle (on CDN), the namespace for this project is maptiler3d. So the layer3D class is available at maptiler3d.Layer3D.
An instance of Layer3D is a custom type of layer that contain a 3D scene, where multiple 3D meshes and lights can be added. Like any other layer in MapTiler SDK/Maplibre GL JS, it must have an ID and then be added to a Map instance:
// Create a map;
const map = new Map({
container: "map",
// ...
});
// Waiting that the map is ready. You can also wait for the "load" event.
map.on("ready", () => {
// Create a Layer3D and add it
const layer3D = new maptiler3d.Layer3D("custom-3D-layer");
map.addLayer(layer3D);
})
Once created and added, a mesh can be added. In this version any glTF and their binary counterpart glb files can be added.
To add a mesh:
// The call can be awaited for the whole download of the mesh to complete
const item3D = await layer3D.addMeshFromURL(
// ID to give to this mesh, unique within this Layer3D instance
"flatiron",
// The URL of the mesh
"https://example.com/meshes/flatiron_building.glb",
// A set of options, these can be modified later
{
lngLat: {lat: 40.74072950731568, lng: -73.98918779556983}, // can also be an array [lng, lat]
heading: 91.1,
scale: 39.5,
visible: true,
altitude: 74.38,
altitudeReference: maptiler3d.AltitudeReference.GROUND,
}
);
// do stuff with the item 3D...
Here are all the options for meshes:
lngLat location of the center of the 3D object, as longitude and latitudealtitude the altitude in meters above the reference point (to the origin of the mesh, that is not always the bottom)altitudeReference reference point of altitude (ground or mean sea level)visible whether the mesh is visiblesourceOrientation applies a correction from the original orientation of the meshscale scaling factor applied to the mesh. Can be a single number for uniform scaling, or an array of three numbers [x, y, z] for non-uniform scaling.heading orientation in degrees (0-360), where 0 and 360 are true north and 90 is eastopacity opacity of the mesh. If the mesh is a group of meshes, this is applied to all the child nodes that can deal with transparencypointSize applicable only to point clouds, set the size of the pointswireframe applicable only to non-point cloud, applies a wireframe rendering to all the child nodes of the mesh that are compatible with the optionstates a set of properties that will be applied on different UI states (hover, active/click). For instance, a mesh could be scaled up on hover.userData a place to store arbitrary data, that can be retrieved at a later stage.transform (only for addMeshFromURL and cloneMesh) a set of props allowing for tweaking of the mesh before it's added to the map. This can be useful if a model internally points in the incorrect direction or if it needs a world space offset without having to tweak LngLat. { rotation: { x, y, z }, offset: { x, y, z } }. These can also be applied when cloning a mesh. Please Note: The offset when cloning a mesh is additive not absolute, it will be added to the world position of the mesh you are cloning.The constructor of the Layer3D class takes two arguments:
Layer3DOptionsHere are more details about the Layer3DOptions type:
type Layer3DOptions = {
/**
* Bellow this zoom level, the meshes are not visible
* Default: 0
*/
minZoom?: number;
/**
* Beyond this zoom level, the meshes are not visible.
* Default: 22
*/
maxZoom?: number;
/**
* Default: true
*/
antialias?: boolean;
/**
* Ambient light color.
* Default: `0xffffff` (white)
*/
ambientLightColor?: ColorRepresentation,
/**
* Ambient light intensity.
* Default: `1`
*/
ambientLightIntensity?: number,
};
Other important types that are exported:
enum AltitudeReference {
/**
* Use the ground as a reference point to compute the altitude
*/
GROUND = 1,
/**
* Uses mean sea level as a reference point to compute the altitude
*/
MEAN_SEA_LEVEL = 2
};
Example: A mesh that is add with the option altitudeReference being AltitudeReference.GROUND and an altitude of 10 will always "fly" 10 meters above the ground, regardless the terrain or the terrain exaggeration. If the provided altitude were to be a negative number, then it would always be beneath the ground surface by this amount (in meters). This mode is convenient for any item that needs to be positions relatively to the ground: cars, buildings, lap post, etc.
On the other hand, mesh that is add with the option altitudeReference being AltitudeReference.MEAN_SEA_LEVEL and the altitude of 1000 means the item will be at an absolute altitude of 1000 meters (3280 feet) above the mean sea level. If located in a place where the terrain shows mountains higher than 1000 meters, then the mesh will be underneath the ground surface and as such not visible. This mode is more convenient for flying objects such as planes, paraglydings, etc. as those thend to measure altitude with an absolute reference.
enum SourceOrientation {
/**
* The mesh was originaly created in a 3D space that uses the x axis as the up direction
*/
X_UP = 1,
/**
* The mesh was originaly created in a 3D space that uses the Y axis as the up direction
*/
Y_UP = 2,
/**
* The mesh was originaly created in a 3D space that uses the Z axis as the up direction
*/
Z_UP = 3,
};
Note that regardless of the original up axis, this module as well as MapTiler SDK/Maplibre GL JS only deal with 3D spaces that follow the right-hand rule.
type GenericObject3DOptions = {
/**
* Position.
* Default: `[0, 0]` (Null Island)
*/
lngLat?: LngLatLike,
/**
* Altitude above the reference (in meters).
* Default: `0` for meshes, or `2000000` for point lights.
*/
altitude?: number,
/**
* Reference to compute and adjust the altitude.
* Default: `AltitudeReference.GROUND` for meshes and `AltitudeReference.MEAN_SEA_LEVEL` for point lights.
*/
altitudeReference?: AltitudeReference,
/**
* Make the object visible or not.
* Default: `true`
*/
visible?: boolean;
};
export type MeshOptions = GenericObject3DOptions & {
/**
* Rotation to apply to the model to add, as a Quaternion.
* Default: a rotation of PI/2 around the x axis, to adjust from the default ThreeJS space (right-hand, Y up) to the Maplibre space (right-hand, Z up)
*/
sourceOrientation?: SourceOrientation;
/**
* Scale the mesh by a factor. Can be a single number for uniform scaling, or an array of three numbers `[x, y, z]` for non-uniform scaling.
* Default: no scaling added
*/
scale?: number | [number, number, number];
/**
* Heading measured in degrees clockwise from true north.
*/
heading?: number;
/**
* Opacity of the mesh
*/
opacity?: number;
/**
* Point size, applicable only to point clouds.
* Default: 1
*/
pointSize?: number;
/**
* Displays a mesh as wireframe if true (does not apply to point cloud)
* Default: `false`
*/
wireframe?: boolean;
/**
* Animation mode. How the animation should update.
* "manual" puts the responsibility on an external render loop to update.
* "continuous" automatically advances the animation with it's on internal animation loop.
* Default: `continuous`
*/
animationMode?: AnimationMode;
/**
* A set of properties that will be applied on different UI states (`hover`, `active`/`click`).
* For instance, a mesh could be scaled up on hover.
*/
states?: Item3DMeshUIStates;
/**
* A place to store arbitrary data, that can be retrieved at a later stage.
*/
userData?: Record<string, any>;
};
// The name of the state. `hover` is triggered on mouse enter/leave, `active` is triggered on mouse down/up.
export type Item3DMeshUIStateName = "default" | "hover" | "active";
// The properties that can be modified for a given state
export type Item3DMeshUIStateProperties = {
opacity?: number;
scale?: number | [number, number, number]; // note: this is _relative scale_ not absolute scale. Eg the scale in comparison to the items current size.
transform?: Item3DTransform;
heading?: number;
altitude?: number;
lngLat?: LngLatLike;
wireframe?: boolean;
pointSize?: number;
elevation?: number;
};
// The object to provide to the `states` option
export type Item3DMeshUIStates = {
[key in Item3DMeshUIStateName]?: Item3DMeshUIStateProperties;
};
// example
const item = layer3D.addMesh('mesh-id', mesh, {
opacity: 0.5,
states: {
hover: { opacity: 1 },
active: { scale: [2,2,2] }
}
})
addMeshFromURL **export type AddMeshFromURLOptions = MeshOptions & {
// wraps the object and transforms it accordingly.
// useful if your model does not point "north" or is not in the position you need it to be.
transform?: {
rotation?: {
x?: number;
y?: number;
z?: number;
};
offset?: {
x?: number;
y?: number;
z?: number;
};
};
};
type PointLightOptions = GenericObject3DOptions & {
/**
* Light color.
* Default: `0xffffff` (white)
*/
color?: ColorRepresentation,
/**
* Intensity of the light.
* Default: `75`
*/
intensity?: number,
/**
* Decay of the light relative to the distance to the subject.
* Default: `0.5`
*/
decay?: number,
};
Here is the list of instance methods of the Layer3D class:
.setAmbientLight(options: {color?: ColorRepresentation, intensity?: number} = {})
To adjust the settings of the ambient light. The type ColorRepresentation means the color can be a number (such as a hex notation 0xff0000, for red), a hex string (such as "#FF0000", for red), or a ThreeJS color (read more about these here).
ℹ️ By default, the ambiant light is white (0xffffff) with an intensity of 0.5.
.addMeshFromURL(id: string, url: string, options: AddMeshFromURLOptions = {}) async
Adds a mesh from a URL to a glTF of glb file, given a mesh ID (will throw if not unique) and a set of options. This method returns an Item3D object that can be modified later on.
.addMesh(id: string, mesh: Mesh | Group | Object3D, options: MeshOptions = {})
Adds a ThreeJS mesh/Group/Object3D, given a mesh ID (will throw if not unique) and a set of options. This method returns a Promise<Item3D> object that can be modified later on.
ℹ️ By default, the mesh will have some settings (if not overwritten by the options):
SourceOrientation.Y_UP0[0, 0]0true.getItem3D(id: string): Item3D | null
Returns the Item3D instance for a given ID. This object can be used to modify the mesh's properties and control animations. See the section bellow for the methods of the Item3D object.
.cloneMesh(sourceId: string, id: string, options: CloneMeshOptions)
Clones a mesh that has a given ID (sourceId) and create another one with a new ID (id). The provided options will overwrite the settings of the source mesh.
.addPointLight(id: string, options: PointLightOptions = {})
Adds a point light with a unique ID (will throw if not unique) and some options.
ℹ️ By default, the light will have some settings (if not overwritten by the options):
[0, 0] (null island)2_000_000 metersAltitudeReference.MEAN_SEA_LEVEL0xffffff (white)750.2.modifyPointLight(id: string, options: PointLightOptions)
Modify a point light given its ID.
ℹ️ Only the settings provided in the option object will be updated, the others will be left as they already are.
.removeMesh(id: string)
Remove a mesh or point light from the scene and frees the GPU memory associated to it
.clear()
Removes all the meshes and point lights from the scene and frees the GPU memory associated with them
Item3D objectThe addMesh, addMeshFromURL and cloneMesh methods return an Item3D object. You can also retrieve it later using layer.getItem3D(id). This object has its own set of methods to modify its properties and control animations.
Here is a list of the most common methods for the Item3D object:
.modify(options: Partial<MeshOptions>)
Modify the settings of a mesh (scale, lntLat, etc.)
ℹ️ Only the settings provided in the option object will be updated, the others will be left as they already are.
.getAnimationNames()
Gets all the animations that were loaded with the model.
.getAnimation(animationName: string)
Get the AnimationAction named animationName.
.playAnimation(animationName: string, loop: AnimationLoopOptions)
Plays animationName. Loop defines how the animation will loop. "loop" loops the animation infinity times; "once" loops it once; "pingPong" plays the animation until the end and then plays it in reverse.
.pauseAnimation(animationName: string)
Pauses animationName.
.stopAnimation(animationName: string)
Stops animationName.
.updateAnimation(delta = 0.02)
Updates the mesh animations by delta seconds. This is only useful when the animationMode is set to manual.
.setAnimationTime(time: number)
sets the animation to time seconds.
This project uses a combination of Vite for building and serving fixtures and Playwright for browser automation to perform end-to-end testing.
Currently we are unable to use Vitest as a test runner at present due to this issue with Playwright.
The testing setup consists of:
Configuration files:
vite.config-e2e.ts: Vite configuration for e2e testingplaywright.config.ts: Playwright browser configurationTest files are organized under the e2e directory:
public/: HTML files that run the test fixturessrc/: Test fixture implementation filesmocks/: Mock data and objects for testingsnapshots/: image snapshots for comparisontsconfig.json that extends the base project configTo add new test fixtures, add entry points to the rollup options in vite-config-e2e.ts.
Two npm scripts are available for testing; they must be run simultaneously:
# Start the test server that serves test fixtures
npm run e2e:serve
# Run the e2e tests against local test server
npm run e2e:local
# to update the snapshots pass the appropriate flag
npm run e2e:local -- --update-snapshots
# to run in ui mode...
npm run e2e:local -- --ui
Note: GitHub Actions integration for automated testing will be implemented in upcoming versions.
MapTiler JS Module
Copyright © 2024 MapTiler AG. All rights reserved.
The software and files (collectively “Software”) in this repository are licensed for use only with MapTiler service(s).
For the license terms, please reference MapTiler JavaScript Module Terms and Conditions.
This license allows users with an active MapTiler account to modify and integrate authorized portions of the Software for use with the relevant MapTiler service(s) in accordance with the MapTiler Terms. This license terminates automatically if a user no longer maintains a MapTiler account or their usage breaches MapTiler Terms.
FAQs
Add 3D things to your map, plugin for MapTiler SDK
We found that @maptiler/3d demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
React disclosed a CVSS 10.0 RCE in React Server Components and is advising users to upgrade affected packages and frameworks to patched versions now.

Research
/Security News
We spotted a wave of auto-generated “elf-*” npm packages published every two minutes from new accounts, with simple malware variants and early takedowns underway.

Security News
TypeScript 6.0 will be the last JavaScript-based major release, as the project shifts to the TypeScript 7 native toolchain with major build speedups.