1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var constants = require('@pixi/constants');
|
6 | var extensions = require('@pixi/extensions');
|
7 | var State = require('./State.js');
|
8 | var mapWebGLBlendModesToPixi = require('./utils/mapWebGLBlendModesToPixi.js');
|
9 |
|
10 | const BLEND = 0;
|
11 | const OFFSET = 1;
|
12 | const CULLING = 2;
|
13 | const DEPTH_TEST = 3;
|
14 | const WINDING = 4;
|
15 | const DEPTH_MASK = 5;
|
16 | const _StateSystem = class {
|
17 | constructor() {
|
18 | this.gl = null;
|
19 | this.stateId = 0;
|
20 | this.polygonOffset = 0;
|
21 | this.blendMode = constants.BLEND_MODES.NONE;
|
22 | this._blendEq = false;
|
23 | this.map = [];
|
24 | this.map[BLEND] = this.setBlend;
|
25 | this.map[OFFSET] = this.setOffset;
|
26 | this.map[CULLING] = this.setCullFace;
|
27 | this.map[DEPTH_TEST] = this.setDepthTest;
|
28 | this.map[WINDING] = this.setFrontFace;
|
29 | this.map[DEPTH_MASK] = this.setDepthMask;
|
30 | this.checks = [];
|
31 | this.defaultState = new State.State();
|
32 | this.defaultState.blend = true;
|
33 | }
|
34 | contextChange(gl) {
|
35 | this.gl = gl;
|
36 | this.blendModes = mapWebGLBlendModesToPixi.mapWebGLBlendModesToPixi(gl);
|
37 | this.set(this.defaultState);
|
38 | this.reset();
|
39 | }
|
40 | set(state) {
|
41 | state = state || this.defaultState;
|
42 | if (this.stateId !== state.data) {
|
43 | let diff = this.stateId ^ state.data;
|
44 | let i = 0;
|
45 | while (diff) {
|
46 | if (diff & 1) {
|
47 | this.map[i].call(this, !!(state.data & 1 << i));
|
48 | }
|
49 | diff = diff >> 1;
|
50 | i++;
|
51 | }
|
52 | this.stateId = state.data;
|
53 | }
|
54 | for (let i = 0; i < this.checks.length; i++) {
|
55 | this.checks[i](this, state);
|
56 | }
|
57 | }
|
58 | forceState(state) {
|
59 | state = state || this.defaultState;
|
60 | for (let i = 0; i < this.map.length; i++) {
|
61 | this.map[i].call(this, !!(state.data & 1 << i));
|
62 | }
|
63 | for (let i = 0; i < this.checks.length; i++) {
|
64 | this.checks[i](this, state);
|
65 | }
|
66 | this.stateId = state.data;
|
67 | }
|
68 | setBlend(value) {
|
69 | this.updateCheck(_StateSystem.checkBlendMode, value);
|
70 | this.gl[value ? "enable" : "disable"](this.gl.BLEND);
|
71 | }
|
72 | setOffset(value) {
|
73 | this.updateCheck(_StateSystem.checkPolygonOffset, value);
|
74 | this.gl[value ? "enable" : "disable"](this.gl.POLYGON_OFFSET_FILL);
|
75 | }
|
76 | setDepthTest(value) {
|
77 | this.gl[value ? "enable" : "disable"](this.gl.DEPTH_TEST);
|
78 | }
|
79 | setDepthMask(value) {
|
80 | this.gl.depthMask(value);
|
81 | }
|
82 | setCullFace(value) {
|
83 | this.gl[value ? "enable" : "disable"](this.gl.CULL_FACE);
|
84 | }
|
85 | setFrontFace(value) {
|
86 | this.gl.frontFace(this.gl[value ? "CW" : "CCW"]);
|
87 | }
|
88 | setBlendMode(value) {
|
89 | if (value === this.blendMode) {
|
90 | return;
|
91 | }
|
92 | this.blendMode = value;
|
93 | const mode = this.blendModes[value];
|
94 | const gl = this.gl;
|
95 | if (mode.length === 2) {
|
96 | gl.blendFunc(mode[0], mode[1]);
|
97 | } else {
|
98 | gl.blendFuncSeparate(mode[0], mode[1], mode[2], mode[3]);
|
99 | }
|
100 | if (mode.length === 6) {
|
101 | this._blendEq = true;
|
102 | gl.blendEquationSeparate(mode[4], mode[5]);
|
103 | } else if (this._blendEq) {
|
104 | this._blendEq = false;
|
105 | gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
|
106 | }
|
107 | }
|
108 | setPolygonOffset(value, scale) {
|
109 | this.gl.polygonOffset(value, scale);
|
110 | }
|
111 | reset() {
|
112 | this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, false);
|
113 | this.forceState(this.defaultState);
|
114 | this._blendEq = true;
|
115 | this.blendMode = -1;
|
116 | this.setBlendMode(0);
|
117 | }
|
118 | updateCheck(func, value) {
|
119 | const index = this.checks.indexOf(func);
|
120 | if (value && index === -1) {
|
121 | this.checks.push(func);
|
122 | } else if (!value && index !== -1) {
|
123 | this.checks.splice(index, 1);
|
124 | }
|
125 | }
|
126 | static checkBlendMode(system, state) {
|
127 | system.setBlendMode(state.blendMode);
|
128 | }
|
129 | static checkPolygonOffset(system, state) {
|
130 | system.setPolygonOffset(1, state.polygonOffset);
|
131 | }
|
132 | destroy() {
|
133 | this.gl = null;
|
134 | }
|
135 | };
|
136 | let StateSystem = _StateSystem;
|
137 | StateSystem.extension = {
|
138 | type: extensions.ExtensionType.RendererSystem,
|
139 | name: "state"
|
140 | };
|
141 | extensions.extensions.add(StateSystem);
|
142 |
|
143 | exports.StateSystem = StateSystem;
|
144 |
|