如何在三个.js的不同类中使用我的"render"函数?



我是three.js的新手,我正在制作一个保龄球游戏。但我有一个问题,我在一个类中创建了物理,现在我需要从我的"应用程序"中访问一个函数。类。我真的不明白这里的问题,我很困惑。

应用程序类:

export class Application {
constructor() {  
this.objects = [];
this.createScene();
}
createScene() {
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(60,
window.innerWidth / window.innerHeight, 1, 10000);
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(this.renderer.domElement);
this.render();
}
getMesh(){
return this.curveObject;
}
update(){

}
render() {
requestAnimationFrame(() => {    
this.render();
});
this.objects.forEach((object) => {
object.update();
});
this.renderer.render(this.scene, this.camera);
}
}

Animate Function within "Physics"类:

animate(){
if (this.pinTest){
//  console.log(this.pinTest);
this.pin1Mesh.position.copy(this.pin1Body.position);
this.pin1Mesh.quaternion.copy(this.pin1Body.quaternion);
this.pinTest.position.copy(this.pin1Body.position);
this.pinTest.quaternion.copy(this.pin1Body.quaternion);
}
// I need to call render here
//this.renderer.render(this.scene, this.camera);
}

基类示例:

class baseAnimation {
animate() {
if (this.pinTest) {
//  console.log(this.pinTest);
this.pin1Mesh.position.copy(this.pin1Body.position);
this.pin1Mesh.quaternion.copy(this.pin1Body.quaternion);
this.pinTest.position.copy(this.pin1Body.position);
this.pinTest.quaternion.copy(this.pin1Body.quaternion);
} else
console.log("pinTest === false");
}
}
class Physics extends baseAnimation {
}

let test = new Physics();
test.pinTest = false;
test.animate();

将对象传递给Animation类(使用静态方法)示例:

class Animation {
static animate(obj) {
if (obj.pinTest) {
//  console.log(this.pinTest);
obj.pin1Mesh.position.copy(obj.pin1Body.position);
obj.pin1Mesh.quaternion.copy(obj.pin1Body.quaternion);
obj.pinTest.position.copy(obj.pin1Body.position);
obj.pinTest.quaternion.copy(obj.pin1Body.quaternion);
} else
console.log("pinTest === false");
}
}
class Physics {
}
let test = new Physics();
test.pinTest = false;
Animation.animate(test);

最新更新