Cesium 基本概念:创建实体和相机控制

基本概念

Entity

javascript 复制代码
// 创建一个实体
const entity_1 = viewer.entities.add({
  position: new Cesium.Cartesian3(0, 0, 10000000),
  point: {
    pixelSize: 10,
    color: Cesium.Color.BLUE
  }
});
javascript 复制代码
// 通过经纬度创建实体
const position = Cesium.Cartesian3.fromDegrees(180.0, 0.0);
// 创建一个恒星实体
let entity_2 = viewer.entities.add({
  name: 'entity_2',
  position: position,
  point: {
    pixelSize: 10,
    color: Cesium.Color.YELLOW,
    outlineColor: Cesium.Color.BLACK,
    outlineWidth: 2
  }
});
javascript 复制代码
    // 创建PolylineVolume实体
    const positions = Cesium.Cartesian3.fromDegreesArrayHeights([
      0, 0, 11742000,
      20, 0, 11742000,
      40, 0., 11742000,
      60, 0, 11742000,
      80, 0., 11742000,
      100, 0, 11742000,
      120, 0., 11742000,
    ]);

    const polyline = viewer.entities.add({
      polyline: {
        width: 4,
        positions: positions, // 路径点数组
        material: Cesium.Color.RED.withAlpha(0.5) // 设置半透明的红色材质
      }
    });

Cesium.Cartesian3代表固连在地球上的笛卡尔坐标系(大地坐标系)上的点。

大地坐标系x轴正方向为从地心指向经纬度(0,0)点方向,y轴从地心指向(90,0)方向,z轴从地心指向(0,90)方向。

Cesium提供(经度,纬度,高度)转大地坐标的方法Cesium.Cartographic.fromCartesian,和大地坐标转经纬高坐标的方法Cesium.Cartesian3.fromDegrees

DataSource

javascript 复制代码
const czml = [
  {
    id: "document",
    name: "CZML Point",
    version: "1.0",
    clock: {
      "interval": "2012-03-15T10:00:00Z/2012-03-15T15:00:00Z",
      "currentTime": "2012-03-15T10:00:00Z",
      "multiplier": 60,
      "range": "LOOP_STOP",
      "step": "SYSTEM_CLOCK_MULTIPLIER"
    }
  },
  {
    id: "point 1",
    name: "point",
    label: {
      fillColor: {
        rgba: [255, 255, 255, 255],
      },
      font: "13pt Lucida Console",
      horizontalOrigin: "LEFT",
      pixelOffset: {
        cartesian2: [20, 0],
      },
      style: "FILL",
      text: "referencing fillColor",
    },
    position: {
      "interpolationAlgorithm": "LAGRANGE",
      "interpolationDegree": 5,
      "referenceFrame": "INERTIAL",
      "epoch": "2012-03-15T10:00:00Z",
      cartographicDegrees: [
        0, .0, .0, 12742000,
        2000, 20.0, .0, 12742000,
        4000, 40.0, .0, 12742000,
        6000, 60.0, .0, 12742000,
        8000, 80.0, .0, 12742000,
        10000, 100.0, .0, 12742000,
        12000, 120.0, .0, 12742000,
        14000, 140.0, .0, 12742000,
        16000, 160.0, .0, 12742000,
        18000, 180.0, .0, 12742000,
      ],
    },
    point: {
      color: {
        rgba: [255, 255, 255, 255],
      },
      pixelSize: 5,
    },
    path: {
      "show": [
        {
          "interval": "2012-03-15T10:00:00Z/2012-03-16T10:00:00Z",
          "boolean": true
        }
      ],
      "width": 1,
      "material": {
        "solidColor": {
          "color": {
            "rgba": [
              255, 255, 255, 100
            ]
          }
        }
      },
      "resolution": 120,
      leadTime: 0,
      trailTime: 1e10
  }
];
javascript 复制代码
const dataSourcePromise = Cesium.CzmlDataSource.load(czml)
viewer.dataSources.add(dataSourcePromise)
dataSourcePromise.then(() => {
	viewer.zoomTo(dataSourcePromise)
})

czml中,第一个元素为描述信息,从第二个元素开始为实体。

其中path属性表示运动轨迹,可以通过leadTimetrailTime控制前后长度。

相机控制

采用以下方式控制相机绕地球自转
center为地心向z轴偏移1个单位的点,viewer.camera.lookAt(center, offfset)offset参数为以center为原点的局部坐标系,z轴正方向为地心到center的方向。

javascript 复制代码
let distance = 50000000
let height = 10000000
let a = 0
let lastTime: number | undefined = undefined
viewer.scene.preRender.addEventListener(() => {
	 // 抵消地球自转
	 let currentTime = viewer.clock.currentTime
	 let days = currentTime.dayNumber + currentTime.secondsOfDay / 24 / 3600
	 if (lastTime != undefined) {
	   a += (days - lastTime) * 2 * Math.PI
	 }
	 lastTime = days
	
	 if (viewer.scene.mode === Cesium.SceneMode.SCENE3D) {
	   viewer.camera.lookAt(center,
	     new Cesium.Cartesian3(distance * Math.sin(a), distance * Math.cos(a), height))
	 }
})
相关推荐
duansamve2 小时前
Cesium快速入门到精通系列教程二十五:以较长经纬度跨度为基准,将多边形充满屏幕,返回此时的中心点坐标及相机高度
cesium
阿琳a_6 天前
在github上部署个人的vitepress文档网站
前端·vue.js·github·网站搭建·cesium
云上飞476369627 天前
glb模型在Cesium中发黑的机理分析
cesium·glb模型发黑
ct9788 天前
Cesium的Primitive API
gis·webgl·cesium
Irene19919 天前
OpenLayers 和 Cesium 都是流行的开源 JavaScript 库,用于在网页上构建地图和地理空间应用
openlayers·cesium
fxshy9 天前
前端直连模型 vs 完整 MCP:大模型驱动地图的原理与实践(技术栈Vue + Cesium + Node.js + WebSocket + MCP)
前端·vue.js·node.js·cesium·mcp
棋鬼王10 天前
Cesium(十) 动态修改白模颜色、白模渐变色、白模光圈特效、白模动态扫描光效、白模着色器
前端·javascript·vue.js·智慧城市·数字孪生·cesium
duansamve10 天前
Cesium快速入门到精通系列教程二十四:限制相机在特定的Level之间展示地图
cesium
duansamve13 天前
Vue3的Vite项目中直接引入的方式使用Cesium
cesium
WebGISer_白茶乌龙桃14 天前
基于 Cesium 的 GLB 建筑模型分层分房间点击拾取技术实现
前端·javascript·vue.js·webgl·cesium