UNPKG

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