UNPKG

2.46 kBJavaScriptView Raw
1import { TARGETS } from "@pixi/constants";
2import { AbstractMultiResource } from "./AbstractMultiResource.mjs";
3class ArrayResource extends AbstractMultiResource {
4 /**
5 * @param source - Number of items in array or the collection
6 * of image URLs to use. Can also be resources, image elements, canvas, etc.
7 * @param options - Options to apply to {@link PIXI.autoDetectResource}
8 * @param {number} [options.width] - Width of the resource
9 * @param {number} [options.height] - Height of the resource
10 */
11 constructor(source, options) {
12 const { width, height } = options || {};
13 let urls, length;
14 Array.isArray(source) ? (urls = source, length = source.length) : length = source, super(length, { width, height }), urls && this.initFromArray(urls, options);
15 }
16 /**
17 * Set a baseTexture by ID,
18 * ArrayResource just takes resource from it, nothing more
19 * @param baseTexture
20 * @param index - Zero-based index of resource to set
21 * @returns - Instance for chaining
22 */
23 addBaseTextureAt(baseTexture, index) {
24 if (baseTexture.resource)
25 this.addResourceAt(baseTexture.resource, index);
26 else
27 throw new Error("ArrayResource does not support RenderTexture");
28 return this;
29 }
30 /**
31 * Add binding
32 * @param baseTexture
33 */
34 bind(baseTexture) {
35 super.bind(baseTexture), baseTexture.target = TARGETS.TEXTURE_2D_ARRAY;
36 }
37 /**
38 * Upload the resources to the GPU.
39 * @param renderer
40 * @param texture
41 * @param glTexture
42 * @returns - whether texture was uploaded
43 */
44 upload(renderer, texture, glTexture) {
45 const { length, itemDirtyIds, items } = this, { gl } = renderer;
46 glTexture.dirtyId < 0 && gl.texImage3D(
47 gl.TEXTURE_2D_ARRAY,
48 0,
49 glTexture.internalFormat,
50 this._width,
51 this._height,
52 length,
53 0,
54 texture.format,
55 glTexture.type,
56 null
57 );
58 for (let i = 0; i < length; i++) {
59 const item = items[i];
60 itemDirtyIds[i] < item.dirtyId && (itemDirtyIds[i] = item.dirtyId, item.valid && gl.texSubImage3D(
61 gl.TEXTURE_2D_ARRAY,
62 0,
63 0,
64 // xoffset
65 0,
66 // yoffset
67 i,
68 // zoffset
69 item.resource.width,
70 item.resource.height,
71 1,
72 texture.format,
73 glTexture.type,
74 item.resource.source
75 ));
76 }
77 return !0;
78 }
79}
80export {
81 ArrayResource
82};
83//# sourceMappingURL=ArrayResource.mjs.map