Threejs 实现3D 地图(02)创建3d 地图

news/2024/10/25 1:49:27/

 

"d3": "^7.9.0",
"three": "^0.169.0",
"vue": "^3.5.10"

地图数据来源: DataV.GeoAtlas地理小工具系列 

<script setup>
import {onMounted, ref} from 'vue'
import * as THREE from 'three'
import * as d3 from "d3";  //莫开托坐标 矫正地图坐标
import map from './constant/map.json'
// 引入轨道控制器扩展库OrbitControls.js
import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
// 拿到页面的宽高
const width = window.innerWidth, height = window.innerHeight;// d3投影转换函数 (109, 34.5  中心点位置)
const handleProject = d3.geoMercator().center([109, 34.5]).scale(1000).translate([0, 0])
// 存储地图容器
const mapContainer = new THREE.Object3D()// 创建场景
const scene = new THREE.Scene();
// 将背景颜色设置为白色
scene.background = new THREE.Color("#000000");// 创建相机
const camera = new THREE.PerspectiveCamera(70, width / height, 0.01, 10000);
// 设置相机位置
camera.position.z = 1000;// // 辅助线 AxesHelper
const axesHelper = new THREE.AxesHelper(500);
scene.add(axesHelper);// 初始化渲染器
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(width, height);// 设置相机控件轨道控制器OrbitControls
const controls = new OrbitControls(camera, renderer.domElement);
//阻尼 更真实
controls.enableDamping = true// 地图数据处理
const initGeom = () => {// 将地图数据显示在屏幕上// 如果是服务器返回的数据,需要先将数据转为geojson格式 这里默认用的本地数据handleData(map)
}const handleData = (jsonData) => {const featureList = jsonData.features;featureList.forEach((feature) => {// 创建省的容器const province = new THREE.Object3D;// 省份名称province.properties = feature.properties.nameprovince.name = feature.properties.name // 省份名称mapContainer.name = feature.properties.name // 省份名称// 省份坐标信息const coordinates = feature.geometry.coordinatesif (feature.geometry.type === 'MultiPolygon') {coordinates.forEach((cord) => {// coordinate 就是边界素组坐标系cord.forEach((coordinate) => {// 三维多边形const extrudeMesh = creatDepthPolygon(coordinate)// 给extrudeMesh对象加一个自定义的属性(properties)用来存放省份名称extrudeMesh.properties = feature.properties.name// 绘制省份边缘线条const line = createLine(coordinate);// 将面加入3d 中province.add(extrudeMesh)// 将线加入3d 中province.add(line)})})}if (feature.geometry.type === 'Polygon') {coordinates.forEach((coordinate) => {// 三维多边形const extrudeMesh = creatDepthPolygon(coordinate)extrudeMesh.properties = feature.properties.name// 线条const line = createLine(coordinate);province.add(extrudeMesh)province.add(line)})}// 将每个省份的容器加入到地图容器中mapContainer.add(province)})// 将地图容器加入到场景中scene.add(mapContainer)
}// 创建三维多边形(也就是每个省份的地图)
const creatDepthPolygon = (coordinate) => {const shape = new THREE.Shape();// 每一个item都是省份边界坐标 需要把 json 的坐标系 转化为d3的坐标系  例如:[117.429915,40.576141]coordinate.forEach((item, index) => {const [x_XYZ, y_XYZ] = handleProject(item)if (index === 0) {// moveTo 的主要作用是定义形状的起点位置shape.moveTo(x_XYZ, -y_XYZ)} else {// 依次按照坐标 形成一个多边形shape.lineTo(x_XYZ, -y_XYZ)}})const extrudeSettings = {steps: 2,depth: 16,bevelEnabled: true,bevelThickness: 1,bevelSize: 1,bevelOffset: 0,bevelSegments: 1};// ExtrudeGeometry  创建一个多边形const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings)  //挤压缓冲几何体// 设置每个身份的背景颜色const material = new THREE.MeshBasicMaterial({// color: new THREE.Color(Math.random() * 0xffffff), // 每个省随机赋色color: '#d13a34',transparent: true,opacity: 0.6})return new THREE.Mesh(geometry, material)
}// 创建省份边界线条
const createLine = (coordinate) => {const material = new THREE.LineBasicMaterial({color: '#ffffff'});const points = []coordinate.forEach((item, index) => {// 每一个item都是省份边界坐标 需要把 json 的坐标系 转化为d3的坐标系  例如:[117.429915,40.576141]const [x_XYZ, y_XYZ] = handleProject(item)points.push(new THREE.Vector3(x_XYZ, -y_XYZ, 25))})const geometry = new THREE.BufferGeometry().setFromPoints(points);return new THREE.Line(geometry, material);
}// 渲染页面
const render = () => {// 将场景(scene)和摄像机(camera 加入进来)renderer.render(scene, camera)// 渲染下一帧的时候会调用render函数requestAnimationFrame(render)controls.update()
}const initLight = () => {// 基本光源const ambLight = new THREE.AmbientLight('#ffffff', 0.3)/*** 设置聚光灯相关的的属性*/const spotLight = new THREE.SpotLight(0xFFFFFF); // 聚光灯spotLight.position.set(40, 200, 10);spotLight.castShadow = true; // 只有该属性为true时,该点光源允许产生阴影,并且下列属性可用scene.add(ambLight, spotLight); // 向场景中添加光源
}onMounted(() => {// 添加物体到场景initGeom()// 渲染render()// 设置环境光initLight()// 将渲染加入到页面上document.body.appendChild(renderer.domElement);
})
</script><template><div id="info"></div>
</template><style scoped></style>

下一篇:Threejs 实现3D 地图(03)3d 地图的上钻下钻操作-CSDN博客


http://www.ppmy.cn/news/1541716.html

相关文章

el-table修改指定列字体颜色 ,覆盖划过行的高亮显示文字颜色

修改指定列字体颜色 ,覆盖划过行的高亮显示文字颜色 代码如下&#xff1a; <div class"c1"><el-table:data"tableData"striperow-class-name"custom-table-row"style"width:100%"cell-mouse-enter"lightFn"cell-…

《PP-OCRv1》论文精读:PaddleOCR是目前SOTA级别的OCR开源技术(截止2024年10月)

PP-OCR: A Practical Ultra Lightweight OCR System论文地址PP-OCRv2: Bag of Tricks for Ultra Lightweight OCR System论文地址PP-OCRv3: More Attempts for the Improvement of Ultra Lightweight OCR System论文地址PaddleOCR Github OCR工具库 43.5K个star PP-OCRv1由百度…

github pages + hugo 搭建静态博客网站

体验地址 1. 起因&#xff0c; 目的: 其实6年前&#xff0c;我就写过这个。 项目代码 博客地址 最近想改写一下。 github 推荐的主题是 Jekyll&#xff0c; 我当时用的就是这个&#xff0c;感觉很麻烦。尤其是文章命名。 新的主题 hugo 用起来还行。 2.过程: 过程记录&am…

nerdctl 安装

nerdctl 是一个轻量级的 Docker CLI 兼容工具&#xff0c;它用于操作 containerd 容器运行时。下面是如何在 CentOS 上安装和配置 nerdctl 的详细步骤。 1. 前置条件 需要 containerd 作为容器运行时&#xff0c;如果尚未安装&#xff0c;请先安装 containerd。需要 runc 作为…

如何利用 OCR 和文档处理,快速提高供应商管理效率 ?

在当今瞬息万变的商业环境中&#xff0c;有效的供应商管理通常需要处理大量实物文档&#xff0c;这带来了巨大的挑战。手动提取供应商名称、编号和其他关键信息等关键细节非常耗时、容易出错&#xff0c;并且会降低整体效率。 为了应对这些挑战&#xff0c;组织正在逐步采用自…

【AI创新】优化ChatGPT提示词Prompt设计:释放AI的无限潜能

【AI创新】优化ChatGPT提示词Prompt设计&#xff1a;释放AI的无限潜能 文章目录 &#x1f31f; 引言&#x1f31f; 第一性原理在Prompt设计中的应用系统与用户信息的深度融合实际应用案例分析结论 &#x1f31f; 系统信息与用户信息的协同作用系统信息&#xff08;SYSTEM Infor…

leetcode 910. 最小差值 II 中等

给你一个整数数组 nums&#xff0c;和一个整数 k 。 对于每个下标 i&#xff08;0 < i < nums.length&#xff09;&#xff0c;将 nums[i] 变成 nums[i] k 或 nums[i] - k 。 nums 的 分数 是 nums 中最大元素和最小元素的差值。 在更改每个下标对应的值之后&#xf…

【漏洞修复】修复上传文件不检测文件内容的问题

修改文件crmeb/crmeb/services/upload/storage/Local.php增加下面代码 $stream fopen($fileHandle->getPathname(), r); $content (fread($stream, filesize($fileHandle->getPathname()))); if (is_resource($stream)) { fclose($stream); } if (preg_match(/thin…