Reactjs 使用另一个函数中的AnimationLoop()

Reactjs 使用另一个函数中的AnimationLoop(),reactjs,three.js,Reactjs,Three.js,在我的Three.JS项目中,我刚刚将我的网格移动到一个单独的文件中,而我的“startAnimationLoop()”函数无法再达到使网格(简单立方体)旋转的值 Mesh.JS import * as THREE from "three"; export default ({ scene, cube }) => { const geometry = new THREE.BoxGeometry(2, 2, 2); const material = new THREE.Me

在我的Three.JS项目中,我刚刚将我的网格移动到一个单独的文件中,而我的“startAnimationLoop()”函数无法再达到使网格(简单立方体)旋转的值

Mesh.JS

import * as THREE from "three";

export default ({ scene, cube }) => {
    const geometry = new THREE.BoxGeometry(2, 2, 2);
    const material = new THREE.MeshPhongMaterial({color: 0x156289, emissive: 
    0x072534, side: THREE.DoubleSide, flatShading: true, wireframe: false});
    cube = new THREE.Mesh(geometry, material);
    scene.add(cube);

  }
导入的网格

import Mesh from './Mesh'

  componentDidMount() {
    this.sceneSetup();
    this.startAnimationLoop();
    Mesh({ scene: this.scene, cube: this.cube });
    Lightss({ scene: this.scene, light: this.lightning })
    window.addEventListener("resize", this.handleWindowResize);
  }
动画功能

  startAnimationLoop = () => {
    this.cube.rotation.x += 0.01;  /* DOESNT WORK */ 
    this.renderer.render(this.scene, this.camera);
    this.requestID = window.requestAnimationFrame(this.startAnimationLoop);
  };
如何到达多维数据集网格
在我的startAnimationLoop()函数中?

解决此问题的一种方法:

  • 为网格指定一个描述性名称,例如
    cube.name='myCube'
  • 在动画循环的文件中,创建名为
    cube
    的模块范围变量
  • 用于从场景图中查询网格,如下所示:
    cube=this.scene.getObjectByName('myCube')
  • 出于性能原因,只执行一次查询,然后将对象缓冲在变量中是很重要的

three.js R108

到达立方体的好方法!但是我得到如下结果:constcube=this.scene.getObjectByName('myCube')//这没问题。这个.cube.rotation.x+=0.01;当我尝试添加旋转时,我得到以下typeError:typeError:无法读取未定义的任何建议的属性“rotation”?已解决!在初始化对象之前,我调用了动画函数。谢谢你,穆根!