基本ツールクラスをカスタマイズ

2072 ワード

ベースシーンとカメラ設定、ウィンドウアダプティブ、デバッグツールクラス(同じプロジェクトで複数のシーンを使用する必要がある場合)を含むベースツールクラスをカスタマイズします.
import * as THREE from 'three'
import OrbitControls from '@/lib/tools/orbitControls'
import Stats from '@/lib/tools/stats'
import dat from 'dat.gui'

export default class Stage {
  /**
  * @param {Boolean} antialias    
  * @param {Boolean} autoRender       
  * @param {Function} onRender       
  */
  constructor(antialias = true, autoRender = true, onRender) {
    this.antialias = antialias
    this.scene = new THREE.Scene()

    this.camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      0.1,
      1000
    )
    this.camera.position.z = 5

    this.initRender()
    // TODO        helper
    this.initHelper()
    this.AddAdaption()

    if(autoRender) {
      this.render()
    }
  }

  initRender() {
    const renderer = this.renderer = new THREE.WebGLRenderer({ antialias: this.antialias })

    renderer.setSize(window.innerWidth, window.innerHeight)
    renderer.setPixelRatio(window.devicePixelRatio)
    document.body.appendChild(renderer.domElement)
    renderer.render(this.scene, this.camera)
  }

  initHelper() {
    this.orbit = new OrbitControls(this.camera, this.renderer.domElement)

    this.gui = new dat.GUI()

    const stats = this.stats = new Stats()
    // 0: fps, 1: ms, 2: mb, 3+: custom
    stats.showPanel(0)
    document.body.appendChild(stats.dom)
  }

  render() {
    requestAnimationFrame(this.render.bind(this))
    this.renderer.render(this.scene, this.camera)

    if(typeof this.onRender === 'function') {
      this.onRender()
    }

    if(this.stats) {
      this.stats.update()
    }
  }

  /**
   *            
   */
  AddAdaption() {
    window.addEventListener(
      'resize',
      function () {
        this.camera.aspect = window.innerWidth / window.innerHeight
        this.camera.updateProjectionMatrix()
        this.renderer.setSize(window.innerWidth, window.innerHeight)
      },
      false
    )
  }
}