[英]Three.js “Uncaught TypeError: Cannot read property 'render' of undefined” error with object literal
I'm trying to convert my codes to object literal style. 我正在尝试将我的代码转换为对象文字样式。 I can create the scene but I got problems with animation.
我可以创建场景,但是在动画方面遇到问题。
In this line I'm getting the "Uncaught TypeError: Cannot read property 'render' of undefined" error. 在这一行中,我收到“ Uncaught TypeError:无法读取未定义的属性'render'”错误。
this.renderer.render(this.scene, this.camera);
This is my object: 这是我的对象:
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();
Check my modified example (I borrowed your objects
function to render a cube.. just for fun of it:) ) 检查我的修改示例(我借用了您的
objects
函数来渲染一个多维数据集..只是为了好玩:))
Basically, you need to pass the context along with your animate method using the Function.prototype.bind 基本上,您需要使用Function.prototype.bind将上下文与动画方法一起传递
requestAnimationFrame(this.animate.bind(this));
..what happens behind the curtain is that the first call of this.renderer.render(this.scene, this.camera);
..幕后发生的事情是
this.renderer.render(this.scene, this.camera);
的第一个调用this.renderer.render(this.scene, this.camera);
happens just fine without issues because the context is passed along with this.animate();
发生的一切都很好,因为上下文与
this.animate();
一起传递了this.animate();
method. 方法。 However, the second time the
animate
is called, by requestAnimationFrame
method, the context is not there. 但是,第二次通过
requestAnimationFrame
方法调用animate
,上下文不存在。 Hence you need to pass it manually. 因此,您需要手动传递它。
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.