Skip to content

Group

Group意为,其目的是将不同的物体分为一组,成为一体

Group继承自Object3D,他几乎和Object3D是相同的,其目的是使得组中对象在语法上的结构更加清晰。

1.构造函数

js
const group = new THREE.Group()

2.属性

共有属性参照其基类Object3D

isGroup

类型:Boolean

只读,判断一个物体是否为Group

type

一个字符串,值为"Group",此属性不应当被改变

3.方法

共有方法参照其基类Object3D

4.实际使用

当我们想给一些几何体做统一的几何变换时

js
// 创建个立方体
const cubeGeometry = new THREE.BoxGeometry(5, 5, 5)
const cubeMaterial = new THREE.MeshLambertMaterial({})
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
cube.translateX(5)

// 创建个球体
const sphereGeometry = new THREE.SphereGeometry(2, 30, 30)
const sphereMaterial = new THREE.MeshLambertMaterial()
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)

// 创建个组
const group = new THREE.Group()
group.add(cube)
group.add(sphere)

// 把组添加到场景
scene.add(group) // 把物体添加到场景

// 省略部分代码

// 动画帧
const animation = () => {
    requestAnimationFrame(animation)

    group.rotation.y -= 0.01
    renderer.render(scene, camera)
}
animation()

PixPin_2024-10-15_10-13-49

TIP

上面的操作把Group换成Object3D对象也是可以的,Group只是更语义化

js
const group = new THREE.Group()

// 可以换成
const group = new THREE.Object3D()

MIT Licensed