将其添加到场景中然后定位相机以填充其视图后,ThreeJS网格不再出现

时间:2019-06-07 15:26:33

标签: three.js

在这段代码中,将.obj模型加载到场景中,然后将相机的z位置移动到网格边界框填充视图的点。

问题在于,除非手动摆弄轨道控件或注释掉camera.position.z行,否则不会出现网格。

是否有一种方法可以强制相机渲染或以其他方式强制网格显示?

function loadModel() {
    mesh.traverse( function ( child ) {
        if ( child.isMesh ) child.material.map = texture;
    } );

    var geometry = mesh.children[0].geometry;
    geometry.center();

    mesh.scale.copy(new THREE.Vector3(100,100,100));

    scene.add( mesh );

    var box = new THREE.Box3().setFromObject( mesh );
    var object3DWidth  = box.max.x - box.min.x;
    var object3DHeight = box.max.y - box.min.y;
    var object3DDepth  = box.max.z - box.min.z;

    // position camera to fill view according to bounding box size
    var size=Math.max(object3DHeight,object3DWidth);
    var dist= size / 2 / Math.tan(Math.PI * cameraFov / 360); 

    camera.position.z = dist; 
 //// if I remove the line above the mesh will appear but not zoomed ////
}

0 个答案:

没有答案