UNPKG

3.42 kBJavaScriptView Raw
1import { MSAA_QUALITY, SCALE_MODES, MIPMAP_MODES, FORMATS, TYPES } from "@pixi/constants";
2import { Runner } from "@pixi/runner";
3import { BaseTexture } from "../textures/BaseTexture.mjs";
4class Framebuffer {
5 /**
6 * @param width - Width of the frame buffer
7 * @param height - Height of the frame buffer
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 * Reference to the colorTexture.
16 * @readonly
17 */
18 get colorTexture() {
19 return this.colorTextures[0];
20 }
21 /**
22 * Add texture to the colorTexture array.
23 * @param index - Index of the array to add the texture to
24 * @param texture - Texture to add to the array
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 * Add a depth texture to the frame buffer.
37 * @param texture - Texture to add.
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 /** Enable depth on the frame buffer. */
51 enableDepth() {
52 return this.depth = !0, this.dirtyId++, this.dirtyFormat++, this;
53 }
54 /** Enable stencil on the frame buffer. */
55 enableStencil() {
56 return this.stencil = !0, this.dirtyId++, this.dirtyFormat++, this;
57 }
58 /**
59 * Resize the frame buffer
60 * @param width - Width of the frame buffer to resize to
61 * @param height - Height of the frame buffer to resize to
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 /** Disposes WebGL resources that are connected to this geometry. */
79 dispose() {
80 this.disposeRunner.emit(this, !1);
81 }
82 /** Destroys and removes the depth texture added to this framebuffer. */
83 destroyDepthTexture() {
84 this.depthTexture && (this.depthTexture.destroy(), this.depthTexture = null, ++this.dirtyId, ++this.dirtyFormat);
85 }
86}
87export {
88 Framebuffer
89};
90//# sourceMappingURL=Framebuffer.mjs.map