@pixi/core
Version:
Core PixiJS
266 lines (265 loc) • 14.2 kB
JavaScript
"use strict";
var math = require("@pixi/math"), settings = require("@pixi/settings"), utils = require("@pixi/utils"), BaseTexture = require("./BaseTexture.js"), ImageResource = require("./resources/ImageResource.js"), TextureUvs = require("./TextureUvs.js");
const DEFAULT_UVS = new TextureUvs.TextureUvs();
function removeAllHandlers(tex) {
tex.destroy = function() {
}, tex.on = function() {
}, tex.once = function() {
}, tex.emit = function() {
};
}
class Texture extends utils.EventEmitter {
/**
* @param baseTexture - The base texture source to create the texture from
* @param frame - The rectangle frame of the texture to show
* @param orig - The area of original texture
* @param trim - Trimmed rectangle of original texture
* @param rotate - indicates how the texture was rotated by texture packer. See {@link PIXI.groupD8}
* @param anchor - Default anchor point used for sprite placement / rotation
* @param borders - Default borders used for 9-slice scaling. See {@link PIXI.NineSlicePlane}
*/
constructor(baseTexture, frame, orig, trim, rotate, anchor, borders) {
if (super(), this.noFrame = !1, frame || (this.noFrame = !0, frame = new math.Rectangle(0, 0, 1, 1)), baseTexture instanceof Texture && (baseTexture = baseTexture.baseTexture), this.baseTexture = baseTexture, this._frame = frame, this.trim = trim, this.valid = !1, this.destroyed = !1, this._uvs = DEFAULT_UVS, this.uvMatrix = null, this.orig = orig || frame, this._rotate = Number(rotate || 0), rotate === !0)
this._rotate = 2;
else if (this._rotate % 2 !== 0)
throw new Error("attempt to use diamond-shaped UVs. If you are sure, set rotation manually");
this.defaultAnchor = anchor ? new math.Point(anchor.x, anchor.y) : new math.Point(0, 0), this.defaultBorders = borders, this._updateID = 0, this.textureCacheIds = [], baseTexture.valid ? this.noFrame ? baseTexture.valid && this.onBaseTextureUpdated(baseTexture) : this.frame = frame : baseTexture.once("loaded", this.onBaseTextureUpdated, this), this.noFrame && baseTexture.on("update", this.onBaseTextureUpdated, this);
}
/**
* Updates this texture on the gpu.
*
* Calls the TextureResource update.
*
* If you adjusted `frame` manually, please call `updateUvs()` instead.
*/
update() {
this.baseTexture.resource && this.baseTexture.resource.update();
}
/**
* Called when the base texture is updated
* @protected
* @param baseTexture - The base texture.
*/
onBaseTextureUpdated(baseTexture) {
if (this.noFrame) {
if (!this.baseTexture.valid)
return;
this._frame.width = baseTexture.width, this._frame.height = baseTexture.height, this.valid = !0, this.updateUvs();
} else
this.frame = this._frame;
this.emit("update", this);
}
/**
* Destroys this texture
* @param [destroyBase=false] - Whether to destroy the base texture as well
* @fires PIXI.Texture#destroyed
*/
destroy(destroyBase) {
if (this.baseTexture) {
if (destroyBase) {
const { resource } = this.baseTexture;
resource?.url && utils.TextureCache[resource.url] && Texture.removeFromCache(resource.url), this.baseTexture.destroy();
}
this.baseTexture.off("loaded", this.onBaseTextureUpdated, this), this.baseTexture.off("update", this.onBaseTextureUpdated, this), this.baseTexture = null;
}
this._frame = null, this._uvs = null, this.trim = null, this.orig = null, this.valid = !1, Texture.removeFromCache(this), this.textureCacheIds = null, this.destroyed = !0, this.emit("destroyed", this), this.removeAllListeners();
}
/**
* Creates a new texture object that acts the same as this one.
* @returns - The new texture
*/
clone() {
const clonedFrame = this._frame.clone(), clonedOrig = this._frame === this.orig ? clonedFrame : this.orig.clone(), clonedTexture = new Texture(
this.baseTexture,
!this.noFrame && clonedFrame,
clonedOrig,
this.trim?.clone(),
this.rotate,
this.defaultAnchor,
this.defaultBorders
);
return this.noFrame && (clonedTexture._frame = clonedFrame), clonedTexture;
}
/**
* Updates the internal WebGL UV cache. Use it after you change `frame` or `trim` of the texture.
* Call it after changing the frame
*/
updateUvs() {
this._uvs === DEFAULT_UVS && (this._uvs = new TextureUvs.TextureUvs()), this._uvs.set(this._frame, this.baseTexture, this.rotate), this._updateID++;
}
/**
* Helper function that creates a new Texture based on the source you provide.
* The source can be - frame id, image url, video url, canvas element, video element, base texture
* @param {string|PIXI.BaseTexture|HTMLImageElement|HTMLVideoElement|ImageBitmap|PIXI.ICanvas} source -
* Source or array of sources to create texture from
* @param options - See {@link PIXI.BaseTexture}'s constructor for options.
* @param {string} [options.pixiIdPrefix=pixiid] - If a source has no id, this is the prefix of the generated id
* @param {boolean} [strict] - Enforce strict-mode, see {@link PIXI.settings.STRICT_TEXTURE_CACHE}.
* @returns {PIXI.Texture} The newly created texture
*/
static from(source, options = {}, strict = settings.settings.STRICT_TEXTURE_CACHE) {
const isFrame = typeof source == "string";
let cacheId = null;
if (isFrame)
cacheId = source;
else if (source instanceof BaseTexture.BaseTexture) {
if (!source.cacheId) {
const prefix = options?.pixiIdPrefix || "pixiid";
source.cacheId = `${prefix}-${utils.uid()}`, BaseTexture.BaseTexture.addToCache(source, source.cacheId);
}
cacheId = source.cacheId;
} else {
if (!source._pixiId) {
const prefix = options?.pixiIdPrefix || "pixiid";
source._pixiId = `${prefix}_${utils.uid()}`;
}
cacheId = source._pixiId;
}
let texture = utils.TextureCache[cacheId];
if (isFrame && strict && !texture)
throw new Error(`The cacheId "${cacheId}" does not exist in TextureCache.`);
return !texture && !(source instanceof BaseTexture.BaseTexture) ? (options.resolution || (options.resolution = utils.getResolutionOfUrl(source)), texture = new Texture(new BaseTexture.BaseTexture(source, options)), texture.baseTexture.cacheId = cacheId, BaseTexture.BaseTexture.addToCache(texture.baseTexture, cacheId), Texture.addToCache(texture, cacheId)) : !texture && source instanceof BaseTexture.BaseTexture && (texture = new Texture(source), Texture.addToCache(texture, cacheId)), texture;
}
/**
* Useful for loading textures via URLs. Use instead of `Texture.from` because
* it does a better job of handling failed URLs more effectively. This also ignores
* `PIXI.settings.STRICT_TEXTURE_CACHE`. Works for Videos, SVGs, Images.
* @param url - The remote URL or array of URLs to load.
* @param options - Optional options to include
* @returns - A Promise that resolves to a Texture.
*/
static fromURL(url, options) {
const resourceOptions = Object.assign({ autoLoad: !1 }, options?.resourceOptions), texture = Texture.from(url, Object.assign({ resourceOptions }, options), !1), resource = texture.baseTexture.resource;
return texture.baseTexture.valid ? Promise.resolve(texture) : resource.load().then(() => Promise.resolve(texture));
}
/**
* Create a new Texture with a BufferResource from a typed array.
* @param buffer - The optional array to use. If no data is provided, a new Float32Array is created.
* @param width - Width of the resource
* @param height - Height of the resource
* @param options - See {@link PIXI.BaseTexture}'s constructor for options.
* Default properties are different from the constructor's defaults.
* @param {PIXI.FORMATS} [options.format] - The format is not given, the type is inferred from the
* type of the buffer: `RGBA` if Float32Array, Int8Array, Uint8Array, or Uint8ClampedArray,
* otherwise `RGBA_INTEGER`.
* @param {PIXI.TYPES} [options.type] - The type is not given, the type is inferred from the
* type of the buffer. Maps Float32Array to `FLOAT`, Int32Array to `INT`, Uint32Array to
* `UNSIGNED_INT`, Int16Array to `SHORT`, Uint16Array to `UNSIGNED_SHORT`, Int8Array to `BYTE`,
* Uint8Array/Uint8ClampedArray to `UNSIGNED_BYTE`.
* @param {PIXI.ALPHA_MODES} [options.alphaMode=PIXI.ALPHA_MODES.NPM]
* @param {PIXI.SCALE_MODES} [options.scaleMode=PIXI.SCALE_MODES.NEAREST]
* @returns - The resulting new BaseTexture
*/
static fromBuffer(buffer, width, height, options) {
return new Texture(BaseTexture.BaseTexture.fromBuffer(buffer, width, height, options));
}
/**
* Create a texture from a source and add to the cache.
* @param {HTMLImageElement|HTMLVideoElement|ImageBitmap|PIXI.ICanvas|string} source - The input source.
* @param imageUrl - File name of texture, for cache and resolving resolution.
* @param name - Human readable name for the texture cache. If no name is
* specified, only `imageUrl` will be used as the cache ID.
* @param options
* @returns - Output texture
*/
static fromLoader(source, imageUrl, name, options) {
const baseTexture = new BaseTexture.BaseTexture(source, Object.assign({
scaleMode: BaseTexture.BaseTexture.defaultOptions.scaleMode,
resolution: utils.getResolutionOfUrl(imageUrl)
}, options)), { resource } = baseTexture;
resource instanceof ImageResource.ImageResource && (resource.url = imageUrl);
const texture = new Texture(baseTexture);
return name || (name = imageUrl), BaseTexture.BaseTexture.addToCache(texture.baseTexture, name), Texture.addToCache(texture, name), name !== imageUrl && (BaseTexture.BaseTexture.addToCache(texture.baseTexture, imageUrl), Texture.addToCache(texture, imageUrl)), texture.baseTexture.valid ? Promise.resolve(texture) : new Promise((resolve) => {
texture.baseTexture.once("loaded", () => resolve(texture));
});
}
/**
* Adds a Texture to the global TextureCache. This cache is shared across the whole PIXI object.
* @param texture - The Texture to add to the cache.
* @param id - The id that the Texture will be stored against.
*/
static addToCache(texture, id) {
id && (texture.textureCacheIds.includes(id) || texture.textureCacheIds.push(id), utils.TextureCache[id] && utils.TextureCache[id] !== texture && console.warn(`Texture added to the cache with an id [${id}] that already had an entry`), utils.TextureCache[id] = texture);
}
/**
* Remove a Texture from the global TextureCache.
* @param texture - id of a Texture to be removed, or a Texture instance itself
* @returns - The Texture that was removed
*/
static removeFromCache(texture) {
if (typeof texture == "string") {
const textureFromCache = utils.TextureCache[texture];
if (textureFromCache) {
const index = textureFromCache.textureCacheIds.indexOf(texture);
return index > -1 && textureFromCache.textureCacheIds.splice(index, 1), delete utils.TextureCache[texture], textureFromCache;
}
} else if (texture?.textureCacheIds) {
for (let i = 0; i < texture.textureCacheIds.length; ++i)
utils.TextureCache[texture.textureCacheIds[i]] === texture && delete utils.TextureCache[texture.textureCacheIds[i]];
return texture.textureCacheIds.length = 0, texture;
}
return null;
}
/**
* Returns resolution of baseTexture
* @readonly
*/
get resolution() {
return this.baseTexture.resolution;
}
/**
* The frame specifies the region of the base texture that this texture uses.
* Please call `updateUvs()` after you change coordinates of `frame` manually.
*/
get frame() {
return this._frame;
}
set frame(frame) {
this._frame = frame, this.noFrame = !1;
const { x, y, width, height } = frame, xNotFit = x + width > this.baseTexture.width, yNotFit = y + height > this.baseTexture.height;
if (xNotFit || yNotFit) {
const relationship = xNotFit && yNotFit ? "and" : "or", errorX = `X: ${x} + ${width} = ${x + width} > ${this.baseTexture.width}`, errorY = `Y: ${y} + ${height} = ${y + height} > ${this.baseTexture.height}`;
throw new Error(`Texture Error: frame does not fit inside the base Texture dimensions: ${errorX} ${relationship} ${errorY}`);
}
this.valid = width && height && this.baseTexture.valid, !this.trim && !this.rotate && (this.orig = frame), this.valid && this.updateUvs();
}
/**
* Indicates whether the texture is rotated inside the atlas
* set to 2 to compensate for texture packer rotation
* set to 6 to compensate for spine packer rotation
* can be used to rotate or mirror sprites
* See {@link PIXI.groupD8} for explanation
*/
get rotate() {
return this._rotate;
}
set rotate(rotate) {
this._rotate = rotate, this.valid && this.updateUvs();
}
/** The width of the Texture in pixels. */
get width() {
return this.orig.width;
}
/** The height of the Texture in pixels. */
get height() {
return this.orig.height;
}
/** Utility function for BaseTexture|Texture cast. */
castToBaseTexture() {
return this.baseTexture;
}
/** An empty texture, used often to not have to create multiple empty textures. Can not be destroyed. */
static get EMPTY() {
return Texture._EMPTY || (Texture._EMPTY = new Texture(new BaseTexture.BaseTexture()), removeAllHandlers(Texture._EMPTY), removeAllHandlers(Texture._EMPTY.baseTexture)), Texture._EMPTY;
}
/** A white texture of 16x16 size, used for graphics and other things Can not be destroyed. */
static get WHITE() {
if (!Texture._WHITE) {
const canvas = settings.settings.ADAPTER.createCanvas(16, 16), context = canvas.getContext("2d");
canvas.width = 16, canvas.height = 16, context.fillStyle = "white", context.fillRect(0, 0, 16, 16), Texture._WHITE = new Texture(BaseTexture.BaseTexture.from(canvas)), removeAllHandlers(Texture._WHITE), removeAllHandlers(Texture._WHITE.baseTexture);
}
return Texture._WHITE;
}
}
exports.Texture = Texture;
//# sourceMappingURL=Texture.js.map