Any way to get a bounding box from a three.js Object3D?

前端 未结 4 1123
情话喂你
情话喂你 2020-11-28 06:42

I\'m loading an OBJ file using Three.js and OBJLoader.js. This returns a Three.Object3D object, which has what you\'d expect from a 3D model (position vector, up vector...)

4条回答
  •  伪装坚强ぢ
    2020-11-28 07:36

    For any shape, on its geometry object, there is a boundingBox property. This property holds a THREE.Box3 object. This Box3 object consists of two THREE.Vector3 objects, min and max.

    var geometry = new THREE.CylinderGeometry(...);
    var material = new THREE.LineBasicMaterial(...);
    var mesh = new THREE.Mesh(geometry, material);
    
    var boundingBox = mesh.geometry.boundingBox.clone();
    alert('bounding box coordinates: ' + 
        '(' + boundingBox.min.x + ', ' + boundingBox.min.y + ', ' + boundingBox.min.z + '), ' + 
        '(' + boundingBox.max.x + ', ' + boundingBox.max.y + ', ' + boundingBox.max.z + ')' );
    

    For more complex shapes, like those loaded from JSON Object files, the bounding box property is undefined by default. It must be computed explicitly.

    var loader = new THREE.ObjectLoader();
    loader.load(imagePath, function(object){
    
        geometry = object.children[0].children[0].geometry;  // substitute the path to your geometry
    
        geometry.computeBoundingBox();  // otherwise geometry.boundingBox will be undefined
    
        var boundingBox = geometry.boundingBox.clone();
        alert('bounding box coordinates: ' + 
            '(' + boundingBox.min.x + ', ' + boundingBox.min.y + ', ' + boundingBox.min.z + '), ' + 
            '(' + boundingBox.max.x + ', ' + boundingBox.max.y + ', ' + boundingBox.max.z + ')' );
    }
    

提交回复
热议问题