Three.js“未捕获类型错误:无法读取未定义的属性‘渲染’”对象字面量错误

ozg*_*zer 6 javascript object-literal three.js

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

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

this.renderer.render(this.scene, this.camera);
Run Code Online (Sandbox Code Playgroud)

这是我的对象:

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();
Run Code Online (Sandbox Code Playgroud)

Iva*_*vak 9

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

基本上,您需要使用Function.prototype.bind传递上下文以及动画方法

requestAnimationFrame(this.animate.bind(this));
Run Code Online (Sandbox Code Playgroud)

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

requestAnimationFrame(this.animate.bind(this));
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)