@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
88 lines (83 loc) • 2.62 kB
JavaScript
import { BoundingBox } from '../../core/shape/bounding-box.js';
/**
* Holds information about batched mesh instances. Created in {@link BatchManager#create}.
*
* @category Graphics
*/
class Batch {
/**
* Create a new Batch instance.
*
* @param {import('../mesh-instance.js').MeshInstance[]} meshInstances - The mesh instances to
* be batched.
* @param {boolean} dynamic - Whether this batch is dynamic (supports transforming mesh
* instances at runtime).
* @param {number} batchGroupId - Link this batch to a specific batch group. This is done
* automatically with default batches.
*/
constructor(meshInstances, dynamic, batchGroupId) {
/** @private */
this._aabb = new BoundingBox();
/**
* An array of original mesh instances, from which this batch was generated.
*
* @type {import('../mesh-instance.js').MeshInstance[]}
*/
this.origMeshInstances = void 0;
/**
* A single combined mesh instance, the result of batching.
*
* @type {import('../mesh-instance.js').MeshInstance}
*/
this.meshInstance = null;
/**
* Whether this batch is dynamic (supports transforming mesh instances at runtime).
*
* @type {boolean}
*/
this.dynamic = void 0;
/**
* Link this batch to a specific batch group. This is done automatically with default batches.
*
* @type {number}
*/
this.batchGroupId = void 0;
this.origMeshInstances = meshInstances;
this.dynamic = dynamic;
this.batchGroupId = batchGroupId;
}
// Removes the batch meshes from all layers and destroys it
destroy(scene, layers) {
if (this.meshInstance) {
this.removeFromLayers(scene, layers);
this.meshInstance.destroy();
this.meshInstance = null;
}
}
addToLayers(scene, layers) {
for (let i = 0; i < layers.length; i++) {
const layer = scene.layers.getLayerById(layers[i]);
if (layer) {
layer.addMeshInstances([this.meshInstance]);
}
}
}
removeFromLayers(scene, layers) {
for (let i = 0; i < layers.length; i++) {
const layer = scene.layers.getLayerById(layers[i]);
if (layer) {
layer.removeMeshInstances([this.meshInstance]);
}
}
}
// Updates bounding box for a batch
updateBoundingBox() {
this._aabb.copy(this.origMeshInstances[0].aabb);
for (let i = 1; i < this.origMeshInstances.length; i++) {
this._aabb.add(this.origMeshInstances[i].aabb);
}
this.meshInstance.aabb = this._aabb;
this.meshInstance._aabbVer = 0;
}
}
export { Batch };