当前位置: 首页 > news >正文

three.js 使用geojson ,实现中国地图区域,边缘流动效果

three.js 使用geojson ,实现中国地图区域,边缘流动效果

在线链接:https://threehub.cn/#/codeMirror?navigation=ThreeJS&classify=expand&id=geoBorder

国内站点预览:http://threehub.cn

github地址: https://github.com/z2586300277/three-cesium-examples

在这里插入图片描述

import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'const box = document.getElementById('box')const scene = new THREE.Scene()const camera = new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 1000)camera.position.set(0, 0, 500)const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })renderer.setSize(box.clientWidth, box.clientHeight)box.appendChild(renderer.domElement)const controls = new OrbitControls(camera, renderer.domElement)controls.enableDamping = truescene.add(new THREE.AmbientLight(0xffffff, 1))const directionalLight = new THREE.DirectionalLight(0xffffff, 2)directionalLight.position.set(300, 300, 300)scene.add(directionalLight)const map = new THREE.TextureLoader().load(`https://file.threehub.cn/` + 'images/channels/lmap.png')map.wrapS = THREE.RepeatWrappingmap.wrapT = THREE.RepeatWrappingmap.needsUpdate = trueanimate()function animate() {requestAnimationFrame(animate)map.offset.x += 0.001controls.update()renderer.render(scene, camera)}window.onresize = () => {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect = box.clientWidth / box.clientHeightcamera.updateProjectionMatrix()}/* 边界 */
const group = new THREE.Group()fetch(`https://file.threehub.cn/` + 'files/json/chinaBound.json').then(r => r.json()).then(res => {const { features } = resfeatures.forEach((i) => {if (i.geometry.type === 'MultiPolygon') i.geometry.coordinates.forEach((j) => j.forEach((z) => createShapeWithCoord(group, z)))else if (i.geometry.type === 'Polygon') i.geometry.coordinates.forEach((j) => createShapeWithCoord(group, j))else if (i.geometry.type === 'LineString') i.geometry.coordinates.length > 1 && createShapeWithCoord(group, i.geometry.coordinates)})translationOriginForGroup(group)scene.add(group)})function createShapeWithCoord(group, coordinates) {if (coordinates.length < 1000) return // 设置点数限制 如果点太少则不绘制const curvePoints = coordinates.map((k) => coordToVector3(k))const curve = new THREE.CatmullRomCurve3(curvePoints)const geometry = new THREE.TubeGeometry(curve, curvePoints.length - 1, 1, 40, false)const material = new THREE.MeshPhongMaterial({ color: 0xffffff , map , transparent: true })const mesh = new THREE.Mesh(geometry, material)translationOriginForMesh(mesh)group.attach(mesh)}function coordToVector3(coord, slace = 10000) {const [lng, lat] = coordconst x = lng * 20037508.34 / 180const y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180) * 20037508.34 / 180return new THREE.Vector3(x / slace, y / slace, 0)}function translationOriginForMesh(mesh) {const boundingBox = new THREE.Box3().setFromObject(mesh)boundingBox.getCenter(mesh.position)mesh.geometry.center()}// 设置组中心点
function translationOriginForGroup(group) {const boundingBox = new THREE.Box3().setFromObject(group)boundingBox.getCenter(group.position)group.traverse((c) => {c.isMesh && c.position.sub(group.position)})group.position.set(0, 0, 0)}/*** 名称: 地理边界* 作者: 优雅永不过时 https://github.com/z2586300277
*/
http://www.lryc.cn/news/463903.html

相关文章:

  • 数据中台业务架构图
  • Docker学习笔记(2)- Docker的安装
  • PostgreSql的备份和升级
  • 联系拯救者Y9000P2022笔记本电脑进入BIOS快捷键
  • compose navigation 自定义navtype
  • 实现对redis过期键监听案例
  • yocto基础 -- bb 文件字段解析
  • Android开发相关的重要网站
  • MySQL 中utfmb3和utfmb4字符集区别
  • 【C语言】文件操作(1)(文件打开关闭和顺序读写函数的万字笔记)
  • 今日总结10.18
  • React Agent 自定义实现
  • RabbitMQ 入门(六)SpringAMQP五种消息类型(Direct Exchange)
  • 2062:【例1.3】电影票
  • Python | Leetcode Python题解之第477题汉明距离总和
  • Leecode刷题之路第25天之K个一组翻转链表
  • nuxtjs3 使用tailwindcss做自适应
  • 数据资产目录构建方法与应用
  • 【Python爬虫实战】从文件到数据库:全面掌握Python爬虫数据存储技巧
  • 断其一指,无惧!ProFusion3D: 相机或者激光失效仍高效的多传感器融合3D目标检测算法
  • CCS字体、字号更改+CCS下载官方链接
  • YOLO11改进|注意力机制篇|引入SEAM注意力机制
  • 简历修订与求职经历 - Chap04
  • 鸿蒙开发案例:推箱子
  • mysql--表的约束
  • Ubuntu 上安装 docker 并配置 Docker Compose 详细步骤
  • MySQL去除空白字符(如非标准空格、制表符等)
  • 2063:【例1.4】牛吃牧草
  • QT开发:深入掌握 QtGui 和 QtWidgets 布局管理:QVBoxLayout、QHBoxLayout 和 QGridLayout 的高级应用
  • Bootstrapping、Bagging 和 Boosting