- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
import { AbstractMultiResource } from './AbstractMultiResource';
import { TARGETS } from '@pixi/constants';
import type { BaseTexture } from '../BaseTexture';
import type { BaseImageResource } from './BaseImageResource';
import type { Renderer } from '../../Renderer';
import type { GLTexture } from '../GLTexture';
import type { ISize } from '@pixi/math';
/**
* A resource that contains a number of sources.
* @memberof PIXI
*/
export class ArrayResource extends AbstractMultiResource
{
/**
* @param source - Number of items in array or the collection
* of image URLs to use. Can also be resources, image elements, canvas, etc.
* @param options - Options to apply to {@link PIXI.autoDetectResource}
* @param {number} [options.width] - Width of the resource
* @param {number} [options.height] - Height of the resource
*/
constructor(source: number | Array<any>, options?: ISize)
{
const { width, height } = options || {};
let urls;
let length: number;
if (Array.isArray(source))
{
urls = source;
length = source.length;
}
else
{
length = source;
}
super(length, { width, height });
if (urls)
{
this.initFromArray(urls, options);
}
}
/**
* Set a baseTexture by ID,
* ArrayResource just takes resource from it, nothing more
* @param baseTexture
* @param index - Zero-based index of resource to set
* @returns - Instance for chaining
*/
addBaseTextureAt(baseTexture: BaseTexture, index: number): this
{
if (baseTexture.resource)
{
this.addResourceAt(baseTexture.resource, index);
}
else
{
throw new Error('ArrayResource does not support RenderTexture');
}
return this;
}
/**
* Add binding
* @param baseTexture
*/
bind(baseTexture: BaseTexture): void
{
super.bind(baseTexture);
baseTexture.target = TARGETS.TEXTURE_2D_ARRAY;
}
/**
* Upload the resources to the GPU.
* @param renderer
* @param texture
* @param glTexture
* @returns - whether texture was uploaded
*/
upload(renderer: Renderer, texture: BaseTexture, glTexture: GLTexture): boolean
{
const { length, itemDirtyIds, items } = this;
const { gl } = renderer;
if (glTexture.dirtyId < 0)
{
gl.texImage3D(
gl.TEXTURE_2D_ARRAY,
0,
glTexture.internalFormat,
this._width,
this._height,
length,
0,
texture.format,
glTexture.type,
null
);
}
for (let i = 0; i < length; i++)
{
const item = items[i];
if (itemDirtyIds[i] < item.dirtyId)
{
itemDirtyIds[i] = item.dirtyId;
if (item.valid)
{
gl.texSubImage3D(
gl.TEXTURE_2D_ARRAY,
0,
0, // xoffset
0, // yoffset
i, // zoffset
item.resource.width,
item.resource.height,
1,
texture.format,
glTexture.type,
(item.resource as BaseImageResource).source
);
}
}
}
return true;
}
}