UNPKG

14.6 kBJavaScriptView Raw
1import { SCALE_MODES, ALPHA_MODES, FORMATS, TYPES, MIPMAP_MODES, WRAP_MODES, TARGETS } from "@pixi/constants";
2import { settings } from "@pixi/settings";
3import { EventEmitter, uid, isPow2, BaseTextureCache, TextureCache } from "@pixi/utils";
4import { autoDetectResource } from "./resources/autoDetectResource.mjs";
5import { BufferResource } from "./resources/BufferResource.mjs";
6import { Resource } from "./resources/Resource.mjs";
7const defaultBufferOptions = {
8 scaleMode: SCALE_MODES.NEAREST,
9 alphaMode: ALPHA_MODES.NPM
10}, _BaseTexture = class _BaseTexture2 extends EventEmitter {
11 /**
12 * @param {PIXI.Resource|PIXI.ImageSource|string} [resource=null] -
13 * The current resource to use, for things that aren't Resource objects, will be converted
14 * into a Resource.
15 * @param options - Collection of options, default options inherited from {@link PIXI.BaseTexture.defaultOptions}.
16 * @param {PIXI.MIPMAP_MODES} [options.mipmap] - If mipmapping is enabled for texture
17 * @param {number} [options.anisotropicLevel] - Anisotropic filtering level of texture
18 * @param {PIXI.WRAP_MODES} [options.wrapMode] - Wrap mode for textures
19 * @param {PIXI.SCALE_MODES} [options.scaleMode] - Default scale mode, linear, nearest
20 * @param {PIXI.FORMATS} [options.format] - GL format type
21 * @param {PIXI.TYPES} [options.type] - GL data type
22 * @param {PIXI.TARGETS} [options.target] - GL texture target
23 * @param {PIXI.ALPHA_MODES} [options.alphaMode] - Pre multiply the image alpha
24 * @param {number} [options.width=0] - Width of the texture
25 * @param {number} [options.height=0] - Height of the texture
26 * @param {number} [options.resolution=PIXI.settings.RESOLUTION] - Resolution of the base texture
27 * @param {object} [options.resourceOptions] - Optional resource options,
28 * see {@link PIXI.autoDetectResource autoDetectResource}
29 */
30 constructor(resource = null, options = null) {
31 super(), options = Object.assign({}, _BaseTexture2.defaultOptions, options);
32 const {
33 alphaMode,
34 mipmap,
35 anisotropicLevel,
36 scaleMode,
37 width,
38 height,
39 wrapMode,
40 format,
41 type,
42 target,
43 resolution,
44 resourceOptions
45 } = options;
46 resource && !(resource instanceof Resource) && (resource = autoDetectResource(resource, resourceOptions), resource.internal = !0), this.resolution = resolution || settings.RESOLUTION, this.width = Math.round((width || 0) * this.resolution) / this.resolution, this.height = Math.round((height || 0) * this.resolution) / this.resolution, this._mipmap = mipmap, this.anisotropicLevel = anisotropicLevel, this._wrapMode = wrapMode, this._scaleMode = scaleMode, this.format = format, this.type = type, this.target = target, this.alphaMode = alphaMode, this.uid = uid(), this.touched = 0, this.isPowerOfTwo = !1, this._refreshPOT(), this._glTextures = {}, this.dirtyId = 0, this.dirtyStyleId = 0, this.cacheId = null, this.valid = width > 0 && height > 0, this.textureCacheIds = [], this.destroyed = !1, this.resource = null, this._batchEnabled = 0, this._batchLocation = 0, this.parentTextureArray = null, this.setResource(resource);
47 }
48 /**
49 * Pixel width of the source of this texture
50 * @readonly
51 */
52 get realWidth() {
53 return Math.round(this.width * this.resolution);
54 }
55 /**
56 * Pixel height of the source of this texture
57 * @readonly
58 */
59 get realHeight() {
60 return Math.round(this.height * this.resolution);
61 }
62 /**
63 * Mipmap mode of the texture, affects downscaled images
64 * @default PIXI.MIPMAP_MODES.POW2
65 */
66 get mipmap() {
67 return this._mipmap;
68 }
69 set mipmap(value) {
70 this._mipmap !== value && (this._mipmap = value, this.dirtyStyleId++);
71 }
72 /**
73 * The scale mode to apply when scaling this texture
74 * @default PIXI.SCALE_MODES.LINEAR
75 */
76 get scaleMode() {
77 return this._scaleMode;
78 }
79 set scaleMode(value) {
80 this._scaleMode !== value && (this._scaleMode = value, this.dirtyStyleId++);
81 }
82 /**
83 * How the texture wraps
84 * @default PIXI.WRAP_MODES.CLAMP
85 */
86 get wrapMode() {
87 return this._wrapMode;
88 }
89 set wrapMode(value) {
90 this._wrapMode !== value && (this._wrapMode = value, this.dirtyStyleId++);
91 }
92 /**
93 * Changes style options of BaseTexture
94 * @param scaleMode - Pixi scalemode
95 * @param mipmap - enable mipmaps
96 * @returns - this
97 */
98 setStyle(scaleMode, mipmap) {
99 let dirty;
100 return scaleMode !== void 0 && scaleMode !== this.scaleMode && (this.scaleMode = scaleMode, dirty = !0), mipmap !== void 0 && mipmap !== this.mipmap && (this.mipmap = mipmap, dirty = !0), dirty && this.dirtyStyleId++, this;
101 }
102 /**
103 * Changes w/h/resolution. Texture becomes valid if width and height are greater than zero.
104 * @param desiredWidth - Desired visual width
105 * @param desiredHeight - Desired visual height
106 * @param resolution - Optionally set resolution
107 * @returns - this
108 */
109 setSize(desiredWidth, desiredHeight, resolution) {
110 return resolution = resolution || this.resolution, this.setRealSize(desiredWidth * resolution, desiredHeight * resolution, resolution);
111 }
112 /**
113 * Sets real size of baseTexture, preserves current resolution.
114 * @param realWidth - Full rendered width
115 * @param realHeight - Full rendered height
116 * @param resolution - Optionally set resolution
117 * @returns - this
118 */
119 setRealSize(realWidth, realHeight, resolution) {
120 return this.resolution = resolution || this.resolution, this.width = Math.round(realWidth) / this.resolution, this.height = Math.round(realHeight) / this.resolution, this._refreshPOT(), this.update(), this;
121 }
122 /**
123 * Refresh check for isPowerOfTwo texture based on size
124 * @private
125 */
126 _refreshPOT() {
127 this.isPowerOfTwo = isPow2(this.realWidth) && isPow2(this.realHeight);
128 }
129 /**
130 * Changes resolution
131 * @param resolution - res
132 * @returns - this
133 */
134 setResolution(resolution) {
135 const oldResolution = this.resolution;
136 return oldResolution === resolution ? this : (this.resolution = resolution, this.valid && (this.width = Math.round(this.width * oldResolution) / resolution, this.height = Math.round(this.height * oldResolution) / resolution, this.emit("update", this)), this._refreshPOT(), this);
137 }
138 /**
139 * Sets the resource if it wasn't set. Throws error if resource already present
140 * @param resource - that is managing this BaseTexture
141 * @returns - this
142 */
143 setResource(resource) {
144 if (this.resource === resource)
145 return this;
146 if (this.resource)
147 throw new Error("Resource can be set only once");
148 return resource.bind(this), this.resource = resource, this;
149 }
150 /** Invalidates the object. Texture becomes valid if width and height are greater than zero. */
151 update() {
152 this.valid ? (this.dirtyId++, this.dirtyStyleId++, this.emit("update", this)) : this.width > 0 && this.height > 0 && (this.valid = !0, this.emit("loaded", this), this.emit("update", this));
153 }
154 /**
155 * Handle errors with resources.
156 * @private
157 * @param event - Error event emitted.
158 */
159 onError(event) {
160 this.emit("error", this, event);
161 }
162 /**
163 * Destroys this base texture.
164 * The method stops if resource doesn't want this texture to be destroyed.
165 * Removes texture from all caches.
166 * @fires PIXI.BaseTexture#destroyed
167 */
168 destroy() {
169 this.resource && (this.resource.unbind(this), this.resource.internal && this.resource.destroy(), this.resource = null), this.cacheId && (delete BaseTextureCache[this.cacheId], delete TextureCache[this.cacheId], this.cacheId = null), this.valid = !1, this.dispose(), _BaseTexture2.removeFromCache(this), this.textureCacheIds = null, this.destroyed = !0, this.emit("destroyed", this), this.removeAllListeners();
170 }
171 /**
172 * Frees the texture from WebGL memory without destroying this texture object.
173 * This means you can still use the texture later which will upload it to GPU
174 * memory again.
175 * @fires PIXI.BaseTexture#dispose
176 */
177 dispose() {
178 this.emit("dispose", this);
179 }
180 /** Utility function for BaseTexture|Texture cast. */
181 castToBaseTexture() {
182 return this;
183 }
184 /**
185 * Helper function that creates a base texture based on the source you provide.
186 * The source can be - image url, image element, canvas element. If the
187 * source is an image url or an image element and not in the base texture
188 * cache, it will be created and loaded.
189 * @static
190 * @param {PIXI.ImageSource|string|string[]} source - The
191 * source to create base texture from.
192 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
193 * @param {string} [options.pixiIdPrefix=pixiid] - If a source has no id, this is the prefix of the generated id
194 * @param {boolean} [strict] - Enforce strict-mode, see {@link PIXI.settings.STRICT_TEXTURE_CACHE}.
195 * @returns {PIXI.BaseTexture} The new base texture.
196 */
197 static from(source, options, strict = settings.STRICT_TEXTURE_CACHE) {
198 const isFrame = typeof source == "string";
199 let cacheId = null;
200 if (isFrame)
201 cacheId = source;
202 else {
203 if (!source._pixiId) {
204 const prefix = options?.pixiIdPrefix || "pixiid";
205 source._pixiId = `${prefix}_${uid()}`;
206 }
207 cacheId = source._pixiId;
208 }
209 let baseTexture = BaseTextureCache[cacheId];
210 if (isFrame && strict && !baseTexture)
211 throw new Error(`The cacheId "${cacheId}" does not exist in BaseTextureCache.`);
212 return baseTexture || (baseTexture = new _BaseTexture2(source, options), baseTexture.cacheId = cacheId, _BaseTexture2.addToCache(baseTexture, cacheId)), baseTexture;
213 }
214 /**
215 * Create a new Texture with a BufferResource from a typed array.
216 * @param buffer - The optional array to use. If no data is provided, a new Float32Array is created.
217 * @param width - Width of the resource
218 * @param height - Height of the resource
219 * @param options - See {@link PIXI.BaseTexture}'s constructor for options.
220 * Default properties are different from the constructor's defaults.
221 * @param {PIXI.FORMATS} [options.format] - The format is not given, the type is inferred from the
222 * type of the buffer: `RGBA` if Float32Array, Int8Array, Uint8Array, or Uint8ClampedArray,
223 * otherwise `RGBA_INTEGER`.
224 * @param {PIXI.TYPES} [options.type] - The type is not given, the type is inferred from the
225 * type of the buffer. Maps Float32Array to `FLOAT`, Int32Array to `INT`, Uint32Array to
226 * `UNSIGNED_INT`, Int16Array to `SHORT`, Uint16Array to `UNSIGNED_SHORT`, Int8Array to `BYTE`,
227 * Uint8Array/Uint8ClampedArray to `UNSIGNED_BYTE`.
228 * @param {PIXI.ALPHA_MODES} [options.alphaMode=PIXI.ALPHA_MODES.NPM]
229 * @param {PIXI.SCALE_MODES} [options.scaleMode=PIXI.SCALE_MODES.NEAREST]
230 * @returns - The resulting new BaseTexture
231 */
232 static fromBuffer(buffer, width, height, options) {
233 buffer = buffer || new Float32Array(width * height * 4);
234 const resource = new BufferResource(buffer, { width, height, ...options?.resourceOptions });
235 let format, type;
236 return buffer instanceof Float32Array ? (format = FORMATS.RGBA, type = TYPES.FLOAT) : buffer instanceof Int32Array ? (format = FORMATS.RGBA_INTEGER, type = TYPES.INT) : buffer instanceof Uint32Array ? (format = FORMATS.RGBA_INTEGER, type = TYPES.UNSIGNED_INT) : buffer instanceof Int16Array ? (format = FORMATS.RGBA_INTEGER, type = TYPES.SHORT) : buffer instanceof Uint16Array ? (format = FORMATS.RGBA_INTEGER, type = TYPES.UNSIGNED_SHORT) : buffer instanceof Int8Array ? (format = FORMATS.RGBA, type = TYPES.BYTE) : (format = FORMATS.RGBA, type = TYPES.UNSIGNED_BYTE), resource.internal = !0, new _BaseTexture2(resource, Object.assign({}, defaultBufferOptions, { type, format }, options));
237 }
238 /**
239 * Adds a BaseTexture to the global BaseTextureCache. This cache is shared across the whole PIXI object.
240 * @param {PIXI.BaseTexture} baseTexture - The BaseTexture to add to the cache.
241 * @param {string} id - The id that the BaseTexture will be stored against.
242 */
243 static addToCache(baseTexture, id) {
244 id && (baseTexture.textureCacheIds.includes(id) || baseTexture.textureCacheIds.push(id), BaseTextureCache[id] && BaseTextureCache[id] !== baseTexture && console.warn(`BaseTexture added to the cache with an id [${id}] that already had an entry`), BaseTextureCache[id] = baseTexture);
245 }
246 /**
247 * Remove a BaseTexture from the global BaseTextureCache.
248 * @param {string|PIXI.BaseTexture} baseTexture - id of a BaseTexture to be removed, or a BaseTexture instance itself.
249 * @returns {PIXI.BaseTexture|null} The BaseTexture that was removed.
250 */
251 static removeFromCache(baseTexture) {
252 if (typeof baseTexture == "string") {
253 const baseTextureFromCache = BaseTextureCache[baseTexture];
254 if (baseTextureFromCache) {
255 const index = baseTextureFromCache.textureCacheIds.indexOf(baseTexture);
256 return index > -1 && baseTextureFromCache.textureCacheIds.splice(index, 1), delete BaseTextureCache[baseTexture], baseTextureFromCache;
257 }
258 } else if (baseTexture?.textureCacheIds) {
259 for (let i = 0; i < baseTexture.textureCacheIds.length; ++i)
260 delete BaseTextureCache[baseTexture.textureCacheIds[i]];
261 return baseTexture.textureCacheIds.length = 0, baseTexture;
262 }
263 return null;
264 }
265};
266_BaseTexture.defaultOptions = {
267 /**
268 * If mipmapping is enabled for texture.
269 * @type {PIXI.MIPMAP_MODES}
270 * @default PIXI.MIPMAP_MODES.POW2
271 */
272 mipmap: MIPMAP_MODES.POW2,
273 /** Anisotropic filtering level of texture */
274 anisotropicLevel: 0,
275 /**
276 * Default scale mode, linear, nearest.
277 * @type {PIXI.SCALE_MODES}
278 * @default PIXI.SCALE_MODES.LINEAR
279 */
280 scaleMode: SCALE_MODES.LINEAR,
281 /**
282 * Wrap mode for textures.
283 * @type {PIXI.WRAP_MODES}
284 * @default PIXI.WRAP_MODES.CLAMP
285 */
286 wrapMode: WRAP_MODES.CLAMP,
287 /**
288 * Pre multiply the image alpha
289 * @type {PIXI.ALPHA_MODES}
290 * @default PIXI.ALPHA_MODES.UNPACK
291 */
292 alphaMode: ALPHA_MODES.UNPACK,
293 /**
294 * GL texture target
295 * @type {PIXI.TARGETS}
296 * @default PIXI.TARGETS.TEXTURE_2D
297 */
298 target: TARGETS.TEXTURE_2D,
299 /**
300 * GL format type
301 * @type {PIXI.FORMATS}
302 * @default PIXI.FORMATS.RGBA
303 */
304 format: FORMATS.RGBA,
305 /**
306 * GL data type
307 * @type {PIXI.TYPES}
308 * @default PIXI.TYPES.UNSIGNED_BYTE
309 */
310 type: TYPES.UNSIGNED_BYTE
311}, /** Global number of the texture batch, used by multi-texture renderers. */
312_BaseTexture._globalBatch = 0;
313let BaseTexture = _BaseTexture;
314export {
315 BaseTexture
316};
317//# sourceMappingURL=BaseTexture.mjs.map