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