glTF plugin version 2
You can add glTF models to the map using the glTF plugin version 2. The plugin loads and renders models entirely using the MapGL engine. This allows you to apply various effects to the models that you set in the map style: fog, light, shadows, and others. Use the plugin if you need to show multiple models on the map without affecting the base map style.
With the plugin you can:
- add glTF models to the map, show, and hide them
- customize interactive scenes: add floor plans for buildings on the map, switch floors, and view information about rooms
Important
Unlike its first version, the version 2 of the glTF plugin does not use the Three.js library now. To switch to the second version, see the Key changes in version 2 section.
Also, you can add models to the map using the Style editor and data sources: for more details, see the 3D models instruction.
Getting started
To get started with the glTF plugin version 2, follow these steps:
- Add the plugin.
- Initialize the plugin.
- (Optional) Subscribe to plugin events.
1. Add the plugin
Using script tag
To use the plugin, add the line to the page code:
<script src="https://unpkg.com/@2gis/mapgl-gltf@^2/dist/bundle.js"></script>
The main plugin class is available in the mapgl
namespace:
const plugin = new mapgl.GltfPlugin(map);
Using npm package
If you use the npm, include the library using the @2gis/mapgl-gltf package:
npm install @2gis/mapgl-gltf
2. Initialize the plugin
To initialize the plugin, pass an existing map entity to the plugin constructor:
const plugin = new GltfPlugin(map);
Default values are applied to PluginOptions. To customize the plugin, pass additional options as the second argument:
const plugin = new GltfPlugin(map, {
hoverOptions: {
color: '#ff000088',
},
labelGroupDefaults: {
fontSize: 16,
},
modelsBaseUrl: 'https://example.com/s3_storage/gltf_models',
modelsLoadStrategy: 'dontWaitAll',
floorsControl: {
position: 'centerLeft',
},
groundCoveringColor: '#000000CC',
zIndex: 5,
});
Where:
-
hoverOptions
- models in the hover state. Only thecolor
option is available. The default value is#FFFFFF
. Color is applied by multiplying all the components. -
labelGroupDefaults
- default values for label groups, if LabelGroupOptions are not specified:-
fontSize
- font size for labels. The default value is14
. -
fontColor
- font color for labels. The default value is#000000
. -
image
- options for the label text background. Applied to the group if theimage
value in LabelGroupOptions is set asdefault
. The default value:{
url: 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjgiIGhlaWdodD0iMjgiIHZpZXdCb3g9IjAgMCAyOCAyOCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cmVjdCB3aWR0aD0iMjgiIGhlaWdodD0iMjgiIHJ4PSI0IiBmaWxsPSIjZWFlYWVhIi8+PHJlY3QgeD0iMSIgeT0iMSIgd2lkdGg9IjI2IiBoZWlnaHQ9IjI2IiByeD0iMyIgZmlsbD0id2hpdGUiLz48L3N2Zz4=',
size: [38, 38],
stretchX: [[4, 24]],
stretchY: [[4, 24]],
padding: [5, 10, 5, 10],
}
-
-
modelsBaseUrl
- URL to define the relative path of the model. If the model URL is set without a protocol (http://
orhttps://
) and the domain name, models are loaded relatively to this parameter. The default value is''
, so all models are loaded relatively to the web application host. -
modelsLoadStrategy
- strategy of loading and displaying models. Possible values:waitAll
- all models are loaded and then displayed on the map simultaneously (default value). For interactive scenes, all models from the scene configuration (including floor plan models) are loaded simultaneously. This increases the amount of loaded data and the time of displaying the scene on the map. However, switching floor plans happens without delays. All models from the realty scene appear on the map synchronously.dontWaitAll
- each model is displayed as soon as it is loaded. For interactive scenes, only the main models (including a floor plan if the initial scene state is defined) are loaded. This enables reducing the amount of loaded data and displaying the scene as soon as possible. Models are displayed asynchronously as each model is loaded. During floor plan switching, the target floor is shown with a delay unless it has been loaded before.
-
floorsControl
- settings of the UI element with floor buttons on the interactive realty scene. Only theposition
option is currently available. The default value iscenterLeft
. See the list of possible values in the ControlPosition. -
groundCoveringColor
- map background color when an underground floor is displayed. The default value is#F8F8EBCC
-
zIndex
- order of rendering plugin objects (models, labels) relative to other Objects on the map.
3. Subscribe to plugin events
To subscribe to plugin events that occur during interaction with models and labels:
plugin.on('click', (event) => {
console.log(event);
});
See the list of events that you can subscribe to at GltfPluginEventTable. Depending on the object provokes an event, a GltfPluginModelEvent or a GltfPluginLabelEvent is passed to the handler function.
Working with models
To add a model to the map, you need to upload and display it. The way the model is displayed on the map depends on the modelsLoadStrategy
option specified during plugin initialization.
Adding a single model
-
Before uploading, make sure a model meets the requirements.
-
To upload the model, use the
addModel()
method and pass ModelOptions as the first argument (the second argument is not specified):plugin.addModel({
modelId: 'ea234f1',
modelUrl: 'http://example.com/models/model1.glb',
coordinates: [55.27087, 25.19668],
rotateZ: -116,
scale: 2,
interactive: true,
});Specify:
-
modelId
- unique identifier of the model. -
modelUrl
- URL for uploading the model. -
coordinates
- geographic coordinates of the model (longitude and latitude). -
rotateZ
- rotation of the model relative to the Z-axis in degrees. -
scale
- scale of the model specified in relative units based on the model original size. -
interactive
- flag indicating whether the model is interactive.
Note
Instead of manually adjusting the model positioning settings (coordinates, rotation, and scale), use the tool for positioning models on the map. You can position the model and copy the configuration values: for more details, see the Placing models on the map section.
-
-
If you need to upload the model into the cache without displaying it immediately, pass
true
as the second argument to theaddModel()
method. -
To display the model, use the
showModel()
method and pass its identifier:plugin.showModel('ea234f1');
Adding multiple models
-
Before uploading, make sure models meet the requirements.
-
To upload models, use the
addModels()
method and pass an array of ModelOptions as the first argument (the second argument is not specified):const models = [
{
modelId: 'ea234f1',
modelUrl: 'http://example.com/models/model1.glb',
coordinates: [55.27087, 25.19668],
rotateZ: -116,
scale: 2,
interactive: true,
},
{
modelId: '347da1',
modelUrl: 'http://example.com/models/model2.glb',
coordinates: [55.27087, 25.19668],
rotateZ: -116,
scale: 2,
},
];
plugin.addModels(models);Specify:
-
modelId
- unique identifier of the model. -
modelUrl
- URL for uploading the model. -
coordinates
- geographic coordinates of the model (longitude and latitude). -
rotateZ
- rotation of the model relative to the Z-axis in degrees. -
scale
- scale of the model specified in relative units based on the model original size. -
interactive
- flag indicating whether the model is interactive.
Note
Instead of manually adjusting the model positioning settings (coordinates, rotation, and scale), use the tool for positioning models on the map. You can position the model and copy the configuration values: for more details, see the Placing models on the map section.
-
-
If you need to upload the models into the cache but display only some of them, pass an array of their identifiers as the second argument to the
addModels()
method:plugin.addModels(models, ['ea234f1']);
To upload the models into the cache without displaying them immediately, pass an empty array as the second argument:
plugin.addModels(models, []);
-
To display the models, use the
showModels()
orshowModel()
methods and pass their identifiers:plugin.showModels(['ea234f1', '347da1']);
Or:
plugin.showModel('ea234f1');
Example of adding models to the map:
Replacing a map object with a model
If you need to display a model instead of an object on the map (for example, a building) that overlaps the model, hide the object:
-
Get object identifiers:
-
Enable logging of object identifiers on the map when clicked:
map.on('click', (e) => {
console.log(e);
}); -
Open the browser Developer Tools (press F12) and click the objects you want to hide. Copy the identifier from the
target.id
field of the click event. For example:{
"originalEvent": {
"isTrusted": true
},
"lngLat": [55.27087, 25.19668],
"point": [420, 231],
"target": {
"id": "13933647002592567"
},
"targetData": {
"type": "default",
"id": "13933647002592567"
}
}
-
-
Pass an array of object identifiers to the
linkedIds
field in ModelOptions:plugin.addModel({
modelId: 'ea234f1',
modelUrl: 'http://example.com/models/model1.glb',
linkedIds: ['13933647002592567'],
coordinates: [55.27087, 25.19668],
});
Adding labels
You can display additional text on the map using labels. The labels can be linked to a model or not. Labels can contain any information, such as building characteristics, apartment area, location details, or links. Label settings are added to the target
field of the plugin label event GltfPluginLabelEvent.
To add labels to the map, use the addLabelGroup()
method and pass LabelGroupOptions as an argument:
const labelGroup = {
id: '722ea9',
image: 'default',
minZoom: 17,
elevation: 30,
interactive: true,
labels: [
{
coordinates: [55.27087, 25.19668],
text: '10 sqrt',
userData: {},
},
{
coordinates: [55.27087, 25.19668],
text: '20 sqrt',
elevation: 33,
interactive: false,
},
],
};
plugin.addLabelGroup(labelGroup);
Specify:
minZoom
- the minimum zoom level at which labels in the group are displayed.elevation
- height of labels in the group.interactive
- flag indicating whether labels in the group are interactive.labels
- array of settings (LabelOptions) for each label. If settings for a particular label are specified (for example,elevation
orinteractive
), the value from the label takes precedence over the group value:coordinates
- geographic coordinates of the label (longitude and latitude).text
- label text.userData
- custom parameters.elevation
- label height.interactive
- flag indicating whether the label is interactive.
To link a group of labels to a model (e.g., a building), pass the model options BuildingState as the second argument:
plugin.addLabelGroup(labelGroup, {
buildingId: 'ea234f1',
});
To link a group of labels to a building floor, specify the floorId
:
plugin.addLabelGroup(labelGroup, {
buildingId: 'ea234f1',
floorId: '123456',
});
Removing labels
To remove a group of labels from the map, use the removeLabelGroup()
method and pass the group identifier as an argument:
plugin.removeLabelGroup('722ea9');