Three.js每秒调用一次函数

时间:2018-11-26 12:38:48

标签: reactjs animation three.js frame

我有一个向量数组。我想使用这些数组矢量移动对象,如下所示:

animate() {

this.lorryBounds.forEach(vector => {
  this.sceneManager.boxes[0].position.set(vector)
})

this.renderer.render(this.scene, this.camera)
this.frameId = window.requestAnimationFrame(this.animate)

}

但是我正在努力做到每一秒,而不是每一帧。我读了很多问题,但没有找到解决方案。我尝试了THREE.Clock()和deltaTime()。有一种方法requestAnimationFrame,但是我不明白如何在React.js中使用它。我的文件是:

    import React, { Component } from 'react'
import * as THREE from 'three'
import {SceneManager} from '../Three/SceneManager'
import Box from '../Models/Box'

class Scene extends Component {
  constructor(props) {
    super(props)

    this.start = this.start.bind(this)
    this.animate = this.animate.bind(this)
    this.Update = this.Update.bind(this)

  }

  componentDidMount() {

    const sceneManager = new SceneManager();
          sceneManager.setupLorry();
          sceneManager.calculateLorryBounds()

    const lorry = sceneManager.lorry
    const lorryBounds = sceneManager.lorryBounds


    const width = this.mount.clientWidth;
    const height = this.mount.clientHeight;

    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);
          camera.position.z = 20;

    const renderer = new THREE.WebGLRenderer({ antialias: true });
          renderer.setClearColor('#ffffff');
          renderer.setSize(width, height);

    //instantiate boxes
    var boxes = []
    boxes.push(new Box(1,1,1))
    boxes.push(new Box(1,2,1))
    boxes.push(new Box(2,1,1))

    sceneManager.createBoxesFromParams(boxes)
    sceneManager.boxes.forEach(element => {
      scene.add(element)
    })

    //sceneManager.boxes[0].position.set(5,-2,2)
    //console.log(sceneManager.boxes[0].position)


    scene.add(lorry);
    //get bounds
    lorry.geometry.computeBoundingBox()

    console.log(lorry.geometry.boundingBox.max)
    console.log("min bounding box : " )

    lorryBounds.forEach( vector => {
      setTimeout(() => {
        console.log(sceneManager.boxes[0].position.set(vector))
      }, 2000);

    })
    //const geometry = new THREE.BoxGeometry(10, 2, 2)
    //const geometry2 = new THREE.BoxGeometry(5, 1, 1)
    //var mat = new THREE.LineBasicMaterial( { color: 0xff2fff, linewidth: 2 } );
    //var material = new THREE.MeshBasicMaterial({color: '#FF2222'})
    //var cube = new THREE.Mesh(geometry2, material);
    //const edges = new THREE.EdgesGeometry(geometry)
    //var wireframe3 = new THREE.LineSegments( edges, mat );
    //var pivotPoint = new THREE.Object3D();



    //scene.add(wireframe3)


    //cube.add(pivotPoint);
    //scene.add(cube);



    this.scene = scene
    this.camera = camera
    this.renderer = renderer
    this.sceneManager = sceneManager
    this.lorryBounds = lorryBounds
    this.clock = new THREE.Clock();
    this.time = 0;
    this.delta = 0;
    this.speed = 1000; // units a second - 2 seconds
    this.last = 0

    this.mount.appendChild(this.renderer.domElement)
    this.start()
  }

  componentWillUnmount() {
    cancelAnimationFrame(this.frameId)
    this.mount.removeChild(this.renderer.domElement)
  }

  start() {
    if (!this.frameId) {
      this.frameId = requestAnimationFrame(this.animate)
    }
  }

  animate() {

    this.lorryBounds.forEach(vector => {
      this.sceneManager.boxes[0].position.set(vector)
    })

    this.renderer.render(this.scene, this.camera)
    this.frameId = window.requestAnimationFrame(this.animate)

  }

  render() {
    return (
      <div
        style={{ width: '100%', height: '800px' }}
        ref={(mount) => { this.mount = mount }}
      />
    )
  }
}

export default Scene

1 个答案:

答案 0 :(得分:1)

您可以使用某些throttle函数每秒执行一次功能,而您的帧速率要高得多。 (https://www.npmjs.com/package/throttle-debounce

var setBoxPosition = throttle(1000, function () {
    this.lorryBounds.forEach(vector => {
        this.sceneManager.boxes[0].position.set(vector)
    });
});

function animate() {

    setBoxPosition();

    this.renderer.render(this.scene, this.camera)
    this.frameId = window.requestAnimationFrame(this.animate)
}

或者您可以使用window.setInterval方法:https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

更新

如果要每秒对向量数组应用一个向量,可以执行以下操作:

var currentIndex = 0;
var intervalID;

function setBoxPosition() {

  this.sceneManager.boxes[0].position.set( this.lorryBounds[currentIndex] );

  if (currentIndex < this.lorryBounds.length - 1) {
    currentIndex++;
  }
  else {
    clearInterval(intervalID);
  }
}

function start() {
  if (!this.frameId) {
    this.frameId = requestAnimationFrame(this.animate)
  }

  intervalID = setInterval(setBoxPosition, 1000);
}

function animate() {
  this.renderer.render(this.scene, this.camera)
  this.frameId = window.requestAnimationFrame(this.animate)
}