melonjs 10.9.0 → 10.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +32 -25
  3. package/dist/melonjs.js +1635 -2383
  4. package/dist/melonjs.min.js +4 -4
  5. package/dist/melonjs.module.d.ts +929 -1504
  6. package/dist/melonjs.module.js +1581 -2344
  7. package/package.json +18 -18
  8. package/src/audio/audio.js +0 -1
  9. package/src/camera/camera2d.js +1 -16
  10. package/src/entity/entity.js +6 -11
  11. package/src/game.js +2 -2
  12. package/src/geometries/ellipse.js +19 -28
  13. package/src/geometries/line.js +5 -8
  14. package/src/geometries/path2d.js +14 -24
  15. package/src/geometries/poly.js +29 -47
  16. package/src/geometries/rectangle.js +25 -37
  17. package/src/geometries/roundrect.js +8 -12
  18. package/src/index.js +7 -2
  19. package/src/input/gamepad.js +3 -14
  20. package/src/input/keyboard.js +1 -9
  21. package/src/input/pointer.js +0 -1
  22. package/src/input/pointerevent.js +14 -23
  23. package/src/lang/deprecated.js +9 -6
  24. package/src/level/level.js +0 -9
  25. package/src/level/tiled/TMXGroup.js +0 -4
  26. package/src/level/tiled/TMXLayer.js +0 -8
  27. package/src/level/tiled/TMXObject.js +0 -3
  28. package/src/level/tiled/TMXTile.js +4 -5
  29. package/src/level/tiled/TMXTileMap.js +1 -7
  30. package/src/level/tiled/TMXTileset.js +0 -5
  31. package/src/level/tiled/TMXTilesetGroup.js +1 -4
  32. package/src/level/tiled/TMXUtils.js +1 -4
  33. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +2 -3
  34. package/src/level/tiled/renderer/TMXIsometricRenderer.js +0 -1
  35. package/src/level/tiled/renderer/TMXRenderer.js +1 -7
  36. package/src/loader/loader.js +0 -11
  37. package/src/loader/loadingscreen.js +16 -5
  38. package/src/math/color.js +10 -30
  39. package/src/math/math.js +0 -10
  40. package/src/math/matrix2.js +12 -27
  41. package/src/math/matrix3.js +1 -22
  42. package/src/math/observable_vector2.js +0 -29
  43. package/src/math/observable_vector3.js +0 -29
  44. package/src/math/vector2.js +3 -40
  45. package/src/math/vector3.js +4 -41
  46. package/src/particles/emitter.js +11 -12
  47. package/src/physics/body.js +4 -5
  48. package/src/physics/bounds.js +5 -19
  49. package/src/physics/collision.js +1 -13
  50. package/src/physics/detector.js +6 -56
  51. package/src/physics/quadtree.js +0 -7
  52. package/src/physics/response.js +48 -0
  53. package/src/physics/sat.js +4 -4
  54. package/src/physics/world.js +0 -4
  55. package/src/plugin/plugin.js +0 -2
  56. package/src/polyfill/roundrect.js +4 -2
  57. package/src/renderable/GUI.js +11 -14
  58. package/src/renderable/collectable.js +1 -0
  59. package/src/renderable/colorlayer.js +9 -7
  60. package/src/renderable/container.js +38 -64
  61. package/src/renderable/dragndrop.js +1 -10
  62. package/src/renderable/imagelayer.js +8 -12
  63. package/src/renderable/light2d.js +118 -0
  64. package/src/renderable/renderable.js +27 -48
  65. package/src/renderable/sprite.js +17 -29
  66. package/src/renderable/trigger.js +10 -3
  67. package/src/state/stage.js +72 -6
  68. package/src/state/state.js +22 -23
  69. package/src/system/device.js +14 -53
  70. package/src/system/event.js +11 -0
  71. package/src/system/pooling.js +20 -9
  72. package/src/system/save.js +9 -11
  73. package/src/system/timer.js +239 -218
  74. package/src/text/bitmaptextdata.js +1 -4
  75. package/src/text/glyph.js +2 -2
  76. package/src/text/text.js +25 -24
  77. package/src/text/textmetrics.js +0 -2
  78. package/src/tweens/easing.js +1 -1
  79. package/src/tweens/interpolation.js +2 -2
  80. package/src/tweens/tween.js +1 -13
  81. package/src/utils/agent.js +1 -3
  82. package/src/utils/array.js +0 -3
  83. package/src/utils/file.js +0 -2
  84. package/src/utils/function.js +0 -2
  85. package/src/utils/string.js +0 -6
  86. package/src/utils/utils.js +0 -3
  87. package/src/video/canvas/canvas_renderer.js +73 -90
  88. package/src/video/renderer.js +34 -49
  89. package/src/video/{texture.js → texture/atlas.js} +10 -8
  90. package/src/video/{texture_cache.js → texture/cache.js} +4 -5
  91. package/src/video/texture/canvas_texture.js +99 -0
  92. package/src/video/video.js +3 -3
  93. package/src/video/webgl/glshader.js +0 -5
  94. package/src/video/webgl/utils/uniforms.js +3 -6
  95. package/src/video/webgl/webgl_compositor.js +0 -14
  96. package/src/video/webgl/webgl_renderer.js +73 -97
@@ -2,14 +2,13 @@ import Color from "./../../math/color.js";
2
2
  import Matrix2d from "./../../math/matrix2.js";
3
3
  import WebGLCompositor from "./webgl_compositor.js";
4
4
  import Renderer from "./../renderer.js";
5
- import TextureCache from "./../texture_cache.js";
6
- import { TextureAtlas, createAtlas } from "./../texture.js";
5
+ import TextureCache from "./../texture/cache.js";
6
+ import { TextureAtlas, createAtlas } from "./../texture/atlas.js";
7
7
  import { createCanvas, renderer } from "./../video.js";
8
8
  import * as event from "./../../system/event.js";
9
9
  import pool from "./../../system/pooling.js";
10
10
  import { isPowerOfTwo, nextPowerOfTwo } from "./../../math/math.js";
11
11
 
12
-
13
12
  /**
14
13
  * @classdesc
15
14
  * a WebGL renderer object
@@ -70,8 +69,8 @@ class WebGLRenderer extends Renderer {
70
69
  /**
71
70
  * The WebGL context
72
71
  * @name gl
73
- * @memberof WebGLRenderer#
74
- * type {WebGLRenderingContext}
72
+ * @memberof WebGLRenderer
73
+ * @type {WebGLRenderingContext}
75
74
  */
76
75
  this.context = this.gl = this.getContextGL(this.getScreenCanvas(), options.transparent);
77
76
 
@@ -171,8 +170,7 @@ class WebGLRenderer extends Renderer {
171
170
  /**
172
171
  * Reset context state
173
172
  * @name reset
174
- * @memberof WebGLRenderer.prototype
175
- * @function
173
+ * @memberof WebGLRenderer
176
174
  */
177
175
  reset() {
178
176
  super.reset();
@@ -196,10 +194,8 @@ class WebGLRenderer extends Renderer {
196
194
  /**
197
195
  * set the active compositor for this renderer
198
196
  * @name setCompositor
199
- * @function
200
197
  * @param {WebGLCompositor|string} compositor a compositor name or instance
201
- * @memberof WebGLRenderer.prototype
202
- * @function
198
+ * @memberof WebGLRenderer
203
199
  */
204
200
  setCompositor(compositor = "default") {
205
201
 
@@ -224,8 +220,7 @@ class WebGLRenderer extends Renderer {
224
220
  /**
225
221
  * Reset the gl transform to identity
226
222
  * @name resetTransform
227
- * @memberof WebGLRenderer.prototype
228
- * @function
223
+ * @memberof WebGLRenderer
229
224
  */
230
225
  resetTransform() {
231
226
  this.currentTransform.identity();
@@ -271,8 +266,7 @@ class WebGLRenderer extends Renderer {
271
266
  /**
272
267
  * Create a pattern with the specified repetition
273
268
  * @name createPattern
274
- * @memberof WebGLRenderer.prototype
275
- * @function
269
+ * @memberof WebGLRenderer
276
270
  * @param {Image} image Source image
277
271
  * @param {string} repeat Define how the pattern should be repeated
278
272
  * @returns {TextureAtlas}
@@ -304,8 +298,7 @@ class WebGLRenderer extends Renderer {
304
298
  /**
305
299
  * Flush the compositor to the frame buffer
306
300
  * @name flush
307
- * @memberof WebGLRenderer.prototype
308
- * @function
301
+ * @memberof WebGLRenderer
309
302
  */
310
303
  flush() {
311
304
  this.currentCompositor.flush();
@@ -314,8 +307,7 @@ class WebGLRenderer extends Renderer {
314
307
  /**
315
308
  * set/change the current projection matrix (WebGL only)
316
309
  * @name setProjection
317
- * @memberof WebGLRenderer.prototype
318
- * @function
310
+ * @memberof WebGLRenderer
319
311
  * @param {Matrix3d} matrix
320
312
  */
321
313
  setProjection(matrix) {
@@ -326,8 +318,7 @@ class WebGLRenderer extends Renderer {
326
318
  /**
327
319
  * Clears the gl context with the given color.
328
320
  * @name clearColor
329
- * @memberof WebGLRenderer.prototype
330
- * @function
321
+ * @memberof WebGLRenderer
331
322
  * @param {Color|string} [color="#000000"] CSS color.
332
323
  * @param {boolean} [opaque=false] Allow transparency [default] or clear the surface completely [true]
333
324
  */
@@ -354,8 +345,7 @@ class WebGLRenderer extends Renderer {
354
345
  /**
355
346
  * Erase the pixels in the given rectangular area by setting them to transparent black (rgba(0,0,0,0)).
356
347
  * @name clearRect
357
- * @memberof WebGLRenderer.prototype
358
- * @function
348
+ * @memberof WebGLRenderer
359
349
  * @param {number} x x axis of the coordinate for the rectangle starting point.
360
350
  * @param {number} y y axis of the coordinate for the rectangle starting point.
361
351
  * @param {number} width The rectangle's width.
@@ -404,8 +394,7 @@ class WebGLRenderer extends Renderer {
404
394
  /**
405
395
  * Draw an image to the gl context
406
396
  * @name drawImage
407
- * @memberof WebGLRenderer.prototype
408
- * @function
397
+ * @memberof WebGLRenderer
409
398
  * @param {Image} image An element to draw into the context. The specification permits any canvas image source (CanvasImageSource), specifically, a CSSImageValue, an HTMLImageElement, an SVGImageElement, an HTMLVideoElement, an HTMLCanvasElement, an ImageBitmap, or an OffscreenCanvas.
410
399
  * @param {number} sx The X coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
411
400
  * @param {number} sy The Y coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
@@ -457,8 +446,7 @@ class WebGLRenderer extends Renderer {
457
446
  /**
458
447
  * Draw a pattern within the given rectangle.
459
448
  * @name drawPattern
460
- * @memberof WebGLRenderer.prototype
461
- * @function
449
+ * @memberof WebGLRenderer
462
450
  * @param {TextureAtlas} pattern Pattern object
463
451
  * @param {number} x
464
452
  * @param {number} y
@@ -475,8 +463,7 @@ class WebGLRenderer extends Renderer {
475
463
  /**
476
464
  * return a reference to the screen canvas corresponding WebGL Context
477
465
  * @name getScreenContext
478
- * @memberof WebGLRenderer.prototype
479
- * @function
466
+ * @memberof WebGLRenderer
480
467
  * @returns {WebGLRenderingContext}
481
468
  */
482
469
  getScreenContext() {
@@ -486,8 +473,7 @@ class WebGLRenderer extends Renderer {
486
473
  /**
487
474
  * Returns the WebGL Context object of the given Canvas
488
475
  * @name getContextGL
489
- * @memberof WebGLRenderer.prototype
490
- * @function
476
+ * @memberof WebGLRenderer
491
477
  * @param {HTMLCanvasElement} canvas
492
478
  * @param {boolean} [transparent=true] use false to disable transparency
493
479
  * @returns {WebGLRenderingContext}
@@ -544,8 +530,7 @@ class WebGLRenderer extends Renderer {
544
530
  * Returns the WebGLContext instance for the renderer
545
531
  * return a reference to the system 2d Context
546
532
  * @name getContext
547
- * @memberof WebGLRenderer.prototype
548
- * @function
533
+ * @memberof WebGLRenderer
549
534
  * @returns {WebGLRenderingContext}
550
535
  */
551
536
  getContext() {
@@ -565,8 +550,7 @@ class WebGLRenderer extends Renderer {
565
550
  * <img src="images/screen-blendmode.png" width="510"/> <br>
566
551
  * @name setBlendMode
567
552
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
568
- * @memberof WebGLRenderer.prototype
569
- * @function
553
+ * @memberof WebGLRenderer
570
554
  * @param {string} [mode="normal"] blend mode : "normal", "multiply", "lighter", "additive", "screen"
571
555
  * @param {WebGLRenderingContext} [gl]
572
556
  */
@@ -616,8 +600,7 @@ class WebGLRenderer extends Renderer {
616
600
  /**
617
601
  * restores the canvas context
618
602
  * @name restore
619
- * @memberof WebGLRenderer.prototype
620
- * @function
603
+ * @memberof WebGLRenderer
621
604
  */
622
605
  restore() {
623
606
  // do nothing if there is no saved states
@@ -652,8 +635,7 @@ class WebGLRenderer extends Renderer {
652
635
  /**
653
636
  * saves the canvas context
654
637
  * @name save
655
- * @memberof WebGLRenderer.prototype
656
- * @function
638
+ * @memberof WebGLRenderer
657
639
  */
658
640
  save() {
659
641
  this._colorStack.push(this.currentColor.clone());
@@ -670,8 +652,7 @@ class WebGLRenderer extends Renderer {
670
652
  /**
671
653
  * rotates the uniform matrix
672
654
  * @name rotate
673
- * @memberof WebGLRenderer.prototype
674
- * @function
655
+ * @memberof WebGLRenderer
675
656
  * @param {number} angle in radians
676
657
  */
677
658
  rotate(angle) {
@@ -681,8 +662,7 @@ class WebGLRenderer extends Renderer {
681
662
  /**
682
663
  * scales the uniform matrix
683
664
  * @name scale
684
- * @memberof WebGLRenderer.prototype
685
- * @function
665
+ * @memberof WebGLRenderer
686
666
  * @param {number} x
687
667
  * @param {number} y
688
668
  */
@@ -702,8 +682,7 @@ class WebGLRenderer extends Renderer {
702
682
  /**
703
683
  * Set the global alpha
704
684
  * @name setGlobalAlpha
705
- * @memberof WebGLRenderer.prototype
706
- * @function
685
+ * @memberof WebGLRenderer
707
686
  * @param {number} alpha 0.0 to 1.0 values accepted.
708
687
  */
709
688
  setGlobalAlpha(alpha) {
@@ -713,8 +692,7 @@ class WebGLRenderer extends Renderer {
713
692
  /**
714
693
  * Return the global alpha
715
694
  * @name getGlobalAlpha
716
- * @memberof WebGLRenderer.prototype
717
- * @function
695
+ * @memberof WebGLRenderer
718
696
  * @returns {number} global alpha value
719
697
  */
720
698
  getGlobalAlpha() {
@@ -725,8 +703,7 @@ class WebGLRenderer extends Renderer {
725
703
  * Set the current fill & stroke style color.
726
704
  * By default, or upon reset, the value is set to #000000.
727
705
  * @name setColor
728
- * @memberof WebGLRenderer.prototype
729
- * @function
706
+ * @memberof WebGLRenderer
730
707
  * @param {Color|string} color css color string.
731
708
  */
732
709
  setColor(color) {
@@ -738,8 +715,7 @@ class WebGLRenderer extends Renderer {
738
715
  /**
739
716
  * Set the line width
740
717
  * @name setLineWidth
741
- * @memberof WebGLRenderer.prototype
742
- * @function
718
+ * @memberof WebGLRenderer
743
719
  * @param {number} width Line width
744
720
  */
745
721
  setLineWidth(width) {
@@ -749,8 +725,7 @@ class WebGLRenderer extends Renderer {
749
725
  /**
750
726
  * Stroke an arc at the specified coordinates with given radius, start and end points
751
727
  * @name strokeArc
752
- * @memberof WebGLRenderer.prototype
753
- * @function
728
+ * @memberof WebGLRenderer
754
729
  * @param {number} x arc center point x-axis
755
730
  * @param {number} y arc center point y-axis
756
731
  * @param {number} radius
@@ -777,8 +752,7 @@ class WebGLRenderer extends Renderer {
777
752
  /**
778
753
  * Fill an arc at the specified coordinates with given radius, start and end points
779
754
  * @name fillArc
780
- * @memberof WebGLRenderer.prototype
781
- * @function
755
+ * @memberof WebGLRenderer
782
756
  * @param {number} x arc center point x-axis
783
757
  * @param {number} y arc center point y-axis
784
758
  * @param {number} radius
@@ -793,8 +767,7 @@ class WebGLRenderer extends Renderer {
793
767
  /**
794
768
  * Stroke an ellipse at the specified coordinates with given radius
795
769
  * @name strokeEllipse
796
- * @memberof WebGLRenderer.prototype
797
- * @function
770
+ * @memberof WebGLRenderer
798
771
  * @param {number} x ellipse center point x-axis
799
772
  * @param {number} y ellipse center point y-axis
800
773
  * @param {number} w horizontal radius of the ellipse
@@ -819,8 +792,7 @@ class WebGLRenderer extends Renderer {
819
792
  /**
820
793
  * Fill an ellipse at the specified coordinates with given radius
821
794
  * @name fillEllipse
822
- * @memberof WebGLRenderer.prototype
823
- * @function
795
+ * @memberof WebGLRenderer
824
796
  * @param {number} x ellipse center point x-axis
825
797
  * @param {number} y ellipse center point y-axis
826
798
  * @param {number} w horizontal radius of the ellipse
@@ -833,8 +805,7 @@ class WebGLRenderer extends Renderer {
833
805
  /**
834
806
  * Stroke a line of the given two points
835
807
  * @name strokeLine
836
- * @memberof WebGLRenderer.prototype
837
- * @function
808
+ * @memberof WebGLRenderer
838
809
  * @param {number} startX the start x coordinate
839
810
  * @param {number} startY the start y coordinate
840
811
  * @param {number} endX the end x coordinate
@@ -855,8 +826,7 @@ class WebGLRenderer extends Renderer {
855
826
  /**
856
827
  * Fill a line of the given two points
857
828
  * @name fillLine
858
- * @memberof WebGLRenderer.prototype
859
- * @function
829
+ * @memberof WebGLRenderer
860
830
  * @param {number} startX the start x coordinate
861
831
  * @param {number} startY the start y coordinate
862
832
  * @param {number} endX the end x coordinate
@@ -869,8 +839,7 @@ class WebGLRenderer extends Renderer {
869
839
  /**
870
840
  * Stroke a me.Polygon on the screen with a specified color
871
841
  * @name strokePolygon
872
- * @memberof WebGLRenderer.prototype
873
- * @function
842
+ * @memberof WebGLRenderer
874
843
  * @param {Polygon} poly the shape to draw
875
844
  * @param {boolean} [fill=false] also fill the shape with the current color if true
876
845
  */
@@ -901,8 +870,7 @@ class WebGLRenderer extends Renderer {
901
870
  /**
902
871
  * Fill a me.Polygon on the screen
903
872
  * @name fillPolygon
904
- * @memberof WebGLRenderer.prototype
905
- * @function
873
+ * @memberof WebGLRenderer
906
874
  * @param {Polygon} poly the shape to draw
907
875
  */
908
876
  fillPolygon(poly) {
@@ -912,8 +880,7 @@ class WebGLRenderer extends Renderer {
912
880
  /**
913
881
  * Draw a stroke rectangle at the specified coordinates
914
882
  * @name strokeRect
915
- * @memberof WebGLRenderer.prototype
916
- * @function
883
+ * @memberof WebGLRenderer
917
884
  * @param {number} x
918
885
  * @param {number} y
919
886
  * @param {number} width
@@ -937,8 +904,7 @@ class WebGLRenderer extends Renderer {
937
904
  /**
938
905
  * Draw a filled rectangle at the specified coordinates
939
906
  * @name fillRect
940
- * @memberof WebGLRenderer.prototype
941
- * @function
907
+ * @memberof WebGLRenderer
942
908
  * @param {number} x
943
909
  * @param {number} y
944
910
  * @param {number} width
@@ -951,8 +917,7 @@ class WebGLRenderer extends Renderer {
951
917
  /**
952
918
  * Stroke a rounded rectangle at the specified coordinates
953
919
  * @name strokeRoundRect
954
- * @memberof WebGLRenderer.prototype
955
- * @function
920
+ * @memberof WebGLRenderer
956
921
  * @param {number} x
957
922
  * @param {number} y
958
923
  * @param {number} width
@@ -978,8 +943,7 @@ class WebGLRenderer extends Renderer {
978
943
  /**
979
944
  * Draw a rounded filled rectangle at the specified coordinates
980
945
  * @name fillRoundRect
981
- * @memberof WebGLRenderer.prototype
982
- * @function
946
+ * @memberof WebGLRenderer
983
947
  * @param {number} x
984
948
  * @param {number} y
985
949
  * @param {number} width
@@ -994,8 +958,7 @@ class WebGLRenderer extends Renderer {
994
958
  * Reset (overrides) the renderer transformation matrix to the
995
959
  * identity one, and then apply the given transformation matrix.
996
960
  * @name setTransform
997
- * @memberof WebGLRenderer.prototype
998
- * @function
961
+ * @memberof WebGLRenderer
999
962
  * @param {Matrix2d} mat2d Matrix to transform by
1000
963
  */
1001
964
  setTransform(mat2d) {
@@ -1006,8 +969,7 @@ class WebGLRenderer extends Renderer {
1006
969
  /**
1007
970
  * Multiply given matrix into the renderer tranformation matrix
1008
971
  * @name transform
1009
- * @memberof WebGLRenderer.prototype
1010
- * @function
972
+ * @memberof WebGLRenderer
1011
973
  * @param {Matrix2d} mat2d Matrix to transform by
1012
974
  */
1013
975
  transform(mat2d) {
@@ -1024,8 +986,7 @@ class WebGLRenderer extends Renderer {
1024
986
  /**
1025
987
  * Translates the uniform matrix by the given coordinates
1026
988
  * @name translate
1027
- * @memberof WebGLRenderer.prototype
1028
- * @function
989
+ * @memberof WebGLRenderer
1029
990
  * @param {number} x
1030
991
  * @param {number} y
1031
992
  */
@@ -1047,8 +1008,7 @@ class WebGLRenderer extends Renderer {
1047
1008
  * and restore it (with the restore() method) any time in the future.
1048
1009
  * (<u>this is an experimental feature !</u>)
1049
1010
  * @name clipRect
1050
- * @memberof WebGLRenderer.prototype
1051
- * @function
1011
+ * @memberof WebGLRenderer
1052
1012
  * @param {number} x
1053
1013
  * @param {number} y
1054
1014
  * @param {number} width
@@ -1095,31 +1055,45 @@ class WebGLRenderer extends Renderer {
1095
1055
  * So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
1096
1056
  * Mask are not preserved through renderer context save and restore.
1097
1057
  * @name setMask
1098
- * @memberof WebGLRenderer.prototype
1099
- * @function
1100
- * @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
1058
+ * @memberof WebGLRenderer
1059
+ * @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] a shape defining the mask to be applied
1060
+ * @param {boolean} [invert=false] either the given shape should define what is visible (default) or the opposite
1101
1061
  */
1102
- setMask(mask) {
1062
+ setMask(mask, invert = false) {
1103
1063
  var gl = this.gl;
1104
1064
 
1105
1065
  // flush the compositor
1106
1066
  this.flush();
1107
1067
 
1108
- // Enable and setup GL state to write to stencil buffer
1109
- gl.enable(gl.STENCIL_TEST);
1110
- gl.clear(gl.STENCIL_BUFFER_BIT);
1068
+ if (this.maskLevel === 0) {
1069
+ // Enable and setup GL state to write to stencil buffer
1070
+ gl.enable(gl.STENCIL_TEST);
1071
+ gl.clear(gl.STENCIL_BUFFER_BIT);
1072
+
1073
+
1074
+ }
1075
+
1076
+ this.maskLevel++;
1077
+
1111
1078
  gl.colorMask(false, false, false, false);
1112
- gl.stencilFunc(gl.NOTEQUAL, 1, 1);
1079
+ gl.stencilFunc(gl.EQUAL, this.maskLevel, 1);
1113
1080
  gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
1114
1081
 
1082
+
1083
+ // fill the given mask shape
1115
1084
  this.fill(mask);
1116
1085
 
1117
1086
  // flush the compositor
1118
1087
  this.flush();
1119
1088
 
1120
- // Use stencil buffer to affect next rendering object
1121
1089
  gl.colorMask(true, true, true, true);
1122
- gl.stencilFunc(gl.EQUAL, 1, 1);
1090
+
1091
+ // Use stencil buffer to affect next rendering object
1092
+ if (invert === true) {
1093
+ gl.stencilFunc(gl.EQUAL, this.maskLevel + 1, 1);
1094
+ } else {
1095
+ gl.stencilFunc(gl.NOTEQUAL, this.maskLevel + 1, 1);
1096
+ }
1123
1097
  gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
1124
1098
  }
1125
1099
 
@@ -1127,13 +1101,15 @@ class WebGLRenderer extends Renderer {
1127
1101
  * disable (remove) the rendering mask set through setMask.
1128
1102
  * @name clearMask
1129
1103
  * @see WebGLRenderer#setMask
1130
- * @memberof WebGLRenderer.prototype
1131
- * @function
1104
+ * @memberof WebGLRenderer
1132
1105
  */
1133
1106
  clearMask() {
1134
- // flush the compositor
1135
- this.flush();
1136
- this.gl.disable(this.gl.STENCIL_TEST);
1107
+ if (this.maskLevel > 0) {
1108
+ // flush the compositor
1109
+ this.flush();
1110
+ this.maskLevel = 0;
1111
+ this.gl.disable(this.gl.STENCIL_TEST);
1112
+ }
1137
1113
  }
1138
1114
  };
1139
1115