UNPKG

2.85 kBJavaScriptView Raw
1import { ExtensionType, extensions } from "@pixi/extensions";
2import { ObjectRenderer } from "./ObjectRenderer.mjs";
3class BatchSystem {
4 /**
5 * @param renderer - The renderer this System works for.
6 */
7 constructor(renderer) {
8 this.renderer = renderer, this.emptyRenderer = new ObjectRenderer(renderer), this.currentRenderer = this.emptyRenderer;
9 }
10 /**
11 * Changes the current renderer to the one given in parameter
12 * @param objectRenderer - The object renderer to use.
13 */
14 setObjectRenderer(objectRenderer) {
15 this.currentRenderer !== objectRenderer && (this.currentRenderer.stop(), this.currentRenderer = objectRenderer, this.currentRenderer.start());
16 }
17 /**
18 * This should be called if you wish to do some custom rendering
19 * It will basically render anything that may be batched up such as sprites
20 */
21 flush() {
22 this.setObjectRenderer(this.emptyRenderer);
23 }
24 /** Reset the system to an empty renderer */
25 reset() {
26 this.setObjectRenderer(this.emptyRenderer);
27 }
28 /**
29 * Handy function for batch renderers: copies bound textures in first maxTextures locations to array
30 * sets actual _batchLocation for them
31 * @param arr - arr copy destination
32 * @param maxTextures - number of copied elements
33 */
34 copyBoundTextures(arr, maxTextures) {
35 const { boundTextures } = this.renderer.texture;
36 for (let i = maxTextures - 1; i >= 0; --i)
37 arr[i] = boundTextures[i] || null, arr[i] && (arr[i]._batchLocation = i);
38 }
39 /**
40 * Assigns batch locations to textures in array based on boundTextures state.
41 * All textures in texArray should have `_batchEnabled = _batchId`,
42 * and their count should be less than `maxTextures`.
43 * @param texArray - textures to bound
44 * @param boundTextures - current state of bound textures
45 * @param batchId - marker for _batchEnabled param of textures in texArray
46 * @param maxTextures - number of texture locations to manipulate
47 */
48 boundArray(texArray, boundTextures, batchId, maxTextures) {
49 const { elements, ids, count } = texArray;
50 let j = 0;
51 for (let i = 0; i < count; i++) {
52 const tex = elements[i], loc = tex._batchLocation;
53 if (loc >= 0 && loc < maxTextures && boundTextures[loc] === tex) {
54 ids[i] = loc;
55 continue;
56 }
57 for (; j < maxTextures; ) {
58 const bound = boundTextures[j];
59 if (bound && bound._batchEnabled === batchId && bound._batchLocation === j) {
60 j++;
61 continue;
62 }
63 ids[i] = j, tex._batchLocation = j, boundTextures[j] = tex;
64 break;
65 }
66 }
67 }
68 /**
69 * @ignore
70 */
71 destroy() {
72 this.renderer = null;
73 }
74}
75BatchSystem.extension = {
76 type: ExtensionType.RendererSystem,
77 name: "batch"
78};
79extensions.add(BatchSystem);
80export {
81 BatchSystem
82};
83//# sourceMappingURL=BatchSystem.mjs.map