UNPKG

12.3 kBTypeScriptView Raw
1import { EventEmitter } from '@pixi/utils';
2import { FORMATS, SCALE_MODES, TARGETS, TYPES, ALPHA_MODES } from '@pixi/constants';
3import { Resource } from './resources/Resource';
4import { BufferResource } from './resources/BufferResource';
5import type { MSAA_QUALITY, MIPMAP_MODES, WRAP_MODES } from '@pixi/constants';
6import type { ICanvas } from '@pixi/settings';
7import type { IAutoDetectOptions } from './resources/autoDetectResource';
8import type { GLTexture } from './GLTexture';
9export declare type ImageSource = HTMLImageElement | HTMLVideoElement | ImageBitmap | ICanvas;
10export interface IBaseTextureOptions<RO = any> {
11 alphaMode?: ALPHA_MODES;
12 mipmap?: MIPMAP_MODES;
13 anisotropicLevel?: number;
14 scaleMode?: SCALE_MODES;
15 width?: number;
16 height?: number;
17 wrapMode?: WRAP_MODES;
18 format?: FORMATS;
19 type?: TYPES;
20 target?: TARGETS;
21 resolution?: number;
22 multisample?: MSAA_QUALITY;
23 resourceOptions?: RO;
24 pixiIdPrefix?: string;
25}
26export interface BaseTexture extends GlobalMixins.BaseTexture, EventEmitter {
27}
28/**
29 * A Texture stores the information that represents an image.
30 * All textures have a base texture, which contains information about the source.
31 * Therefore you can have many textures all using a single BaseTexture
32 * @memberof PIXI
33 * @typeParam R - The BaseTexture's Resource type.
34 * @typeParam RO - The options for constructing resource.
35 */
36export declare class BaseTexture<R extends Resource = Resource, RO = IAutoDetectOptions> extends EventEmitter {
37 /**
38 * The width of the base texture set when the image has loaded
39 * @readonly
40 */
41 width: number;
42 /**
43 * The height of the base texture set when the image has loaded
44 * @readonly
45 */
46 height: number;
47 /**
48 * The resolution / device pixel ratio of the texture
49 * @readonly
50 * @default PIXI.settings.RESOLUTION
51 */
52 resolution: number;
53 /**
54 * How to treat premultiplied alpha, see {@link PIXI.ALPHA_MODES}.
55 * @member {PIXI.ALPHA_MODES}
56 * @default PIXI.ALPHA_MODES.UNPACK
57 */
58 alphaMode?: ALPHA_MODES;
59 /**
60 * Anisotropic filtering level of texture
61 * @member {number}
62 * @default PIXI.settings.ANISOTROPIC_LEVEL
63 */
64 anisotropicLevel?: number;
65 /**
66 * The pixel format of the texture
67 * @default PIXI.FORMATS.RGBA
68 */
69 format?: FORMATS;
70 /**
71 * The type of resource data
72 * @default PIXI.TYPES.UNSIGNED_BYTE
73 */
74 type?: TYPES;
75 /**
76 * The target type
77 * @default PIXI.TARGETS.TEXTURE_2D
78 */
79 target?: TARGETS;
80 /**
81 * Global unique identifier for this BaseTexture
82 * @protected
83 */
84 readonly uid: number;
85 /**
86 * Used by automatic texture Garbage Collection, stores last GC tick when it was bound
87 * @protected
88 */
89 touched: number;
90 /**
91 * Whether or not the texture is a power of two, try to use power of two textures as much
92 * as you can
93 * @readonly
94 * @default false
95 */
96 isPowerOfTwo: boolean;
97 /**
98 * The map of render context textures where this is bound
99 * @private
100 */
101 _glTextures: {
102 [key: number]: GLTexture;
103 };
104 /**
105 * Used by TextureSystem to only update texture to the GPU when needed.
106 * Please call `update()` to increment it.
107 * @readonly
108 */
109 dirtyId: number;
110 /**
111 * Used by TextureSystem to only update texture style when needed.
112 * @protected
113 */
114 dirtyStyleId: number;
115 /**
116 * Currently default cache ID.
117 * @member {string}
118 */
119 cacheId: string;
120 /**
121 * Generally speaking means when resource is loaded.
122 * @readonly
123 * @member {boolean}
124 */
125 valid: boolean;
126 /**
127 * The collection of alternative cache ids, since some BaseTextures
128 * can have more than one ID, short name and longer full URL
129 * @member {Array<string>}
130 * @readonly
131 */
132 textureCacheIds: Array<string>;
133 /**
134 * Flag if BaseTexture has been destroyed.
135 * @member {boolean}
136 * @readonly
137 */
138 destroyed: boolean;
139 /**
140 * The resource used by this BaseTexture, there can only
141 * be one resource per BaseTexture, but textures can share
142 * resources.
143 * @member {PIXI.Resource}
144 * @readonly
145 */
146 resource: R;
147 /**
148 * Number of the texture batch, used by multi-texture renderers
149 * @member {number}
150 */
151 _batchEnabled: number;
152 /**
153 * Location inside texture batch, used by multi-texture renderers
154 * @member {number}
155 */
156 _batchLocation: number;
157 /**
158 * Whether its a part of another texture, handled by ArrayResource or CubeResource
159 * @member {PIXI.BaseTexture}
160 */
161 parentTextureArray: BaseTexture;
162 private _mipmap?;
163 private _scaleMode?;
164 private _wrapMode?;
165 /**
166 * @param {PIXI.Resource|HTMLImageElement|HTMLVideoElement|ImageBitmap|ICanvas|string} [resource=null] -
167 * The current resource to use, for things that aren't Resource objects, will be converted
168 * into a Resource.
169 * @param options - Collection of options
170 * @param {PIXI.MIPMAP_MODES} [options.mipmap=PIXI.settings.MIPMAP_TEXTURES] - If mipmapping is enabled for texture
171 * @param {number} [options.anisotropicLevel=PIXI.settings.ANISOTROPIC_LEVEL] - Anisotropic filtering level of texture
172 * @param {PIXI.WRAP_MODES} [options.wrapMode=PIXI.settings.WRAP_MODE] - Wrap mode for textures
173 * @param {PIXI.SCALE_MODES} [options.scaleMode=PIXI.settings.SCALE_MODE] - Default scale mode, linear, nearest
174 * @param {PIXI.FORMATS} [options.format=PIXI.FORMATS.RGBA] - GL format type
175 * @param {PIXI.TYPES} [options.type=PIXI.TYPES.UNSIGNED_BYTE] - GL data type
176 * @param {PIXI.TARGETS} [options.target=PIXI.TARGETS.TEXTURE_2D] - GL texture target
177 * @param {PIXI.ALPHA_MODES} [options.alphaMode=PIXI.ALPHA_MODES.UNPACK] - Pre multiply the image alpha
178 * @param {number} [options.width=0] - Width of the texture
179 * @param {number} [options.height=0] - Height of the texture
180 * @param {number} [options.resolution=PIXI.settings.RESOLUTION] - Resolution of the base texture
181 * @param {object} [options.resourceOptions] - Optional resource options,
182 * see {@link PIXI.autoDetectResource autoDetectResource}
183 */
184 constructor(resource?: R | ImageSource | string | any, options?: IBaseTextureOptions<RO>);
185 /**
186 * Pixel width of the source of this texture
187 * @readonly
188 */
189 get realWidth(): number;
190 /**
191 * Pixel height of the source of this texture
192 * @readonly
193 */
194 get realHeight(): number;
195 /**
196 * Mipmap mode of the texture, affects downscaled images
197 * @default PIXI.settings.MIPMAP_TEXTURES
198 */
199 get mipmap(): MIPMAP_MODES;
200 set mipmap(value: MIPMAP_MODES);
201 /**
202 * The scale mode to apply when scaling this texture
203 * @default PIXI.settings.SCALE_MODE
204 */
205 get scaleMode(): SCALE_MODES;
206 set scaleMode(value: SCALE_MODES);
207 /**
208 * How the texture wraps
209 * @default PIXI.settings.WRAP_MODE
210 */
211 get wrapMode(): WRAP_MODES;
212 set wrapMode(value: WRAP_MODES);
213 /**
214 * Changes style options of BaseTexture
215 * @param scaleMode - Pixi scalemode
216 * @param mipmap - enable mipmaps
217 * @returns - this
218 */
219 setStyle(scaleMode?: SCALE_MODES, mipmap?: MIPMAP_MODES): this;
220 /**
221 * Changes w/h/resolution. Texture becomes valid if width and height are greater than zero.
222 * @param desiredWidth - Desired visual width
223 * @param desiredHeight - Desired visual height
224 * @param resolution - Optionally set resolution
225 * @returns - this
226 */
227 setSize(desiredWidth: number, desiredHeight: number, resolution?: number): this;
228 /**
229 * Sets real size of baseTexture, preserves current resolution.
230 * @param realWidth - Full rendered width
231 * @param realHeight - Full rendered height
232 * @param resolution - Optionally set resolution
233 * @returns - this
234 */
235 setRealSize(realWidth: number, realHeight: number, resolution?: number): this;
236 /**
237 * Refresh check for isPowerOfTwo texture based on size
238 * @private
239 */
240 protected _refreshPOT(): void;
241 /**
242 * Changes resolution
243 * @param resolution - res
244 * @returns - this
245 */
246 setResolution(resolution: number): this;
247 /**
248 * Sets the resource if it wasn't set. Throws error if resource already present
249 * @param resource - that is managing this BaseTexture
250 * @returns - this
251 */
252 setResource(resource: R): this;
253 /** Invalidates the object. Texture becomes valid if width and height are greater than zero. */
254 update(): void;
255 /**
256 * Handle errors with resources.
257 * @private
258 * @param event - Error event emitted.
259 */
260 onError(event: ErrorEvent): void;
261 /**
262 * Destroys this base texture.
263 * The method stops if resource doesn't want this texture to be destroyed.
264 * Removes texture from all caches.
265 */
266 destroy(): void;
267 /**
268 * Frees the texture from WebGL memory without destroying this texture object.
269 * This means you can still use the texture later which will upload it to GPU
270 * memory again.
271 * @fires PIXI.BaseTexture#dispose
272 */
273 dispose(): void;
274 /** Utility function for BaseTexture|Texture cast. */
275 castToBaseTexture(): BaseTexture;
276 /**
277 * Helper function that creates a base texture based on the source you provide.
278 * The source can be - image url, image element, canvas element. If the
279 * source is an image url or an image element and not in the base texture
280 * cache, it will be created and loaded.
281 * @static
282 * @param {HTMLImageElement|HTMLVideoElement|ImageBitmap|PIXI.ICanvas|string|string[]} source - The
283 * source to create base texture from.
284 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
285 * @param {string} [options.pixiIdPrefix=pixiid] - If a source has no id, this is the prefix of the generated id
286 * @param {boolean} [strict] - Enforce strict-mode, see {@link PIXI.settings.STRICT_TEXTURE_CACHE}.
287 * @returns {PIXI.BaseTexture} The new base texture.
288 */
289 static from<R extends Resource = Resource, RO = IAutoDetectOptions>(source: ImageSource | string | string[], options?: IBaseTextureOptions<RO>, strict?: boolean): BaseTexture<R>;
290 /**
291 * Create a new BaseTexture with a BufferResource from a Float32Array.
292 * RGBA values are floats from 0 to 1.
293 * @param {Float32Array|Uint8Array} buffer - The optional array to use, if no data
294 * is provided, a new Float32Array is created.
295 * @param width - Width of the resource
296 * @param height - Height of the resource
297 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
298 * Default properties are different from the constructor's defaults.
299 * @param {PIXI.FORMATS} [options.format=PIXI.FORMATS.RGBA] - GL format type
300 * @param {PIXI.ALPHA_MODES} [options.alphaMode=PIXI.ALPHA_MODES.NPM] - Image alpha, not premultiplied by default
301 * @param {PIXI.SCALE_MODES} [options.scaleMode=PIXI.SCALE_MODES.NEAREST] - Scale mode, pixelating by default
302 * @returns - The resulting new BaseTexture
303 */
304 static fromBuffer(buffer: Float32Array | Uint8Array, width: number, height: number, options?: IBaseTextureOptions): BaseTexture<BufferResource>;
305 /**
306 * Adds a BaseTexture to the global BaseTextureCache. This cache is shared across the whole PIXI object.
307 * @param {PIXI.BaseTexture} baseTexture - The BaseTexture to add to the cache.
308 * @param {string} id - The id that the BaseTexture will be stored against.
309 */
310 static addToCache(baseTexture: BaseTexture, id: string): void;
311 /**
312 * Remove a BaseTexture from the global BaseTextureCache.
313 * @param {string|PIXI.BaseTexture} baseTexture - id of a BaseTexture to be removed, or a BaseTexture instance itself.
314 * @returns {PIXI.BaseTexture|null} The BaseTexture that was removed.
315 */
316 static removeFromCache(baseTexture: string | BaseTexture): BaseTexture | null;
317 /** Global number of the texture batch, used by multi-texture renderers. */
318 static _globalBatch: number;
319}