Skip to content
bg2 engine

Pipeline

The Pipeline class defines a set of render state parameters (blend equation, blend functions, depth testing, face culling) that can be activated for groups of draw calls. It is used to manage render passes with the same rendering state, such as opaque and transparent object batches.

The base class is abstract; WebGL implementations are created via renderer.factory.pipeline().

import Pipeline, { BlendEquation, BlendFunction } from "bg2e-js/ts/render/Pipeline.ts";

Defines how RGB color results are combined:

import { BlendEquation } from "bg2e-js/ts/render/Pipeline.ts";
const eq = BlendEquation.ADD;
  • ADD (1) — Default. Source + Destination (src + dst)
  • SUBTRACT (2) — Source - Destination (src - dst)
  • REVERSE_SUBTRACT (3) — Destination - Source (dst - src)

Defines blending factors used in calculations:

import { BlendFunction } from "bg2e-js/ts/render/Pipeline.ts";
const func = BlendFunction.ONE;
ConstantValueWebGL EquivalentFormula Factor
NULL0N/ANull (not used)
ZERO1GL.ZERO0.0
ONE2GL.ONE1.0
SRC_COLOR3GL.SRC_COLOR(Rs, Gs, Bs, As) × Cs
ONE_MINUS_SRC_COLOR4GL.ONE_MINUS_SRC_COLOR(1,0,0,0) - Cs
DST_COLOR5GL.DST_COLOR(Rd, Gd, Bd, Ad) × Cs
ONE_MINUS_DST_COLOR6GL.ONE_MINUS_DST_COLOR(1,0,0,0) - Cd
SRC_ALPHA7GL.SRC_ALPHAAs × Cs
ONE_MINUS_SRC_ALPHA8GL.ONE_MINUS_SRC_ALPHA(1,0,0,0) - As × Cs
DST_ALPHA9GL.DST_ALPHAAd × Cs
ONE_MINUS_DST_ALPHA10GL.ONE_MINUS_DST_ALPHA(1,0,0,0) - Ad × Cs

Specifies all blending parameters:

interface BlendState {
enabled: boolean;
blendEquation: BlendEquation;
blendFuncSrc: BlendFunction; // Source RGB function (default: SRC_ALPHA)
blendFuncDst: BlendFunction; // Destination RGB function (default: ONE_MINUS_SRC_ALPHA)
blendFuncSrcAlpha: BlendFunction; // Source Alpha function (default: SRC_ALPHA)
blendFuncDstAlpha: BlendFunction; // Destination Alpha function (default: ONE_MINUS_SRC_ALPHA)
}
interface BlendStateParams {
enabled?: boolean;
blendEquation?: BlendEquation;
blendFuncSrc?: BlendFunction;
blendFuncDst?: BlendFunction;
blendFuncSrcAlpha?: BlendFunction;
blendFuncDstAlpha?: BlendFunction;
}
PropertyTypeDescription
enabledbooleanEnable/disable blending. Default: true.
blendEquationBlendEquationRGB blend equation. Default: ADD.
blendFuncSrcBlendFunctionSource RGB blend function. Default: SRC_ALPHA (0/1).
blendFuncDstBlendFunctionDestination RGB blend function. Default: ONE_MINUS_SRC_ALPHA.
blendFuncSrcAlphaBlendFunctionSource Alpha blend function. Default: SRC_ALPHA (0/1).
blendFuncDstAlphaBlendFunctionDestination Alpha blend function. Default: ONE_MINUS_SRC_ALPHA.
depthTestboolean (read/write)Enable/disable depth testing per pipeline.
cullFaceboolean (read/write)Enable/disable face culling per pipeline.
blendStateBlendState (read)Returns the current complete blend state object.
pipeline.depthTest = true; // Enable depth testing (default)
pipeline.cullFace = false; // Disable face culling for this pipeline

Creates a new pipeline instance. The renderer must implement the factory methods to create pipelines.

setBlendState(params: BlendStateParams = ): void

Section titled “setBlendState(params: BlendStateParams = ): void”

Sets the blend state from optional parameters. All parameters are optional with defaults:

pipeline.setBlendState({ enabled: true }); // Enable blending with default functions
// or fully customized:
pipeline.setBlendState({
enabled: true,
blendEquation: BlendEquation.ADD,
blendFuncSrc: BlendFunction.SRC_ALPHA,
blendFuncDst: BlendFunction.ONE_MINUS_SRC_ALPHA,
blendFuncSrcAlpha: BlendFunction.ONE,
blendFuncDstAlpha: BlendFunction.ONE_MINUS_SRC_ALPHA
});

Prepares the pipeline for activation. This sets up internal WebGL state. Must be called once after configuration and before activate().

Applies the pipeline settings to the WebGL context. Enables/disables blending, depth test, face culling and sets blend equations/functions:

opaquePipeline.activate(); // Activates this pipeline's settings
// ... draw calls ...
transparentPipeline.activate(); // Switch to transparent pipeline

Basic opaque and transparent pipelines:

const opaquePipeline = renderer.factory.pipeline();
opaquePipeline.setBlendState({ enabled: false }); // No blending for opaque objects
opaquePipeline.create();
const transparentPipeline = renderer.factory.pipeline();
transparentPipeline.setBlendState({ enabled: true }); // Blending for transparent objects
transparentPipeline.create();
// In display():
renderer.frameBuffer.clear();
opaquePipeline.activate();
renderStates.forEach(rs => {
if (rs.isLayerEnabled(RenderLayer.OPAQUE_DEFAULT)) {
rs.draw(); // Uses opaquePipeline settings
} else {
transparentPipeline.activate();
rs.draw(); // Uses transparentPipeline settings
}
});