Skip to main content

Model highlight selection

Highlight the 3D model by moving the mouse.

show
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Model Highlight Selection</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"/>
<style>
body {
margin: 0;
padding: 0;
}

#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
<script src="https://delivery.mapmost.com/cdn/sdk/webgl/v9.16.0/mapmost-webgl-min.js"></script>
</head>
<body>
<div id="map"></div>
<script>
let map = new mapmost.Map({
container: 'map',
style: "<your style url>",
center: [120.69647280968667, 31.35628749488366],
zoom: 18,
pitch: 60,
bearing: -7.200086687507338,
userId: '***', // Authorization code
env3D: {
exposure: 2.0,
envMap: '../../../../static/example_data/hdr/WhiteBG_Ref_1K.hdr'
}
});

let THREE = mapmost.THREE;
map.on('load', function () {

let modelLayer,selectedObj;

//Add model
let models_obj = [{
type: 'glb',
url: "../example_data/house.glb"
}];

const center = [120.69647280968667, 31.35628749488366];

let options = {
id: 'model_id',
type: 'model',
callback: function (group, layer) {
modelLayer = layer;

// add house
modelLayer.addModel(models_obj, center);
}
};
map.addLayer(options);


//Add mouse hover event
map.on('mousemove', function (e) {
if (modelLayer) {
selectObj(e.point)
}
});

//Select a model based on the screen mouse position
function selectObj(point) {

let intersect = modelLayer.selectModel(point)[0];

if (intersect) {
let nearestObject = intersect.object;

// The selected layer is not the previously selected layer
if (!(selectedObj === nearestObject.parent)) {

_remove();

selectedObj = nearestObject.parent;

selectedObj.traverse(function (child) {
if (child instanceof THREE.Mesh) {

// Store the original material into custom properties
child.baseMaterial = child.material;

//Copy the original material and modify the color
child.material = child.material.clone();
child.material.color = new THREE.Color(0xffff00);
}
});
}

} else {
_remove();
}
}

// restore
function _remove() {
if (selectedObj) {
selectedObj.traverse(function (child) {
child.material = child.baseMaterial;
});
selectedObj = undefined;
}
}
})
</script>
</body>
</html>