UNPKG

5.08 kBJavaScriptView Raw
1"use strict";
2var constants = require("@pixi/constants"), utils = require("@pixi/utils"), BaseRenderTexture = require("./BaseRenderTexture.js"), RenderTexture = require("./RenderTexture.js");
3class RenderTexturePool {
4 /**
5 * @param textureOptions - options that will be passed to BaseRenderTexture constructor
6 * @param {PIXI.SCALE_MODES} [textureOptions.scaleMode] - See {@link PIXI.SCALE_MODES} for possible values.
7 */
8 constructor(textureOptions) {
9 this.texturePool = {}, this.textureOptions = textureOptions || {}, this.enableFullScreen = !1, this._pixelsWidth = 0, this._pixelsHeight = 0;
10 }
11 /**
12 * Creates texture with params that were specified in pool constructor.
13 * @param realWidth - Width of texture in pixels.
14 * @param realHeight - Height of texture in pixels.
15 * @param multisample - Number of samples of the framebuffer.
16 */
17 createTexture(realWidth, realHeight, multisample = constants.MSAA_QUALITY.NONE) {
18 const baseRenderTexture = new BaseRenderTexture.BaseRenderTexture(Object.assign({
19 width: realWidth,
20 height: realHeight,
21 resolution: 1,
22 multisample
23 }, this.textureOptions));
24 return new RenderTexture.RenderTexture(baseRenderTexture);
25 }
26 /**
27 * Gets a Power-of-Two render texture or fullScreen texture
28 * @param minWidth - The minimum width of the render texture.
29 * @param minHeight - The minimum height of the render texture.
30 * @param resolution - The resolution of the render texture.
31 * @param multisample - Number of samples of the render texture.
32 * @returns The new render texture.
33 */
34 getOptimalTexture(minWidth, minHeight, resolution = 1, multisample = constants.MSAA_QUALITY.NONE) {
35 let key;
36 minWidth = Math.max(Math.ceil(minWidth * resolution - 1e-6), 1), minHeight = Math.max(Math.ceil(minHeight * resolution - 1e-6), 1), !this.enableFullScreen || minWidth !== this._pixelsWidth || minHeight !== this._pixelsHeight ? (minWidth = utils.nextPow2(minWidth), minHeight = utils.nextPow2(minHeight), key = ((minWidth & 65535) << 16 | minHeight & 65535) >>> 0, multisample > 1 && (key += multisample * 4294967296)) : key = multisample > 1 ? -multisample : -1, this.texturePool[key] || (this.texturePool[key] = []);
37 let renderTexture = this.texturePool[key].pop();
38 return renderTexture || (renderTexture = this.createTexture(minWidth, minHeight, multisample)), renderTexture.filterPoolKey = key, renderTexture.setResolution(resolution), renderTexture;
39 }
40 /**
41 * Gets extra texture of the same size as input renderTexture
42 *
43 * `getFilterTexture(input, 0.5)` or `getFilterTexture(0.5, input)`
44 * @param input - renderTexture from which size and resolution will be copied
45 * @param resolution - override resolution of the renderTexture
46 * It overrides, it does not multiply
47 * @param multisample - number of samples of the renderTexture
48 */
49 getFilterTexture(input, resolution, multisample) {
50 const filterTexture = this.getOptimalTexture(
51 input.width,
52 input.height,
53 resolution || input.resolution,
54 multisample || constants.MSAA_QUALITY.NONE
55 );
56 return filterTexture.filterFrame = input.filterFrame, filterTexture;
57 }
58 /**
59 * Place a render texture back into the pool.
60 * @param renderTexture - The renderTexture to free
61 */
62 returnTexture(renderTexture) {
63 const key = renderTexture.filterPoolKey;
64 renderTexture.filterFrame = null, this.texturePool[key].push(renderTexture);
65 }
66 /**
67 * Alias for returnTexture, to be compliant with FilterSystem interface.
68 * @param renderTexture - The renderTexture to free
69 */
70 returnFilterTexture(renderTexture) {
71 this.returnTexture(renderTexture);
72 }
73 /**
74 * Clears the pool.
75 * @param destroyTextures - Destroy all stored textures.
76 */
77 clear(destroyTextures) {
78 if (destroyTextures = destroyTextures !== !1, destroyTextures)
79 for (const i in this.texturePool) {
80 const textures = this.texturePool[i];
81 if (textures)
82 for (let j = 0; j < textures.length; j++)
83 textures[j].destroy(!0);
84 }
85 this.texturePool = {};
86 }
87 /**
88 * If screen size was changed, drops all screen-sized textures,
89 * sets new screen size, sets `enableFullScreen` to true
90 *
91 * Size is measured in pixels, `renderer.view` can be passed here, not `renderer.screen`
92 * @param size - Initial size of screen.
93 */
94 setScreenSize(size) {
95 if (!(size.width === this._pixelsWidth && size.height === this._pixelsHeight)) {
96 this.enableFullScreen = size.width > 0 && size.height > 0;
97 for (const i in this.texturePool) {
98 if (!(Number(i) < 0))
99 continue;
100 const textures = this.texturePool[i];
101 if (textures)
102 for (let j = 0; j < textures.length; j++)
103 textures[j].destroy(!0);
104 this.texturePool[i] = [];
105 }
106 this._pixelsWidth = size.width, this._pixelsHeight = size.height;
107 }
108 }
109}
110RenderTexturePool.SCREEN_KEY = -1;
111exports.RenderTexturePool = RenderTexturePool;
112//# sourceMappingURL=RenderTexturePool.js.map