@pixi/core
Version:
Core PixiJS
104 lines (99 loc) • 3.45 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var constants = require('@pixi/constants');
var utils = require('@pixi/utils');
var BaseRenderTexture = require('./BaseRenderTexture.js');
var RenderTexture = require('./RenderTexture.js');
class RenderTexturePool {
constructor(textureOptions) {
this.texturePool = {};
this.textureOptions = textureOptions || {};
this.enableFullScreen = false;
this._pixelsWidth = 0;
this._pixelsHeight = 0;
}
createTexture(realWidth, realHeight, multisample = constants.MSAA_QUALITY.NONE) {
const baseRenderTexture = new BaseRenderTexture.BaseRenderTexture(Object.assign({
width: realWidth,
height: realHeight,
resolution: 1,
multisample
}, this.textureOptions));
return new RenderTexture.RenderTexture(baseRenderTexture);
}
getOptimalTexture(minWidth, minHeight, resolution = 1, multisample = constants.MSAA_QUALITY.NONE) {
let key;
minWidth = Math.ceil(minWidth * resolution - 1e-6);
minHeight = Math.ceil(minHeight * resolution - 1e-6);
if (!this.enableFullScreen || minWidth !== this._pixelsWidth || minHeight !== this._pixelsHeight) {
minWidth = utils.nextPow2(minWidth);
minHeight = utils.nextPow2(minHeight);
key = ((minWidth & 65535) << 16 | minHeight & 65535) >>> 0;
if (multisample > 1) {
key += multisample * 4294967296;
}
} else {
key = multisample > 1 ? -multisample : -1;
}
if (!this.texturePool[key]) {
this.texturePool[key] = [];
}
let renderTexture = this.texturePool[key].pop();
if (!renderTexture) {
renderTexture = this.createTexture(minWidth, minHeight, multisample);
}
renderTexture.filterPoolKey = key;
renderTexture.setResolution(resolution);
return renderTexture;
}
getFilterTexture(input, resolution, multisample) {
const filterTexture = this.getOptimalTexture(input.width, input.height, resolution || input.resolution, multisample || constants.MSAA_QUALITY.NONE);
filterTexture.filterFrame = input.filterFrame;
return filterTexture;
}
returnTexture(renderTexture) {
const key = renderTexture.filterPoolKey;
renderTexture.filterFrame = null;
this.texturePool[key].push(renderTexture);
}
returnFilterTexture(renderTexture) {
this.returnTexture(renderTexture);
}
clear(destroyTextures) {
destroyTextures = destroyTextures !== false;
if (destroyTextures) {
for (const i in this.texturePool) {
const textures = this.texturePool[i];
if (textures) {
for (let j = 0; j < textures.length; j++) {
textures[j].destroy(true);
}
}
}
}
this.texturePool = {};
}
setScreenSize(size) {
if (size.width === this._pixelsWidth && size.height === this._pixelsHeight) {
return;
}
this.enableFullScreen = size.width > 0 && size.height > 0;
for (const i in this.texturePool) {
if (!(Number(i) < 0)) {
continue;
}
const textures = this.texturePool[i];
if (textures) {
for (let j = 0; j < textures.length; j++) {
textures[j].destroy(true);
}
}
this.texturePool[i] = [];
}
this._pixelsWidth = size.width;
this._pixelsHeight = size.height;
}
}
RenderTexturePool.SCREEN_KEY = -1;
exports.RenderTexturePool = RenderTexturePool;
//# sourceMappingURL=RenderTexturePool.js.map