forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.js
More file actions
61 lines (55 loc) · 2.33 KB
/
batch.js
File metadata and controls
61 lines (55 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { BoundingBox } from '../../shape/bounding-box.js';
/**
* @class
* @name Batch
* @classdesc Holds information about batched mesh instances. Created in {@link BatchManager#create}.
* @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.
* @property {MeshInstance[]} origMeshInstances An array of original mesh instances, from which this batch was generated.
* @property {MeshInstance} meshInstance A single combined mesh instance, the result of batching.
* @property {boolean} dynamic Whether this batch is dynamic (supports transforming mesh instances at runtime).
* @property {number} [batchGroupId] Link this batch to a specific batch group. This is done automatically with default batches.
*/
class Batch {
constructor(meshInstances, dynamic, batchGroupId) {
this.origMeshInstances = meshInstances;
this._aabb = new BoundingBox();
this.meshInstance = null;
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();
}
}
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 };