gpt4 book ai didi

javascript - Three.js "Uncaught TypeError: Cannot read property ' render' of undefined"error with object literal

转载 作者:行者123 更新时间:2023-11-29 21:01:42 25 4
gpt4 key购买 nike

我正在尝试将我的代码转换为对象文字样式。我可以创建场景,但我遇到了动画问题。

在这一行中,我收到“未捕获的类型错误:无法读取未定义的属性‘渲染’”错误。

this.renderer.render(this.scene, this.camera);

这是我的对象:

var three = {
objects: function() {
/*objects*/
},
createScene: function() {
this.container = document.getElementById("container");

this.scene = new THREE.Scene();

this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.001, (26 * 10));
this.camera.position.set(26, (26 / 2), 26);

window.addEventListener("resize", function() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
});

this.objects();

this.renderer = new THREE.WebGLRenderer();
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.container.appendChild(this.renderer.domElement);

this.controls = new THREE.OrbitControls(this.camera, this.renderer.domElement);
},
animate: function() {
this.renderer.render(this.scene, this.camera);
requestAnimationFrame(this.animate);
},
render: function() {
this.createScene();
this.animate();
}
};

three.render();

最佳答案

检查我修改过的例子(我借用了你的 objects 函数来渲染一个立方体..只是为了好玩:))

基本上,您需要使用 Function.prototype.bind 将上下文与动画方法一起传递

requestAnimationFrame(this.animate.bind(this));

..幕后发生的事情是 this.renderer.render(this.scene, this.camera); 的第一次调用发生得很好,没有问题,因为上下文与this.animate(); 方法。但是,第二次通过 requestAnimationFrame 方法调用 animate 时,上下文不存在。因此,您需要手动传递它。

var three = {
objects: function() {
/*objects*/
var geometry = new THREE.BoxBufferGeometry( 3, 3, 3 );
var material = new THREE.MeshBasicMaterial( { color: 0xffaa00 } );
this.mesh = new THREE.Mesh( geometry, material );
this.mesh.position.set(24, 14, 12);
this.scene.add( this.mesh );
},
createScene: function() {
this.container = document.getElementById("container");

this.scene = new THREE.Scene();

this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.001, (26 * 10));
this.camera.position.set(26, (26 / 2), 26);

window.addEventListener("resize", function() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}.bind(this));

this.objects();

this.renderer = new THREE.WebGLRenderer();
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.container.appendChild(this.renderer.domElement);
},
animate: function() {
this.mesh.rotation.x += 0.005;
this.mesh.rotation.y += 0.01;

this.renderer.render(this.scene, this.camera);
requestAnimationFrame(this.animate.bind(this));
},
render: function() {
this.createScene();
this.animate();
}
};

three.render();
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/87/three.min.js"></script>

<div id="container"></div>

关于javascript - Three.js "Uncaught TypeError: Cannot read property ' render' of undefined"error with object literal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46130737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com