使用three.js渲染obj文件,什么都没有显示?

时间:2013-05-29 15:23:23

标签: 3d rendering three.js

我正在尝试使用three.js渲染一个.obj,我看了几个例子并想出了怎么做但我的html代码似乎不起作用!我只看到一个黑屏。我附上了下面的代码,我做错了什么?我是一个完全的菜鸟,所以任何帮助将不胜感激。谢谢!

<body>
    <div id="info">
    <a href="http://threejs.org" target="_blank">three.js</a> - OBJLoader test
    </div>

    <script src="../build/three.min.js"></script>
    <script src="js/loaders/OBJLoader.js"></script>

    <script src="js/Detector.js"></script>
    <script src="js/libs/stats.min.js"></script>

    <script>

        var container, stats;

        var camera, scene, renderer;

        var mouseX = 0, mouseY = 0;

        var windowHalfX = window.innerWidth / 2;
        var windowHalfY = window.innerHeight / 2;


        init();
        animate();


        function init() {

            container = document.createElement( 'div' );
            document.body.appendChild( container );

            camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
            camera.position.z = 100;

            // scene

            scene = new THREE.Scene();

            var ambient = new THREE.AmbientLight( 0x101030 );
            scene.add( ambient );

            var directionalLight = new THREE.DirectionalLight( 0xffeedd );
            directionalLight.position.set( 0, 0, 1 ).normalize();
            scene.add( directionalLight );


            // model

            var loader = new THREE.OBJLoader();
            loader.load( "/obj/smiley.obj", function ( object ) {
            scene.add( object );
            } );

            //

            renderer = new THREE.WebGLRenderer();
            renderer.setSize( window.innerWidth, window.innerHeight );
            container.appendChild( renderer.domElement );

            document.addEventListener( 'mousemove', onDocumentMouseMove, false );

            //

            window.addEventListener( 'resize', onWindowResize, false );

        }

        function onWindowResize() {

            windowHalfX = window.innerWidth / 2;
            windowHalfY = window.innerHeight / 2;

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize( window.innerWidth, window.innerHeight );

        }

        function onDocumentMouseMove( event ) {

            mouseX = ( event.clientX - windowHalfX ) / 2;
            mouseY = ( event.clientY - windowHalfY ) / 2;

        }

        //

        function animate() {

            requestAnimationFrame( animate );
            render();

        }

        function render() {

            camera.position.x += ( mouseX - camera.position.x ) * .05;
            camera.position.y += ( - mouseY - camera.position.y ) * .05;

            camera.lookAt( scene.position );

            renderer.render( scene, camera );

        }

    </script>

</body>

2 个答案:

答案 0 :(得分:1)

您需要使用EventListener:

var loader = new THREE.OBJLoader();
loader.addEventListener( 'load', function ( event ) {

    scene.add( event.content );

});
loader.load( "/obj/smiley.obj' );

答案 1 :(得分:0)

.obj文件是否附带.mtl文件? 它们必须放在同一个文件夹中,否则OBJLoader将不会加载任何内容。