@pixi/core
Version:
Core PixiJS
267 lines (264 loc) • 7.92 kB
JavaScript
import { SCALE_MODES, FORMATS, ALPHA_MODES, TYPES, MIPMAP_MODES, WRAP_MODES, TARGETS } from '@pixi/constants';
import { settings } from '@pixi/settings';
import { EventEmitter, uid, isPow2, BaseTextureCache, TextureCache } from '@pixi/utils';
import { autoDetectResource } from './resources/autoDetectResource.mjs';
import { BufferResource } from './resources/BufferResource.mjs';
import { Resource } from './resources/Resource.mjs';
const defaultBufferOptions = {
scaleMode: SCALE_MODES.NEAREST,
format: FORMATS.RGBA,
alphaMode: ALPHA_MODES.NPM
};
const _BaseTexture = class extends 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 = autoDetectResource(resource, resourceOptions);
resource.internal = true;
}
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 = 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 = isPow2(this.realWidth) && 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 BaseTextureCache[this.cacheId];
delete 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.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}_${uid()}`;
}
cacheId = source._pixiId;
}
let baseTexture = 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(buffer, { width, height });
const type = buffer instanceof Float32Array ? TYPES.FLOAT : 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 (BaseTextureCache[id] && BaseTextureCache[id] !== baseTexture) {
console.warn(`BaseTexture added to the cache with an id [${id}] that already had an entry`);
}
BaseTextureCache[id] = baseTexture;
}
}
static removeFromCache(baseTexture) {
if (typeof baseTexture === "string") {
const baseTextureFromCache = BaseTextureCache[baseTexture];
if (baseTextureFromCache) {
const index = baseTextureFromCache.textureCacheIds.indexOf(baseTexture);
if (index > -1) {
baseTextureFromCache.textureCacheIds.splice(index, 1);
}
delete BaseTextureCache[baseTexture];
return baseTextureFromCache;
}
} else if (baseTexture?.textureCacheIds) {
for (let i = 0; i < baseTexture.textureCacheIds.length; ++i) {
delete BaseTextureCache[baseTexture.textureCacheIds[i]];
}
baseTexture.textureCacheIds.length = 0;
return baseTexture;
}
return null;
}
};
let BaseTexture = _BaseTexture;
BaseTexture.defaultOptions = {
mipmap: MIPMAP_MODES.POW2,
anisotropicLevel: 0,
scaleMode: SCALE_MODES.LINEAR,
wrapMode: WRAP_MODES.CLAMP,
alphaMode: ALPHA_MODES.UNPACK,
target: TARGETS.TEXTURE_2D,
format: FORMATS.RGBA,
type: TYPES.UNSIGNED_BYTE
};
BaseTexture._globalBatch = 0;
export { BaseTexture };
//# sourceMappingURL=BaseTexture.mjs.map