forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometryMask.js
More file actions
152 lines (132 loc) · 4.13 KB
/
Copy pathGeometryMask.js
File metadata and controls
152 lines (132 loc) · 4.13 KB
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
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
/**
* @classdesc
* [description]
*
* @class GeometryMask
* @memberOf Phaser.Display.Masks
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
* @param {Phaser.GameObjects.Graphics} graphicsGeometry - [description]
*/
var GeometryMask = new Class({
initialize:
function GeometryMask (scene, graphicsGeometry)
{
/**
* [description]
*
* @name Phaser.Display.Masks.GeometryMask#geometryMask
* @type {Phaser.GameObjects.Graphics}
* @since 3.0.0
*/
this.geometryMask = graphicsGeometry;
},
/**
* [description]
*
* @method Phaser.Display.Masks.GeometryMask#setShape
* @since 3.0.0
*
* @param {Phaser.GameObjects.Graphics} graphicsGeometry - [description]
*/
setShape: function (graphicsGeometry)
{
this.geometryMask = graphicsGeometry;
},
/**
* [description]
*
* @method Phaser.Display.Masks.GeometryMask#preRenderWebGL
* @since 3.0.0
*
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - [description]
* @param {Phaser.GameObjects.GameObject} mask - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
*/
preRenderWebGL: function (renderer, mask, camera)
{
var gl = renderer.gl;
var geometryMask = this.geometryMask;
// Force flushing before drawing to stencil buffer
renderer.flush();
// Enable and setup GL state to write to stencil buffer
gl.enable(gl.STENCIL_TEST);
gl.clear(gl.STENCIL_BUFFER_BIT);
gl.colorMask(false, false, false, false);
gl.stencilFunc(gl.NOTEQUAL, 1, 1);
gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
// Write stencil buffer
geometryMask.renderWebGL(renderer, geometryMask, 0.0, camera);
renderer.flush();
// Use stencil buffer to affect next rendering object
gl.colorMask(true, true, true, true);
gl.stencilFunc(gl.EQUAL, 1, 1);
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
},
/**
* [description]
*
* @method Phaser.Display.Masks.GeometryMask#postRenderWebGL
* @since 3.0.0
*
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - [description]
*/
postRenderWebGL: function (renderer)
{
var gl = renderer.gl;
// Force flush before disabling stencil test
renderer.flush();
gl.disable(gl.STENCIL_TEST);
},
/**
* [description]
*
* @method Phaser.Display.Masks.GeometryMask#preRenderCanvas
* @since 3.0.0
*
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - [description]
* @param {Phaser.GameObjects.GameObject} mask - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
*/
preRenderCanvas: function (renderer, mask, camera)
{
var geometryMask = this.geometryMask;
renderer.currentContext.save();
geometryMask.renderCanvas(renderer, geometryMask, 0, camera, null, null, true);
renderer.currentContext.clip();
},
/**
* [description]
*
* @method Phaser.Display.Masks.GeometryMask#postRenderCanvas
* @since 3.0.0
*
* @param {Phaser.Renderer.Canvas.CanvasRenderer} renderer - [description]
*/
postRenderCanvas: function (renderer)
{
renderer.currentContext.restore();
},
/**
* Destroys this GeometryMask and nulls any references it holds.
*
* Note that if a Game Object is currently using this mask it will _not_ automatically detect you have destroyed it,
* so be sure to call `clearMask` on any Game Object using it, before destroying it.
*
* @method Phaser.Display.Masks.GeometryMask#destroy
* @since 3.7.0
*/
destroy: function ()
{
this.geometryMask = null;
}
});
module.exports = GeometryMask;