@pixi/core
Version:
Core PixiJS
91 lines (90 loc) • 3.42 kB
JavaScript
import { MSAA_QUALITY, SCALE_MODES, MIPMAP_MODES, FORMATS, TYPES } from "@pixi/constants";
import { Runner } from "@pixi/runner";
import { BaseTexture } from "../textures/BaseTexture.mjs";
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("disposeFramebuffer"), this.multisample = 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(null, {
scaleMode: SCALE_MODES.NEAREST,
resolution: 1,
mipmap: 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(null, {
scaleMode: SCALE_MODES.NEAREST,
resolution: 1,
width: this.width,
height: this.height,
mipmap: MIPMAP_MODES.OFF,
format: FORMATS.DEPTH_COMPONENT,
type: 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);
}
}
export {
Framebuffer
};
//# sourceMappingURL=Framebuffer.mjs.map