【问题标题】:FBXLoader: I can't load more than one objectFBXLoader:我不能加载多个对象
【发布时间】:2019-01-20 14:19:25
【问题描述】:

我只是想效仿示例,让自己将自己的模型放入场景中。但是,在使用 FBXLoader 时,我不确定我所做的是否正确。我一次只能加载一个 FBX。首先我不是编码员,如果我的代码看起来很草率,请见谅:

我有一个名为 range_burners.fbx 的对象。它可以自行加载。我有一个名为 range_main.fbx 的模型,它本身就可以正常工作。但是他们两个在一起,那么 range_main.fbx 似乎总是先例。

<!DOCTYPE html>
<!-- saved from url=(0022)http://127.0.0.1:1024/ -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>three.js webgl - FBX loader</title>

        <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 {
                color: #046;
                font-weight: bold;
            }
        </style>
    </head>

    <body>
        <div id="info">
            <a href="http://threejs.org/" target="_blank" rel="noopener">three.js</a> - FBXLoader<br>
            Character and animation from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">Mixamo</a>
        </div>

        <script src="./index_files/three.js.download"></script>

        <script src="./index_files/inflate.min.js.download"></script>
        <script src="./index_files/FBXLoader.js.download"></script>

        <script src="./index_files/OrbitControls.js.download"></script>

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

        <script>

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

            var container, stats, controls;
            var camera, scene, renderer, light;

            var clock = new THREE.Clock();

            var mixers = [];

            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.set( 100, 200, 300 );

                controls = new THREE.OrbitControls( camera );
                controls.target.set( 0, 100, 0 );
                controls.update();

                scene = new THREE.Scene();
                scene.background = new THREE.Color( 0xa0a0a0 );
                scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );

                light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
                light.position.set( 0, 200, 0 );
                scene.add( light );

                light = new THREE.DirectionalLight( 0xffffff );
                light.position.set( 0, 200, 100 );
                light.castShadow = true;
                light.shadow.camera.top = 180;
                light.shadow.camera.bottom = -100;
                light.shadow.camera.left = -120;
                light.shadow.camera.right = 120;
                scene.add( light );

                // scene.add( new THREE.CameraHelper( light.shadow.camera ) );

                // ground
                var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
                mesh.rotation.x = - Math.PI / 2;
                mesh.receiveShadow = true;
                scene.add( mesh );

                var grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
                grid.material.opacity = 0.2;
                grid.material.transparent = true;
                scene.add( grid );

                var burners = new THREE.FBXLoader();
                burners.load( '/fbx/range_burners.fbx', function ( object ) {
                    object.traverse( function ( child ) {
                        if( child.material ) {
                            child.material = new THREE.MeshStandardMaterial ( {
                                color: 0x151515,
                                metalness: 0.8,
                                roughness: 0.6

                            } );
                        }
                    } )
                    object.position.set(0, 200, 0);
                    scene.add ( object );
                } );

                // model
                var range = new THREE.FBXLoader();
                range.load( '/fbx/range_main.fbx', function ( object2 ) {
                    object2.traverse( function ( child2 ) {
                        if( child2.material ) {
                            child2.material = new THREE.MeshStandardMaterial ( {
                                color: 0xffffff,
                                metalness: 0.8,
                                roughness: 0.4
                            } );
                        }
                    } )
                    object2.position.set(-150, 0, 0);
                    scene.add ( object2 );
                } );

                //burners




                renderer = new THREE.WebGLRenderer( { antialias: true } );
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                renderer.shadowMap.enabled = true;
                container.appendChild( renderer.domElement );

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

                // stats
                stats = new Stats();
                container.appendChild( stats.dom );

            }

            function onWindowResize() {

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

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

            }

            //

            function animate() {

                requestAnimationFrame( animate );

                if ( mixers.length > 0 ) {

                    for ( var i = 0; i < mixers.length; i ++ ) {

                        mixers[ i ].update( clock.getDelta() );

                    }

                }

                renderer.render( scene, camera );

                stats.update();

            }

        </script><div><canvas width="1920" height="898" style="width: 1920px; height: 898px;"></canvas><div style="position: fixed; top: 0px; left: 0px; cursor: pointer; opacity: 0.9; z-index: 10000;"><canvas width="80" height="48" style="width: 80px; height: 48px; display: block;"></canvas><canvas width="80" height="48" style="width: 80px; height: 48px; display: none;"></canvas><canvas width="80" height="48" style="width: 80px; height: 48px; display: none;"></canvas></div></div><div><canvas width="928" height="898" style="width: 928px; height: 898px;"></canvas><div style="position: fixed; top: 0px; left: 0px; cursor: pointer; opacity: 0.9; z-index: 10000;"><canvas width="80" height="48" style="width: 80px; height: 48px; display: block;"></canvas><canvas width="80" height="48" style="width: 80px; height: 48px; display: none;"></canvas><canvas width="80" height="48" style="width: 80px; height: 48px; display: none;"></canvas></div></div>




</body></html>

【问题讨论】:

    标签: three.js loader fbx


    【解决方案1】:

    您的代码看起来不错。对我来说,为什么一个会覆盖另一个并不明显,除非它可能隐藏在另一个后面? 可以尝试移动他们的初始位置吗?

    另外,您可能希望在启动加载程序之前进行 THREE.WebGLRenderer 初始化......即将加载程序放在初始化的末尾...... 另一种调试方法是在你的材质上设置“wireframe:true”,这样你就可以看到其中一个隐藏在另一个里面......

    【讨论】:

    • 感谢您的回答。我已经多次移动了初始位置,但似乎没有帮助。我肯定已经将它移出该区域,无论是上方还是下方,较大的单元(炉子的主要部分)。然而!我没有尝试进行 THREE.WEBGLRenderer 初始化,我还将检查线框,我会尽快回复您。再次感谢您的回答。
    • 所以我重新组织,设置线框等。加载r​​ange_main时,其他模型肯定不会加载。
    • 好的。 2 个选项.. 制作一个有效的小提琴,我们可以尝试调试它.. 或者进入我们的松弛频道,我们中的一些人在那里闲逛并尝试帮助人们,我们也许可以通过它与您交谈...@987654321 @
    • @ScottRinehart 我在不同的场景中使用 FBXLoader,可能会遇到与您相同的问题。我想和你一起检查一下你是否取得了进展?
    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 2018-12-22
    • 2013-05-08
    • 2023-04-01
    • 2016-09-09
    • 2013-11-09
    相关资源
    最近更新 更多