UNPKG

12.4 kBTypeScriptView Raw
1import { ALPHA_MODES, FORMATS, MIPMAP_MODES, SCALE_MODES, TARGETS, TYPES, WRAP_MODES } from '@pixi/constants';
2import { EventEmitter } from '@pixi/utils';
3import { BufferResource } from './resources/BufferResource';
4import { Resource } from './resources/Resource';
5import type { MSAA_QUALITY } from '@pixi/constants';
6import type { ICanvas } from '@pixi/settings';
7import type { GLTexture } from './GLTexture';
8import type { IAutoDetectOptions } from './resources/autoDetectResource';
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 0
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 * Default options used when creating BaseTexture objects.
167 * @static
168 * @memberof PIXI.BaseTexture
169 * @type {PIXI.IBaseTextureOptions}
170 */
171 static defaultOptions: IBaseTextureOptions;
172 /**
173 * @param {PIXI.Resource|HTMLImageElement|HTMLVideoElement|ImageBitmap|ICanvas|string} [resource=null] -
174 * The current resource to use, for things that aren't Resource objects, will be converted
175 * into a Resource.
176 * @param options - Collection of options, default options inherited from {@link PIXI.BaseTexture.defaultOptions}.
177 * @param {PIXI.MIPMAP_MODES} [options.mipmap] - If mipmapping is enabled for texture
178 * @param {number} [options.anisotropicLevel] - Anisotropic filtering level of texture
179 * @param {PIXI.WRAP_MODES} [options.wrapMode] - Wrap mode for textures
180 * @param {PIXI.SCALE_MODES} [options.scaleMode] - Default scale mode, linear, nearest
181 * @param {PIXI.FORMATS} [options.format] - GL format type
182 * @param {PIXI.TYPES} [options.type] - GL data type
183 * @param {PIXI.TARGETS} [options.target] - GL texture target
184 * @param {PIXI.ALPHA_MODES} [options.alphaMode] - Pre multiply the image alpha
185 * @param {number} [options.width=0] - Width of the texture
186 * @param {number} [options.height=0] - Height of the texture
187 * @param {number} [options.resolution=PIXI.settings.RESOLUTION] - Resolution of the base texture
188 * @param {object} [options.resourceOptions] - Optional resource options,
189 * see {@link PIXI.autoDetectResource autoDetectResource}
190 */
191 constructor(resource?: R | ImageSource | string | any, options?: IBaseTextureOptions<RO>);
192 /**
193 * Pixel width of the source of this texture
194 * @readonly
195 */
196 get realWidth(): number;
197 /**
198 * Pixel height of the source of this texture
199 * @readonly
200 */
201 get realHeight(): number;
202 /**
203 * Mipmap mode of the texture, affects downscaled images
204 * @default PIXI.MIPMAP_MODES.POW2
205 */
206 get mipmap(): MIPMAP_MODES;
207 set mipmap(value: MIPMAP_MODES);
208 /**
209 * The scale mode to apply when scaling this texture
210 * @default PIXI.SCALE_MODES.LINEAR
211 */
212 get scaleMode(): SCALE_MODES;
213 set scaleMode(value: SCALE_MODES);
214 /**
215 * How the texture wraps
216 * @default PIXI.WRAP_MODES.CLAMP
217 */
218 get wrapMode(): WRAP_MODES;
219 set wrapMode(value: WRAP_MODES);
220 /**
221 * Changes style options of BaseTexture
222 * @param scaleMode - Pixi scalemode
223 * @param mipmap - enable mipmaps
224 * @returns - this
225 */
226 setStyle(scaleMode?: SCALE_MODES, mipmap?: MIPMAP_MODES): this;
227 /**
228 * Changes w/h/resolution. Texture becomes valid if width and height are greater than zero.
229 * @param desiredWidth - Desired visual width
230 * @param desiredHeight - Desired visual height
231 * @param resolution - Optionally set resolution
232 * @returns - this
233 */
234 setSize(desiredWidth: number, desiredHeight: number, resolution?: number): this;
235 /**
236 * Sets real size of baseTexture, preserves current resolution.
237 * @param realWidth - Full rendered width
238 * @param realHeight - Full rendered height
239 * @param resolution - Optionally set resolution
240 * @returns - this
241 */
242 setRealSize(realWidth: number, realHeight: number, resolution?: number): this;
243 /**
244 * Refresh check for isPowerOfTwo texture based on size
245 * @private
246 */
247 protected _refreshPOT(): void;
248 /**
249 * Changes resolution
250 * @param resolution - res
251 * @returns - this
252 */
253 setResolution(resolution: number): this;
254 /**
255 * Sets the resource if it wasn't set. Throws error if resource already present
256 * @param resource - that is managing this BaseTexture
257 * @returns - this
258 */
259 setResource(resource: R): this;
260 /** Invalidates the object. Texture becomes valid if width and height are greater than zero. */
261 update(): void;
262 /**
263 * Handle errors with resources.
264 * @private
265 * @param event - Error event emitted.
266 */
267 onError(event: ErrorEvent): void;
268 /**
269 * Destroys this base texture.
270 * The method stops if resource doesn't want this texture to be destroyed.
271 * Removes texture from all caches.
272 */
273 destroy(): void;
274 /**
275 * Frees the texture from WebGL memory without destroying this texture object.
276 * This means you can still use the texture later which will upload it to GPU
277 * memory again.
278 * @fires PIXI.BaseTexture#dispose
279 */
280 dispose(): void;
281 /** Utility function for BaseTexture|Texture cast. */
282 castToBaseTexture(): BaseTexture;
283 /**
284 * Helper function that creates a base texture based on the source you provide.
285 * The source can be - image url, image element, canvas element. If the
286 * source is an image url or an image element and not in the base texture
287 * cache, it will be created and loaded.
288 * @static
289 * @param {HTMLImageElement|HTMLVideoElement|ImageBitmap|PIXI.ICanvas|string|string[]} source - The
290 * source to create base texture from.
291 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
292 * @param {string} [options.pixiIdPrefix=pixiid] - If a source has no id, this is the prefix of the generated id
293 * @param {boolean} [strict] - Enforce strict-mode, see {@link PIXI.settings.STRICT_TEXTURE_CACHE}.
294 * @returns {PIXI.BaseTexture} The new base texture.
295 */
296 static from<R extends Resource = Resource, RO = IAutoDetectOptions>(source: ImageSource | string | string[], options?: IBaseTextureOptions<RO>, strict?: boolean): BaseTexture<R>;
297 /**
298 * Create a new BaseTexture with a BufferResource from a Float32Array.
299 * RGBA values are floats from 0 to 1.
300 * @param {Float32Array|Uint8Array} buffer - The optional array to use, if no data
301 * is provided, a new Float32Array is created.
302 * @param width - Width of the resource
303 * @param height - Height of the resource
304 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
305 * Default properties are different from the constructor's defaults.
306 * @param {PIXI.FORMATS} [options.format=PIXI.FORMATS.RGBA] - GL format type
307 * @param {PIXI.ALPHA_MODES} [options.alphaMode=PIXI.ALPHA_MODES.NPM] - Image alpha, not premultiplied by default
308 * @param {PIXI.SCALE_MODES} [options.scaleMode=PIXI.SCALE_MODES.NEAREST] - Scale mode, pixelating by default
309 * @returns - The resulting new BaseTexture
310 */
311 static fromBuffer(buffer: Float32Array | Uint8Array, width: number, height: number, options?: IBaseTextureOptions): BaseTexture<BufferResource>;
312 /**
313 * Adds a BaseTexture to the global BaseTextureCache. This cache is shared across the whole PIXI object.
314 * @param {PIXI.BaseTexture} baseTexture - The BaseTexture to add to the cache.
315 * @param {string} id - The id that the BaseTexture will be stored against.
316 */
317 static addToCache(baseTexture: BaseTexture, id: string): void;
318 /**
319 * Remove a BaseTexture from the global BaseTextureCache.
320 * @param {string|PIXI.BaseTexture} baseTexture - id of a BaseTexture to be removed, or a BaseTexture instance itself.
321 * @returns {PIXI.BaseTexture|null} The BaseTexture that was removed.
322 */
323 static removeFromCache(baseTexture: string | BaseTexture): BaseTexture | null;
324 /** Global number of the texture batch, used by multi-texture renderers. */
325 static _globalBatch: number;
326}