• WebGL基础(4)物体的位置、缩放、旋转属性


    <template>
      <div class="home">
        <div id="threeContainer" class="threeContainer"></div>
      </div>
    </template>
    
    <script>
    import * as THREE from 'three'
    import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' // 导入轨道控制器
    export default {
      name: 'Home',
      data () {
        return {
          scene: null,  //场景对象
          camera: null,  //相机对象
          cube: null, //物体
          renderer: null  //渲染器对象
        }
      },
      mounted () {
        this.init()
      },
      methods: {
        init () {
          // 1. 创建场景
          this.scene = new THREE.Scene()
          
          // 2. 创建相机
          this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 )
          this.camera.position.set( 0, 0, 10 ) // 设置相机位置
          this.scene.add(this.camera)
    
          // 3. 添加物体
          // 创建几何体
          const geometry = new THREE.BoxGeometry()
          const material = new THREE.MeshBasicMaterial( {color: 0x00ff00} )
          
          // 根据几何体和材质创建物体
          this.cube = new THREE.Mesh( geometry, material )
          // 修改物体的位置
          // this.cube.position.set(5, 0, 0)
          // this.cube.position.x = 3
          // 物体的缩放
          // this.cube.scale.set(3, 2, 1)
          // this.cube.scale.x = 5
          // 物体的旋转
          // this.cube.rotation.set(Math.PI / 4, 0, 0)
          // this.cube.scale.x = 5
    
          // 将几何体添加到场景中
          this.scene.add( this.cube )
          
          // 4. 初始化渲染器
          this.renderer = new THREE.WebGLRenderer()
          // 设置渲染器的尺寸大小
          this.renderer.setSize( window.innerWidth, window.innerHeight )
          // 将webgl渲染的canvas内容添加到body中
          document.getElementById('threeContainer').appendChild( this.renderer.domElement )
          // // 使用渲染器 通过相机将场景渲染出来
          // this.renderer.render( this.scene, this.camera )
          this.render()
    
          // 创建轨道控制器
          const controls = new OrbitControls( this.camera, this.renderer.domElement )
          controls.update()
    
          // 添加坐标轴辅助器
          const axesHelper = new THREE.AxesHelper( 5 )
          this.scene.add( axesHelper )
        },
        // 渲染函数
        render (time) {
          console.log(time)
          // 物体会沿着x轴一直循环运动
          this.cube.position.x += 0.01
          this.cube.rotation.x += 0.1
          if (this.cube.position.x > 5) {
            this.cube.position.x = 0
          }
          this.renderer.render( this.scene, this.camera )
          // 渲染下一帧的时候就会调用render函数
          requestAnimationFrame( this.render )
        }
      }
    }
    </script>
    

      

  • 相关阅读:
    查看系统的所有port的状态
    python技巧26[python的egg包的安装和制作]
    python类库31[进程subprocess与管道pipe]
    [BuildRelease Management]hudson插件
    python类库31[使用xml.etree.ElementTree读写xml]
    开机自动运行VMWare
    python实例26[计算MD5]
    2021年最新大厂php+go面试题集(四)
    Jumpserver开源跳板机系统
    报错:ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or director(亲测可用)
  • 原文地址:https://www.cnblogs.com/yulingjia/p/16540588.html
Copyright © 2020-2023  润新知