Source: gfx/core/renderers/webgl/managers/StencilManager.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
var WebGLManager = require('./WebGLManager'),
  utils = require('../../../utils');

/**
 * @class
 * @param renderer {WebGLRenderer} The renderer this manager works for.
 */
function WebGLMaskManager(renderer) {
  WebGLManager.call(this, renderer);
  this.stencilMaskStack = null;
}

WebGLMaskManager.prototype = Object.create(WebGLManager.prototype);
WebGLMaskManager.prototype.constructor = WebGLMaskManager;
module.exports = WebGLMaskManager;

/**
 * Changes the mask stack that is used by this manager.
 *
 * @param stencilMaskStack {StencilMaskStack} The mask stack
 */
WebGLMaskManager.prototype.setMaskStack = function(stencilMaskStack) {
  this.stencilMaskStack = stencilMaskStack;

  var gl = this.renderer.gl;

  if (stencilMaskStack.stencilStack.length === 0) {
    gl.disable(gl.STENCIL_TEST);
  }
  else {
    gl.enable(gl.STENCIL_TEST);
  }
};

/**
 * Applies the Mask and adds it to the current filter stack. @alvin
 *
 * @param graphics {Graphics}
 * @param webGLData {any[]}
 */
WebGLMaskManager.prototype.pushStencil = function(graphics, webGLData) {
  this.renderer.currentRenderTarget.attachStencilBuffer();

  var gl = this.renderer.gl,
    sms = this.stencilMaskStack;

  this.bindGraphics(graphics, webGLData);

  if (sms.stencilStack.length === 0) {
    gl.enable(gl.STENCIL_TEST);
    gl.clear(gl.STENCIL_BUFFER_BIT);
    sms.reverse = true;
    sms.count = 0;
  }

  sms.stencilStack.push(webGLData);

  var level = sms.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 (sms.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 (sms.reverse) {
      gl.stencilFunc(gl.EQUAL,0xFF - (level + 1), 0xFF);
    }
    else {
      gl.stencilFunc(gl.EQUAL,level + 1, 0xFF);
    }

    sms.reverse = !sms.reverse;
  }
  else {
    if (!sms.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 (!sms.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);

  sms.count++;
};

/**
 * TODO this does not belong here!
 *
 * @param graphics {Graphics}
 * @param webGLData {any[]}
 */
WebGLMaskManager.prototype.bindGraphics = function(graphics, webGLData) {
    // if (this._currentGraphics === graphics)return;
  var gl = this.renderer.gl;

     // bind the graphics object..
  var shader;// = this.renderer.shaderManager.plugins.primitiveShader;

  if (webGLData.mode === 1) {
    shader = this.renderer.shaderManager.complexPrimitiveShader;

    this.renderer.shaderManager.setShader(shader);

    gl.uniformMatrix3fv(shader.uniforms.translationMatrix._location, false, graphics.worldTransform.toArray(true));

    gl.uniformMatrix3fv(shader.uniforms.projectionMatrix._location, false, this.renderer.currentRenderTarget.projectionMatrix.toArray(true));

    gl.uniform3fv(shader.uniforms.tint._location, utils.hex2rgb(graphics.tint));

    gl.uniform3fv(shader.uniforms.color._location, webGLData.color);

    gl.uniform1f(shader.uniforms.alpha._location, graphics.worldAlpha);

    gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer);

    gl.vertexAttribPointer(shader.attributes.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 {
        // this.renderer.shaderManager.activatePrimitiveShader();
    shader = this.renderer.shaderManager.primitiveShader;

    this.renderer.shaderManager.setShader(shader);

    gl.uniformMatrix3fv(shader.uniforms.translationMatrix._location, false, graphics.worldTransform.toArray(true));

    gl.uniformMatrix3fv(shader.uniforms.projectionMatrix._location, false, this.renderer.currentRenderTarget.projectionMatrix.toArray(true));

    gl.uniform3fv(shader.uniforms.tint._location, utils.hex2rgb(graphics.tint));

    gl.uniform1f(shader.uniforms.alpha._location, graphics.worldAlpha);

    gl.bindBuffer(gl.ARRAY_BUFFER, webGLData.buffer);

    gl.vertexAttribPointer(shader.attributes.aVertexPosition, 2, gl.FLOAT, false, 4 * 6, 0);
    gl.vertexAttribPointer(shader.attributes.aColor, 4, gl.FLOAT, false,4 * 6, 2 * 4);

        // set the index buffer!
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, webGLData.indexBuffer);
  }
};

/**
 * TODO @alvin
 * @param graphics {Graphics}
 * @param webGLData {any[]}
 */
WebGLMaskManager.prototype.popStencil = function(graphics, webGLData) {
  var gl = this.renderer.gl,
    sms = this.stencilMaskStack;

  sms.stencilStack.pop();

  sms.count--;

  if (sms.stencilStack.length === 0) {
        // the stack is empty!
    gl.disable(gl.STENCIL_TEST);

  }
  else {

    var level = sms.count;

    this.bindGraphics(graphics, webGLData);

    gl.colorMask(false, false, false, false);

    if (webGLData.mode === 1) {
      sms.reverse = !sms.reverse;

      if (sms.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);

      this.renderer.drawCount += 2;

      if (!sms.reverse) {
        gl.stencilFunc(gl.EQUAL,0xFF - (level), 0xFF);
      }
      else {
        gl.stencilFunc(gl.EQUAL,level, 0xFF);
      }

    }
    else {
          //  console.log("<<>>")
      if (!sms.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);

      this.renderer.drawCount++;

      if (!sms.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);


  }
};

/**
 * Destroys the mask stack.
 *
 */
WebGLMaskManager.prototype.destroy = function() {
  WebGLManager.prototype.destroy.call(this);

  this.stencilMaskStack.stencilStack = null;
};

/**
 * Applies the Mask and adds it to the current filter stack.
 *
 * @param maskData {any[]} The mask data structure to use
 */
WebGLMaskManager.prototype.pushMask = function(maskData) {


  this.renderer.setObjectRenderer(this.renderer.plugins.graphics);

  if (maskData.dirty) {
    this.renderer.plugins.graphics.updateGraphics(maskData, this.renderer.gl);
  }

  if (!maskData._webGL[this.renderer.gl.id].data.length) {
    return;
  }

  this.pushStencil(maskData, maskData._webGL[this.renderer.gl.id].data[0]);
};

/**
 * Removes the last filter from the filter stack and doesn't return it.
 *
 * @param maskData {any[]}
 */
WebGLMaskManager.prototype.popMask = function(maskData) {
  this.renderer.setObjectRenderer(this.renderer.plugins.graphics);

  this.popStencil(maskData, maskData._webGL[this.renderer.gl.id].data[0]);
};