vue+cesium示例:地形開挖(附源碼下載)

基于cesium和vue繪制多邊形實現地形開挖效果,適合學習Cesium與前端框架結合開發3D可視化項目。

demo源碼運行環境以及配置

運行環境:依賴Node安裝環境,demo本地Node版本:推薦v18+。

運行工具:vscode或者其他工具。

配置方式:下載demo源碼,vscode打開,然后順序執行以下命令:
(1)下載demo環境依賴包命令:npm install
(2)啟動demo命令:npm run dev
(3)打包demo命令: npm run build

技術棧

Vue 3.5.13

Vite 6.2.0

Cesium 1.128.0

示例效果
在這里插入圖片描述
核心源碼

<template><div id="cesiumContainer" class="cesium-container"><!-- 模型調整控制面板 --><div class="control-panel"><div class="panel-header"><h3>地形開挖</h3></div><div class="panel-body"><div class="control-group"><button @click="startDrawPolygon">繪制多邊形</button></div><div class="control-group"><button @click="clearDrawing">清除繪制</button></div><div class="control-group" v-if="drawingInstructions"><span>{{ drawingInstructions }}</span></div></div></div></div>
</template><script setup>
import { onMounted, onUnmounted, ref } from 'vue';
import * as Cesium from 'cesium';Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxZjhjYjhkYS1jMzA1LTQ1MTEtYWE1Mi0zODc5NDljOGVkMDYiLCJpZCI6MTAzNjEsInNjb3BlcyI6WyJhc2wiLCJhc3IiLCJhc3ciLCJnYyJdLCJpYXQiOjE1NzA2MDY5ODV9.X7tj92tunUvx6PkDpj3LFsMVBs_SBYyKbIL_G9xKESA';
// 聲明Cesium Viewer實例
let viewer = null;
// 聲明變量用于存儲事件處理器和繪制狀態
let handler = null;
let activeShapePoints = [];
let activeShape = null;
let floatingPoint = null;
let excavateInstance = null;// 繪制狀態提示
const drawingInstructions = ref('');// 組件掛載后初始化Cesium
onMounted(async () => {const files = ["./excavateTerrain/excavateTerrain.js"];loadScripts(files, function () {console.log("All scripts loaded");initMap();});
});
const loadScripts = (files, callback) => {// Make Cesium available globally for the scriptswindow.Cesium = Cesium;if (files.length === 0) {callback();return;}const file = files.shift();const script = document.createElement("script");script.onload = function () {loadScripts(files, callback);};script.src = file;document.head.appendChild(script);
};
const initMap = async () => {// 初始化Cesium Viewerviewer = new Cesium.Viewer('cesiumContainer', {// 基礎配置animation: false, // 動畫小部件baseLayerPicker: false, // 底圖選擇器fullscreenButton: false, // 全屏按鈕vrButton: false, // VR按鈕geocoder: false, // 地理編碼搜索框homeButton: false, // 主頁按鈕infoBox: false, // 信息框 - 禁用點擊彈窗sceneModePicker: false, // 場景模式選擇器selectionIndicator: false, // 選擇指示器timeline: false, // 時間軸navigationHelpButton: false, // 導航幫助按鈕navigationInstructionsInitiallyVisible: false, // 導航說明初始可見性scene3DOnly: false, // 僅3D場景terrain: Cesium.Terrain.fromWorldTerrain(), // 使用世界地形});// 隱藏logoviewer.cesiumWidget.creditContainer.style.display = "none";viewer.scene.globe.enableLighting = true;// 禁用大氣層和太陽viewer.scene.skyAtmosphere.show = false;//前提先把場景上的圖層全部移除或者隱藏 // viewer.scene.globe.baseColor = Cesium.Color.BLACK; //修改地圖藍色背景viewer.scene.globe.baseColor = new Cesium.Color(0.0, 0.1, 0.2, 1.0); //修改地圖為暗藍色背景// 設置抗鋸齒viewer.scene.postProcessStages.fxaa.enabled = true;// 清除默認底圖viewer.imageryLayers.remove(viewer.imageryLayers.get(0));// 加載底圖 - 使用更暗的地圖服務const imageryProvider = await Cesium.ArcGisMapServerImageryProvider.fromUrl("https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer");viewer.imageryLayers.addImageryProvider(imageryProvider);// 設置默認視圖位置 - 默認顯示全球視圖viewer.camera.setView({destination: Cesium.Cartesian3.fromDegrees(104.0, 30.0, 10000000.0), // 中國中部上空orientation: {heading: 0.0,pitch: -Cesium.Math.PI_OVER_TWO,roll: 0.0}});// 啟用地形深度測試,確保地形正確渲染viewer.scene.globe.depthTestAgainstTerrain = true;// 清除默認地形// viewer.scene.terrainProvider = new Cesium.EllipsoidTerrainProvider({});// 開啟幀率viewer.scene.debugShowFramesPerSecond = true;
}
// 開始繪制多邊形
const startDrawPolygon = () => {// 清除之前的繪制clearDrawing();// 設置繪制提示drawingInstructions.value = '左鍵點擊添加頂點,右鍵完成繪制';// 創建新的事件處理器handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);// 監聽左鍵點擊事件 - 添加頂點handler.setInputAction((click) => {// 獲取點擊位置的笛卡爾坐標const cartesian = viewer.scene.pickPosition(click.position);if (!Cesium.defined(cartesian)) {return;}// 將點添加到活動形狀點數組activeShapePoints.push(cartesian);// 如果是第一個點,創建浮動點if (activeShapePoints.length === 1) {floatingPoint = createPoint(cartesian);// 創建動態多邊形activeShape = createPolygon(activeShapePoints);}}, Cesium.ScreenSpaceEventType.LEFT_CLICK);// 監聽鼠標移動事件 - 更新動態多邊形handler.setInputAction((movement) => {if (activeShapePoints.length >= 1) {const cartesian = viewer.scene.pickPosition(movement.endPosition);if (!Cesium.defined(cartesian)) {return;}// 更新浮動點位置if (floatingPoint) {floatingPoint.position.setValue(cartesian);}// 更新動態多邊形if (activeShape) {const positions = activeShapePoints.concat([cartesian]);activeShape.polygon.hierarchy = new Cesium.PolygonHierarchy(positions);}}}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);// 監聽右鍵點擊事件 - 完成繪制handler.setInputAction(() => {if (activeShapePoints.length < 3) {// 至少需要3個點才能形成多邊形drawingInstructions.value = '至少需要3個點才能形成多邊形,請繼續繪制';return;}// 完成繪制terminateShape();// 執行地形開挖performExcavation(activeShapePoints);// 清除繪制狀態drawingInstructions.value = '繪制完成,地形已開挖';// 清除事件處理器handler.destroy();handler = null;}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
};// 創建點實體
const createPoint = (position) => {return viewer.entities.add({position: position,point: {color: Cesium.Color.WHITE,pixelSize: 10,heightReference: Cesium.HeightReference.CLAMP_TO_GROUND}});
};// 創建多邊形實體
const createPolygon = (positions) => {return viewer.entities.add({polygon: {hierarchy: new Cesium.PolygonHierarchy(positions),material: new Cesium.ColorMaterialProperty(Cesium.Color.WHITE.withAlpha(0.3)),outline: true,outlineColor: Cesium.Color.WHITE,// heightReference: Cesium.HeightReference.CLAMP_TO_GROUND// 關鍵屬性:貼地模式clampToGround: true,// 禁用擠壓高度// height: 0,// height: 0,// perPositionHeight: false,// classificationType: Cesium.ClassificationType.BOTH,// zIndex: 100}});
};// 完成繪制形狀
const terminateShape = () => {// 移除動態實體if (floatingPoint) {viewer.entities.remove(floatingPoint);floatingPoint = null;}if (activeShape) {viewer.entities.remove(activeShape);activeShape = null;}// 創建最終的多邊形// if (activeShapePoints.length >= 3) {//   const finalPolygon = viewer.entities.add({//     polygon: {//       hierarchy: new Cesium.PolygonHierarchy(activeShapePoints),//       material: new Cesium.ColorMaterialProperty(Cesium.Color.GREEN.withAlpha(0.3)),//       outline: true,//       outlineColor: Cesium.Color.GREEN,//       height: 0,//       perPositionHeight: false,//       classificationType: Cesium.ClassificationType.BOTH,//       zIndex: 100//     }//   });// }
};// 執行地形開挖
const performExcavation = (positions) => {// 如果已有開挖實例,先嘗試清除if (excavateInstance) {try {// 重置地形開挖viewer.scene.globe.clippingPlanes = undefined;viewer.entities.removeById("entityDM");viewer.entities.removeById("entityDMBJ");} catch (error) {console.error("清除之前的開挖失敗", error);}}// 創建新的地形開挖實例excavateInstance = new excavateTerrain(viewer, {positions: positions,height: 50,bottom: "./excavateTerrain/excavationregion_side.jpg",side: "./excavateTerrain/excavationregion_top.jpg",});
};// 清除繪制
const clearDrawing = () => {// 清除事件處理器if (handler) {handler.destroy();handler = null;}// 清除動態實體if (floatingPoint) {viewer.entities.remove(floatingPoint);floatingPoint = null;}if (activeShape) {viewer.entities.remove(activeShape);activeShape = null;}// 清空點數組activeShapePoints = [];viewer.entities.removeAll();// 清除繪制提示drawingInstructions.value = '';// 重置地形開挖if (excavateInstance) {try {viewer.scene.globe.clippingPlanes = undefined;viewer.entities.removeById("entityDM");viewer.entities.removeById("entityDMBJ");excavateInstance = null;} catch (error) {console.error("清除地形開挖失敗", error);}}
};// 組件卸載前清理資源
onUnmounted(() => {clearDrawing();if (viewer) {viewer.destroy();viewer = null;}
});
</script><style scoped>
.cesium-container {width: 100%;height: 100vh;margin: 0;padding: 0;overflow: hidden;position: relative;
}.control-panel {position: absolute;top: 20px;left: 20px;width: 300px;background-color: rgba(38, 38, 38, 0.85);border-radius: 8px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);color: white;z-index: 1000;overflow: hidden;transition: all 0.3s ease;
}.panel-header {background-color: rgba(0, 0, 0, 0.5);padding: 10px 15px;border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}.panel-header h3 {margin: 0;font-size: 16px;font-weight: 500;
}.panel-body {padding: 15px;
}.control-group {margin-bottom: 15px;
}.control-group label {display: block;margin-bottom: 5px;font-size: 14px;
}.control-group input[type="range"] {width: 100%;margin-bottom: 5px;background-color: rgba(255, 255, 255, 0.2);border-radius: 4px;
}.control-group span {font-size: 12px;color: rgba(255, 255, 255, 0.7);display: block;margin-top: 5px;line-height: 1.4;
}.control-group span {font-size: 12px;color: rgba(255, 255, 255, 0.7);
}.control-group button {background-color: #4285f4;color: white;border: none;padding: 8px 15px;border-radius: 4px;cursor: pointer;font-size: 14px;width: 100%;transition: background-color 0.3s ease;
}.control-group button:hover {background-color: #3367d6;
}
</style>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/82386.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/82386.shtml
英文地址,請注明出處:http://en.pswp.cn/web/82386.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

qwen大模型在進行詞嵌入向量時,針對的詞表中的唯一數字還是其他的?

qwen大模型在進行詞嵌入向量時,針對的詞表中的唯一數字還是其他的? Qwen大模型進行詞嵌入向量時,針對的是詞表中每個 Token 對應的唯一數字(Token ID) ,核心邏輯結合詞表構建、嵌入過程展開 一、Qwen 詞表與 Token ID Qwen 用 BPE 分詞器(基于 tiktoken,以 cl100k 為…

動態規劃-1143.最長公共子序列-力扣(LeetCode)

一、題目解析 對于給定了兩個字符串中&#xff0c;需要找到最長的公共子序列&#xff0c;也就是兩個字符串所共同擁有的子序列。 二、算法原理 1、狀態表示 dp[i][j]&#xff1a;表示s1的[0,i]和s2的[0,j]區間內所有子序列&#xff0c;最長子序列的長度 2、狀態轉移方程 根…

互聯網c++開發崗位偏少,測開怎么樣?

通過這標題&#xff0c;不難看出問這個問題的&#xff0c;就是沒工作過的。如果工作過&#xff0c;那就是不斷往深的鉆研&#xff0c;路越走越窄&#xff0c;找工作一般就是找原來方向的。沒工作過的&#xff0c;那一般就是學生。 學生找什么方向的工作比較好&#xff1f; 學生…

推薦算法八股

跑路了&#xff0c;暑期0offer&#xff0c;華為主管面掛了&#xff0c;真幽默&#xff0c;性格測評就掛了居然給我一路放到主管面&#xff0c;科大迅飛太囂張&#xff0c;直接跟人說后面要面華為&#xff0c;元戎啟行&#xff0c;學了C后python完全忘了怎么寫&#xff0c;挺尷尬…

Spring Boot微服務架構(九):設計哲學是什么?

一、Spring Boot設計哲學是什么&#xff1f; Spring Boot 的設計哲學可以概括為 ??“約定優于配置”?? 和 ??“開箱即用”??&#xff0c;其核心目標是??極大地簡化基于 Spring 框架的生產級應用的初始搭建和開發過程??&#xff0c;讓開發者能夠快速啟動并運行項目…

前端導入Excel表格

前端如何在 Vue 3 中導入 Excel 文件&#xff08;.xls 和 .xlsx&#xff09;&#xff1f; 在日常開發中&#xff0c;我們經常需要處理 Excel 文件&#xff0c;比如導入數據表格、分析數據等。文章將在 Vue 3 中實現導入 .xls 和 .xlsx 格式的文件&#xff0c;并解析其中的數據…

C++和C#界面開發方式的全面對比

文章目錄 C界面開發方式1. **MFC&#xff08;Microsoft Foundation Classes&#xff09;**2. **Qt**3. **WTL&#xff08;Windows Template Library&#xff09;**4. **wxWidgets**5. **DirectUI** C#界面開發方式1. **WPF&#xff08;Windows Presentation Foundation&#xf…

刷leetcode hot100返航必勝版--鏈表6/3

鏈表初始知識 鏈表種類&#xff1a;單鏈表&#xff0c;雙鏈表&#xff0c;循環鏈表 鏈表初始化 struct ListNode{ int val; ListNode* next; ListNode(int x): val&#xff08;x&#xff09;,next(nullptr) {} }; //初始化 ListNode* head new ListNode(5); 刪除節點、添加…

軟考 系統架構設計師系列知識點之雜項集萃(78)

接前一篇文章&#xff1a;軟考 系統架構設計師系列知識點之雜項集萃&#xff08;77&#xff09; 第139題 以下關于軟件測試工具的敘述&#xff0c;錯誤的是&#xff08;&#xff09;。 A. 靜態測試工具可用于對軟件需求、結構設計、詳細設計和代碼進行評審、走查和審查 B. 靜…

【Unity】云渲染

1 前言 最近在搞Unity云渲染的東西&#xff0c;所以研究了下官方提供的云渲染方案Unity Renderstreaming。注&#xff1a;本文使用的Unity渲染管線是URP。 2 文檔 本文也只是介紹基本的使用方法&#xff0c;更詳細內容參閱官方文檔。官方文檔&#xff1a;Unity Renderstreamin…

組相對策略優化(GRPO):原理及源碼解析

文章目錄 PPO vs GRPOPPO的目標函數GRPO的目標函數KL散度約束與估計ORM監督RL的結果PRM監督RL的過程迭代RL算法流程 GRPO損失的不同版本GRPO源碼解析 DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models PPO vs GRPO PPO的目標函數 J P P O…

Linux或者Windows下PHP版本查看方法總結

確定當前服務器或本地環境中 PHP 的版本,可以通過以下幾種方法進行操作: 1. 通過命令行檢查 這是最直接且常用的方法,適用于本地開發環境或有 SSH 訪問權限的服務器。 方法一:php -v 命令 php -v輸出示例:PHP 8.1.12 (cli) (built: Oct 12 2023 12:34:56) (NTS) Copyri…

[Linux] MySQL源碼編譯安裝

目錄 環境包安裝 創建程序用戶 解壓源碼包 配置cmake ?編輯編譯 安裝 配置修改屬性 屬主和屬組替換成mysql用戶管理 系統環境變量配置 初始化數據庫 服務管理 啟動 環境包安裝 yum -y install ncurses ncurses-devel bison cmake gcc gcc-c 重點強調&#xff1a;采…

【C++項目】負載均衡在線OJ系統-1

文章目錄 前言項目結果演示技術棧&#xff1a;結構與總體思路compiler編譯功能-common/util.hpp 拼接編譯臨時文件-common/log.hpp 開放式日志-common/util.hpp 獲取時間戳方法-秒級-common/util.hpp 文件是否存在-compile_server/compiler.hpp 編譯功能編寫&#xff08;重要&a…

轉戰海外 Web3 遠程工作指南

目錄 一、明確職業目標和技能 二、準備常用軟件 &#xff08;一&#xff09;通訊聊天工具 &#xff08;二&#xff09;媒體類平臺 &#xff08;三&#xff09;線上會議軟件 &#xff08;四&#xff09;辦公協作工具 &#xff08;五&#xff09;云存儲工具 &#xff08;六…

MongoDB賬號密碼筆記

先連接數據庫&#xff0c;新增用戶密碼 admin用戶密碼 use admin db.createUser({ user: "admin", pwd: "yourStrongPassword", roles: [ { role: "root", db: "admin" } ] })用戶數據庫用戶密碼 use myappdb db.createUser({ user: &…

CSS強制div單行顯示不換行

在CSS中&#xff0c;要讓<div>的內容強制單行顯示且不換行&#xff0c;可通過以下屬性組合實現&#xff1a; 核心解決方案&#xff1a; css 復制 下載 div {white-space: nowrap; /* 禁止文本換行 */overflow: hidden; /* 隱藏溢出內容 */text-overflow: e…

RK3568-快速部署codesys runtime

前期準備 PC-win10系統 RK3568-debian系統,內核已打入實時補丁,開啟ssh服務。PC下載安裝CODESYS Development System V3.5.17.0 https://store.codesys.com/en/codesys.html#product.attributes.wrapperPC下載安裝 CODESYS Control for Linux ARM64 SL 4.1.0.0.package ht…

中英混合編碼解碼全解析

qwen模型分詞器怎么映射的:中英混合編碼解碼全解析 中英文混合編碼與解碼的過程,本質是 字符編碼標準(如 UTF-8)對多語言字符的統一處理 ,核心邏輯圍繞“字節序列 ? 字符映射”展開 北京智源人工智能研究院中文tokenID qwen模型分詞器文件 一、編碼階段:統一轉為字節序…

React 事件處理與合成事件機制揭秘

引言 在現代前端開發的技術生態中&#xff0c;React憑借其高效的組件化設計和聲明式編程范式&#xff0c;已成為構建交互式用戶界面的首選框架之一。除了虛擬DOM和單向數據流等核心概念&#xff0c;React的事件處理系統也是其成功的關鍵因素。 這套系統通過"合成事件&qu…