@pixi/core
Version:
Core PixiJS
88 lines (87 loc) • 3.49 kB
JavaScript
"use strict";
var constants = require("@pixi/constants"), runner = require("@pixi/runner"), BaseTexture = require("../textures/BaseTexture.js");
class Framebuffer {
/**
* @param width - Width of the frame buffer
* @param height - Height of the frame buffer
*/
constructor(width, height) {
if (this.width = Math.round(width), this.height = Math.round(height), !this.width || !this.height)
throw new Error("Framebuffer width or height is zero");
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;
}
/**
* Reference to the colorTexture.
* @readonly
*/
get colorTexture() {
return this.colorTextures[0];
}
/**
* Add texture to the colorTexture array.
* @param index - Index of the array to add the texture to
* @param texture - Texture to add to the array
*/
addColorTexture(index = 0, texture) {
return this.colorTextures[index] = texture || new BaseTexture.BaseTexture(null, {
scaleMode: constants.SCALE_MODES.NEAREST,
resolution: 1,
mipmap: constants.MIPMAP_MODES.OFF,
width: this.width,
height: this.height
}), this.dirtyId++, this.dirtyFormat++, this;
}
/**
* Add a depth texture to the frame buffer.
* @param texture - Texture to add.
*/
addDepthTexture(texture) {
return this.depthTexture = texture || new BaseTexture.BaseTexture(null, {
scaleMode: constants.SCALE_MODES.NEAREST,
resolution: 1,
width: this.width,
height: this.height,
mipmap: constants.MIPMAP_MODES.OFF,
format: constants.FORMATS.DEPTH_COMPONENT,
type: constants.TYPES.UNSIGNED_SHORT
}), this.dirtyId++, this.dirtyFormat++, this;
}
/** Enable depth on the frame buffer. */
enableDepth() {
return this.depth = !0, this.dirtyId++, this.dirtyFormat++, this;
}
/** Enable stencil on the frame buffer. */
enableStencil() {
return this.stencil = !0, this.dirtyId++, this.dirtyFormat++, this;
}
/**
* Resize the frame buffer
* @param width - Width of the frame buffer to resize to
* @param height - Height of the frame buffer to resize to
*/
resize(width, height) {
if (width = Math.round(width), height = Math.round(height), !width || !height)
throw new Error("Framebuffer width and height must not be zero");
if (!(width === this.width && height === this.height)) {
this.width = width, this.height = height, this.dirtyId++, this.dirtySize++;
for (let i = 0; i < this.colorTextures.length; i++) {
const texture = this.colorTextures[i], resolution = texture.resolution;
texture.setSize(width / resolution, height / resolution);
}
if (this.depthTexture) {
const resolution = this.depthTexture.resolution;
this.depthTexture.setSize(width / resolution, height / resolution);
}
}
}
/** Disposes WebGL resources that are connected to this geometry. */
dispose() {
this.disposeRunner.emit(this, !1);
}
/** Destroys and removes the depth texture added to this framebuffer. */
destroyDepthTexture() {
this.depthTexture && (this.depthTexture.destroy(), this.depthTexture = null, ++this.dirtyId, ++this.dirtyFormat);
}
}
exports.Framebuffer = Framebuffer;
//# sourceMappingURL=Framebuffer.js.map