UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

98 lines (97 loc) 3.12 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { Debug } from "../../core/debug.js"; import { BoundingBox } from "../../core/shape/bounding-box.js"; class Batch { /** * Create a new Batch instance. * * @param {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 */ __publicField(this, "_aabb", new BoundingBox()); /** * An array of original mesh instances, from which this batch was generated. * * @type {MeshInstance[]} */ __publicField(this, "origMeshInstances"); /** * A single combined mesh instance, the result of batching. * * @type {MeshInstance} */ __publicField(this, "meshInstance", null); /** * Whether this batch is dynamic (supports transforming mesh instances at runtime). * * @type {boolean} */ __publicField(this, "dynamic"); /** * Link this batch to a specific batch group. This is done automatically with default batches. * * @type {number} */ __publicField(this, "batchGroupId"); this.origMeshInstances = meshInstances; this.dynamic = dynamic; this.batchGroupId = batchGroupId; } /** * Removes the batch from the layers and destroys it. * * @param {Scene} scene - The scene. * @param {number[]} layers - The layers to remove the batch from. */ 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; } /** * @type {undefined} * @deprecated * @ignore */ get model() { Debug.removed("pc.Batch#model was removed. Use pc.Batch#meshInstance to access batched mesh instead."); return void 0; } } export { Batch };