1 | import { TARGETS } from '@pixi/constants';
|
2 | import { AbstractMultiResource } from './AbstractMultiResource.mjs';
|
3 |
|
4 | const _CubeResource = class extends AbstractMultiResource {
|
5 | constructor(source, options) {
|
6 | const { width, height, autoLoad, linkBaseTexture } = options || {};
|
7 | if (source && source.length !== _CubeResource.SIDES) {
|
8 | throw new Error(`Invalid length. Got ${source.length}, expected 6`);
|
9 | }
|
10 | super(6, { width, height });
|
11 | for (let i = 0; i < _CubeResource.SIDES; i++) {
|
12 | this.items[i].target = TARGETS.TEXTURE_CUBE_MAP_POSITIVE_X + i;
|
13 | }
|
14 | this.linkBaseTexture = linkBaseTexture !== false;
|
15 | if (source) {
|
16 | this.initFromArray(source, options);
|
17 | }
|
18 | if (autoLoad !== false) {
|
19 | this.load();
|
20 | }
|
21 | }
|
22 | bind(baseTexture) {
|
23 | super.bind(baseTexture);
|
24 | baseTexture.target = TARGETS.TEXTURE_CUBE_MAP;
|
25 | }
|
26 | addBaseTextureAt(baseTexture, index, linkBaseTexture) {
|
27 | if (linkBaseTexture === void 0) {
|
28 | linkBaseTexture = this.linkBaseTexture;
|
29 | }
|
30 | if (!this.items[index]) {
|
31 | throw new Error(`Index ${index} is out of bounds`);
|
32 | }
|
33 | if (!this.linkBaseTexture || baseTexture.parentTextureArray || Object.keys(baseTexture._glTextures).length > 0) {
|
34 | if (baseTexture.resource) {
|
35 | this.addResourceAt(baseTexture.resource, index);
|
36 | } else {
|
37 | throw new Error(`CubeResource does not support copying of renderTexture.`);
|
38 | }
|
39 | } else {
|
40 | baseTexture.target = TARGETS.TEXTURE_CUBE_MAP_POSITIVE_X + index;
|
41 | baseTexture.parentTextureArray = this.baseTexture;
|
42 | this.items[index] = baseTexture;
|
43 | }
|
44 | if (baseTexture.valid && !this.valid) {
|
45 | this.resize(baseTexture.realWidth, baseTexture.realHeight);
|
46 | }
|
47 | this.items[index] = baseTexture;
|
48 | return this;
|
49 | }
|
50 | upload(renderer, _baseTexture, glTexture) {
|
51 | const dirty = this.itemDirtyIds;
|
52 | for (let i = 0; i < _CubeResource.SIDES; i++) {
|
53 | const side = this.items[i];
|
54 | if (dirty[i] < side.dirtyId || glTexture.dirtyId < _baseTexture.dirtyId) {
|
55 | if (side.valid && side.resource) {
|
56 | side.resource.upload(renderer, side, glTexture);
|
57 | dirty[i] = side.dirtyId;
|
58 | } else if (dirty[i] < -1) {
|
59 | renderer.gl.texImage2D(side.target, 0, glTexture.internalFormat, _baseTexture.realWidth, _baseTexture.realHeight, 0, _baseTexture.format, glTexture.type, null);
|
60 | dirty[i] = -1;
|
61 | }
|
62 | }
|
63 | }
|
64 | return true;
|
65 | }
|
66 | static test(source) {
|
67 | return Array.isArray(source) && source.length === _CubeResource.SIDES;
|
68 | }
|
69 | };
|
70 | let CubeResource = _CubeResource;
|
71 | CubeResource.SIDES = 6;
|
72 |
|
73 | export { CubeResource };
|
74 |
|