27.cesuim分析之限高分析

文章展示了如何在Cesium中使用HeightLimit类创建一个可穿透的高亮区域,用于地图上的建筑物高度限制,提供定制颜色和透明度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

在这里插入图片描述

前端代码

HeightLimit实体类

const {
    Cartesian3,
    GeometryInstance,
    PolygonGeometry,
    PolygonHierarchy,
    ColorGeometryInstanceAttribute,
    Color,
    ClassificationPrimitive,
    ClassificationType,
    Cartographic,
    Matrix4,
    ShowGeometryInstanceAttribute
  } = Cesium
  
  const handleColor = (color, alpha)=> {
    return Color.fromCssColorString(color).withAlpha(alpha || 1)
  }
  
  export default class HeightLimit {
    limitHeightPrimitive = null
    constructor({ viewer, data, style = {} }) {
      this.viewer = viewer
      this.data = data
      this.style = style
      this.addPrimitive()
    }
    addPrimitive() {
      let { height = 0, color = 'pink', alpha } = this.style
      color = handleColor(color, alpha)
      const limitHeightPrimitive = this.viewer.scene.primitives.add(
          /**
          https://cesium.com/learn/cesiumjs/ref-doc/ClassificationPrimitive.html?classFilter=ClassificationPrimitive
          ClassificationPrimitive 用来生成可以穿透
          */
        new ClassificationPrimitive({
          geometryInstances: new GeometryInstance({
            geometry: new PolygonGeometry({
              polygonHierarchy: new PolygonHierarchy(
                Cartesian3.fromDegreesArray(this.data)
              ),
              height,
              extrudedHeight: height + 3000
            }),
            attributes: {
              color: ColorGeometryInstanceAttribute.fromColor(color),
              show: new ShowGeometryInstanceAttribute(true)
            }
          }),
          releaseGeometryInstances: false,
          /**
          https://cesium.com/learn/cesiumjs/ref-doc/global.html#ClassificationType
          只有 3D Tiles 会被分类
           */
          classificationType: ClassificationType.CESIUM_3D_TILE
        })
      )
      limitHeightPrimitive.readyPromise.then(() => {
      })
  
      // 这里主要是为了添加一个高出地面的 polygon , 用到的经纬度数据和需要校验的建筑物数据一样
      this.setPolygon(height, alpha )
    }
    // 这里主要是为了添加一个高出地面的 polygon , 用到的经纬度数据和需要校验的建筑物数据一样
    setPolygon(height, alpha ) {
      this.viewer.entities.add({
        polygon: {
          hierarchy: new PolygonHierarchy(
            Cartesian3.fromDegreesArray(this.data)
          ),
          material: Color.fromCssColorString('#FFF8DC').withAlpha(alpha || 1),
          height,
          perPositionHeight: false,
        }
      })
    }
  }
 

    

调用

 new HeightLimit({
            viewer: viewer,
            data: [
                108.9371687725441,34.20162482945407,

                108.9671687725441,34.20162482945407,
                108.9671687725441,34.24162482945407,
                108.931687725441,34.24162482945407,

                108.9371687725441,34.20162482945407
            ],
            style: {
              color: 'pink',
              alpha: 0.5,
              height: 410
            }
          })
### 将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画布上下文使它们协同工作。请注意,实际部署时还需要解决诸如性能优化、事件监听同步等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

紫雪giser

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

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

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

打赏作者

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

抵扣说明:

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

余额充值