Skip to content
bg2 engine

State

The State class provides a wrapper around the WebGL rendering state for the current context. Access it through renderer.state.

import { webgl } from "bg2e-js/ts/render/webgl/index.ts";
const state = renderer.state;
PropertyTypeDescription
viewportVec / (number, number)Set/get the viewport. Use a 2-element Vec or array [width, height].
maxViewportDimsVecMaximum supported viewport dimensions (read-only) from WebGL.
clearColorVec(4)Set/get the clear color (RGBA, values 0-1). Write access calls gl.clearColor().
clearDepthnumberSet/get the default depth clear value (0-1).
clearStencilnumberSet/get the default stencil clear value (integer).
depthMaskbooleanSet/get whether depth buffer writes are enabled.
frontFacenumberSet/get the front face orientation. Use constants from this class: cw, ccw.
cullFacenumberSet/get the culling mode: gl.FRONT, gl.BACK, or gl.FRONT_AND_BACK.
depthTestEnabledbooleanEnable/disable the depth test. Recommended to set to true for 3D scenes.
cullFaceEnabledbooleanEnable/disable back-face culling. Default is true.
blendEnabledbooleanEnable/disable blending. Note: for per-pipeline blend configuration, use Pipeline instead — this is the global state toggle.
shaderProgramShaderProgram | nullSet/get the currently active shader program in WebGL. Setting this calls useProgram().
CWnumberWebGL constant for clockwise winding order.
CCWnumberWebGL constant for counter-clockwise winding order.
FRONTnumberWebGL constant for front face culling mode.
BACKnumberWebGL constant for back face culling mode.
FRONT_AND_BACKnumberWebGL constant for both faces culling mode.

Clear the color, depth and/or stencil buffers:

state.clear({ color = true, depth = true, stencil = false }: { color?: boolean; depth?: boolean; stencil?: boolean }): void

Equivalent to calling renderer.frameBuffer.clear(). Use it when you need all three buffers cleared:

state.clear();
// or selectively:
state.clear({ color: true, depth: false });
  • renderer — Returns the Renderer object this state belongs to.
  • gl — Returns the webGLRenderingContext directly. This gives access to raw WebGL functions from custom shaders.