• VUE3+Cesium+Three项目源码


    继上一篇Vue+Cesium+Three项目构建的详解后,为了方便使用,进一步对该项目进行模块化封装。 

    VUE3+Cesium+Three项目构建_HM-hhxx!的博客-CSDN博客VUE3+Cesium+Three项目创建及原理详解。https://blog.csdn.net/damadashen/article/details/126550168

    目录

    1.初始化Cesium:initCesium.js

    2.初始化Three:initThree.js

    3.HomeView.vue


    该版本项目中,将cesium初始化与Three初始化剥离开,在日后使用中更为便捷,并在vue

    初始化时加入router及vuex状态管理,便于后续大型项目开发使用,项目结构如下:

    1.初始化Cesium:initCesium.js

    1. import * as Cesium from "cesium";
    2. import "../Widgets/widgets.css";
    3. // 设置cesium的token
    4. Cesium.Ion.defaultAccessToken =
    5. "your_token";
    6. // cesium默认资源路径
    7. window.CESIUM_BASE_URL = "/Cesium/";
    8. // 设置默认的视角为中国
    9. Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(
    10. // 西边经度
    11. 89.5,
    12. // 南边维度
    13. 20.4,
    14. // 东边经度
    15. 110.4,
    16. // 北边维度
    17. 61.2
    18. );
    19. // 设置全局cesium对象
    20. let cesium = {
    21. viewer: null,
    22. };
    23. function initCesium(minWGS84, maxWGS84, cesiumContainerid) {
    24. var cesiumContainer = cesiumContainerid;
    25. // 设置cesium容器
    26. var cesiumContainer = document.getElementById("cesiumContainer");
    27. // 初始化cesium渲染器
    28. cesium.viewer = new Cesium.Viewer(cesiumContainer, {
    29. useDefaultRenderLoop: false,
    30. selectionIndicator: false,
    31. homeButton: false,
    32. infoBox: false,
    33. sceneModePicker: false,
    34. navigationHelpButton: false,
    35. animation: false,
    36. timeline: false,
    37. fullscreenButton: false,
    38. baseLayerPicker: false,
    39. clock: false,
    40. geocoder: false,
    41. // 天地图矢量路径图
    42. imageryProvider: new Cesium.WebMapTileServiceImageryProvider({
    43. url: "http://t0.tianditu.com/vec_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=vec&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=30d07720fa76f07732d83c748bb84211",
    44. layer: "tdtBasicLayer",
    45. style: "default",
    46. format: "image/jpeg",
    47. tileMatrixSetID: "GoogleMapsCompatible",
    48. }),
    49. //cesium中webgl选项
    50. contextOptions: {
    51. webgl: {
    52. //透明度
    53. alpha: false,
    54. // 抗锯齿
    55. antialias: true,
    56. //深度检测
    57. depth: true,
    58. },
    59. },
    60. });
    61. //设置隐藏logo
    62. cesium.viewer.cesiumWidget.creditContainer.style.display = "none";
    63. // 设置抗锯齿
    64. cesium.viewer.scene.postProcessStages.fxaa.enabled = true;
    65. // 地图叠加
    66. var imageryLayers = cesium.viewer.imageryLayers;
    67. //console.log(imageryLayers);
    68. var layer = imageryLayers.addImageryProvider(
    69. new Cesium.WebMapTileServiceImageryProvider({
    70. url: "http://t0.tianditu.com/img_w/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}&style=default&format=tiles&tk=30d07720fa76f07732d83c748bb84211",
    71. layer: "tdtBasicLayer",
    72. style: "default",
    73. format: "image/jpeg",
    74. tileMatrixSetID: "GoogleMapsCompatible",
    75. })
    76. );
    77. layer.alpha = 0.5;
    78. // 设置前往地点
    79. let center = Cesium.Cartesian3.fromDegrees(
    80. (minWGS84[0] + maxWGS84[0]) / 2,
    81. (minWGS84[1] + maxWGS84[1]) / 2,
    82. 20000
    83. );
    84. // 设置相机飞往该区域
    85. cesium.viewer.camera.flyTo({
    86. destination: center,
    87. duration: 2,
    88. orientation: {
    89. heading: Cesium.Math.toRadians(0),
    90. pitch: Cesium.Math.toRadians(-90),
    91. roll: 0,
    92. },
    93. });
    94. }
    95. function renderCesium() {
    96. cesium.viewer.render();
    97. }
    98. function exportCesium() {
    99. let cesiumdone = cesium;
    100. return cesiumdone;
    101. }
    102. export { exportCesium,initCesium, renderCesium };

    2.初始化Three:initThree.js

    1. import * as THREE from "three";
    2. import * as Cesium from "cesium";
    3. // three全局对象
    4. let three = {
    5. renderer: null,
    6. camera: null,
    7. scene: null,
    8. };
    9. // three.js物体
    10. let objects3D = [];
    11. //封装three物体(使three物体具有经纬度)
    12. function Object3D(mesh, minWGS84, maxWGS84) {
    13. this.threeMesh = mesh; //物体
    14. this.minWGS84 = minWGS84; //范围
    15. this.maxWGS84 = maxWGS84; //范围
    16. }
    17. function initThree() {
    18. //初始化Three
    19. // 设置相机配置
    20. let fov = 45; //视角
    21. let aspect = window.innerWidth / window.innerHeight; //宽高比例
    22. let near = 0.1;
    23. let far = 10 * 1000 * 1000; //视域范围
    24. // 初始化场景
    25. three.scene = new THREE.Scene();
    26. three.camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
    27. three.renderer = new THREE.WebGLRenderer({
    28. antialias: true, //抗锯齿
    29. alpha: true,
    30. });
    31. // 设置渲染器大小
    32. three.renderer.setSize(window.innerWidth, window.innerHeight);
    33. // 添加环境光
    34. let ambientLight = new THREE.AmbientLight(0xffffff, 1);
    35. three.scene.add(ambientLight);
    36. // 添加three.jscanvas元素到cesium容器
    37. cesiumContainer.appendChild(three.renderer.domElement);
    38. }
    39. // 创建three.js物体
    40. function createMesh(minWGS84, maxWGS84) {
    41. let geometry = new THREE.BoxBufferGeometry(1, 1, 1);
    42. let material = new THREE.MeshBasicMaterial({
    43. color: 0x00ff00,
    44. });
    45. let mesh = new THREE.Mesh(geometry, material);
    46. // 放大物体
    47. mesh.scale.set(100, 100, 100); // 放大
    48. mesh.position.set(0, 0, 50); // 平移
    49. let meshGroup = new THREE.Group();
    50. meshGroup.add(mesh);
    51. // 添加至场景
    52. three.scene.add(meshGroup);
    53. // 创建3d物体
    54. let OB3d = new Object3D(
    55. meshGroup,
    56. [minWGS84[0], minWGS84[1]],
    57. [maxWGS84[0], maxWGS84[1]]
    58. );
    59. // 添加到3d物体数组
    60. objects3D.push(OB3d);
    61. }
    62. function renderThree(cesium) {
    63. // 设置相机跟cesium保持一致
    64. three.camera.fov = Cesium.Math.toDegrees(cesium.viewer.camera.frustum.fovy);
    65. //console.log(cesium);
    66. // 声明一个将cesium框架的cartesian3转换为three.js的vector3(笛卡尔坐标转换为三维向量)
    67. let cartToVec = function (cart) {
    68. return new THREE.Vector3(cart.x, cart.y, cart.z);
    69. };
    70. // 将3D的物体通过经纬度转换成对应的位置
    71. objects3D.forEach((item, index) => {
    72. // 通过经纬度获取中心点的位置
    73. let center = Cesium.Cartesian3.fromDegrees(
    74. (item.minWGS84[0] + item.maxWGS84[0]) / 2,
    75. (item.minWGS84[1] + item.maxWGS84[1]) / 2
    76. );
    77. item.threeMesh.position.copy(cartToVec(center));
    78. //计算朝向(切面方向-切线向量)
    79. //中心高度点
    80. let centerHeight = Cesium.Cartesian3.fromDegrees(
    81. (item.minWGS84[0] + item.maxWGS84[0]) / 2,
    82. (item.minWGS84[1] + item.maxWGS84[1]) / 2,
    83. 1
    84. );
    85. //左下
    86. let bottomLeft = cartToVec(
    87. Cesium.Cartesian3.fromDegrees(item.minWGS84[0], item.minWGS84[1])
    88. );
    89. //左上
    90. let topLeft = cartToVec(
    91. Cesium.Cartesian3.fromDegrees(item.minWGS84[0], item.maxWGS84[1])
    92. );
    93. //朝向()
    94. let latDir = new THREE.Vector3()
    95. .subVectors(bottomLeft, topLeft)
    96. .normalize();
    97. // console.log(item);
    98. //设置查看方向
    99. item.threeMesh.lookAt(centerHeight.x, centerHeight.y, centerHeight.z);
    100. //设置朝向
    101. item.threeMesh.up.copy(latDir);
    102. });
    103. //设置摄像机矩阵
    104. // 设置相机跟cesium保持一致
    105. three.camera.matrixAutoUpdate = false; //自动更新
    106. //复制cesium相机矩阵
    107. let cvm = cesium.viewer.camera.viewMatrix;
    108. let civm = cesium.viewer.camera.inverseViewMatrix;
    109. // three相机默认朝向0,0,0
    110. three.camera.lookAt(0, 0, 0);
    111. // 设置threejs相机矩阵
    112. three.camera.matrixWorld.set(
    113. civm[0],
    114. civm[4],
    115. civm[8],
    116. civm[12],
    117. civm[1],
    118. civm[5],
    119. civm[9],
    120. civm[13],
    121. civm[2],
    122. civm[6],
    123. civm[10],
    124. civm[14],
    125. civm[3],
    126. civm[7],
    127. civm[11],
    128. civm[15]
    129. );
    130. three.camera.matrixWorldInverse.set(
    131. cvm[0],
    132. cvm[4],
    133. cvm[8],
    134. cvm[12],
    135. cvm[1],
    136. cvm[5],
    137. cvm[9],
    138. cvm[13],
    139. cvm[2],
    140. cvm[6],
    141. cvm[10],
    142. cvm[14],
    143. cvm[3],
    144. cvm[7],
    145. cvm[11],
    146. cvm[15]
    147. );
    148. //设置宽高比例
    149. let width = cesiumContainer.clientWidth;
    150. let height = cesiumContainer.clientHeight;
    151. three.camera.aspect = width / height;
    152. //更新相机矩阵
    153. three.camera.updateProjectionMatrix();
    154. //设置尺寸大小
    155. three.renderer.setSize(width, height);
    156. three.renderer.clear();
    157. three.renderer.render(three.scene, three.camera);
    158. }
    159. export {initThree,createMesh,renderThree};

    3.HomeView.vue

    1. <script setup>
    2. import { onMounted } from "vue";
    3. import * as Cesium from "cesium";
    4. //import "cesium/Build/Cesium/Widgets/widgets.css";
    5. import "../Widgets/widgets.css";
    6. import * as THREE from "three";
    7. import { exportCesium, initCesium, renderCesium } from "../cesium/cesium_init.js";
    8. import { initThree, createMesh, renderThree } from "../three/three_init.js"
    9. onMounted(() => {
    10. main();
    11. });
    12. function main() {
    13. // 设置北京显示模型的渲染范围(用于设置范围)
    14. var minWGS84 = [115.39, 38.9];
    15. var maxWGS84 = [117.39, 40.9];
    16. //初始化Cesium
    17. initCesium(minWGS84, maxWGS84, cesiumContainer);
    18. var cesium = exportCesium();
    19. //初始化Three
    20. initThree(minWGS84, maxWGS84);
    21. //创建物体
    22. createMesh(minWGS84, maxWGS84);
    23. //循环函数,不断请求动画帧渲染
    24. function loop() {
    25. requestAnimationFrame(loop);
    26. // cesium渲染
    27. renderCesium();
    28. // three.js渲染
    29. renderThree(cesium);
    30. }
    31. loop();
    32. }
    33. script>
    34. <style>
    35. * {
    36. margin: 0;
    37. padding: 0;
    38. }
    39. #cesiumContainer {
    40. width: 100vw;
    41. height: 100vh;
    42. position: relative;
    43. }
    44. #cesiumContainer>canvas {
    45. position: absolute;
    46. top: 0;
    47. left: 0;
    48. /* 设置鼠标事件穿透 */
    49. pointer-events: none;
    50. }
    51. style>

    源码地址:

    GitHub - Harsh-M1/vue_cesium_three_templateContribute to Harsh-M1/vue_cesium_three_template development by creating an account on GitHub.https://github.com/Harsh-M1/vue_cesium_three_template


    vue_cesium_three_template: 使用VUE3+cesium+three.js构建的基础想木模板!https://gitee.com/lei-aimiao/vue_cesium_three_template

  • 相关阅读:
    业务总结思考 | 额度授信模型/拒绝捞回模型/定义坏样本
    CFS内网穿透靶场实战
    Java错题归纳day19
    C++基础知识(十九)--- 函数对象
    【LeetCode】Day132-单词搜索
    「程序员转型技术管理」必修的 10 个能力提升方向
    手把手带你从官网下载安装 Vivado
    SpringBoot多数据源以及事务处理
    java电话本项目
    Linux创建用户及sumba服务器创建用户
  • 原文地址:https://blog.csdn.net/damadashen/article/details/126585531