import { Util } from "../util" ; import { Mesh } from "./mesh" ; import { Object3D } from "./object_3d" ; /** A group represents a collection of 3D objects. */ export class Group extends Object3D { children : Object3D [ ] = [ ] ; add ( child ) { if ( child . parent ) child . parent . remove ( child ) ; this . children . push ( child ) ; child . parent = this ; } remove ( child ) { Util . removeFromArray ( this . children , child ) ; child . parent = null ; } updateWorldTransform ( ) { if ( ! this . needsWorldTransformUpdate ) return ; super . updateWorldTransform ( ) ; for ( let child of this . children ) { if ( child . needsWorldTransformUpdate ) child . updateWorldTransform ( ) ; } } changedTransform ( ) { super . changedTransform ( ) ; for ( let child of this . children ) child . changedTransform ( ) ; } /** Traverses this group and all its descendants and calls the callback on each non-group. */ traverse ( fn ) { fn ( this ) ; for ( let child of this . children ) { if ( child instanceof Group ) child . traverse ( fn ) ; else fn ( child ) ; } } /** Recursively sets the opacity of all objects in this group's subtree. */ setOpacity ( value ) { for ( let child of this . children ) { if ( child instanceof Group ) child . setOpacity ( value ) ; else ( child as Mesh ) . opacity = value ; } } }