大网数据广告活动
Web 标准与前端开发

WebGLRenderingContext: stencilFunc() method

· 52CE 我爱测

The **`WebGLRenderingContext.stencilFunc()`** method of the WebGL API sets the front and back function and reference value for stencil testing.

WebGL

The WebGLRenderingContext.stencilFunc() method of the WebGL API sets the front and back function and reference value for stencil testing.

Stenciling enables and disables drawing on a per-pixel basis. It is typically used in multipass rendering to achieve special effects.

Syntax

stencilFunc(func, ref, mask)

Parameters

  • func

    • : A WebGL_API/Types specifying the test function. The default function is gl.ALWAYS. The possible values are:
      • gl.NEVER: Never pass.
      • gl.LESS: Pass if (ref & mask) < (stencil & mask).
      • gl.EQUAL: Pass if (ref & mask) = (stencil & mask).
      • gl.LEQUAL: Pass if (ref & mask) <= (stencil & mask).
      • gl.GREATER: Pass if (ref & mask) > (stencil & mask).
      • gl.NOTEQUAL: Pass if (ref & mask) !== (stencil & mask).
      • gl.GEQUAL: Pass if (ref & mask) >= (stencil & mask).
      • gl.ALWAYS: Always pass.
  • ref

    • : A WebGL_API/Types specifying the reference value for the stencil test. This value is clamped to the range 0 to 2^n - 1 where n is the number of bitplanes in the stencil buffer. The default value is 0.
  • mask

    • : A WebGL_API/Types specifying a bit-wise mask that is used to AND the reference value and the stored stencil value when the test is done. The default value is all 1.

Return value

None (undefined).

Examples

The stencil testing is disabled by default. To enable or disable stencil testing, use the WebGLRenderingContext.enable and WebGLRenderingContext.disable methods with the argument gl.STENCIL_TEST.

gl.enable(gl.STENCIL_TEST);
gl.stencilFunc(gl.LESS, 0, 0b1110011);

To get the current stencil function, reference value, or other stencil information, query the following constants with WebGLRenderingContext.getParameter.

gl.getParameter(gl.STENCIL_FUNC);
gl.getParameter(gl.STENCIL_VALUE_MASK);
gl.getParameter(gl.STENCIL_REF);
gl.getParameter(gl.STENCIL_BACK_FUNC);
gl.getParameter(gl.STENCIL_BACK_VALUE_MASK);
gl.getParameter(gl.STENCIL_BACK_REF);
gl.getParameter(gl.STENCIL_BITS);

Specifications

Browser compatibility

See also

  • WebGLRenderingContext.stencilFuncSeparate()
  • WebGLRenderingContext.stencilMask()
  • WebGLRenderingContext.stencilMaskSeparate()
  • WebGLRenderingContext.stencilOp()
  • WebGLRenderingContext.stencilOpSeparate()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。