javascript – three.js:无法使用THREE.OBJLoader显示obj文件

我在使用示例代码加载obj文件时遇到问题.加载示例文件male02.obj时没有问题,但是当我插入文件时,不显示对象.使用Python转换器脚本和JSONLoader时遇到了同样的问题.

这是OBJLoader的所有代码

<!doctype html>
<html lang="en">
    <head>
        <title>three.js webgl - loaders - OBJ loader</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <style>
            body {
                font-family: Monospace;
                background-color: #000;
                color: #fff;
                margin: 0px;
                overflow: hidden;
            }
            #info {
                color: #fff;
                position: absolute;
                top: 10px;
                width: 100%;
                text-align: center;
                z-index: 100;
                display:block;
            }
            #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
        </style>
    </head>

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

        <script src="javascripts/Three.js"></script>
        <script src="javascripts/OBJLoader.js"></script>

        <script src="javascripts/Detector.js"></script>
        <script src="javascripts/Stats.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 );

                scene = new THREE.Scene();

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

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

                var loader = new THREE.OBJLoader();
        //loader.load( "img/male02.obj", function ( object ) {
        loader.load( "img/originalMeanModel.obj", function ( object ) {
                    object.position.y = - 80;
                    scene.add( object );
                } );

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

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

            }

            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>
</html>

我使用的是最新版本的three.js(49).

这是我试图加载https://dl.dropbox.com/u/23384412/originalMeanModel.obj的obj文件的链接

我注意到当我尝试JSONLoader时导出的JSON文件没有任何法线,颜色或uv.这在Blender或MeshLab中查看文件时不会影响,但是会对three.js产生影响吗?

如果有人可以帮助我,将不胜感激.

提前致谢.

解决方法:

必须根据模型的位置和尺寸设置对象和摄像机位置.

“评论”所有这些行:

camera.position.z = 100;
...
object.position.y = - 80;
...
document.addEventListener( 'mousemove', onDocumentMouseMove, false );

并开始播放代码,更改相机位置:

camera.position.z = 2;
上一篇:javascript – 如何在不模糊的情况下拉伸WebGL画布?风格“图像渲染”不起作用


下一篇:javascript – 同源图像texImage2D的SecurityError