问题描述:

I am working on a three.js parser which can parse obj and JSON file.

The problem is different objects different alignments. I need to hard code value of camera.position.z. but this not dynamic.

How can I calculate this value dynamically? I already tried solutions from @WestLangley but it's not working. Maybe I am doing it wrong.

here is my code :

camera = new THREE.PerspectiveCamera( 45, width/ height, 1, 10000 );

camera.position.z = 200;

// scene

scene = new THREE.Scene();

scene.add( camera );

THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );

var mtlLoader = new THREE.MTLLoader();

mtlLoader.setPath( object_path.dir );

mtlLoader.load( object_path.mtl, function( materials ) {

materials.preload();

var objLoader = new THREE.OBJLoader();

objLoader.setMaterials( materials );

objLoader.setPath( object_path.dir);

objLoader.load( object_path.object, function ( object ) {

// object.position.y = - 95;

camera.lookAt(object.position);

scene.add( object );

}, onProgress, onError );

});

renderer = new THREE.WebGLRenderer({preserveDrawingBuffer: true });

renderer.setPixelRatio( window.devicePixelRatio );

renderer.setSize( width, height);

container.appendChild( renderer.domElement );

controls = new THREE.OrbitControls( camera, renderer.domElement );

网友答案:

You need to call a render()-Function. And in this function you can calculate the camera.z dynamically depending on your current state. Like:

      var render = function () {
            requestAnimationFrame( render );

            camera.position.z =  ...
            renderer.render(scene, camera);
        };

      render();
相关阅读:
Top