1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var constants = require('@pixi/constants');
|
6 | var utils = require('@pixi/utils');
|
7 | var BaseRenderTexture = require('./BaseRenderTexture.js');
|
8 | var RenderTexture = require('./RenderTexture.js');
|
9 |
|
10 | class RenderTexturePool {
|
11 | constructor(textureOptions) {
|
12 | this.texturePool = {};
|
13 | this.textureOptions = textureOptions || {};
|
14 | this.enableFullScreen = false;
|
15 | this._pixelsWidth = 0;
|
16 | this._pixelsHeight = 0;
|
17 | }
|
18 | createTexture(realWidth, realHeight, multisample = constants.MSAA_QUALITY.NONE) {
|
19 | const baseRenderTexture = new BaseRenderTexture.BaseRenderTexture(Object.assign({
|
20 | width: realWidth,
|
21 | height: realHeight,
|
22 | resolution: 1,
|
23 | multisample
|
24 | }, this.textureOptions));
|
25 | return new RenderTexture.RenderTexture(baseRenderTexture);
|
26 | }
|
27 | getOptimalTexture(minWidth, minHeight, resolution = 1, multisample = constants.MSAA_QUALITY.NONE) {
|
28 | let key;
|
29 | minWidth = Math.ceil(minWidth * resolution - 1e-6);
|
30 | minHeight = Math.ceil(minHeight * resolution - 1e-6);
|
31 | if (!this.enableFullScreen || minWidth !== this._pixelsWidth || minHeight !== this._pixelsHeight) {
|
32 | minWidth = utils.nextPow2(minWidth);
|
33 | minHeight = utils.nextPow2(minHeight);
|
34 | key = ((minWidth & 65535) << 16 | minHeight & 65535) >>> 0;
|
35 | if (multisample > 1) {
|
36 | key += multisample * 4294967296;
|
37 | }
|
38 | } else {
|
39 | key = multisample > 1 ? -multisample : -1;
|
40 | }
|
41 | if (!this.texturePool[key]) {
|
42 | this.texturePool[key] = [];
|
43 | }
|
44 | let renderTexture = this.texturePool[key].pop();
|
45 | if (!renderTexture) {
|
46 | renderTexture = this.createTexture(minWidth, minHeight, multisample);
|
47 | }
|
48 | renderTexture.filterPoolKey = key;
|
49 | renderTexture.setResolution(resolution);
|
50 | return renderTexture;
|
51 | }
|
52 | getFilterTexture(input, resolution, multisample) {
|
53 | const filterTexture = this.getOptimalTexture(input.width, input.height, resolution || input.resolution, multisample || constants.MSAA_QUALITY.NONE);
|
54 | filterTexture.filterFrame = input.filterFrame;
|
55 | return filterTexture;
|
56 | }
|
57 | returnTexture(renderTexture) {
|
58 | const key = renderTexture.filterPoolKey;
|
59 | renderTexture.filterFrame = null;
|
60 | this.texturePool[key].push(renderTexture);
|
61 | }
|
62 | returnFilterTexture(renderTexture) {
|
63 | this.returnTexture(renderTexture);
|
64 | }
|
65 | clear(destroyTextures) {
|
66 | destroyTextures = destroyTextures !== false;
|
67 | if (destroyTextures) {
|
68 | for (const i in this.texturePool) {
|
69 | const textures = this.texturePool[i];
|
70 | if (textures) {
|
71 | for (let j = 0; j < textures.length; j++) {
|
72 | textures[j].destroy(true);
|
73 | }
|
74 | }
|
75 | }
|
76 | }
|
77 | this.texturePool = {};
|
78 | }
|
79 | setScreenSize(size) {
|
80 | if (size.width === this._pixelsWidth && size.height === this._pixelsHeight) {
|
81 | return;
|
82 | }
|
83 | this.enableFullScreen = size.width > 0 && size.height > 0;
|
84 | for (const i in this.texturePool) {
|
85 | if (!(Number(i) < 0)) {
|
86 | continue;
|
87 | }
|
88 | const textures = this.texturePool[i];
|
89 | if (textures) {
|
90 | for (let j = 0; j < textures.length; j++) {
|
91 | textures[j].destroy(true);
|
92 | }
|
93 | }
|
94 | this.texturePool[i] = [];
|
95 | }
|
96 | this._pixelsWidth = size.width;
|
97 | this._pixelsHeight = size.height;
|
98 | }
|
99 | }
|
100 | RenderTexturePool.SCREEN_KEY = -1;
|
101 |
|
102 | exports.RenderTexturePool = RenderTexturePool;
|
103 |
|