UNPKG

1.61 kBJavaScriptView Raw
1import { TARGETS } from '@pixi/constants';
2import { AbstractMultiResource } from './AbstractMultiResource.mjs';
3
4class ArrayResource extends AbstractMultiResource {
5 constructor(source, options) {
6 const { width, height } = options || {};
7 let urls;
8 let length;
9 if (Array.isArray(source)) {
10 urls = source;
11 length = source.length;
12 } else {
13 length = source;
14 }
15 super(length, { width, height });
16 if (urls) {
17 this.initFromArray(urls, options);
18 }
19 }
20 addBaseTextureAt(baseTexture, index) {
21 if (baseTexture.resource) {
22 this.addResourceAt(baseTexture.resource, index);
23 } else {
24 throw new Error("ArrayResource does not support RenderTexture");
25 }
26 return this;
27 }
28 bind(baseTexture) {
29 super.bind(baseTexture);
30 baseTexture.target = TARGETS.TEXTURE_2D_ARRAY;
31 }
32 upload(renderer, texture, glTexture) {
33 const { length, itemDirtyIds, items } = this;
34 const { gl } = renderer;
35 if (glTexture.dirtyId < 0) {
36 gl.texImage3D(gl.TEXTURE_2D_ARRAY, 0, glTexture.internalFormat, this._width, this._height, length, 0, texture.format, glTexture.type, null);
37 }
38 for (let i = 0; i < length; i++) {
39 const item = items[i];
40 if (itemDirtyIds[i] < item.dirtyId) {
41 itemDirtyIds[i] = item.dirtyId;
42 if (item.valid) {
43 gl.texSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, i, item.resource.width, item.resource.height, 1, texture.format, glTexture.type, item.resource.source);
44 }
45 }
46 }
47 return true;
48 }
49}
50
51export { ArrayResource };
52//# sourceMappingURL=ArrayResource.mjs.map