import { hex2rgb } from '../../util/math.js'; import type { Graphics } from '../graphics.js'; import type { GraphicsData } from './graphics_data.js'; import type { RenderSession } from '../render_session.js'; export class WebGLStencilManager { public gl!: WebGLRenderingContext; public stencilStack!: GraphicsData[]; public reverse!: boolean; public count!: number; /** * Initializes the stencil manager. */ public constructor() { this.stencilStack = []; this.reverse = true; this.count = 0; } /** * Binds the stencil buffer for rendering. * @param {WebGLRenderingContext} gl - The WebGL rendering context. */ public setContext(gl: WebGLRenderingContext): void { this.gl = gl; } /** * Sets up the stencil buffer for rendering. */ public destroy(): void { this.stencilStack = []; } /** * Renders the stencil buffer for graphics. * @param {Graphics} graphics - The graphics object to render. * @param {GraphicsData} webGLData - The WebGL graphics data. * @param {object} renderSession - The rendering session. */ public pushStencil(graphics: Graphics, webGLData: GraphicsData, renderSession: RenderSession): void { const { gl } = renderSession; this.bindGraphics(graphics, webGLData, renderSession); if (this.stencilStack.length === 0) { gl.enable(gl.STENCIL_TEST); gl.clear(gl.STENCIL_BUFFER_BIT); this.reverse = true; this.count = 0; } this.stencilStack.push(webGLData); const level = this.count; gl.colorMask(false, false, false, false); gl.stencilFunc(gl.ALWAYS, 0, 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.INVERT); // draw the triangle strip! if (webGLData.mode === 1) { gl.drawElements(gl.TRIANGLE_FAN, webGLData.indices.length - 4, gl.UNSIGNED_SHORT, 0); if (this.reverse) { gl.stencilFunc(gl.EQUAL, 0xff - level, 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR); } else { gl.stencilFunc(gl.EQUAL, level, 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR); } // draw a quad to increment.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, (webGLData.indices.length - 4) * 2); if (this.reverse) { gl.stencilFunc(gl.EQUAL, 0xff - (level + 1), 0xff); } else { gl.stencilFunc(gl.EQUAL, level + 1, 0xff); } this.reverse = !this.reverse; } else { if (!this.reverse) { gl.stencilFunc(gl.EQUAL, 0xff - level, 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR); } else { gl.stencilFunc(gl.EQUAL, level, 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR); } gl.drawElements(gl.TRIANGLE_STRIP, webGLData.indices.length, gl.UNSIGNED_SHORT, 0); if (!this.reverse) { gl.stencilFunc(gl.EQUAL, 0xff - (level + 1), 0xff); } else { gl.stencilFunc(gl.EQUAL, level + 1, 0xff); } } gl.colorMask(true, true, true, true); gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP); this.count += 1; } /** * Renders the stencil buffer for graphics. * @param {Graphics} graphics - The graphics object to render. * @param {GraphicsData} webGLData - The WebGL graphics data. * @param {object} renderSession - The rendering session. */ public bindGraphics(graphics: Graphics, webGLData: GraphicsData, renderSession: RenderSession): void { // if(this._currentGraphics === graphics)return; // this._currentGraphics = graphics; const { gl } = renderSession; // bind the graphics object.. const { projection } = renderSession; const { offset } = renderSession; let shader; // = renderSession.shaderManager.primitiveShader; if (webGLData.mode === 1) { shader = renderSession.shaderManager.complexPrimitiveShader; if (!shader) { return; } renderSession.shaderManager.setShader(shader); gl.uniform1f(shader.flipY, renderSession.flipY); gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform3fv(shader.color, webGLData.color); gl.uniform1f(shader.alpha, graphics.worldAlpha * webGLData.alpha); gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, 4 * 2, 0); // now do the rest.. // set the index buffer! gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, webGLData.indexBuffer); } else { // renderSession.shaderManager.activatePrimitiveShader(); shader = renderSession.shaderManager.primitiveShader; if (!shader) { return; } renderSession.shaderManager.setShader(shader); gl.uniformMatrix3fv(shader.translationMatrix, false, graphics.worldTransform.toArray(true)); gl.uniform1f(shader.flipY, renderSession.flipY); gl.uniform2f(shader.projectionVector, projection.x, -projection.y); gl.uniform2f(shader.offsetVector, -offset.x, -offset.y); gl.uniform3fv(shader.tintColor, hex2rgb(graphics.tint)); gl.uniform1f(shader.alpha, graphics.worldAlpha); gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer); gl.vertexAttribPointer(shader.aVertexPosition, 2, gl.FLOAT, false, 4 * 6, 0); gl.vertexAttribPointer(shader.colorAttribute, 4, gl.FLOAT, false, 4 * 6, 2 * 4); // set the index buffer! gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, webGLData.indexBuffer); } } /** * Renders the stencil buffer for graphics. * @param {Graphics} graphics - The graphics object to render. * @param {GraphicsData} webGLData - The WebGL graphics data. * @param {object} renderSession - The rendering session. */ public popStencil(graphics: Graphics, webGLData: GraphicsData, renderSession: RenderSession): void { const { gl } = renderSession; this.stencilStack.pop(); this.count -= 1; if (this.stencilStack.length === 0) { // the stack is empty! gl.disable(gl.STENCIL_TEST); } else { const level = this.count; this.bindGraphics(graphics, webGLData, renderSession); gl.colorMask(false, false, false, false); if (webGLData.mode === 1) { this.reverse = !this.reverse; if (this.reverse) { gl.stencilFunc(gl.EQUAL, 0xff - (level + 1), 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR); } else { gl.stencilFunc(gl.EQUAL, level + 1, 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR); } // draw a quad to increment.. gl.drawElements(gl.TRIANGLE_FAN, 4, gl.UNSIGNED_SHORT, (webGLData.indices.length - 4) * 2); gl.stencilFunc(gl.ALWAYS, 0, 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.INVERT); // draw the triangle strip! gl.drawElements(gl.TRIANGLE_FAN, webGLData.indices.length - 4, gl.UNSIGNED_SHORT, 0); if (!this.reverse) { gl.stencilFunc(gl.EQUAL, 0xff - level, 0xff); } else { gl.stencilFunc(gl.EQUAL, level, 0xff); } } else { if (!this.reverse) { gl.stencilFunc(gl.EQUAL, 0xff - (level + 1), 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR); } else { gl.stencilFunc(gl.EQUAL, level + 1, 0xff); gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR); } gl.drawElements(gl.TRIANGLE_STRIP, webGLData.indices.length, gl.UNSIGNED_SHORT, 0); if (!this.reverse) { gl.stencilFunc(gl.EQUAL, 0xff - level, 0xff); } else { gl.stencilFunc(gl.EQUAL, level, 0xff); } } gl.colorMask(true, true, true, true); gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP); } } }