-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
batch.js
94 lines (83 loc) · 2.66 KB
/
batch.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { BoundingBox } from '../../core/shape/bounding-box.js';
/**
* Holds information about batched mesh instances. Created in {@link BatchManager#create}.
*
* @category Graphics
*/
class Batch {
/** @private */
_aabb = new BoundingBox();
/**
* An array of original mesh instances, from which this batch was generated.
*
* @type {import('../mesh-instance.js').MeshInstance[]}
*/
origMeshInstances;
/**
* A single combined mesh instance, the result of batching.
*
* @type {import('../mesh-instance.js').MeshInstance}
*/
meshInstance = null;
/**
* Whether this batch is dynamic (supports transforming mesh instances at runtime).
*
* @type {boolean}
*/
dynamic;
/**
* Link this batch to a specific batch group. This is done automatically with default batches.
*
* @type {number}
*/
batchGroupId;
/**
* 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) {
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 };