cesium加载魔方立方体

cesium加载多个小立方体,组合拼成一个大立方体。

地理坐标拼合的大立方体有错位问题。必须进行坐标转换。

javascript 复制代码
<template>
	<div class="map">
		<div id="mapContainer" ref="mapContainer" class="map-container"></div>
		<div class="map-other">
			<Steps progress-dot :current="currentVal" @change="changeStepsEvt">
				<Step title="1"></Step>
				<Step title="2"></Step>
				<Step title="3"></Step>
				<Step title="4"></Step>
			</Steps>
		</div>
	</div>
</template>
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { Steps, Step } from 'ant-design-vue';
import * as THREE from 'three';
import { log } from 'console';
const currentVal = ref(0);
const win: AnyObject = window;
const Cesium = win.Cesium;
let viewer: any,
	cubeEntity: any = {};
let count = 0;
const init = () => {
	viewer = new Cesium.Viewer('mapContainer', {
		animation: false, //动画小部件
		baseLayerPicker: false, //地图图层组件
		infoBox: false, //信息框
		selectionIndicator: false, //选取指示器组件
	});
	// 获取CreditDisplay实例
	let creditDisplay = viewer.scene.frameState.creditDisplay;

	// 隐藏所有logo
	creditDisplay.show = false;
	// 立方体的边长
	const cubeSize = 100.0; // 100米边长

	// 第一个立方体的位置(成都经纬度,100米高度)
	const longitude = 104.0668;
	const latitude = 30.5728;
	const height = 100;
	const position1 = Cesium.Cartesian3.fromDegrees(
		longitude,
		latitude,
		height,
	);

	// 将地理坐标转换为局部坐标系
	const ellipsoid = viewer.scene.globe.ellipsoid;
	const origin = Cesium.Transforms.eastNorthUpToFixedFrame(
		position1,
		ellipsoid,
	);

	for (let i = 0; i < 3; i++) {
		for (let j = 0; j < 3; j++) {
			for (let k = 0; k < 3; k++) {
				const offset = new Cesium.Cartesian3(
					cubeSize * i,
					cubeSize * j,
					cubeSize * k,
				); // 在X轴上偏移
				const position = Cesium.Matrix4.multiplyByPoint(
					origin,
					offset,
					new Cesium.Cartesian3(),
				);
				cubeEntity[count] = viewer.entities.add({
					name: `Cube-${i}-${j}-${k}`,
					position: position,
					box: {
						dimensions: new Cesium.Cartesian3(
							cubeSize,
							cubeSize,
							cubeSize,
						),
						// material: Cesium.Color.fromRandom({ alpha: 1 }), // 半透明的蓝色
						material: Cesium.Color.fromBytes(177, 0, 4, 200), // 半透明的蓝色
						outline: true,
						outlineColor: Cesium.Color.WHITE,
						outlineWidth: 2,
					},
				});
				count++;
			}
		}
	}

	// 将视角调整到立方体的中心
	// viewer.zoomTo(viewer.entities);

	viewer.camera.flyTo({
		destination: Cesium.Cartesian3.fromDegrees(104.0658, 30.5682, 1200), // 目标位置和高度
		orientation: {
			heading: Cesium.Math.toRadians(30), // 相机航向角
			pitch: Cesium.Math.toRadians(-60), // 相机俯仰角
			roll: Cesium.Math.toRadians(0), // 相机滚转角
		},
		duration: 1, // 飞行持续时间(秒)
	});
};
onMounted(() => {
	init();
});
const colorList = [
	{
		color: Cesium.Color.fromBytes(177, 0, 4, 200),
	},
	{
		color: Cesium.Color.fromBytes(0, 206, 209, 200),
	},
	{
		color: Cesium.Color.fromBytes(106, 90, 205, 200),
	},
	{
		color: Cesium.Color.fromBytes(255, 255, 0, 100),
	},
];
const changeStepsEvt = (current) => {
	currentVal.value = current;
	for (let i = 0; i < count; i++) {
		cubeEntity[i].box.material = colorList[currentVal.value].color;
	}
};
setInterval(() => {
	currentVal.value++;
	if (currentVal.value >= 4) {
		currentVal.value = 0;
	}
	for (let i = 0; i < count; i++) {
		cubeEntity[i].box.material = colorList[currentVal.value].color;
	}
}, 3000);
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
相关推荐
QGC二次开发5 分钟前
Vue3 : Pinia的性质与作用
前端·javascript·vue.js·typescript·前端框架·vue
子非鱼9211 小时前
【前端】ES6:Set与Map
前端·javascript·es6
想退休的搬砖人1 小时前
vue选项式写法项目案例(购物车)
前端·javascript·vue.js
啥子花道2 小时前
Vue3.4 中 v-model 双向数据绑定新玩法详解
前端·javascript·vue.js
麒麟而非淇淋2 小时前
AJAX 入门 day3
前端·javascript·ajax
茶茶只知道学习2 小时前
通过鼠标移动来调整两个盒子的宽度(响应式)
前端·javascript·css
清汤饺子2 小时前
实践指南之网页转PDF
前端·javascript·react.js
蒟蒻的贤2 小时前
Web APIs 第二天
开发语言·前端·javascript
清灵xmf2 小时前
揭开 Vue 3 中大量使用 ref 的隐藏危机
前端·javascript·vue.js·ref
蘑菇头爱平底锅2 小时前
十万条数据渲染到页面上如何优化
前端·javascript·面试