1 | import { MSAA_QUALITY, SCALE_MODES, MIPMAP_MODES, FORMATS, TYPES } from '@pixi/constants';
|
2 | import { Runner } from '@pixi/runner';
|
3 | import { BaseTexture } from '../textures/BaseTexture.mjs';
|
4 | import { DepthResource } from '../textures/resources/DepthResource.mjs';
|
5 |
|
6 | class Framebuffer {
|
7 | constructor(width, height) {
|
8 | this.width = Math.round(width || 100);
|
9 | this.height = Math.round(height || 100);
|
10 | this.stencil = false;
|
11 | this.depth = false;
|
12 | this.dirtyId = 0;
|
13 | this.dirtyFormat = 0;
|
14 | this.dirtySize = 0;
|
15 | this.depthTexture = null;
|
16 | this.colorTextures = [];
|
17 | this.glFramebuffers = {};
|
18 | this.disposeRunner = new Runner("disposeFramebuffer");
|
19 | this.multisample = MSAA_QUALITY.NONE;
|
20 | }
|
21 | get colorTexture() {
|
22 | return this.colorTextures[0];
|
23 | }
|
24 | addColorTexture(index = 0, texture) {
|
25 | this.colorTextures[index] = texture || new BaseTexture(null, {
|
26 | scaleMode: SCALE_MODES.NEAREST,
|
27 | resolution: 1,
|
28 | mipmap: MIPMAP_MODES.OFF,
|
29 | width: this.width,
|
30 | height: this.height
|
31 | });
|
32 | this.dirtyId++;
|
33 | this.dirtyFormat++;
|
34 | return this;
|
35 | }
|
36 | addDepthTexture(texture) {
|
37 | this.depthTexture = texture || new BaseTexture(new DepthResource(null, { width: this.width, height: this.height }), {
|
38 | scaleMode: SCALE_MODES.NEAREST,
|
39 | resolution: 1,
|
40 | width: this.width,
|
41 | height: this.height,
|
42 | mipmap: MIPMAP_MODES.OFF,
|
43 | format: FORMATS.DEPTH_COMPONENT,
|
44 | type: TYPES.UNSIGNED_SHORT
|
45 | });
|
46 | this.dirtyId++;
|
47 | this.dirtyFormat++;
|
48 | return this;
|
49 | }
|
50 | enableDepth() {
|
51 | this.depth = true;
|
52 | this.dirtyId++;
|
53 | this.dirtyFormat++;
|
54 | return this;
|
55 | }
|
56 | enableStencil() {
|
57 | this.stencil = true;
|
58 | this.dirtyId++;
|
59 | this.dirtyFormat++;
|
60 | return this;
|
61 | }
|
62 | resize(width, height) {
|
63 | width = Math.round(width);
|
64 | height = Math.round(height);
|
65 | if (width === this.width && height === this.height)
|
66 | return;
|
67 | this.width = width;
|
68 | this.height = height;
|
69 | this.dirtyId++;
|
70 | this.dirtySize++;
|
71 | for (let i = 0; i < this.colorTextures.length; i++) {
|
72 | const texture = this.colorTextures[i];
|
73 | const resolution = texture.resolution;
|
74 | texture.setSize(width / resolution, height / resolution);
|
75 | }
|
76 | if (this.depthTexture) {
|
77 | const resolution = this.depthTexture.resolution;
|
78 | this.depthTexture.setSize(width / resolution, height / resolution);
|
79 | }
|
80 | }
|
81 | dispose() {
|
82 | this.disposeRunner.emit(this, false);
|
83 | }
|
84 | destroyDepthTexture() {
|
85 | if (this.depthTexture) {
|
86 | this.depthTexture.destroy();
|
87 | this.depthTexture = null;
|
88 | ++this.dirtyId;
|
89 | ++this.dirtyFormat;
|
90 | }
|
91 | }
|
92 | }
|
93 |
|
94 | export { Framebuffer };
|
95 |
|