WebGL基础(1)一个基本的WebGL静态页面

 
<template>
  <div class="home">
    <div id="threeContainer" class="threeContainer"></div>
  </div>
</template>

<script>
import * as THREE from 'three'
export default {
  name: 'Home',
  data () {
    return {
      scene: null,  //场景对象
      camera: null,  //相机对象
      renderer: null,  //渲染器对象
      mesh: null,  //网格模型对象Mesh
    }
  },
  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} )
      // 根据几何体和材质创建物体
      const cube = new THREE.Mesh( geometry, material )
      // 将几何体添加到场景中
      this.scene.add( 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 )
    }
  }
}
</script>

  

posted @ 2022-08-01 14:45  yulingjia  阅读(112)  评论(0编辑  收藏  举报