three.js使相机沿指定路径运动,相机漫游,使物体在指定路径上运动(vue中使用three.js66)

1.demo效果

在这里插入图片描述

如上图,该demo实现相机沿指定路径运动始终看向地球,绿色小球沿相机相反方向运动

2. 实现要点

2.1 相机运动

2.1.1 创建运动路径

创建运动路径通过 THREE.CatmullRomCurve3 对象来创建,将会生成一条由指定点绘制的平滑三维样条曲线

//使用指定的点创建一条平滑的三维样条曲线当做相机运动路径
this.cameraCurve = new THREE.CatmullRomCurve3(
  [
    new THREE.Vector3(-30, 40, 50),
    new THREE.Vector3(35, 45, 45),
    new THREE.Vector3(40, 25, -40),
    new THREE.Vector3(40, -10, -40),
    new THREE.Vector3(40, 20, -40),
    new THREE.Vector3(-45, 35, -45)
  ],
  true
)      

2.1.2 render中更相机坐标

//相机路径的索引在0~1000中往复增加
this.cameraPathIndex += 1
if (this.cameraPathIndex === 1000) {
  this.cameraPathIndex = 0
}
const curveIndex = this.cameraPathIndex / 1000

//取相机路径上当前点的坐标
const tmpCameraPosition = this.cameraCurve.getPointAt(curveIndex) //curveIndex取值0~1
//设置相机坐标为在相机路径上当前点的位置
this.camera.position.set(
  tmpCameraPosition.x,
  tmpCameraPosition.y,
  tmpCameraPosition.z
)
this.camera.lookAt(new THREE.Vector3(0, 0, 0)) //相机看向原点

2.2 相机轨道参考线

//做一个小0.6倍的路径添加到场景,作相机运动路径参考
this.sphereCurve = this.cameraCurve.clone()
this.sphereCurve.points.forEach(point => {
  point.x *= 0.6
  point.y *= 0.2
  point.z *= 0.6
  return point
})

//参考路径上取100个点,每个点上添加蓝色小球
const pathPoints = this.sphereCurve.getPoints(100)
pathPoints.forEach(point => {
  const sphere = new THREE.SphereGeometry(0.2)
  const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0x0000ff })
  const sphereMesh = new THREE.Mesh(sphere, sphereMaterial)
  sphereMesh.position.set(point.x, point.y, point.z)
  this.scene.add(sphereMesh)
})

//绘制一条红色的路径参考线
const geometry = new THREE.BufferGeometry().setFromPoints(pathPoints)
const material = new THREE.LineBasicMaterial({ color: 0xff0000 })
const curveObject = new THREE.Line(geometry, material)
this.scene.add(curveObject)

2.3 小球运动

2.3.1 创建小球

//做一个较大的绿色小球沿相机相反反向移动
const biggerSphere = new THREE.SphereGeometry(2)
const sphereMaterial1 = new THREE.MeshBasicMaterial({ color: 0x0ff00 })
this.biggerSphereMesh = new THREE.Mesh(biggerSphere, sphereMaterial1)
this.biggerSphereMesh.position.set(
  pathPoints[0].x,
  pathPoints[0].y,
  pathPoints[0].z
)
this.scene.add(this.biggerSphereMesh)   

2.3.2 render中更小球坐标

//参考路径的索引在1001~0中往复减少
if (this.shperePathIndex === 0) {
  this.shperePathIndex = 1001
}
this.shperePathIndex -= 1

//取相参考径上当前点的坐标
const sphereCurveIndex = this.shperePathIndex / 1000 //取值0~1
const tmpSpherePosition = this.sphereCurve.getPointAt(sphereCurveIndex)
//设置绿色球的位置为参考路径上当前点的位置
this.biggerSphereMesh.position.set(
  tmpSpherePosition.x,
  tmpSpherePosition.y,
  tmpSpherePosition.z
)

3. demo代码

<template>
  <div id="container" />
</template>

<script>
import * as THREE from 'three'
export default {
  data() {
    return {
      earchMesh: null,
      camera: null,
      scene: null,
      renderer: null,
      controls: null,
      cameraPathIndex: 0,
      cameraCurve: null,
      biggerSphereMesh: null,
      sphereCurve: null,
      shperePathIndex: 1001
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 初始化
    init() {
      this.createScene() // 创建场景
      this.createModels() // 创建模型
      this.createLight() // 创建光源
      this.createCamera() // 创建相机
      this.createRender() // 创建渲染器
      this.render() // 渲染
    },
    // 创建场景
    createScene() {
      this.scene = new THREE.Scene()
    },
    // 创建模型
    createModels() {
      //使用贴图绘制地球
      const publicPath = process.env.BASE_URL
      const planetTexture = THREE.ImageUtils.loadTexture(
        `${publicPath}textures/planets/Earth.png`
      )
      const specularTexture = THREE.ImageUtils.loadTexture(
        `${publicPath}textures/planets/EarthSpec.png`
      )
      const normalTexture = THREE.ImageUtils.loadTexture(
        `${publicPath}textures/planets/EarthNormal.png`
      )
      const planetMaterial = new THREE.MeshPhongMaterial()
      planetMaterial.specularMap = specularTexture
      planetMaterial.shininess = 2
      planetMaterial.normalMap = normalTexture
      planetMaterial.map = planetTexture
      const sphereGeom = new THREE.SphereGeometry(20, 40, 40)
      this.earchMesh = new THREE.Mesh(sphereGeom, planetMaterial)
      this.scene.add(this.earchMesh)

      //使用指定的点创建一条平滑的三维样条曲线当做相机运动路径
      this.cameraCurve = new THREE.CatmullRomCurve3(
        [
          new THREE.Vector3(-30, 40, 50),
          new THREE.Vector3(35, 45, 45),
          new THREE.Vector3(40, 25, -40),
          new THREE.Vector3(40, -10, -40),
          new THREE.Vector3(40, 20, -40),
          new THREE.Vector3(-45, 35, -45)
        ],
        true
      )

      //做一个小0.6倍的路径添加到场景,作相机运动路径参考
      this.sphereCurve = this.cameraCurve.clone()
      this.sphereCurve.points.forEach(point => {
        point.x *= 0.6
        point.y *= 0.2
        point.z *= 0.6
        return point
      })

      //参考路径上取100个点,每个点上添加蓝色小球
      const pathPoints = this.sphereCurve.getPoints(100)
      pathPoints.forEach(point => {
        const sphere = new THREE.SphereGeometry(0.2)
        const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0x0000ff })
        const sphereMesh = new THREE.Mesh(sphere, sphereMaterial)
        sphereMesh.position.set(point.x, point.y, point.z)
        this.scene.add(sphereMesh)
      })

      //做一个较大的绿色小球沿相机相反反向移动
      const biggerSphere = new THREE.SphereGeometry(2)
      const sphereMaterial1 = new THREE.MeshBasicMaterial({ color: 0x0ff00 })
      this.biggerSphereMesh = new THREE.Mesh(biggerSphere, sphereMaterial1)
      this.biggerSphereMesh.position.set(
        pathPoints[0].x,
        pathPoints[0].y,
        pathPoints[0].z
      )
      this.scene.add(this.biggerSphereMesh)

      //绘制一条红色的路径参考线
      const geometry = new THREE.BufferGeometry().setFromPoints(pathPoints)
      const material = new THREE.LineBasicMaterial({ color: 0xff0000 })
      const curveObject = new THREE.Line(geometry, material)
      this.scene.add(curveObject)
    },

    // 创建光源
    createLight() {
      // 环境光
      const ambientLight = new THREE.AmbientLight(0x111111) // 创建环境光
      this.scene.add(ambientLight) // 将环境光添加到场景

      const directionLight = new THREE.DirectionalLight(0xffffff)
      directionLight.position.set(-20, 30, 40)
      directionLight.intensity = 1.5
      this.scene.add(directionLight)
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById('container')
      const width = element.clientWidth // 窗口宽度
      const height = element.clientHeight // 窗口高度
      const k = width / height // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      this.camera = new THREE.PerspectiveCamera(45, k, 0.1, 1000)
      this.camera.position.set(130, 130, 130) // 设置相机位置

      this.camera.lookAt(new THREE.Vector3(0, 0, 0)) // 设置相机方向
      this.scene.add(this.camera)
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById('container')
      this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
      this.renderer.setSize(element.clientWidth, element.clientHeight) // 设置渲染区域尺寸
      //this.renderer.shadowMap.enabled = true // 显示阴影
      // this.renderer.shadowMap.type = THREE.PCFSoftShadowMap
      this.renderer.setClearColor(0x000000, 1) // 设置背景颜色
      element.appendChild(this.renderer.domElement)
    },

    render() {
      //相机路径的索引在0~1000中往复增加
      this.cameraPathIndex += 1
      if (this.cameraPathIndex === 1000) {
        this.cameraPathIndex = 0
      }
      const curveIndex = this.cameraPathIndex / 1000

      //取相机路径上当前点的坐标
      const tmpCameraPosition = this.cameraCurve.getPointAt(curveIndex) //curveIndex取值0~1
      //设置相机坐标为在相机路径上当前点的位置
      this.camera.position.set(
        tmpCameraPosition.x,
        tmpCameraPosition.y,
        tmpCameraPosition.z
      )
      this.camera.lookAt(new THREE.Vector3(0, 0, 0)) //相机看向原点

      //参考路径的索引在1001~0中往复减少
      if (this.shperePathIndex === 0) {
        this.shperePathIndex = 1001
      }
      this.shperePathIndex -= 1

      //取相参考径上当前点的坐标
      const sphereCurveIndex = this.shperePathIndex / 1000 //取值0~1
      const tmpSpherePosition = this.sphereCurve.getPointAt(sphereCurveIndex)
      //设置绿色球的位置为参考路径上当前点的位置
      this.biggerSphereMesh.position.set(
        tmpSpherePosition.x,
        tmpSpherePosition.y,
        tmpSpherePosition.z
      )

      this.renderer.render(this.scene, this.camera)
      requestAnimationFrame(this.render)
    }
  }
}
</script>

<style>
#container {
  position: absolute;
  width: 100%;
  height: 100%;
}
</style>

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
### 回答1: 百度地图getpoint是一个用于获取批量坐标的接口。在使用该接口前,我们需要准备好一份数据,这份数据包括待查询的地址或名称等信息。然后,通过调用getpoint接口,将数据传入接口,接口会返回查询结果,并将结果所有地址或名称的经纬度坐标信息一一对应。 使用getpoint接口还需要注意一些细节问题。首先,我们需要选择适当的查询方式。百度地图提供了多种查询方式,如通过地名或地址查询,通过经纬度范围查询等。在选择查询方式时,应根据实际需求和数据特点进行选择。其次,我们还需要注意接口参数的设置,例如查询方式、查询范围等。最后,我们可以对查询结果进行适当的处理和展示,如地图标注、统计分析等。 总之,百度地图getpoint是一个方便快捷的批量坐标查询工具,可以帮助我们高效地处理大量坐标信息,节省查询时间和成本。 ### 回答2: 百度地图API有一个名为getPoint的方法可以帮助我们快速获取多条地理位置坐标信息。通过getPoint方法,我们可以传入一组地址信息,然后获取到这些地址对应的经纬度信息。 这个方法的使用非常简单,只需要在代码引入百度地图的Javascript API,然后创建Map对象。在Map对象调用getPoint方法,传入参数,即可获取到所需的地址信息。 例如,我们可以编写以下代码来获取北京市朝阳区三里屯的地理坐标信息: var map = new BMap.Map("container"); var addressArr = ["北京市朝阳区三里屯SOHO"]; map.centerAndZoom("北京市", 15); var pointArr = []; for (var i = 0; i < addressArr.length; i++) { var address = addressArr[i]; var myGeo = new BMap.Geocoder(); myGeo.getPoint(address, function(point){ pointArr.push(point); console.log(pointArr); }); } 以上代码将创建一个地图对象,并定义一个包含北京市朝阳区三里屯商业区名字的地址数组。接着通过遍历地址数组,使用BMap.Geocoder的getPoint方法获取每个地址对应的地理位置坐标信息,并将这些坐标存储到一个数组,最后在控制台打印输出。 通过这种方式,我们可以方便地获取多个地理位置的坐标信息,再分别在地图上显示出来。百度地图API的getPoint方法大大简化了我们获取地理信息的过程,可以有效提高地图应用的开发效率。 ### 回答3: 百度地图getpoint是一种用于获取地图上点坐标的API。该API允许用户通过在地图上单击或拖动鼠标来获取点坐标。同时,还可以使用批量获取的方法,通过传入坐标的数组来实现一次获取多个坐标点的功能。 具体来说,当我们需要获取多个坐标点时,可以将坐标点放入数组,然后通过调用getpoint方法并传入包含坐标点数组的参数来实现批量获取。在返回的结果,可以得到每个坐标点的经度和纬度信息,以及详细的位置描述信息。 除了普通的批量获取方法外,百度地图还提供了基于“矩形框”和“圆形范围”两种特定区域的批量坐标获取方式。用户可以根据需求选择不同的方法来获取所需要的坐标信息。 总之,百度地图getpoint提供了多种方式来获取批量坐标,极大地方便了用户在进行地图位置定位和数据分析等方面的工作,帮助用户更高效地完成地图相关的任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值