After converting my FBX file to a .gltf, the model is incredibly small, why?

折月煮酒 提交于 2019-12-13 16:08:19

问题


QUESTION:

After converting my FBX file to a .gltf, the model is incredibly small, why ?

I tried scaling the model with frontObject.scale.set( 1000, 1000, 1000 ); but I get the following error:

TypeError: Cannot read property 'set' of undefined

Furthermore, in

function ( xhr ) {
                    progressBar.style.display = "block";
                    progressBar.style.width = ( xhr.loaded / xhr.total * 100 ) + '%';
                    loadingText.innerHTML = 'Loading... '+xhr.loaded+"/"+xhr.total;
                    // For some reason, xhr.total = 0, so I can't use it
                    setTimeout(function () {
                        frontObject.scale.set( 1000, 1000, 1000 );
                    }, 3000);
                },

xhr.total is always equal to 0 and xhr.loaded is equal to an absurdly large number.

All I did was convert my file from .fbx to .gltf and change the size of the textures from 2048x2048 to 1024x1024.

Here is a screenshot of what I am now seeing:

Before, the model would take the whole vertical height.


CODE:

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

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

var clock = new THREE.Clock();

var frontObject;

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 );
    camera.lookAt(new THREE.Vector3(0,0,0));

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

    light = new THREE.DirectionalLight( 0xffffff );
    light.position.set( 0, 0, -50 );
    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 );

    load();

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

}

    function load() {

        // Instantiate a loader
        var loader = new THREE.GLTFLoader();

        // Optional: Provide a DRACOLoader instance to decode compressed mesh data
        THREE.DRACOLoader.setDecoderPath( '../path/' );
        THREE.DRACOLoader.setDecoderConfig( { type: 'js' } );
        loader.setDRACOLoader( new THREE.DRACOLoader() );

        // Load a glTF resource
        loader.load(
            // resource URL
            '../path/file.gltf',
            // called when the resource is loaded
            function ( gltf ) {

                scene.add( gltf.scene );

                gltf.animations; // Array<THREE.AnimationClip>
                gltf.scene; // THREE.Scene
                gltf.scenes; // Array<THREE.Scene>
                gltf.cameras; // Array<THREE.Camera>
                gltf.asset; // Object
                frontObject = gltf.asset;

            },
            // called while loading is progressing
            function ( xhr ) {
                progressBar.style.display = "block";
                progressBar.style.width = ( xhr.loaded / xhr.total * 100 ) + '%';
                loadingText.innerHTML = 'Loading... '+xhr.loaded+"/"+xhr.total;
                // For some reason, xhr.total = 0, so I can't use it
                setTimeout(function () {
                    frontObject.scale.set( 1000, 1000, 1000 );
                }, 3000);
            },
            // called when loading has errors
            function ( error ) {

                console.log( 'An error happened: '+error );

            }
        );
    }


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

}

FIDDLE:

https://jsfiddle.net/Username100/y54kpe1h/64 I was able to load the model at the correct size ! But it seems that the default loading manager was incorrectly indicating that everything had loaded, how could that be ?


回答1:


Sometimes in GLTF files the final size of the object is dictated by parent nodes that apply transforms.

In your code, i'm not sure that

  gltf.asset; // Object
  frontObject = gltf.asset;

is really your object.

The real object is a child of the gltf.scene and can be found using gltf.scene.traverse or .getObjectByName(

I fixed up your raycasting thing:

https://jsfiddle.net/manthrax/8evurmyx/44/



来源:https://stackoverflow.com/questions/51785250/after-converting-my-fbx-file-to-a-gltf-the-model-is-incredibly-small-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!