From d665d89c5f35667aa8c1be727426ab8991ac4e99 Mon Sep 17 00:00:00 2001 From: Travis Martin Date: Sun, 8 Oct 2023 00:47:29 -0500 Subject: [PATCH] `colorMask`. --- package.json | 1 + src/constants/constants.ts | 8 ++++++++ src/core/Context.ts | 35 ++++++++++++++++++++++++++++++++++- src/index.ts | 1 + src/types/ColorMask.ts | 14 ++++++++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/types/ColorMask.ts diff --git a/package.json b/package.json index a6e1daad..7215cfd1 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "#BlendFunctionSet": "./dist/types/BlendFunctionSet.js", "#Canvas": "./dist/types/Canvas.js", "#Color": "./dist/types/Color.js", + "#ColorMask": "./dist/types/ColorMask.js", "#FloatTypedArray": "./dist/types/FloatTypedArray.js", "#IntTypedArray": "./dist/types/IntTypedArray.js", "#TypedArray": "./dist/types/TypedArray.js", diff --git a/src/constants/constants.ts b/src/constants/constants.ts index 6d6898ab..7580e7c0 100644 --- a/src/constants/constants.ts +++ b/src/constants/constants.ts @@ -235,3 +235,11 @@ export const DEPTH_BUFFER_BIT = 0x00000100; * @internal */ export const STENCIL_BUFFER_BIT = 0x00000400; + +/** + * The mask that specifies which components to enable or disable when + * rendering to a framebuffer. + * @see [`getParameter`](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getParameter) + * @internal + */ +export const COLOR_WRITEMASK = 0x0c23; diff --git a/src/core/Context.ts b/src/core/Context.ts index 65c9a236..aec33902 100644 --- a/src/core/Context.ts +++ b/src/core/Context.ts @@ -13,7 +13,8 @@ import { STENCIL_CLEAR_VALUE, COLOR_BUFFER_BIT, DEPTH_BUFFER_BIT, - STENCIL_BUFFER_BIT + STENCIL_BUFFER_BIT, + COLOR_WRITEMASK } from "#constants"; import ApiInterface from "#ApiInterface"; import type { Canvas } from "#Canvas"; @@ -27,6 +28,7 @@ import type BlendFunctionFullSet from "#BlendFunctionFullSet"; import type BlendFunction from "#BlendFunction"; import ErrorCode from "#ErrorCode"; import WebglError from "#WebglError"; +import type ColorMask from "#ColorMask"; /** * A WebGL2 rendering context. @@ -541,4 +543,35 @@ export default class Context extends ApiInterface { this.gl.clearStencil(value); this.clearStencilCache = value; } + + /** + * The mask that specifies which components to enable or disable when + * rendering to a framebuffer. + * @see [`colorMask`](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/colorMask) + * @internal + */ + private colorMaskCache?: ColorMask; + + /** + * The mask that specifies which components to enable or disable when + * rendering to a framebuffer. + * @see [`colorMask`](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/colorMask) + */ + public get colorMask(): ColorMask { + if (typeof this.colorMaskCache == "undefined") { + this.colorMaskCache = this.gl.getParameter(COLOR_WRITEMASK); + } + + return this.colorMaskCache!; + } + + /** + * The mask that specifies which components to enable or disable when + * rendering to a framebuffer. + * @see [`colorMask`](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/colorMask) + */ + public set colorMask(value: ColorMask) { + this.gl.colorMask(value[0], value[1], value[2], value[3]); + this.colorMaskCache = value; + } } diff --git a/src/index.ts b/src/index.ts index 1fd27ef5..e66cb986 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,7 @@ export type { default as BlendFunctionFullSet } from "#BlendFunctionFullSet"; export type { default as BlendFunctionSet } from "#BlendFunctionSet"; export type { Canvas } from "#Canvas"; export type { default as Color } from "#Color"; +export type { default as ColorMask } from "#ColorMask"; export type { FloatTypedArray } from "#FloatTypedArray"; export type { TypedArray } from "#TypedArray"; export type { IntTypedArray } from "#IntTypedArray"; diff --git a/src/types/ColorMask.ts b/src/types/ColorMask.ts new file mode 100644 index 00000000..3f220f0f --- /dev/null +++ b/src/types/ColorMask.ts @@ -0,0 +1,14 @@ +/** A color mask. */ +export default interface ColorMask { + /** Whether to write to the red component. */ + 0: boolean; + + /** Whether to write to the green component. */ + 1: boolean; + + /** Whether to write to the blue component. */ + 2: boolean; + + /** Whether to write to the alpha component. */ + 3: boolean; +}