UNPKG

@pixi/core

Version:
271 lines (266 loc) 8.15 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var constants = require('@pixi/constants'); var settings = require('@pixi/settings'); var utils = require('@pixi/utils'); var autoDetectResource = require('./resources/autoDetectResource.js'); var BufferResource = require('./resources/BufferResource.js'); var Resource = require('./resources/Resource.js'); const defaultBufferOptions = { scaleMode: constants.SCALE_MODES.NEAREST, format: constants.FORMATS.RGBA, alphaMode: constants.ALPHA_MODES.NPM }; const _BaseTexture = class extends utils.EventEmitter { constructor(resource = null, options = null) { super(); options = Object.assign({}, _BaseTexture.defaultOptions, options); const { alphaMode, mipmap, anisotropicLevel, scaleMode, width, height, wrapMode, format, type, target, resolution, resourceOptions } = options; if (resource && !(resource instanceof Resource.Resource)) { resource = autoDetectResource.autoDetectResource(resource, resourceOptions); resource.internal = true; } 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 = false; this._refreshPOT(); this._glTextures = {}; this.dirtyId = 0; this.dirtyStyleId = 0; this.cacheId = null; this.valid = width > 0 && height > 0; this.textureCacheIds = []; this.destroyed = false; this.resource = null; this._batchEnabled = 0; this._batchLocation = 0; this.parentTextureArray = null; this.setResource(resource); } get realWidth() { return Math.round(this.width * this.resolution); } get realHeight() { return Math.round(this.height * this.resolution); } get mipmap() { return this._mipmap; } set mipmap(value) { if (this._mipmap !== value) { this._mipmap = value; this.dirtyStyleId++; } } get scaleMode() { return this._scaleMode; } set scaleMode(value) { if (this._scaleMode !== value) { this._scaleMode = value; this.dirtyStyleId++; } } get wrapMode() { return this._wrapMode; } set wrapMode(value) { if (this._wrapMode !== value) { this._wrapMode = value; this.dirtyStyleId++; } } setStyle(scaleMode, mipmap) { let dirty; if (scaleMode !== void 0 && scaleMode !== this.scaleMode) { this.scaleMode = scaleMode; dirty = true; } if (mipmap !== void 0 && mipmap !== this.mipmap) { this.mipmap = mipmap; dirty = true; } if (dirty) { this.dirtyStyleId++; } return this; } setSize(desiredWidth, desiredHeight, resolution) { resolution = resolution || this.resolution; return this.setRealSize(desiredWidth * resolution, desiredHeight * resolution, resolution); } setRealSize(realWidth, realHeight, resolution) { this.resolution = resolution || this.resolution; this.width = Math.round(realWidth) / this.resolution; this.height = Math.round(realHeight) / this.resolution; this._refreshPOT(); this.update(); return this; } _refreshPOT() { this.isPowerOfTwo = utils.isPow2(this.realWidth) && utils.isPow2(this.realHeight); } setResolution(resolution) { const oldResolution = this.resolution; if (oldResolution === resolution) { return this; } this.resolution = resolution; if (this.valid) { this.width = Math.round(this.width * oldResolution) / resolution; this.height = Math.round(this.height * oldResolution) / resolution; this.emit("update", this); } this._refreshPOT(); return this; } setResource(resource) { if (this.resource === resource) { return this; } if (this.resource) { throw new Error("Resource can be set only once"); } resource.bind(this); this.resource = resource; return this; } update() { if (!this.valid) { if (this.width > 0 && this.height > 0) { this.valid = true; this.emit("loaded", this); this.emit("update", this); } } else { this.dirtyId++; this.dirtyStyleId++; this.emit("update", this); } } onError(event) { this.emit("error", this, event); } destroy() { if (this.resource) { this.resource.unbind(this); if (this.resource.internal) { this.resource.destroy(); } this.resource = null; } if (this.cacheId) { delete utils.BaseTextureCache[this.cacheId]; delete utils.TextureCache[this.cacheId]; this.cacheId = null; } this.dispose(); _BaseTexture.removeFromCache(this); this.textureCacheIds = null; this.destroyed = true; } dispose() { this.emit("dispose", this); } castToBaseTexture() { return this; } 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._pixiId) { const prefix = options?.pixiIdPrefix || "pixiid"; source._pixiId = `${prefix}_${utils.uid()}`; } cacheId = source._pixiId; } let baseTexture = utils.BaseTextureCache[cacheId]; if (isFrame && strict && !baseTexture) { throw new Error(`The cacheId "${cacheId}" does not exist in BaseTextureCache.`); } if (!baseTexture) { baseTexture = new _BaseTexture(source, options); baseTexture.cacheId = cacheId; _BaseTexture.addToCache(baseTexture, cacheId); } return baseTexture; } static fromBuffer(buffer, width, height, options) { buffer = buffer || new Float32Array(width * height * 4); const resource = new BufferResource.BufferResource(buffer, { width, height }); const type = buffer instanceof Float32Array ? constants.TYPES.FLOAT : constants.TYPES.UNSIGNED_BYTE; return new _BaseTexture(resource, Object.assign({}, defaultBufferOptions, options || { width, height, type })); } static addToCache(baseTexture, id) { if (id) { if (!baseTexture.textureCacheIds.includes(id)) { baseTexture.textureCacheIds.push(id); } if (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; } } static removeFromCache(baseTexture) { if (typeof baseTexture === "string") { const baseTextureFromCache = utils.BaseTextureCache[baseTexture]; if (baseTextureFromCache) { const index = baseTextureFromCache.textureCacheIds.indexOf(baseTexture); if (index > -1) { baseTextureFromCache.textureCacheIds.splice(index, 1); } delete utils.BaseTextureCache[baseTexture]; return baseTextureFromCache; } } else if (baseTexture?.textureCacheIds) { for (let i = 0; i < baseTexture.textureCacheIds.length; ++i) { delete utils.BaseTextureCache[baseTexture.textureCacheIds[i]]; } baseTexture.textureCacheIds.length = 0; return baseTexture; } return null; } }; let BaseTexture = _BaseTexture; BaseTexture.defaultOptions = { mipmap: constants.MIPMAP_MODES.POW2, anisotropicLevel: 0, scaleMode: constants.SCALE_MODES.LINEAR, wrapMode: constants.WRAP_MODES.CLAMP, alphaMode: constants.ALPHA_MODES.UNPACK, target: constants.TARGETS.TEXTURE_2D, format: constants.FORMATS.RGBA, type: constants.TYPES.UNSIGNED_BYTE }; BaseTexture._globalBatch = 0; exports.BaseTexture = BaseTexture; //# sourceMappingURL=BaseTexture.js.map