简体   繁体   English

Three.js“未捕获的TypeError:无法读取属性'render'的未定义”错误与对象文字

[英]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.

相关问题 Three.js - 未捕获的TypeError:无法读取未定义的属性“normal” - Three.js - Uncaught TypeError: Cannot read property 'normal' of undefined Three.js 未捕获类型错误:无法读取未定义的属性“addEventListener” - Three.js Uncaught TypeError: Cannot read property 'addEventListener' of undefined Three.js + OrbitControls - 未捕获的类型错误:无法读取未定义的属性“渲染” - Three.js + OrbitControls - Uncaught TypeError: Cannot read property 'render' of undefined webgl three.js-如何显示精灵? 错误:未被捕获的TypeError:无法在SpritePlugin.render读取未定义的属性&#39;x&#39; - webgl three.js - how to display a sprite ? Error: Uncaught TypeError: Cannot read property 'x' of undefined at SpritePlugin.render Three.js “webgl-template.html:69 Uncaught TypeError: Cannot read property 'domElement' of undefined”错误 - Three.js “webgl-template.html:69 Uncaught TypeError: Cannot read property 'domElement' of undefined" error 三.js - “未捕获的类型错误:无法读取未定义的属性&#39;中心&#39; - 三.js:6754”当我添加另一个网格时发生 - three.js - "Uncaught TypeError: Cannot read property 'center' of undefined - three.js:6754" Is happenning when i add another Mesh Three.js导入Blender模型。 未捕获的TypeError:无法读取未定义的属性“ x” - Three.js importing Blender Model. Uncaught TypeError: Cannot read property 'x' of undefined Three.js + OrbitControls - 未捕获的类型错误:无法读取未定义的属性“addEventListener” - Three.js + OrbitControls - Uncaught TypeError: Cannot read property 'addEventListener' of undefined three.js TypeError:无法读取未定义的属性“旋转” - three.js TypeError: Cannot read property 'rotation' of undefined 未捕获的TypeError:无法读取未定义的属性“ render” - Uncaught TypeError: Cannot read property 'render' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM