简体   繁体   English

Three.js - 未捕获TypeError:undefined不是函数

[英]Three.js - Uncaught TypeError: undefined is not a function

I'm getting the Uncaught TypeError: undefined is not a function while using Three.js. 我得到了Uncaught TypeError: undefined is not a function在使用Three.js时Uncaught TypeError: undefined is not a function The error is being shown for the line where I'm creating a THREE.PerspectiveCamera . 我正在创建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. 我检查了three.js文档和示例给出的基本相同。 So I'm stuck on what to do. 所以我不知所措。

UPDATE: I was using a local copy of Three.js when I encountered the last error. 更新:当我遇到最后一个错误时,我正在使用Three.js的本地副本。 I switched it with the link http://www.html5canvastutorials.com/libraries/Three.js . 我用链接http://www.html5canvastutorials.com/libraries/Three.js切换它。 Now, the PerspectiveCamera error is gone, but it produces a new error inside the Three.js script. 现在,PerspectiveCamera错误消失了,但它在Three.js脚本中产生了一个新错误。 The error is Uncaught TypeError: Cannot read property 'x' of undefined on line 337 of the Three.js script 错误是Uncaught TypeError: Cannot read property 'x' of undefined在Three.js脚本的第337行Uncaught TypeError: Cannot read property 'x' of undefined

Thanks. 谢谢。

The problem with the local copy is you used the unbuilt Three.js file (that would be src/Three.js). 本地副本的问题是您使用了未构建的Three.js文件(即src / Three.js)。 The one you want is either build/three.js or build/three.min.js. 你想要的是build / three.js或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. 我将其复制到我的项目中,并在脚本标记的src属性中更改了对它的引用,这一切都开始工作了。

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
       ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 THREE.js - 未捕获的TypeError - THREE.js - Uncaught TypeError 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 CSS3D渲染器-未捕获的TypeError:undefined不是函数 - Three.js CSS3D Renderer - Uncaught TypeError: undefined is not a function 三.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 TypeError:Firefox中未定义“ global” - THREE.js TypeError: 'global' is undefined in Firefox Three.js导入Blender模型。 未捕获的TypeError:无法读取未定义的属性“ x” - Three.js importing Blender Model. Uncaught TypeError: Cannot read property 'x' of undefined Three.js“未捕获的TypeError:无法读取属性&#39;render&#39;的未定义”错误与对象文字 - Three.js “Uncaught TypeError: Cannot read property 'render' of undefined” error with object literal Three.js + OrbitControls - 未捕获的类型错误:无法读取未定义的属性“渲染” - Three.js + OrbitControls - Uncaught TypeError: Cannot read property 'render' of undefined 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM