@pixi/core
Version:
Core PixiJS
141 lines (138 loc) • 3.91 kB
JavaScript
import { BLEND_MODES } from '@pixi/constants';
import { ExtensionType, extensions } from '@pixi/extensions';
import { State } from './State.mjs';
import { mapWebGLBlendModesToPixi } from './utils/mapWebGLBlendModesToPixi.mjs';
const BLEND = 0;
const OFFSET = 1;
const CULLING = 2;
const DEPTH_TEST = 3;
const WINDING = 4;
const DEPTH_MASK = 5;
const _StateSystem = class {
constructor() {
this.gl = null;
this.stateId = 0;
this.polygonOffset = 0;
this.blendMode = BLEND_MODES.NONE;
this._blendEq = false;
this.map = [];
this.map[BLEND] = this.setBlend;
this.map[OFFSET] = this.setOffset;
this.map[CULLING] = this.setCullFace;
this.map[DEPTH_TEST] = this.setDepthTest;
this.map[WINDING] = this.setFrontFace;
this.map[DEPTH_MASK] = this.setDepthMask;
this.checks = [];
this.defaultState = new State();
this.defaultState.blend = true;
}
contextChange(gl) {
this.gl = gl;
this.blendModes = mapWebGLBlendModesToPixi(gl);
this.set(this.defaultState);
this.reset();
}
set(state) {
state = state || this.defaultState;
if (this.stateId !== state.data) {
let diff = this.stateId ^ state.data;
let i = 0;
while (diff) {
if (diff & 1) {
this.map[i].call(this, !!(state.data & 1 << i));
}
diff = diff >> 1;
i++;
}
this.stateId = state.data;
}
for (let i = 0; i < this.checks.length; i++) {
this.checks[i](this, state);
}
}
forceState(state) {
state = state || this.defaultState;
for (let i = 0; i < this.map.length; i++) {
this.map[i].call(this, !!(state.data & 1 << i));
}
for (let i = 0; i < this.checks.length; i++) {
this.checks[i](this, state);
}
this.stateId = state.data;
}
setBlend(value) {
this.updateCheck(_StateSystem.checkBlendMode, value);
this.gl[value ? "enable" : "disable"](this.gl.BLEND);
}
setOffset(value) {
this.updateCheck(_StateSystem.checkPolygonOffset, value);
this.gl[value ? "enable" : "disable"](this.gl.POLYGON_OFFSET_FILL);
}
setDepthTest(value) {
this.gl[value ? "enable" : "disable"](this.gl.DEPTH_TEST);
}
setDepthMask(value) {
this.gl.depthMask(value);
}
setCullFace(value) {
this.gl[value ? "enable" : "disable"](this.gl.CULL_FACE);
}
setFrontFace(value) {
this.gl.frontFace(this.gl[value ? "CW" : "CCW"]);
}
setBlendMode(value) {
if (value === this.blendMode) {
return;
}
this.blendMode = value;
const mode = this.blendModes[value];
const gl = this.gl;
if (mode.length === 2) {
gl.blendFunc(mode[0], mode[1]);
} else {
gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);
}
if (mode.length === 6) {
this._blendEq = true;
gl.blendEquationSeparate(mode[4], mode[5]);
} else if (this._blendEq) {
this._blendEq = false;
gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
}
}
setPolygonOffset(value, scale) {
this.gl.polygonOffset(value, scale);
}
reset() {
this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false);
this.forceState(this.defaultState);
this._blendEq = true;
this.blendMode = -1;
this.setBlendMode(0);
}
updateCheck(func, value) {
const index = this.checks.indexOf(func);
if (value && index === -1) {
this.checks.push(func);
} else if (!value && index !== -1) {
this.checks.splice(index, 1);
}
}
static checkBlendMode(system, state) {
system.setBlendMode(state.blendMode);
}
static checkPolygonOffset(system, state) {
system.setPolygonOffset(1, state.polygonOffset);
}
destroy() {
this.gl = null;
}
};
let StateSystem = _StateSystem;
StateSystem.extension = {
type: ExtensionType.RendererSystem,
name: "state"
};
extensions.add(StateSystem);
export { StateSystem };
//# sourceMappingURL=StateSystem.mjs.map