BufferGeometry.boundingBox的应用:BoxHelper的实现
通过boundingBox实现的BoxHelper
官方提供了一个BoxHelper
,查看源码,它是通过boundingBox
实现的。
BoxHelper
通过LineSegment
画出一个长方体外框,即需要通过BufferAttributes
指定要画的顶点属性(这里主要是position
以及index
),通过创建LineSegment
对象进行绘制,BoxHelper
继承了LineSegemnt
。
// BoxHelper.js
// class BoxHelper extends LineSegment
constructor( object, color = 0xffff00 ) {const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );const positions = new Float32Array( 8 * 3 );const geometry = new BufferGeometry();geometry.setIndex( new BufferAttribute( indices, 1 ) );geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) );super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );this.object = object;this.type = 'BoxHelper';this.matrixAutoUpdate = false;this.update();
}
在构造器中,并没有给出position
的据其位置,这一部分的实现在update
方法,就是,在update
中,间接的使用了boundingBox
。
下面看一下