带有require的Three.js错误

时间:2015-11-09 00:14:36

标签: javascript three.js requirejs

这是我第一次尝试使用Three.js和Require js。我正在尝试使用OBJMTLoader加载Obj和Mtl文件。我使用当前设置在控制台中出现超时错误:

require({
    baseUrl: 'js',
    // three.js should have UMD support soon, but it currently does not
    shim: { 
      'vendor/three': { exports: 'THREE' }, 
      'vendor/MTLLoader': { exports: 'MTLLoader'},
      'vendor/OBJMTLLoader': { deps: ['MTLLoader'], exports: 'OBJMTLLoader'}

       }
}, [
    'vendor/three',
    'vendor/MTLLoader',
    'vendor/OBJMTLLoader'

], function(THREE, OBJMTLLoader) {



var scene, camera, renderer;
var geometry, material, mesh;

init();
animate();


function init() {

    scene = new THREE.Scene();
    var WIDTH = window.innerWidth,
        HEIGHT = window.innerHeight;


    camera = new THREE.PerspectiveCamera(75, WIDTH / HEIGHT, 1, 20000);
    camera.position.set(0,6,1000);
    scene.add(camera);


     window.addEventListener('resize', function() {
      var WIDTH = window.innerWidth,
          HEIGHT = window.innerHeight;
      renderer.setSize(WIDTH, HEIGHT);
      camera.aspect = WIDTH / HEIGHT;
      camera.updateProjectionMatrix();
    });

     var light = new THREE.PointLight(0xffffff);
     light.position.set(-100,200,100);
     scene.add(light);

     var onProgress = function ( xhr ) {
          if ( xhr.lengthComputable ) {
            var percentComplete = xhr.loaded / xhr.total * 100;
            console.log( Math.round(percentComplete, 2) + '% downloaded' );
          }
        };

        // var onError = function ( xhr ) {
        // };


        loader = new THREE.OBJMTLLoader();
        loader.load( '/obj/Shoes_Air_Jordans_4.obj', '/obj/Shoes_Air_Jordans_4.mtl', function ( object ) {

          object.position.y = - 80;
          scene.add( object );

        }, onProgress );

    geometry = new THREE.BoxGeometry( 200, 200, 200 );
    material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );

    mesh = new THREE.Mesh( geometry, material );
    scene.add( mesh );

    renderer = new THREE.WebGLRenderer({alpha:true});
    renderer.setSize( WIDTH, HEIGHT );
    renderer.setClearColor(0x333F47, 1);

    document.body.appendChild( renderer.domElement );

}

function animate() {

    requestAnimationFrame( animate );

    mesh.rotation.x += 0.01;
    mesh.rotation.y += 0.02;

    renderer.render( scene, camera );

}

});

我将这个生成器用于我的三个js实验https://github.com/timmywil/generator-threejs

1 个答案:

答案 0 :(得分:0)

尝试将.obj文件重命名为.txt 尝试将文件加载为.txt

' /obj/Shoes_Air_Jordans_4.txt'

编辑:

您也应该在本地打开它。 Threejs obj加载器无法加载本地文件。将软件用作hfs或在域上加载html文件