@pixi/core
Version:
Core PixiJS
1 lines • 12.4 kB
Source Map (JSON)
{"version":3,"file":"StateSystem.mjs","sources":["../../src/state/StateSystem.ts"],"sourcesContent":["import { BLEND_MODES } from '@pixi/constants';\nimport { extensions, ExtensionType } from '@pixi/extensions';\nimport { State } from './State';\nimport { mapWebGLBlendModesToPixi } from './utils/mapWebGLBlendModesToPixi';\n\nimport type { ExtensionMetadata } from '@pixi/extensions';\nimport type { IRenderingContext } from '../IRenderer';\nimport type { ISystem } from '../system/ISystem';\n\nconst BLEND = 0;\nconst OFFSET = 1;\nconst CULLING = 2;\nconst DEPTH_TEST = 3;\nconst WINDING = 4;\nconst DEPTH_MASK = 5;\n\n/**\n * System plugin to the renderer to manage WebGL state machines.\n * @memberof PIXI\n */\nexport class StateSystem implements ISystem\n{\n /** @ignore */\n static extension: ExtensionMetadata = {\n type: ExtensionType.RendererSystem,\n name: 'state',\n };\n\n /**\n * State ID\n * @readonly\n */\n public stateId: number;\n\n /**\n * Polygon offset\n * @readonly\n */\n public polygonOffset: number;\n\n /**\n * Blend mode\n * @default PIXI.BLEND_MODES.NONE\n * @readonly\n */\n public blendMode: BLEND_MODES | -1;\n\n /** Whether current blend equation is different */\n protected _blendEq: boolean;\n\n /**\n * GL context\n * @member {WebGLRenderingContext}\n * @readonly\n */\n protected gl: IRenderingContext;\n\n protected blendModes: number[][];\n\n /**\n * Collection of calls\n * @member {Function[]}\n */\n protected readonly map: Array<(value: boolean) => void>;\n\n /**\n * Collection of check calls\n * @member {Function[]}\n */\n protected readonly checks: Array<(system: this, state: State) => void>;\n\n /**\n * Default WebGL State\n * @readonly\n */\n protected defaultState: State;\n\n constructor()\n {\n this.gl = null;\n\n this.stateId = 0;\n this.polygonOffset = 0;\n this.blendMode = BLEND_MODES.NONE;\n\n this._blendEq = false;\n\n // map functions for when we set state..\n this.map = [];\n this.map[BLEND] = this.setBlend;\n this.map[OFFSET] = this.setOffset;\n this.map[CULLING] = this.setCullFace;\n this.map[DEPTH_TEST] = this.setDepthTest;\n this.map[WINDING] = this.setFrontFace;\n this.map[DEPTH_MASK] = this.setDepthMask;\n\n this.checks = [];\n\n this.defaultState = new State();\n this.defaultState.blend = true;\n }\n\n contextChange(gl: IRenderingContext): void\n {\n this.gl = gl;\n\n this.blendModes = mapWebGLBlendModesToPixi(gl);\n\n this.set(this.defaultState);\n\n this.reset();\n }\n\n /**\n * Sets the current state\n * @param {*} state - The state to set.\n */\n set(state: State): void\n {\n state = state || this.defaultState;\n\n // TODO maybe to an object check? ( this.state === state )?\n if (this.stateId !== state.data)\n {\n let diff = this.stateId ^ state.data;\n let i = 0;\n\n // order from least to most common\n while (diff)\n {\n if (diff & 1)\n {\n // state change!\n this.map[i].call(this, !!(state.data & (1 << i)));\n }\n\n diff = diff >> 1;\n i++;\n }\n\n this.stateId = state.data;\n }\n\n // based on the above settings we check for specific modes..\n // for example if blend is active we check and set the blend modes\n // or of polygon offset is active we check the poly depth.\n for (let i = 0; i < this.checks.length; i++)\n {\n this.checks[i](this, state);\n }\n }\n\n /**\n * Sets the state, when previous state is unknown.\n * @param {*} state - The state to set\n */\n forceState(state: State): void\n {\n state = state || this.defaultState;\n for (let i = 0; i < this.map.length; i++)\n {\n this.map[i].call(this, !!(state.data & (1 << i)));\n }\n for (let i = 0; i < this.checks.length; i++)\n {\n this.checks[i](this, state);\n }\n\n this.stateId = state.data;\n }\n\n /**\n * Sets whether to enable or disable blending.\n * @param value - Turn on or off WebGl blending.\n */\n setBlend(value: boolean): void\n {\n this.updateCheck(StateSystem.checkBlendMode, value);\n\n this.gl[value ? 'enable' : 'disable'](this.gl.BLEND);\n }\n\n /**\n * Sets whether to enable or disable polygon offset fill.\n * @param value - Turn on or off webgl polygon offset testing.\n */\n setOffset(value: boolean): void\n {\n this.updateCheck(StateSystem.checkPolygonOffset, value);\n\n this.gl[value ? 'enable' : 'disable'](this.gl.POLYGON_OFFSET_FILL);\n }\n\n /**\n * Sets whether to enable or disable depth test.\n * @param value - Turn on or off webgl depth testing.\n */\n setDepthTest(value: boolean): void\n {\n this.gl[value ? 'enable' : 'disable'](this.gl.DEPTH_TEST);\n }\n\n /**\n * Sets whether to enable or disable depth mask.\n * @param value - Turn on or off webgl depth mask.\n */\n setDepthMask(value: boolean): void\n {\n this.gl.depthMask(value);\n }\n\n /**\n * Sets whether to enable or disable cull face.\n * @param {boolean} value - Turn on or off webgl cull face.\n */\n setCullFace(value: boolean): void\n {\n this.gl[value ? 'enable' : 'disable'](this.gl.CULL_FACE);\n }\n\n /**\n * Sets the gl front face.\n * @param {boolean} value - true is clockwise and false is counter-clockwise\n */\n setFrontFace(value: boolean): void\n {\n this.gl.frontFace(this.gl[value ? 'CW' : 'CCW']);\n }\n\n /**\n * Sets the blend mode.\n * @param {number} value - The blend mode to set to.\n */\n setBlendMode(value: number): void\n {\n if (value === this.blendMode)\n {\n return;\n }\n\n this.blendMode = value;\n\n const mode = this.blendModes[value];\n const gl = this.gl;\n\n if (mode.length === 2)\n {\n gl.blendFunc(mode[0], mode[1]);\n }\n else\n {\n gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);\n }\n if (mode.length === 6)\n {\n this._blendEq = true;\n gl.blendEquationSeparate(mode[4], mode[5]);\n }\n else if (this._blendEq)\n {\n this._blendEq = false;\n gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);\n }\n }\n\n /**\n * Sets the polygon offset.\n * @param {number} value - the polygon offset\n * @param {number} scale - the polygon offset scale\n */\n setPolygonOffset(value: number, scale: number): void\n {\n this.gl.polygonOffset(value, scale);\n }\n\n // used\n /** Resets all the logic and disables the VAOs. */\n reset(): void\n {\n this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false);\n\n this.forceState(this.defaultState);\n\n this._blendEq = true;\n this.blendMode = -1;\n this.setBlendMode(0);\n }\n\n /**\n * Checks to see which updates should be checked based on which settings have been activated.\n *\n * For example, if blend is enabled then we should check the blend modes each time the state is changed\n * or if polygon fill is activated then we need to check if the polygon offset changes.\n * The idea is that we only check what we have too.\n * @param func - the checking function to add or remove\n * @param value - should the check function be added or removed.\n */\n updateCheck(func: (system: this, state: State) => void, value: boolean): void\n {\n const index = this.checks.indexOf(func);\n\n if (value && index === -1)\n {\n this.checks.push(func);\n }\n else if (!value && index !== -1)\n {\n this.checks.splice(index, 1);\n }\n }\n\n /**\n * A private little wrapper function that we call to check the blend mode.\n * @param system - the System to perform the state check on\n * @param state - the state that the blendMode will pulled from\n */\n private static checkBlendMode(system: StateSystem, state: State): void\n {\n system.setBlendMode(state.blendMode);\n }\n\n /**\n * A private little wrapper function that we call to check the polygon offset.\n * @param system - the System to perform the state check on\n * @param state - the state that the blendMode will pulled from\n */\n private static checkPolygonOffset(system: StateSystem, state: State): void\n {\n system.setPolygonOffset(1, state.polygonOffset);\n }\n\n /**\n * @ignore\n */\n destroy(): void\n {\n this.gl = null;\n }\n}\n\nextensions.add(StateSystem);\n"],"names":["_StateSystem"],"mappings":";;;;AASA,MAAM,QAAQ,GACR,SAAS,GACT,UAAU,GACV,aAAa,GACb,UAAU,GACV,aAAa,GAMN,eAAN,MAAMA,cACb;AAAA,EAwDI,cACA;AACI,SAAK,KAAK,MAEV,KAAK,UAAU,GACf,KAAK,gBAAgB,GACrB,KAAK,YAAY,YAAY,MAE7B,KAAK,WAAW,IAGhB,KAAK,MAAM,CAAA,GACX,KAAK,IAAI,KAAK,IAAI,KAAK,UACvB,KAAK,IAAI,MAAM,IAAI,KAAK,WACxB,KAAK,IAAI,OAAO,IAAI,KAAK,aACzB,KAAK,IAAI,UAAU,IAAI,KAAK,cAC5B,KAAK,IAAI,OAAO,IAAI,KAAK,cACzB,KAAK,IAAI,UAAU,IAAI,KAAK,cAE5B,KAAK,SAAS,CAAA,GAEd,KAAK,eAAe,IAAI,MAAA,GACxB,KAAK,aAAa,QAAQ;AAAA,EAC9B;AAAA,EAEA,cAAc,IACd;AACI,SAAK,KAAK,IAEV,KAAK,aAAa,yBAAyB,EAAE,GAE7C,KAAK,IAAI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