代码:
// 设为椭球
var obj = [6378137.0, 6378137.0, 6356752.3142451793];
Cesium.Ellipsoid.WGS84 = Object.freeze(new Cesium.Ellipsoid(obj[0], obj[1], obj[2]));
var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene
scene.lightSource.ambientLightColor = new Cesium.Color(0.65, 0.65, 0.65, 1);
var tileset = viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
url: 'http://10.90.3.116:8999/3dTiles/995ab8c02e114841b758259df5305492/tileset.json'
}));
// 定位
tileset.readyPromise.then(function () {
var boundingSphere = tileset.boundingSphere;
viewer.camera.viewBoundingSphere(boundingSphere, new Cesium.HeadingPitchRange(0.0, -0.5, boundingSphere.radius));
viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
}).otherwise(function (error) {
throw(error);
});
// 监听鼠标点击事件
viewer.screenSpaceEventHandler.setInputAction(function(click) {
// 使用ray和场景进行交互检测
var pickedFeature = viewer.scene.pick(click.position);
// 检查是否点击了模型
if (Cesium.defined(pickedFeature) && pickedFeature instanceof Cesium.Cesium3DTileFeature) {
// 获取点击的模型的属性
var attribute = pickedFeature.getProperty('buildid');
console.log('buildid:', attribute);
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);