-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
GraphicsWebGLRenderer.js
349 lines (300 loc) · 10.7 KB
/
GraphicsWebGLRenderer.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2013-2023 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Commands = require('./Commands');
var GetCalcMatrix = require('../GetCalcMatrix');
var TransformMatrix = require('../components/TransformMatrix');
var Utils = require('../../renderer/webgl/Utils');
var Point = function (x, y, width)
{
this.x = x;
this.y = y;
this.width = width;
};
var Path = function (x, y, width)
{
this.points = [];
this.pointsLength = 1;
this.points[0] = new Point(x, y, width);
};
var matrixStack = [];
var tempMatrix = new TransformMatrix();
/**
* Renders this Game Object with the WebGL Renderer to the given Camera.
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
* This method should not be called directly. It is a utility function of the Render module.
*
* @method Phaser.GameObjects.Graphics#renderWebGL
* @since 3.0.0
* @private
*
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
* @param {Phaser.GameObjects.Graphics} src - The Game Object being rendered in this call.
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
*/
var GraphicsWebGLRenderer = function (renderer, src, camera, parentMatrix)
{
if (src.commandBuffer.length === 0)
{
return;
}
camera.addToRenderList(src);
var pipeline = renderer.pipelines.set(src.pipeline, src);
renderer.pipelines.preBatch(src);
var calcMatrix = GetCalcMatrix(src, camera, parentMatrix).calc;
var currentMatrix = tempMatrix.loadIdentity();
var commands = src.commandBuffer;
var alpha = camera.alpha * src.alpha;
var lineWidth = 1;
var fillTint = pipeline.fillTint;
var strokeTint = pipeline.strokeTint;
var tx = 0;
var ty = 0;
var ta = 0;
var iterStep = 0.01;
var PI2 = Math.PI * 2;
var cmd;
var path = [];
var pathIndex = 0;
var pathOpen = true;
var lastPath = null;
var getTint = Utils.getTintAppendFloatAlpha;
for (var cmdIndex = 0; cmdIndex < commands.length; cmdIndex++)
{
cmd = commands[cmdIndex];
switch (cmd)
{
case Commands.BEGIN_PATH:
{
path.length = 0;
lastPath = null;
pathOpen = true;
break;
}
case Commands.CLOSE_PATH:
{
pathOpen = false;
if (lastPath && lastPath.points.length)
{
lastPath.points.push(lastPath.points[0]);
}
break;
}
case Commands.FILL_PATH:
{
for (pathIndex = 0; pathIndex < path.length; pathIndex++)
{
pipeline.batchFillPath(
path[pathIndex].points,
currentMatrix,
calcMatrix
);
}
break;
}
case Commands.STROKE_PATH:
{
for (pathIndex = 0; pathIndex < path.length; pathIndex++)
{
pipeline.batchStrokePath(
path[pathIndex].points,
lineWidth,
pathOpen,
currentMatrix,
calcMatrix
);
}
break;
}
case Commands.LINE_STYLE:
{
lineWidth = commands[++cmdIndex];
var strokeColor = commands[++cmdIndex];
var strokeAlpha = commands[++cmdIndex] * alpha;
var strokeTintColor = getTint(strokeColor, strokeAlpha);
strokeTint.TL = strokeTintColor;
strokeTint.TR = strokeTintColor;
strokeTint.BL = strokeTintColor;
strokeTint.BR = strokeTintColor;
break;
}
case Commands.FILL_STYLE:
{
var fillColor = commands[++cmdIndex];
var fillAlpha = commands[++cmdIndex] * alpha;
var fillTintColor = getTint(fillColor, fillAlpha);
fillTint.TL = fillTintColor;
fillTint.TR = fillTintColor;
fillTint.BL = fillTintColor;
fillTint.BR = fillTintColor;
break;
}
case Commands.GRADIENT_FILL_STYLE:
{
var alphaTL = commands[++cmdIndex] * alpha;
var alphaTR = commands[++cmdIndex] * alpha;
var alphaBL = commands[++cmdIndex] * alpha;
var alphaBR = commands[++cmdIndex] * alpha;
fillTint.TL = getTint(commands[++cmdIndex], alphaTL);
fillTint.TR = getTint(commands[++cmdIndex], alphaTR);
fillTint.BL = getTint(commands[++cmdIndex], alphaBL);
fillTint.BR = getTint(commands[++cmdIndex], alphaBR);
break;
}
case Commands.GRADIENT_LINE_STYLE:
{
lineWidth = commands[++cmdIndex];
var gradientLineAlpha = commands[++cmdIndex] * alpha;
strokeTint.TL = getTint(commands[++cmdIndex], gradientLineAlpha);
strokeTint.TR = getTint(commands[++cmdIndex], gradientLineAlpha);
strokeTint.BL = getTint(commands[++cmdIndex], gradientLineAlpha);
strokeTint.BR = getTint(commands[++cmdIndex], gradientLineAlpha);
break;
}
case Commands.ARC:
{
var iteration = 0;
var x = commands[++cmdIndex];
var y = commands[++cmdIndex];
var radius = commands[++cmdIndex];
var startAngle = commands[++cmdIndex];
var endAngle = commands[++cmdIndex];
var anticlockwise = commands[++cmdIndex];
var overshoot = commands[++cmdIndex];
endAngle -= startAngle;
if (anticlockwise)
{
if (endAngle < -PI2)
{
endAngle = -PI2;
}
else if (endAngle > 0)
{
endAngle = -PI2 + endAngle % PI2;
}
}
else if (endAngle > PI2)
{
endAngle = PI2;
}
else if (endAngle < 0)
{
endAngle = PI2 + endAngle % PI2;
}
if (lastPath === null)
{
lastPath = new Path(x + Math.cos(startAngle) * radius, y + Math.sin(startAngle) * radius, lineWidth);
path.push(lastPath);
iteration += iterStep;
}
while (iteration < 1 + overshoot)
{
ta = endAngle * iteration + startAngle;
tx = x + Math.cos(ta) * radius;
ty = y + Math.sin(ta) * radius;
lastPath.points.push(new Point(tx, ty, lineWidth));
iteration += iterStep;
}
ta = endAngle + startAngle;
tx = x + Math.cos(ta) * radius;
ty = y + Math.sin(ta) * radius;
lastPath.points.push(new Point(tx, ty, lineWidth));
break;
}
case Commands.FILL_RECT:
{
pipeline.batchFillRect(
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
currentMatrix,
calcMatrix
);
break;
}
case Commands.FILL_TRIANGLE:
{
pipeline.batchFillTriangle(
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
currentMatrix,
calcMatrix
);
break;
}
case Commands.STROKE_TRIANGLE:
{
pipeline.batchStrokeTriangle(
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
commands[++cmdIndex],
lineWidth,
currentMatrix,
calcMatrix
);
break;
}
case Commands.LINE_TO:
{
if (lastPath !== null)
{
lastPath.points.push(new Point(commands[++cmdIndex], commands[++cmdIndex], lineWidth));
}
else
{
lastPath = new Path(commands[++cmdIndex], commands[++cmdIndex], lineWidth);
path.push(lastPath);
}
break;
}
case Commands.MOVE_TO:
{
lastPath = new Path(commands[++cmdIndex], commands[++cmdIndex], lineWidth);
path.push(lastPath);
break;
}
case Commands.SAVE:
{
matrixStack.push(currentMatrix.copyToArray());
break;
}
case Commands.RESTORE:
{
currentMatrix.copyFromArray(matrixStack.pop());
break;
}
case Commands.TRANSLATE:
{
x = commands[++cmdIndex];
y = commands[++cmdIndex];
currentMatrix.translate(x, y);
break;
}
case Commands.SCALE:
{
x = commands[++cmdIndex];
y = commands[++cmdIndex];
currentMatrix.scale(x, y);
break;
}
case Commands.ROTATE:
{
currentMatrix.rotate(commands[++cmdIndex]);
break;
}
}
}
renderer.pipelines.postBatch(src);
};
module.exports = GraphicsWebGLRenderer;