三维场景射线拾取坐标
支持定义射线起点以及射线方向上的某一点,获取该射线与三维场景碰撞到的第一个点的坐标。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>三维场景射线拾取坐标</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%;
}
info,
input,
.button {
line-height: 30px;
font-weight: bold;
color: #fff;
text-shadow: 1px 1px 1px #333;
border-radius: 3px;
margin: 0 8px 8px 0;
position: relative;
overflow: hidden;
padding: 0px 15px 0px 15px;
}
</style>
<script src="https://delivery.mapmost.com/cdn/sdk/webgl/v9.3.0/mapmost-webgl-min.js"></script>
<script src="https://delivery.mapmost.com/cdn/sdk/lib/html2canvas.min.js"></script>
</head>
<body>
<div id="divButton" class="param-container tool-bar">
<div>
<span>起点:</span>
<input id="start" class="black" value="120.74281553273873, 31.31016407374276, 110"></input>
</div>
<div>
<span>终点:</span>
<input id="end" class="black" value="120.7430341385251, 31.30940587176734, 0"></input>
</div>
<div>
<span>拾取点:</span>
<info id="info"></info>
</div>
<button type="button" class="button black" onclick="rayPick()">
开始射线拾取
</button>
</div>
<div id="map"></div>
<script>
let map = new mapmost.Map({
container: 'map',
style:'<your style url>',
bearing: 60.28860120101672,
center: [120.74422077120391, 31.310685606567674],
pitch: 61.9938489363649,
zoom: 17.71081067417807,
userId: '***', // 授权码
});
let tileLayer, modelLayer
map.on('load', function () {
// 添加3DTiles图层
map.addLayer({
type: '3DTiles',
id: 'tile-3d-layer',
data: '<your 3DTiles url>',
callback: function (group, layer) {
tileLayer = layer;
},
});
// 添加三维模型图层
let options = {
id: 'model',
type: 'model',
callback: function (group, layer) {
modelLayer = layer;
}
};
map.addLayer(options);
})
// 射线拾取
function rayPick() {
let start = document.getElementById('start').value.split(',');
let end = document.getElementById('end').value.split(',');
let startPoint = [Number(start[0]), Number(start[1]), Number(start[2])];
let endPoint = [Number(end[0]), Number(end[1]), Number(end[2])];
// 添加射线
modelLayer.addLines({
type: "pipe",
color: "#ffff00",
width: 5,
data: [{
coordinate: [startPoint, endPoint]
}]
})
// 射线拾取坐标
let result = map.rayPick({
startPoint: startPoint,
endPoint: endPoint,
layers: [tileLayer]
})
// 显示拾取点坐标
document.getElementById('info').innerText = `${result.toString()}`
};
</script>
</body>
</html>