基于ThreeJS的3D地球

这篇具有很好参考价值的文章主要介绍了基于ThreeJS的3D地球。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

第一次接触threeJS,说实话,挺脑瓜子疼的!

功能:3D地球(纹理贴图),地球上添加标记点(经纬度),点击标记点弹出对应的信息框,地球入场动画,相机移动动画等。

先开效果图吧

threejs 地球,地图,VUE,3d,前端,vue.js

一:添加必要的依赖

yarn add three
yarn add tween
import * as THREE from "three"
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"
import * as TWEEN from "tween"

二:组件代码

<template>
  <div style="width: 100%; height: 100%;">
    <div id="layerMain">
      <div>{{ countryName }}</div>
      <div class="shape"></div>
    </div>
    <div ref="mapId" style="width: 100%; height: 100%;"></div>
  </div>
</template>
<script>
import * as THREE from "three"
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"
import * as TWEEN from "tween"
import map_img from '../../assets/images/map.jpg'
import map_wl from '../../assets/images/wl.png'
let camera, scene, controls, mesh;
let group = new THREE.Group();
let radius = 70;
let fov = 100;
export default {
  name: 'index',
  data () {
    return {
      mapDom: null,
      renderer: null,
      animationType: true, // 地球入场动画
      rotationY: true, // 地球自动旋转
      meshAnimateType: false, // 标记点动画
      lonlat: { x: 0, y: 0, z: 200 },
      countryName: null, // 数据
    }
  },
  mounted () {
    this.info ()
  },
  methods: {
    // 初始化
    info () {
      this.infoThree ()
      this.infoBall ()
      this.infoRender ()
      this.renderer.domElement.addEventListener("click", this.infoMouse)
    },

    // 基本配置
    infoThree () {
      // 场景
      scene = new THREE.Scene()
      // 渲染
      this.renderer = new THREE.WebGLRenderer({
        antialias: true,
      })
      this.mapDom = this.$refs.mapId
      this.renderer.setSize(this.mapDom.clientWidth, this.mapDom.clientHeight)
      this.renderer.setClearColor(0x000, 0)
      this.mapDom.appendChild(this.renderer.domElement)
      // 相机
      camera = new THREE.PerspectiveCamera(
        fov,
        this.mapDom.clientWidth / this.mapDom.clientHeight,
        1,
        1000
      )
      camera.position.set(0, 0, 200)
      camera.lookAt(0, 0, 0)
      // 鼠标
      this.infoOrbitControls()
    },

    // 重新渲染
    infoRender() {
      this.renderer.clear()
      // 地球入场动画
      if (this.animationType) this.ballAnimation()
      // 地球旋转
      if (this.rotationY) this.ballRotationY()
      // 标记点动画
      if (this.meshAnimateType) this.meshAnimate()
      this.renderer.render(scene, camera)
      requestAnimationFrame(this.infoRender)
      TWEEN.update()
    },

    // 鼠标
    infoOrbitControls() {
      controls = new OrbitControls(camera, this.renderer.domElement)
      controls.enableDamping = true
      controls.enableZoom = true
      controls.autoRotate = false
      controls.autoRotateSpeed = 2
      controls.enablePan = true
    },

    // 地球
    infoBall() {
      // 纹理贴图
      let textureLoader = new THREE.TextureLoader();
      textureLoader.load(map_img, function (texture) {
        // 创建球
        let geometry = new THREE.SphereGeometry(radius, 100, 100);
        let material = new THREE.MeshBasicMaterial({
          map: texture, //设置颜色贴图属性值
        });
        //网格模型对象Mesh
        mesh = new THREE.Mesh(geometry, material);
        // 唯一标识
        mesh.name = "ballMain";
        // 添加到场景中
        scene.add(mesh);
      });
    },

    // 地球入场动画
    ballAnimation() {
      fov -= 0.6
      if (fov <= 45) {
        this.animationType = false
        camera.position.set(0, 0, 200)
        camera.lookAt(0, 0, 0)
        this.infoOrbitControls()
      } else {
        camera = new THREE.PerspectiveCamera(
          fov,
          this.mapDom.clientWidth / this.mapDom.clientHeight,
          1,
          1000
        );
        camera.position.set(0, 0, 200)
        camera.lookAt(0, 0, 0)
      }
    },

    // 地球自动旋转
    ballRotationY() {
      scene.rotation.y += 0.003
    },

    // 添加纹理标记点
    infoMark(item) {
      console.log(group)
      let cityGeometry = new THREE.PlaneBufferGeometry(1, 1) //默认在XOY平面上
      let textureLoader = new THREE.TextureLoader()
      let texture = textureLoader.load(map_wl)
      let cityWaveMaterial = new THREE.MeshBasicMaterial({
        color: item.color,
        map: texture,
        transparent: true,
        opacity: 0,
        side: THREE.DoubleSide
      })
      let mesh = new THREE.Mesh(cityGeometry, cityWaveMaterial)
      const coord = this.lon2xyz(radius * 1.01, item.lon, item.lat)
      mesh.scale.set(2, 2, 2)
      // 唯一标识
      mesh.name = item.name
      mesh.privateType = 'mark'
      mesh.position.set(coord.x, coord.y, coord.z)
      const coordVec3 = new THREE.Vector3(
        coord.x,
        coord.y,
        coord.z
      ).normalize()
      const meshNormal = new THREE.Vector3(0, 0, 1)
      mesh.quaternion.setFromUnitVectors(meshNormal, coordVec3)
      if (scene.getObjectByName(item.name) === undefined) {
        group.add(mesh)
        //网格模型添加到场景中
        scene.add(group)
        this.meshAnimateType = true
      }
    },

    // 标记点动画
    meshAnimate() {
      for (let i = 0; i < group.children.length; i++) {
        if (group.children[i].privateType === "mark") {
          // 添加初始随机数,防止动画同步
          group.children[i].material.opacity += Math.random() * 0.05
          group.children[i].scale.set(
            group.children[i].material.opacity + 7,
            group.children[i].material.opacity + 7,
            group.children[i].material.opacity + 7
          )
          if (group.children[i].scale.x >= 9) {
            group.children[i].material.opacity = 0
          }
        }
      }
    },

    // 移动相机
    cameraPos (objList) {
      this.frameDivClose ()
      let layerObj = scene.getObjectByName(objList.name)
      if (layerObj) {
        scene.rotation.y = 0
        this.rotationY = false
        new TWEEN.Tween( { x: this.lonlat.x, y: this.lonlat.y, z: this.lonlat.z } )
            .to( { x: layerObj.position.x * 2.8, y: layerObj.position.y * 2.8, z: layerObj.position.z * 2.8}, 1500 )
            .onUpdate( function () {
              camera.position.x = this.x
              camera.position.y = this.y
              camera.position.z = this.z
              camera.lookAt(0, 0, 0)
            })
            .onComplete ( ()=> {
              this.retrievalLayer (objList.name)
            })
            .easing(TWEEN.Easing.Sinusoidal.InOut)
            .start()
        this.lonlat = camera.position
        // 弹窗面板赋值
        this.countryName = objList.name
      } else {
        console.log('图层数据已被全部删除,请重新刷新界面,或者重新调用数据初始化方法: this.infoMap ()')
        alert('图层数据已被全部删除,请重新刷新界面,或者重新调用数据初始化方法: this.infoMap ()')
      }
    },

    // 检索指定的图层
    retrievalLayer (name) {
      let layerObj = scene.getObjectByName(name)
      this.infoDiv(layerObj.position.x, layerObj.position.y, layerObj.position.z)
    },

    // 鼠标事件(点击标记的点的事件)
    infoMouse(event) {
      event.preventDefault();
      const raycaster = new THREE.Raycaster();
      const mouse = new THREE.Vector2();
      // 通过鼠标点击位置,计算出 raycaster 所需点的位置,以屏幕为中心点,范围 -1 到 1
      let getBoundingClientRect = this.mapDom.getBoundingClientRect();
      mouse.x =
        ((event.clientX - getBoundingClientRect.left) /
          this.mapDom.offsetWidth) *
        2 -
        1;
      mouse.y =
        -(
          (event.clientY - getBoundingClientRect.top) /
          this.mapDom.offsetHeight
        ) *
        2 +
        1;
      //通过鼠标点击的位置(二维坐标)和当前相机的矩阵计算出射线位置
      raycaster.setFromCamera(mouse, camera);
      // 获取与射线相交的对象数组,其中的元素按照距离排序,越近的越靠前
      let intersects = raycaster.intersectObjects(scene.children);
      // 点击对象的处理
      for (let i = 0; i < intersects.length; i++) {
        if (intersects[i].object.name !== 'ballMain') {
          // 弹窗面板赋值
          this.countryName = intersects[i].object.name
          let objList = {
            name: intersects[i].object.name
          }
          this.cameraPos (objList)
          return false
        } else {
          // 开启自动旋转
          this.rotationY = true
          this.frameDivClose ()
        }
      }
    },

    // 标签
    infoDiv(pointx, pointy, pointz) {
      // 坐标转换
      let world_vector = new THREE.Vector3(
        pointx,
        pointy,
        pointz
      )
      let vector = world_vector.project(camera)
      let halfWidth = this.mapDom.offsetWidth / 2,
        halfHeight = this.mapDom.offsetHeight / 2
      let x = Math.round(vector.x * halfWidth + halfWidth)
      let y = Math.round(-vector.y * halfHeight + halfHeight)
      //创建div容器
      let moonDiv = document.getElementById("layerMain")
      moonDiv.style.display = "block"
      moonDiv.style.left = x - 150 + "px"
      moonDiv.style.top = y - 180 + "px"
    },

    // 关闭标签
    frameDivClose() {
      let divHtml = document.getElementById("layerMain")
      divHtml.style.display = "none"
    },

    // 添加光柱
    infoColumn (item) {
      const material = new THREE.MeshBasicMaterial({
        color: item.color,
        transparent: true,
        opacity: .9,
        side: THREE.DoubleSide
      })
      const coord = this.lon2xyz(radius * 1.01, item.lon, item.lat)
      const coordVec3 = new THREE.Vector3(coord.x, coord.y, coord.z).normalize()
      const geometry = new THREE.CylinderGeometry(0.2, 2.8, 30)
      const mesh = new THREE.Mesh(geometry, material)
      mesh.name = item.name
      mesh.privateType = 'column'
      mesh.position.set(coord.x, coord.y, coord.z)
      mesh.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), coordVec3)
      group.add(mesh)
      scene.add(group)
    },

    // 删除所有标记点
    delAll () {
      this.frameDivClose ()
      group.traverse((item) => {
        if (item.type === 'Mesh') {
          item.geometry.dispose()
          item.material.dispose()
        }
      })
      scene.remove(group)
      // 删除group中的children
      if (group.children && group.children.length > 0) {
        let i = 0
        for (i; i < group.children.length; i++) {
          group.remove(group.children[i])
        }
      }
    },

    // 删除指定标记点
    delMark (item) {
      this.frameDivClose ()
      let layerObj = scene.getObjectByName(item.name)
      group.remove(layerObj)
    },

    // 经纬度转坐标
    lon2xyz(R, longitude, latitude) {
      const lon = (Number(longitude) + 90) * (Math.PI / 180)
      const lat = Number(latitude) * (Math.PI / 180)
      const x = R * Math.cos(lat) * Math.sin(lon)
      const y = R * Math.sin(lat)
      const z = R * Math.cos(lon) * Math.cos(lat)
      return { x, y, z }
    },
  }
}
</script>

<style lang="scss">
#layerMain {
  position: absolute;
  width: 300px;
  height: 160px;
  line-height: 160px;
  text-align: center;
  color: white;
  display: none;
  background-color: rgba(34,34,35,.6);
  .shape {
    position: absolute;
    margin: auto;
    left: 0;
    right: 0;
    width: 0;
    height: 0;
    bottom: -40px;
    border: 20px solid transparent;
    border-top-color: rgba(34,34,35,.6);
  }
}
</style>

三:父组件中的代码

<template>
  <div class="home">
    <div class="rightMain">
      <div class="title">跳转操作</div>
      <div class="cont">
        <ul>
          <li v-for="(item, index) in objList" @click="cameraPos(item)">{{ item.name }}</li>
        </ul>
      </div>
      <div class="title">其他操作</div>
      <div class="cont">
        <ul>
          <li @click="rotationChange">{{ rotation }}</li>
          <li @click="columnChange">添加光柱</li>
          <li @click="delAllChange">删除所有</li>
          <li @click="delMarkChange">删除美国标记点</li>
        </ul>
      </div>
      <div class="title">重置操作</div>
      <div class="cont">
        <ul>
          <li @click="reset">初始化数据</li>
        </ul>
      </div>
    </div>
    <threeIndex ref="threeMapId"></threeIndex>
  </div>
</template>

<script>
import threeIndex from '../components/three/Index'
export default {
  name: 'HomeView',
  components: {
    threeIndex
  },
  data () {
    return {
      objList: [
        { lon: 116.358976, lat: 39.803282, name: "中国", color: '#1FFBC6' },
        { lon: 139.812263, lat: 35.677294, name: "日本", color: '#A41FE8'},
        { lon: 77.198596, lat: 28.575136, name: "印度", color: '#E8BB1F' },
        { lon: -77.02238, lat: 38.900042, name: "美国", color: '#E81F56' },
        { lon: 31.266092, lat: 30.085626, name: "埃及", color: '#1FFBC6' },
        { lon: 103.813654, lat: 1.291125, name: '新加坡', color: '#E8BB1F' },
        { lon: -47.930912, lat: -15.781949, name: '巴西', color: '#A41FE8' },
        { lon: 149.130214, lat: -35.318235, name: '澳大利亚', color: '#E81F56' }
      ],
      objList_2: [
        { lon: 116.358976, lat: 39.803282, name: "中国column", color: '#1FFBC6' },
        { lon: 139.812263, lat: 35.677294, name: "日本column", color: '#A41FE8'},
        { lon: 77.198596, lat: 28.575136, name: "印度column", color: '#E8BB1F' },
        { lon: -77.02238, lat: 38.900042, name: "美国column", color: '#E81F56' },
        { lon: 31.266092, lat: 30.085626, name: "埃及column", color: '#1FFBC6' },
        { lon: 103.813654, lat: 1.291125, name: '新加坡column', color: '#E8BB1F' },
        { lon: -47.930912, lat: -15.781949, name: '巴西column', color: '#A41FE8' },
        { lon: 149.130214, lat: -35.318235, name: '澳大利亚column', color: '#E81F56' }
      ],
      rotation: '关闭旋转'
    }
  },
  mounted () {
    this.infoMap ()
  },
  methods: {
    // 重置
    reset () {
      this.infoMap ()
    },

    // 地球添加标记点
    infoMap() {
      for (let i = 0; i < this.objList.length; i++) {
        this.$refs.threeMapId.infoMark(this.objList[i]);
      }
    },

    // 移动相机
    cameraPos(item) {
      this.$refs.threeMapId.cameraPos(item);
    },

    // 开启或关闭地球自动旋转
    rotationChange () {
      this.$refs.threeMapId.rotationY = !this.$refs.threeMapId.rotationY
      this.rotation = this.$refs.threeMapId.rotationY === true?'关闭旋转':'开启旋转'
      this.$refs.threeMapId.frameDivClose()
    },

    // 添加光柱infoColumn
    columnChange () {
      for (let i = 0; i < this.objList_2.length; i++) {
        this.$refs.threeMapId.infoColumn(this.objList_2[i]);
      }
    },

    // 删除所有标记点
    delAllChange () {
      for (let i = 0; i < this.objList.length; i++) {
        this.$refs.threeMapId.delAll(this.objList[i]);
      }
      for (let i = 0; i < this.objList_2.length; i++) {
        this.$refs.threeMapId.delAll(this.objList_2[i]);
      }
    },

    // 删除指定标记点
    delMarkChange () {
      let item = {
        name: '美国'
      }
      this.$refs.threeMapId.delMark(item)
    },
  }
}
</script>

<style lang="scss">
.home {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-image: url("../assets/images/back.png");
  background-size: 100% 130%;
  .rightMain {
    position: absolute;
    right: 0;
    width: 300px;
    height: 100%;
    z-index: 100;
    padding: 10px;
    box-sizing: border-box;
    color: white;
    background-color: rgba(255,255,255, .2);
    .title {
      width: 100%;
      font-size: 18px;
      font-weight: bold;
      margin-bottom: 10px;
    }
    .cont {
      height: 150px;
      ul {
        padding: 0;
        margin-bottom: 0;
        li {
          list-style: none;
          float: left;
          width: 33.33%;
          padding: 10px 0;
          text-align: center;
          cursor: pointer;
          font-size: 14px;
          &:hover {
            color: aquamarine;
          }
        }
      }
    }
  }
}
</style>

四:项目gitee地址

mythree: 基于three的3D地球案例文章来源地址https://www.toymoban.com/news/detail-523294.html

到了这里,关于基于ThreeJS的3D地球的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Threejs实现3d地球记录(4)

    Three.js基础曲线函数有三种: 样条曲线:在三维空间中设置5个顶点,输入三维样条曲线CatmullRomCurve3函数作为参数,然后返回更多个顶点,通过返回的顶点数据,构建一个几何体,然后绘制出来一条沿着5个顶点的光滑样条曲线。 三维三次贝赛尔曲线: 由起点、终点、及两个

    2024年02月02日
    浏览(35)
  • 使用 Threejs 从基础开始构建 3D 地球

    演示效果 git源码地址 场景创建 相机 坐标辅助器 状态检测器 渲染器 轨道控制器 粒子星空 地球和大气层创建 创建星轨环 创建卫星移动轨迹 创建卫星 二维经纬度坐标转三维球坐标 创建标点 绘制飞线 在地球上绘制标点和飞线 一般在执行完上述方法后能看到如下图的效果:

    2024年01月25日
    浏览(44)
  • threeJs实现3D地球-旋转-自定义贴图-透明发光

    //---html (angular)---         //---ts--- 效果图:

    2024年04月17日
    浏览(30)
  • uniapp vue3中使用threejs渲染3D模型

    前言: 因为公司需求, 需要在App中内嵌一个3D模型. 在市场上看了一下情况, 大部分都是vue2的, 并没有vue3的版本...现在vue3也不是个新东西了. 后期模型会放入App内. 下面写法并不支持App(已解决在App中渲染, 关注我可见), 支持h5 template: js: 上面写法并不优雅, 只是临时作为一个demo可

    2024年02月12日
    浏览(29)
  • Vue3集成ThreeJS实现3D效果,threejs+Vite+Vue3+TypeScript 实战课程【一篇文章精通系列】

    这是一个使用Vue3,TypeScript,Vite和Three.js的项目。Vue3是一个流行的JavaScript框架,用于构建用户界面。TypeScript是一种静态类型的编程语言,它是JavaScript的超集,可以编译成纯JavaScript。Vite是一个由Evan You开发的新的前端构建工具,能够提供快速的冷启动和即时热更新。 Three.j

    2024年02月03日
    浏览(38)
  • VUE3+ThreeJs实现3D全景场景,可自由旋转视角

    three.js是一个用于在Web上创建三维图形的JavaScript库。它可以用于创建各种类型的三维场景,包括游戏、虚拟现实、建筑和产品可视化等。three.js提供了许多功能和特性,包括3D渲染、光照、材质、几何形状、动画、交互和相机控制等。使用three.js,开发人员可以轻松地创建复杂

    2024年02月11日
    浏览(37)
  • threejs创建3D炫酷地图(地图流光,飞线,水印动画,高光)

    下载地图的json,这里我们可以在阿里云数据平台上进行下载 2.在代码中解析下载的json const fileloader = new THREE.FileLoader(); fileloader.load(\\\'/china.json\\\',(res)={ res=JSON.parse(res) createMap(res) }) 3.得到的坐标点是经纬度,所以我们要把它转为二维坐标,这里使用插件d3 const projection = d3 .geoMer

    2024年02月09日
    浏览(33)
  • ThreeJS-VUE-3DMax 实现Web3D(简单测试)

            今天使用3DMax建模软件进行3D模型的制作,并且加入动画,通过threejs将模型及其动画部署在VUE框架上。         1. VUE:3.3.4         2. threejs:0.158.0         3. vite:4.4.11         4. 3DMax2021         5. pycharm2021专业版         简单制作一个小植物和一个水壶,实现浇

    2024年02月03日
    浏览(38)
  • vue echarts 3D地球和世界地图的实现,并且显示不同国家的数据

    别忘记给#earth元素设置宽高 效果如下图 别忘记给#world元素设置宽高 其中注意点是world.js 下载地址 下载完成以后需要对其进行改变一下,原本是他是放在一个匿名自执行函数里面,直接在vue里面引用会报错,要把他变成 export 对象,代码片段实例 效果如下图 关键点在globe里面

    2024年02月04日
    浏览(38)
  • 前端前沿web 3d可视化技术 ThreeJS学习全记录

    完整案例与项目代码: gitee开源项目地址 https://gitee.com/jumping-world-line/01_threeJS_basic 随着浏览器性能和网络带宽的提升 使得3D技术不再是桌面的专利 打破传统平面展示模式 前端方向主要流向的3D图形库包括Three.js和WebGL WebGL灵活高性能,但代码量大,难度大,需要掌握很多底层

    2024年02月01日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包