UNPKG

1.8 kBJavaScriptView Raw
1import { ExtensionType, extensions } from '@pixi/extensions';
2import { ObjectRenderer } from './ObjectRenderer.mjs';
3
4class BatchSystem {
5 constructor(renderer) {
6 this.renderer = renderer;
7 this.emptyRenderer = new ObjectRenderer(renderer);
8 this.currentRenderer = this.emptyRenderer;
9 }
10 setObjectRenderer(objectRenderer) {
11 if (this.currentRenderer === objectRenderer) {
12 return;
13 }
14 this.currentRenderer.stop();
15 this.currentRenderer = objectRenderer;
16 this.currentRenderer.start();
17 }
18 flush() {
19 this.setObjectRenderer(this.emptyRenderer);
20 }
21 reset() {
22 this.setObjectRenderer(this.emptyRenderer);
23 }
24 copyBoundTextures(arr, maxTextures) {
25 const { boundTextures } = this.renderer.texture;
26 for (let i = maxTextures - 1; i >= 0; --i) {
27 arr[i] = boundTextures[i] || null;
28 if (arr[i]) {
29 arr[i]._batchLocation = i;
30 }
31 }
32 }
33 boundArray(texArray, boundTextures, batchId, maxTextures) {
34 const { elements, ids, count } = texArray;
35 let j = 0;
36 for (let i = 0; i < count; i++) {
37 const tex = elements[i];
38 const loc = tex._batchLocation;
39 if (loc >= 0 && loc < maxTextures && boundTextures[loc] === tex) {
40 ids[i] = loc;
41 continue;
42 }
43 while (j < maxTextures) {
44 const bound = boundTextures[j];
45 if (bound && bound._batchEnabled === batchId && bound._batchLocation === j) {
46 j++;
47 continue;
48 }
49 ids[i] = j;
50 tex._batchLocation = j;
51 boundTextures[j] = tex;
52 break;
53 }
54 }
55 }
56 destroy() {
57 this.renderer = null;
58 }
59}
60BatchSystem.extension = {
61 type: ExtensionType.RendererSystem,
62 name: "batch"
63};
64extensions.add(BatchSystem);
65
66export { BatchSystem };
67//# sourceMappingURL=BatchSystem.mjs.map