2013-03-18 13 views
11

Ricevo il Uncaught TypeError: undefined is not a function mentre utilizzo Three.js. L'errore viene mostrato per la linea in cui sto creando un THREE.PerspectiveCamera. Lo script èThree.js - Uncaught TypeError: undefined non è una funzione

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); 
    }); 

È questo perché il modo in cui sto dichiarando la fotocamera è sbagliato? Ho controllato la documentazione di three.js e l'esempio indica che c'è praticamente la stessa cosa. Quindi sono bloccato su cosa fare.

UPDATE: Stavo usando una copia locale di Three.js quando ho incontrato l'ultimo errore. L'ho cambiato con il link http://www.html5canvastutorials.com/libraries/Three.js. Ora, l'errore PerspectiveCamera è sparito, ma produce un nuovo errore all'interno dello script Three.js. L'errore è Uncaught TypeError: Cannot read property 'x' of undefined sulla riga 337 dello script Three.js

Grazie.

+0

Ciao, si può provare a digitare questo 'nuova THREE.PerspectiveCamera (45, window.innerWidth/window.innerHeight, 1, 1000);' direttamente alla console e aggiornare il pubblicare con l'output? –

+0

@LimH. Dà lo stesso errore, "UncaughtTypeError: undefined non è una funzione". È perché il browser non supporta la fotocamera prospettica? – rahules

+0

Ho provato lo script in Chromium abilitando l'opzione "Sostituisci rendering software". L'ho provato in firefox e mostra - "TypeError: THREE.PerspectiveCamera non è un costruttore". – rahules

risposta

18

Il problema con la copia locale è che si è utilizzato il file Three.js non installato (che sarebbe src/Three.js). Quello che vuoi è build/three.js o build/three.min.js. L'ho copiato nel mio progetto e ho cambiato il riferimento ad esso nell'attributo src del tag script e tutto ha iniziato a funzionare.

In breve:

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