# Three.js 包围盒 Box3

## 包围矩形 Box2

``````// 参数1对应min属性值，参数2对应max属性值
var box2 = new THREE.Box2(new THREE.Vector2(0, 0), new THREE.Vector2(25, 25))
console.log('box2',box2);
box2.min=new THREE.Vector2(0, 0);
box2.max=new THREE.Vector2(25, 25);
``````

``````box2.min=new THREE.Vector2(0, 0);
box2.max=new THREE.Vector2(25, 25);
``````

## 包围盒 Box3

``````var box3 = new THREE.Box3()
console.log('box3',box3);
box3.min = new THREE.Vector3(-10, -10,0);
box3.max = new THREE.Vector3(100, 20,50);
``````

## 包围球 Sphere

``````// 创建一个包围球对象，球心默认坐标原点，半径默认0.
var sphere = new THREE.Sphere()
console.log('sphere', sphere);
// 设置球心坐标
sphere.center=new THREE.Vector3(-10, -10,0);
// 设置包围球半径
``````

## Box3 方法 .setFromPoints()

``````// 通过球体API创建一个几何体，本质上就是一系列沿着球面分布的顶点
var geometry = new THREE.SphereGeometry(50, 100, 100);
// 创建一个包围盒对象Box3
var box3 = new THREE.Box3()
// 计算点集geometry.vertices的包围盒
box3.setFromPoints(geometry.vertices);
console.log('box3', box3);
``````

## 几何体方法 .computeBoundingBox()

``````var geometry = new THREE.SphereGeometry(50, 100, 100); //球体
// .computeBoundingBox()方法计算.boundingBox的属性值
geometry.computeBoundingBox();
console.log('包围盒属性', geometry.boundingBox);
// 包围球相关属性和计算方法和包围盒一样
geometry.computeBoundingSphere();
console.log('包围球属性', geometry.boundingSphere);
``````

### 几何体居中方法center()

``````// 几何体的中心默认与坐标原点重合
var geometry = new THREE.BoxGeometry(50, 50, 50);
// 几何体沿着x轴平移50，几何体的顶点坐标变化
geometry.translate(50, 0, 0);
// 居中：偏移的几何体居中
geometry.center();
``````

## Box3 方法 .expandByObject()

``````var loader = new THREE.ObjectLoader();
var box3 = new THREE.Box3()
// 计算层级模型group包围盒
box3.expandByObject(group)
console.log('查看包围盒box3', box3);
})
``````

## Box3 方法 .expandByScalar()

``````// 缩放包围盒，尺寸放大1.5倍
box3.expandByScalar(1.5)
``````

## Box3 方法 .getSize()

``````var v3 = new THREE.Vector3()
// 获得包围盒长宽高尺寸，结果保存在参数三维向量对象v3中
box3.getSize(v3)
console.log('查看返回的包围盒尺寸', v3);
``````

## Box3 方法 .getCenter()

``````// 计算一个层级模型对应包围盒的几何体中心
var center = new THREE.Vector3()
box3.getCenter(center)
console.log('查看几何体中心坐标', center);
``````

## Sphere 方法 .getBoundingSphere()

``````var sphere = new THREE.Sphere()
// 计算包围盒box3对应的包围球
box3.getBoundingSphere(sphere)
console.log('查看通过box3重置的sphere', sphere);
``````

11 文章
2 评论
18744 人气