前言
缺少前置学习使用资料,请自行查阅:[/weixin_44402694/article/details/123110136](/weixin_44402694/article/details/123110136)以下示例使用到的公共静态资料,不建议下载,建议官网自行下载超图Build资源,示例所涉及图片会在示例使用到时提供出来。如有需要可下载:[/download/weixin_44402694/82180350](/download/weixin_44402694/82180350)。聚光源设置。
使用
效果完整代码
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>光源 - 聚光源设置</title><link href="./public/Build/Cesium/Widgets/widgets.css" rel="stylesheet" /><scripttype="text/javascript"src="./public/Build/Cesium/Cesium.js"></script><style>* {margin: 0;padding: 0;}html,body,#cesium-container {width: 100%;height: 100%;}.panel {position: fixed;left: 10px;top: 10px;z-index: 1;background-color: #fff;padding: 10px;}.panel .btn {cursor: pointer;}</style></head><body><div id="cesium-container" /><div class="panel"><p class="btn">添加聚光源</p><p class="btn">清除聚光源</p></div><script>// 前置引入echarts|echartlayer js脚本let viewer, scene, spotLightwindow.onload = function () {viewer = new Cesium.Viewer('cesium-container')scene = viewer.sceneloadModelHandler()initBindBtnEventHandler()}// 初始化绑定按钮的点击事件function initBindBtnEventHandler() {const btns = document.querySelectorAll('.panel .btn')btns[0].addEventListener('click', () => {activeCurrentClickBtnHandler(0)addSpotLightHandler()})btns[1].addEventListener('click', () => {activeCurrentClickBtnHandler(1)removeSpotLightHandler()})}// 加载模型function loadModelHandler() {const promise = viewer.scene.open('/realspace/services/3D-BIMbuilding/rest/realspace')Cesium.when(promise, () => {setSceneCameraViewHandler()})}// 添加点光源到场景中function addSpotLightHandler() {const position = new Cesium.Cartesian3(-2180745.273232958,4379070.071537397,4092580.1516873706)const targetPosition = new Cesium.Cartesian3(-2180741.228113831,4379061.016078637,4092570.800119596)const options = {color: new Cesium.Color(1, 1, 1, 1), // 光源颜色distance: 50, // 光照距离decay: 0.5, // 衰减因子intensity: 20, // 光源强度}spotLight = new Cesium.SpotLight(position, targetPosition, options)scene.addLightSource(spotLight)}// 删除点光源function removeSpotLightHandler() {scene.removeLightSource(spotLight)}// 设置场景指定视角function setSceneCameraViewHandler() {scene.camera.setView({destination: new Cesium.Cartesian3(-2180753.065987198,4379023.266141494,4092583.575045952),orientation: {heading: 4.0392222751147955,pitch: 0.010279641987852584,roll: 1.240962888005015e-11,},})}// 高亮当前点击的按钮function activeCurrentClickBtnHandler(idx) {const btns = document.querySelectorAll('.panel .btn')Array.from(btns).forEach((btn, index) => {btn.style.color = index === idx ? 'red' : '#000'})}</script></body></html>