28.cesuim分析之淹没分析

文章介绍了如何在前端使用Cesium库实现一个可交互的水体效果,通过定时器改变水体高度,实现从低到高平滑过渡。

效果图

在这里插入图片描述

前端代码

let Cesium = null

let showWater =false
let waterEntity=null

export const drawWater=(viewer)=> {
    Cesium = DC.Namespace.Cesium
    showWater = true
   waterEntity && viewer.entities.remove(waterEntity)
    const waterCoord = [119.642769, 30.361905, 100, 119.591604, 30.448757, 100, 119.695767, 30.488232, 100, 119.748406, 30.395983, 100]

    let startHeight = 100
    const targetHeight = 500
   waterEntity = viewer.entities.add({
      polygon: {
        hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights(waterCoord),
        material: Cesium.Color.fromBytes(64, 157, 253, 200),
        perPositionHeight: true,
        extrudedHeight: new Cesium.CallbackProperty(() => { return startHeight }, false)
      }
    })
    viewer.flyToPosition(
        new DC.Position(119.66332661540228,30.443762489412983, 2000, 0, -90, 0)
    )
    const waterInterval = setInterval(() => {
      if (startHeight < targetHeight) {
        startHeight += 10
        if (startHeight >= targetHeight) {
          startHeight = targetHeight
          clearInterval(waterInterval)
          showWater = false
        }
        // 使用该方式会闪烁,改用 Cesium.CallbackProperty 平滑
        // this.waterEntity.polygon.extrudedHeight.setValue(startHeight)
      }
    }, 100)

  }

### 将Three.js与Cesium结合用于Web开发 在现代Web地理信息系统(GIS)应用中,将Three.js和Cesium集成可以充分利用两者的优势来创建复杂而高效的三维场景。Three.js是一个轻量级的JavaScript库,专注于提供易于使用的API来进行3D图形渲染;而Cesium则特别擅长处理地球坐标系下的大规模数据集以及高精度的地图展示。 为了实现这种组合,在项目初始化阶段应该考虑采用模块化的方式引入这两个框架。通常情况下,可以通过npm包管理器安装所需的依赖项: ```bash npm install three cesium --save ``` 接着可以在应用程序入口文件中加载这些资源并配置环境变量以支持Cesium Ion资产访问(如果需要的话)[^1]。 对于具体的整合方法之一是在Three.js的应用程序内部嵌入Cesium Widget作为其中的一个组件。这涉及到调整相机视角、灯光设置以及其他可能影响两个引擎之间交互的因素。下面给出一段简单的代码片段用来说明这一过程: ```javascript import * as THREE from 'three'; // Import and configure Cesium ion assets (if necessary) window.CESIUM_BASE_URL = './Build/Cesium/'; import { Viewer } from "cesium"; const viewer = new Viewer('cesiumContainer', { terrainProvider : Cesium.createWorldTerrain() }); // Create a Three.js scene, camera, renderer etc. let scene = new THREE.Scene(); let camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); camera.position.set(-2e6, -8e6, 9e6); // Adjust position according to your needs. let renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // Add the Cesium canvas into the Three.js scene graph by creating an HTML element texture var cesiumCanvasTexture = new THREE.Texture(viewer.canvas); scene.background = cesiumCanvasTexture; function animate() { requestAnimationFrame(animate); // Update the background texture before rendering each frame cesiumCanvasTexture.needsUpdate = true; renderer.render(scene, camera); } animate(); ``` 这段脚本展示了如何在一个页面上同时运行Two.js和Cesium,并通过共享相同的HTML画布上下文使它们协同工作。请注意,实际部署时还需要解决诸如性能优化、事件监听同步等问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫雪giser

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值