three.js使用轨道控件OrbitControls控制相机(vue中使用three.js65)

1.demo效果

在这里插入图片描述

如上图,该demo通过轨道控件OrbitControls控制相机。实现按住鼠标左键旋转物体,按住鼠标右键平移物体,滚动滚轮缩放物体,还可以通过方向键移动物体位置

2.OrbitControls介绍

通过轨道控件OrbitControls 可以实现按住鼠标左键旋转物体,按住鼠标右键平移物体,滚动滚轮缩放物体,还可以通过方向键移动物体位置

OrbitControls操控说明
以下是操控和动作的比对说明

操控动作
按鼠标左键移动在场景中旋转物体
按鼠标右键移动在场景中移动物体
滚动滚轮或按住中键并移动缩放物体
方向键场景中移动物体

3. 实现要点

3.1 vue中引入OrbitControls控制器

在项目工程中引入OrbitControls控制器如下

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

3.2 创建轨道控件实例

// 创建轨道控制器
createControls() {
  this.clock = new THREE.Clock() // 创建THREE.Clock对象,用于计算上次调用经过的时间
  this.controls = new OrbitControls(this.camera, this.renderer.domElement)
  this.controls.autoRotate = true // 是否自动旋转
}

3.3 render中更新轨道控件

render() {
  const delta = this.clock.getDelta() //获取自上次调用的时间差
  this.controls.update(delta) // 相机更新
  this.renderer.render(this.scene, this.camera)
  requestAnimationFrame(this.render)
}

4. demo代码

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

<script>
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
export default {
  data() {
    return {
      earchMesh: null,
      camera: null,
      scene: null,
      renderer: null,
      controls: null
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 初始化
    init() {
      this.createScene() // 创建场景
      this.createModels() // 创建模型
      this.createLight() // 创建光源
      this.createCamera() // 创建相机
      this.createRender() // 创建渲染器
      this.createControls() // 创建控件对象
      this.render() // 渲染
    },
    // 创建场景
    createScene() {
      this.scene = new THREE.Scene()
    },
    // 创建模型
    createModels1() {
      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.specular = new THREE.Color(0xff0000)
      planetMaterial.shininess = 2

      planetMaterial.normalMap = normalTexture
      planetMaterial.map = planetTexture
      // planetMaterial.shininess = 150
      const sphereGeom = new THREE.SphereGeometry(20, 40, 40)
      this.earchMesh = new THREE.Mesh(sphereGeom, planetMaterial)

      this.scene.add(this.earchMesh)
    },

    createModels() {
      const publicPath = process.env.BASE_URL
      const planetTexture = THREE.ImageUtils.loadTexture(
        `${publicPath}textures/planets/mars_1k_color.jpg`
      )
      const normalTexture = THREE.ImageUtils.loadTexture(
        `${publicPath}textures/planets/mars_1k_normal.jpg`
      )

      const planetMaterial = new THREE.MeshPhongMaterial()
      planetMaterial.map = planetTexture
      planetMaterial.bumpMap = normalTexture

      // planetMaterial.shininess = 50
      const sphereGeom = new THREE.SphereGeometry(20, 40, 40)
      this.earchMesh = new THREE.Mesh(sphereGeom, planetMaterial)

      this.scene.add(this.earchMesh)
    },

    // 创建光源
    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(30, 30, 30) // 设置相机位置

      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() {
      const delta = this.clock.getDelta() // 获取自上次调用的时间差
      this.controls.update(delta) // 相机更新
      this.renderer.render(this.scene, this.camera)
      requestAnimationFrame(this.render)
    },
    // 创建轨道控制器
    createControls() {
      this.clock = new THREE.Clock() // 创建THREE.Clock对象,用于计算上次调用经过的时间
      this.controls = new OrbitControls(this.camera, this.renderer.domElement)
      this.controls.autoRotate = true // 是否自动旋转
    }
  }
}
</script>

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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 你可以在Vue3使用Three.js,首先需要安装Three.js库,然后在Vue组件引入Three.js库,可以使用import语句引入。接着,你可以在Vue组件使用Three.js的API来创建3D场景、模型等。需要注意的是,在Vue3使用Three.js时,需要使用Vue的生命周期函数来管理Three.js的渲染和更新。具体实现方法可以参考Three.js官方文档和Vue3官方文档。 ### 回答2: 在Vue3使用Three.js可以通过以下步骤实现: 1. 在你的Vue项目安装Three.js库。可以使用npm或者yarn来安装,命令如下: ``` npm install three ``` 或 ``` yarn add three ``` 2. 在Vue组件引入Three.js库。在需要使用Three.js的组件文件,添加以下代码: ```js import * as THREE from 'three'; ``` 这样,你就可以在该组件使用Three.js提供的所有功能和类。 3. 创建Three.js场景和渲染器。在Vue组件的`mounted`钩子函数,创建一个空的Three.js场景,并将其渲染到HTML页面上的某个容器,代码如下: ```js const scene = new THREE.Scene(); const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.getElementById('container').appendChild(renderer.domElement); ``` 4. 添加和渲染Three.js的物体。在场景创建并添加需要渲染的物体,如立方体等,然后通过调用渲染器的`render`函数将场景渲染到页面上,代码如下: ```js const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); renderer.render(scene, camera); // 其camera为相机对象,需要提前创建并设置 ``` 5. 在Vue组件使用动画循环更新场景。你可以使用Vue的`watch`侦听器或者使用Vue提供的动画插件,来持续更新Three.js场景的状态,并在每一帧重新渲染场景,以实现动态效果。 以上就是在Vue3使用Three.js的基本步骤。通过这些步骤,你可以在Vue项目使用Three.js创建出交互式和动态的3D图形。 ### 回答3: 在Vue3使用Three.js的步骤如下: 1. 安装Three.js:可以通过npm或者yarn命令行安装Three.js库。在项目根目录打开终端,执行以下命令: ``` npm install three ``` 或者 ``` yarn add three ``` 2. 创建Vue组件:在Vue项目创建一个新的Vue组件,用来承载Three.js场景和渲染器。可以在方法或者生命周期函数编写Three.js逻辑。 3. 引入Three.js:在Vue组件使用import语句引入Three.js库: ```javascript import * as THREE from 'three' ``` 4. 创建场景、渲染器和相机:在Vue组件的mounted生命周期函数创建Three.js所需的场景、渲染器和相机: ```javascript mounted () { const scene = new THREE.Scene() const renderer = new THREE.WebGLRenderer() const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000) renderer.setSize(window.innerWidth, window.innerHeight) this.$refs.container.appendChild(renderer.domElement) } ``` 5. 添加物体和光源:可以在Vue组件的mounted生命周期函数添加物体和光源到场景: ```javascript const geometry = new THREE.BoxGeometry() const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }) const cube = new THREE.Mesh(geometry, material) scene.add(cube) const light = new THREE.PointLight(0xffffff) light.position.set(0, 0, 10) scene.add(light) ``` 6. 渲染场景:在Vue组件的mounted生命周期函数使用requestAnimationFrame函数循环渲染场景: ```javascript function animate () { requestAnimationFrame(animate) renderer.render(scene, camera) } animate() ``` 7. 更新和交互:可以在Vue组件监听鼠标和键盘事件,并根据需要更新Three.js场景。 这些是在Vue3使用Three.js的基本步骤。根据具体需求,还可以使用其他Three.js功能,如纹理贴图、动画等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值