最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Three.js - Uncaught TypeError: undefined is not a function - Stack Overflow

programmeradmin4浏览0评论

I'm getting the Uncaught TypeError: undefined is not a function while using Three.js. The error is being shown for the line where I'm creating a THREE.PerspectiveCamera. The script is

window.requestAnimFrame = (function(callback){
        return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback){
            window.setTimeout(callback, 1000 / 60);
        };
    })();

    function animate(lastTime, angularSpeed, three){
        // update
        var date = new Date();
        var time = date.getTime();
        lastTime = time;

        // render
        three.renderer.render(three.scene, three.camera);

        // request new frame
        requestAnimFrame(function(){
            animate(lastTime, angularSpeed, three);
        });
    }

    $(window).bind('load',function(){
        var angularSpeed = 0.2; // revolutions per second
        var lastTime = 0;
        $container = $("#container");
        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        $container.append(renderer.domElement);

        // camera - Uncaught Type Error on the below line
        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
        camera.position.y = -450;
        camera.position.z = 400;
        camera.rotation.x = 45 * (Math.PI / 180);

        // scene
        var scene = new THREE.Scene();

        var material = new THREE.LineBasicMaterial({
                            color: 0x0000ff,
                        });
        var geometry = new THREE.Geometry();
        for(var i=0;i<100;i++){
            geometry.vertices.push(new THREE.Vector3(i-100,i-100,i-100));
            geometry.vertices.push(new THREE.Vector3(i+100,i+100,i+100));
            var line = new Three.Line(geometry,material);
            scene.add(line);
            geometry=new THREE.Geometry();
        }


        // create wrapper object that contains three.js objects
        var three = {
            renderer: renderer,
            camera: camera,
            scene: scene,
        };

        animate(lastTime, angularSpeed, three);
    });

Is this because the way I'm declaring the camera is wrong? I checked the three.js documentation and the example give there is basically the same. So I'm stuck on what to do.

UPDATE: I was using a local copy of Three.js when I encountered the last error. I switched it with the link .js. Now, the PerspectiveCamera error is gone, but it produces a new error inside the Three.js script. The error is Uncaught TypeError: Cannot read property 'x' of undefined on line 337 of the Three.js script

Thanks.

I'm getting the Uncaught TypeError: undefined is not a function while using Three.js. The error is being shown for the line where I'm creating a THREE.PerspectiveCamera. The script is

window.requestAnimFrame = (function(callback){
        return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback){
            window.setTimeout(callback, 1000 / 60);
        };
    })();

    function animate(lastTime, angularSpeed, three){
        // update
        var date = new Date();
        var time = date.getTime();
        lastTime = time;

        // render
        three.renderer.render(three.scene, three.camera);

        // request new frame
        requestAnimFrame(function(){
            animate(lastTime, angularSpeed, three);
        });
    }

    $(window).bind('load',function(){
        var angularSpeed = 0.2; // revolutions per second
        var lastTime = 0;
        $container = $("#container");
        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        $container.append(renderer.domElement);

        // camera - Uncaught Type Error on the below line
        var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
        camera.position.y = -450;
        camera.position.z = 400;
        camera.rotation.x = 45 * (Math.PI / 180);

        // scene
        var scene = new THREE.Scene();

        var material = new THREE.LineBasicMaterial({
                            color: 0x0000ff,
                        });
        var geometry = new THREE.Geometry();
        for(var i=0;i<100;i++){
            geometry.vertices.push(new THREE.Vector3(i-100,i-100,i-100));
            geometry.vertices.push(new THREE.Vector3(i+100,i+100,i+100));
            var line = new Three.Line(geometry,material);
            scene.add(line);
            geometry=new THREE.Geometry();
        }


        // create wrapper object that contains three.js objects
        var three = {
            renderer: renderer,
            camera: camera,
            scene: scene,
        };

        animate(lastTime, angularSpeed, three);
    });

Is this because the way I'm declaring the camera is wrong? I checked the three.js documentation and the example give there is basically the same. So I'm stuck on what to do.

UPDATE: I was using a local copy of Three.js when I encountered the last error. I switched it with the link http://www.html5canvastutorials.com/libraries/Three.js. Now, the PerspectiveCamera error is gone, but it produces a new error inside the Three.js script. The error is Uncaught TypeError: Cannot read property 'x' of undefined on line 337 of the Three.js script

Thanks.

Share Improve this question edited Mar 18, 2013 at 10:10 rahules asked Mar 18, 2013 at 7:43 rahulesrahules 8273 gold badges13 silver badges33 bronze badges 13
  • Hi, can you try to type this new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); directly to the console and update your post with the output? – Lim H. Commented Mar 18, 2013 at 7:48
  • @LimH. It gives the same error, "UncaughtTypeError: undefined is not a function". Is it because the browser doesn't support perspective camera? – rahules Commented Mar 18, 2013 at 8:46
  • I've been trying the script in Chromium by enabling the "Override Software Rendering" option. I tried it in firefox and it shows -- "TypeError: THREE.PerspectiveCamera is not a constructor". – rahules Commented Mar 18, 2013 at 8:55
  • johannes-raida.de/tutorials/three.js/tutorial07/tutorial07.htm check out the conditional statements about if your supporting WebGl. where are you importing the three.js library from? – Four_lo Commented Mar 18, 2013 at 10:03
  • @Four_lo i was using a local copy that i had got from an example till now. I tried another link html5canvastutorials.com/libraries/Three.js which seems to solve the problem with the camera, but produces the same error inside the Three.js file, "Uncaught TypeError: Cannot read property 'x' of undefined " – rahules Commented Mar 18, 2013 at 10:07
 |  Show 8 more comments

1 Answer 1

Reset to default 21

The problem with the local copy is you used the unbuilt Three.js file (that would be src/Three.js). The one you want is either build/three.js or build/three.min.js. I copied that into my project, and changed the reference to it in the script tag's src attribute, and it all started working.

In short:

mrdoob-three.js-2524525(or some other number here)
 |
 +----build
 |     |
 |     +--three.js     <-- YES, CORRECT FILE
 |     +--three.min.js <-- YES
 |     ...
 |
 +----src
 ...   |
       +--Three.js     <-- NO, PREBUILT FILE THAT ONLY CONTAINS CONSTANTS
       ...
发布评论

评论列表(0)

  1. 暂无评论