在three.js中使用二进制加载器加载对象不起作用

时间:2015-03-18 11:01:34

标签: javascript json three.js

我正在尝试修改three.js示例的代码以加载在.js中转换的.obj。这使用BinaryLoader.js加载对象。该对象未在我修改的代码中显示。到目前为止我的代码如下

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>home</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 {
                background:#000;
                color:#fff;
                padding:0;
                margin:0;
                overflow:hidden;
                font-family:georgia;
                text-align:center;
            }

        </style>
    </head>

    <body>

        <script src="js/three.min.js"></script>

        <script src="js/loaders/BinaryLoader.js"></script>

        <script src="js/Detector.js"></script>

        <script>

            if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

            var CARS = {

                "Porsche":  {

                    name:   "Bugatti Veyron",
                    url:    "obj/car/Porsche_911_GT2.js",
                //  author: '<a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1129" target="_blank">Troyano</a>',
                    init_rotation: [ 0, 0, 0 ],
                    scale: 5.5,
                    //init_material: 4,
                    //body_materials: [ 2 ],

                    object: null,
                    buttons: null,
                    materials: null

                }


            };

            var container, stats;

            var camera, scene, renderer;

            init();
            animate();

            function init() {

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


            var loader = new THREE.BinaryLoader( true );
            document.body.appendChild( loader.statusDomElement );
                // CAMERAS

                camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 100000 );

                // SCENE

                scene = new THREE.Scene();

                    // LIGHTS

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

                renderer = new THREE.WebGLRenderer();
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                renderer.setFaceCulling( THREE.CullFaceNone );
                renderer.autoClear = false;

                container.appendChild( renderer.domElement );

                loader.load( CARS[ "Porsche" ].url, function( geometry ) { createScene( geometry, "Porsche" ) } );

            }

            function animate() {

            requestAnimationFrame( animate );

            render();

            }

            function render() {

                var timer = -0.0002 * Date.now();

                camera.position.x = 1000 * Math.cos( timer );
                camera.position.y += ( - mouseY - camera.position.y ) * .05;
                camera.position.z = 1000 * Math.sin( timer );

                camera.lookAt( scene.position );
                    renderer.render( scene, camera );


            }



    </script>
    </body>
</html>

有人知道这个问题吗?

1 个答案:

答案 0 :(得分:1)

你有一个JSON模型所以你需要一个JSONLoader而不是BinaryLoader,另一个问题是你的模型很小,所以你必须将它缩放200才能从你初始化的相机中查看(fov = 70) 。我将使用以下代码:

var loader = new THREE.JSONLoader(true);

loader.load(CARS[ "Porsche" ].url, function (geometry)
{
    /*createScene( geometry, "Porsche" )*/
    var material = new THREE.MeshBasicMaterial({color: 0xff0000});
    var mesh = new THREE.Mesh(geometry, material);
    mesh.scale.set(200,200,200);
    scene.add(mesh);
});

更新:完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <title>home</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 {
            background: #000;
            color: #fff;
            padding: 0;
            margin: 0;
            overflow: hidden;
            font-family: georgia;
            text-align: center;
        }

    </style>
</head>

<body>

<script src="js/three.min.js"></script>

<script src="js/loaders/BinaryLoader.js"></script>

<script src="js/Detector.js"></script>

<script>

    if (!Detector.webgl) {
        Detector.addGetWebGLMessage();
    }
    var mouseY = 0;
    var CARS = {

        "Porsche": {

            name: "Bugatti Veyron",
            url: "obj/car/Porsche_911_GT2.js",
            //  author: '<a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1129" target="_blank">Troyano</a>',
            init_rotation: [ 0, 0, 0 ],
            scale: 5.5,
            //init_material: 4,
            //body_materials: [ 2 ],

            object: null,
            buttons: null,
            materials: null

        }


    };

    var container, stats;

    var camera, scene, renderer;

    init();
    animate();

    function init()
    {

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

        var loader = new THREE.JSONLoader(true);
        document.body.appendChild(loader.statusDomElement);
        // CAMERAS

        camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 100000);

        // SCENE

        scene = new THREE.Scene();

        // LIGHTS

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

        renderer = new THREE.WebGLRenderer();
        renderer.setPixelRatio(window.devicePixelRatio);
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.setFaceCulling(THREE.CullFaceNone);
        renderer.autoClear = false;

        loader.load(CARS[ "Porsche" ].url, function (geometry)
        {
            /*createScene( geometry, "Porsche" )*/
            var material = new THREE.MeshBasicMaterial({color: 0xff0000});
            var mesh = new THREE.Mesh(geometry, material);
            mesh.scale.set(200,200,200);
            scene.add(mesh);
        });

        container.appendChild(renderer.domElement);

    }

    function animate()
    {

        requestAnimationFrame(animate);

        render();

    }

    function render()
    {

        var timer = -0.0002 * Date.now();

        camera.position.x = 1000 * Math.cos(timer);
        camera.position.y += ( -mouseY - camera.position.y ) * .05;
        camera.position.z = 1000 * Math.sin(timer);

        camera.lookAt(scene.position);
        renderer.render(scene, camera);

    }


</script>
</body>
</html>