UNPKG

3.49 kBJavaScriptView Raw
1"use strict";
2var constants = require("@pixi/constants"), runner = require("@pixi/runner"), BaseTexture = require("../textures/BaseTexture.js");
3class Framebuffer {
4 /**
5 * @param width - Width of the frame buffer
6 * @param height - Height of the frame buffer
7 */
8 constructor(width, height) {
9 if (this.width = Math.round(width), this.height = Math.round(height), !this.width || !this.height)
10 throw new Error("Framebuffer width or height is zero");
11 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.Runner("disposeFramebuffer"), this.multisample = constants.MSAA_QUALITY.NONE;
12 }
13 /**
14 * Reference to the colorTexture.
15 * @readonly
16 */
17 get colorTexture() {
18 return this.colorTextures[0];
19 }
20 /**
21 * Add texture to the colorTexture array.
22 * @param index - Index of the array to add the texture to
23 * @param texture - Texture to add to the array
24 */
25 addColorTexture(index = 0, texture) {
26 return this.colorTextures[index] = texture || new BaseTexture.BaseTexture(null, {
27 scaleMode: constants.SCALE_MODES.NEAREST,
28 resolution: 1,
29 mipmap: constants.MIPMAP_MODES.OFF,
30 width: this.width,
31 height: this.height
32 }), this.dirtyId++, this.dirtyFormat++, this;
33 }
34 /**
35 * Add a depth texture to the frame buffer.
36 * @param texture - Texture to add.
37 */
38 addDepthTexture(texture) {
39 return this.depthTexture = texture || new BaseTexture.BaseTexture(null, {
40 scaleMode: constants.SCALE_MODES.NEAREST,
41 resolution: 1,
42 width: this.width,
43 height: this.height,
44 mipmap: constants.MIPMAP_MODES.OFF,
45 format: constants.FORMATS.DEPTH_COMPONENT,
46 type: constants.TYPES.UNSIGNED_SHORT
47 }), this.dirtyId++, this.dirtyFormat++, this;
48 }
49 /** Enable depth on the frame buffer. */
50 enableDepth() {
51 return this.depth = !0, this.dirtyId++, this.dirtyFormat++, this;
52 }
53 /** Enable stencil on the frame buffer. */
54 enableStencil() {
55 return this.stencil = !0, this.dirtyId++, this.dirtyFormat++, this;
56 }
57 /**
58 * Resize the frame buffer
59 * @param width - Width of the frame buffer to resize to
60 * @param height - Height of the frame buffer to resize to
61 */
62 resize(width, height) {
63 if (width = Math.round(width), height = Math.round(height), !width || !height)
64 throw new Error("Framebuffer width and height must not be zero");
65 if (!(width === this.width && height === this.height)) {
66 this.width = width, this.height = height, this.dirtyId++, this.dirtySize++;
67 for (let i = 0; i < this.colorTextures.length; i++) {
68 const texture = this.colorTextures[i], resolution = texture.resolution;
69 texture.setSize(width / resolution, height / resolution);
70 }
71 if (this.depthTexture) {
72 const resolution = this.depthTexture.resolution;
73 this.depthTexture.setSize(width / resolution, height / resolution);
74 }
75 }
76 }
77 /** Disposes WebGL resources that are connected to this geometry. */
78 dispose() {
79 this.disposeRunner.emit(this, !1);
80 }
81 /** Destroys and removes the depth texture added to this framebuffer. */
82 destroyDepthTexture() {
83 this.depthTexture && (this.depthTexture.destroy(), this.depthTexture = null, ++this.dirtyId, ++this.dirtyFormat);
84 }
85}
86exports.Framebuffer = Framebuffer;
87//# sourceMappingURL=Framebuffer.js.map