Capítulo 1 de 859
A special version of a mesh with multi draw batch rendering support. Use this class if you have to render a large number of objects with the same material but with different geometries or world transformations. The usage of BatchedMesh will help you to reduce the number of draw calls and thus improve the overall rendering performance in your application.
true, the individual objects of a batch are frustum culled.true, the individual objects of a batch are sorted to improve overdraw-related artifacts. If the material is marked as "trans…const box = new THREE.BoxGeometry( 1, 1, 1 );
const sphere = new THREE.SphereGeometry( 1, 12, 12 );
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
// initialize and add geometries into the batched mesh
const batchedMesh = new BatchedMesh( 10, 5000, 10000, material );
const boxGeometryId = batchedMesh.addGeometry( box );
const sphereGeometryId = batchedMesh.addGeometry( sphere );
// create instances of those geometries
const boxInstancedId1 = batchedMesh.addInstance( boxGeometryId );
const boxInstancedId2 = batchedMesh.addInstance( boxGeometryId );
const sphereInstancedId1 = batchedMesh.addInstance( sphereGeometryId );
const sphereInstancedId2 = batchedMesh.addInstance( sphereGeometryId );
// position the geometries
batchedMesh.setMatrixAt( boxInstancedId1, boxMatrix1 );
batchedMesh.setMatrixAt( boxInstancedId2, boxMatrix2 );
batchedMesh.setMatrixAt( sphereInstancedId1, sphereMatrix1 );
batchedMesh.setMatrixAt( sphereInstancedId2, sphereMatrix2 );
scene.add( batchedMesh );
Constructor
| Signature |
|---|
| `new BatchedMesh( maxInstanceCount : number, maxVertexCount : number, maxIndexCount : number, material : Material |
Properties
| Property | Description |
|---|---|
.boundingBox : Box3 | The bounding box of the batched mesh. Can be computed via BatchedMesh#computeBoundingBox. |
.boundingSphere : Sphere | The bounding sphere of the batched mesh. Can be computed via BatchedMesh#computeBoundingSphere. |
.customSort : function | Takes a sort a function that is run before render. The function takes a list of instances to sort and a camera. The objects in the list include a "z" field to perform a depth-ordered sort with. |
.instanceCount : number (readonly) | The instance count. |
.isBatchedMesh : boolean (readonly) | This flag can be used for type testing. |
.maxInstanceCount : number (readonly) | The maximum number of individual instances that can be stored in the batch. |
.perObjectFrustumCulled : boolean | When set ot true, the individual objects of a batch are frustum culled. |
.sortObjects : boolean | When set to true, the individual objects of a batch are sorted to improve overdraw-related artifacts. If the material is marked as "transparent" objects are rendered back to front and if not then t… |
.unusedIndexCount : number (readonly) | The number of unused indices. |
.unusedVertexCount : number (readonly) | The number of unused vertices. |
Methods
| Method | Description |
|---|---|
.addGeometry( geometry : BufferGeometry, reservedVertexCount : number, reservedIndexCount : number ) : number | Adds the given geometry to the batch and returns the associated geometry id referring to it to be used in other functions. |
.addInstance( geometryId : number ) : number | Adds a new instance to the batch using the geometry of the given ID and returns a new id referring to the new instance to be used by other functions. |
.computeBoundingBox() | Computes the bounding box, updating BatchedMesh#boundingBox. Bounding boxes aren't computed by default. They need to be explicitly computed, otherwise they are null. |
.computeBoundingSphere() | Computes the bounding sphere, updating BatchedMesh#boundingSphere. Bounding spheres aren't computed by default. They need to be explicitly computed, otherwise they … |
.deleteGeometry( geometryId : number ) : BatchedMesh | Deletes the geometry defined by the given ID from this batch. Any instances referencing this geometry will also be removed as a side effect. |
.deleteInstance( instanceId : number ) : BatchedMesh | Deletes an existing instance from the batch using the given ID. |
.dispose() | Frees the GPU-related resources allocated by this instance. Call this method whenever this instance is no longer used in your app. |
.getBoundingBoxAt( geometryId : number, target : Box3 ) : Box3 | Returns the bounding box for the given geometry. |
.getBoundingSphereAt( geometryId : number, target : Sphere ) : Sphere | Returns the bounding sphere for the given geometry. |
| `.getColorAt( instanceId : number, color : Color | Vector4 ) : Color |
.getGeometryIdAt( instanceId : number ) : number | Returns the geometry ID of the defined instance. |
.getGeometryRangeAt( geometryId : number, target : Object ) : Object | Get the range representing the subset of triangles related to the attached geometry, indicating the starting offset and count, or null if invalid. |
.getMatrixAt( instanceId : number, matrix : Matrix4 ) : Matrix4 | Returns the local transformation matrix of the defined instance. |
.getVisibleAt( instanceId : number ) : boolean | Returns the visibility state of the defined instance. |
.optimize() : BatchedMesh | Repacks the sub geometries in BatchedMesh to remove any unused space remaining from previously deleted geometry, freeing up space to add new geometry. |
| `.setColorAt( instanceId : number, color : Color | Vector4 ) : BatchedMesh` |
.setCustomSort( func : function ) : BatchedMesh | Takes a sort a function that is run before render. The function takes a list of instances to sort and a camera. The objects in the list include a "z" field to perform a depth-ordered sort with. |
.setGeometryAt( geometryId : number, geometry : BufferGeometry ) : number | Replaces the geometry at the given ID with the provided geometry. Throws an error if there is not enough space reserved for geometry. Calling this will change all instances that are rendering that ge… |
.setGeometryIdAt( instanceId : number, geometryId : number ) : BatchedMesh | Sets the geometry ID of the instance at the given index. |
.setGeometrySize( maxVertexCount : number, maxIndexCount : number ) | Resizes the available space in the batch's vertex and index buffer attributes to the provided sizes. If the provided arguments shrink the geometry buffers but there is not enough unused space at the … |
.setInstanceCount( maxInstanceCount : number ) | Resizes the necessary buffers to support the provided number of instances. If the provided arguments shrink the number of instances but there are not enough unused Ids at the end of the list then an … |
.setMatrixAt( instanceId : number, matrix : Matrix4 ) : BatchedMesh | Sets the given local transformation matrix to the defined instance. Negatively scaled matrices are not supported. |
.setVisibleAt( instanceId : number, visible : boolean ) : BatchedMesh | Sets the visibility of the instance. |
.validateGeometryId( geometryId : number ) | Validates the geometry defined by the given ID. |
.validateInstanceId( instanceId : number ) | Validates the instance defined by the given ID. |