Skip to content

Commit 1cc0e7d

Browse files
committed
Moved the rendering code from the pipeline to the Graphics object
1 parent 620c5a3 commit 1cc0e7d

1 file changed

Lines changed: 276 additions & 5 deletions

File tree

src/gameobjects/graphics/GraphicsWebGLRenderer.js

Lines changed: 276 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@
44
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
55
*/
66

7+
var Commands = require('./Commands');
8+
var Utils = require('../../renderer/webgl/Utils');
9+
10+
// TODO: Remove the use of this
11+
var Point = function (x, y, width)
12+
{
13+
this.x = x;
14+
this.y = y;
15+
this.width = width;
16+
};
17+
18+
// TODO: Remove the use of this
19+
var Path = function (x, y, width)
20+
{
21+
this.points = [];
22+
this.pointsLength = 1;
23+
this.points[0] = new Point(x, y, width);
24+
};
25+
26+
var matrixStack = [];
27+
728
/**
829
* Renders this Game Object with the WebGL Renderer to the given Camera.
930
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
@@ -21,15 +42,265 @@
2142
*/
2243
var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
2344
{
24-
var commandBuffer = src.commandBuffer;
25-
var commandBufferLength = commandBuffer.length;
26-
27-
if (commandBufferLength === 0)
45+
if (src.commandBuffer.length === 0)
2846
{
2947
return;
3048
}
3149

32-
this.pipeline.batchGraphics(src, camera, parentMatrix);
50+
var pipeline = this.pipeline;
51+
52+
var camMatrix = pipeline._tempMatrix1;
53+
var graphicsMatrix = pipeline._tempMatrix2;
54+
var currentMatrix = pipeline._tempMatrix4;
55+
56+
renderer.setPipeline(pipeline);
57+
58+
currentMatrix.loadIdentity();
59+
60+
graphicsMatrix.applyITRS(src.x, src.y, src.rotation, src.scaleX, src.scaleY);
61+
62+
camMatrix.copyFrom(camera.matrix);
63+
64+
if (parentMatrix)
65+
{
66+
// Multiply the camera by the parent matrix
67+
camMatrix.multiplyWithOffset(parentMatrix, -camera.scrollX * src.scrollFactorX, -camera.scrollY * src.scrollFactorY);
68+
69+
// Undo the camera scroll
70+
graphicsMatrix.e = src.x;
71+
graphicsMatrix.f = src.y;
72+
73+
// Multiply by the Sprite matrix, store result in calcMatrix
74+
camMatrix.multiply(graphicsMatrix);
75+
}
76+
else
77+
{
78+
graphicsMatrix.e -= camera.scrollX * src.scrollFactorX;
79+
graphicsMatrix.f -= camera.scrollY * src.scrollFactorY;
80+
81+
// Multiply by the Sprite matrix, store result in calcMatrix
82+
camMatrix.multiply(graphicsMatrix);
83+
}
84+
85+
var commands = src.commandBuffer;
86+
var alpha = camera.alpha * src.alpha;
87+
var lineAlpha = 1.0;
88+
var fillAlpha = 1.0;
89+
var lineColor = 0;
90+
var fillColor = 0;
91+
var lineWidth = 1.0;
92+
var lastPath = null;
93+
var iteration = 0;
94+
var iterStep = 0.01;
95+
var tx = 0;
96+
var ty = 0;
97+
var ta = 0;
98+
var x = 0;
99+
var y = 0;
100+
var radius = 0;
101+
var startAngle = 0;
102+
var endAngle = 0;
103+
var cmd;
104+
var path = [];
105+
var pathIndex = 0;
106+
var pathOpen = false;
107+
108+
for (var cmdIndex = 0; cmdIndex < commands.length; cmdIndex++)
109+
{
110+
cmd = commands[cmdIndex];
111+
112+
switch (cmd)
113+
{
114+
case Commands.BEGIN_PATH:
115+
116+
path.length = 0;
117+
lastPath = null;
118+
pathOpen = true;
119+
break;
120+
121+
case Commands.CLOSE_PATH:
122+
123+
pathOpen = false;
124+
125+
if (lastPath && lastPath.points.length)
126+
{
127+
lastPath.points.push(lastPath.points[0]);
128+
}
129+
break;
130+
131+
case Commands.FILL_PATH:
132+
for (pathIndex = 0; pathIndex < path.length; pathIndex++)
133+
{
134+
pipeline.batchFillPath(
135+
path[pathIndex].points,
136+
currentMatrix,
137+
camMatrix
138+
);
139+
}
140+
break;
141+
142+
case Commands.STROKE_PATH:
143+
for (pathIndex = 0; pathIndex < path.length; pathIndex++)
144+
{
145+
pipeline.batchStrokePath(
146+
path[pathIndex].points,
147+
lineWidth,
148+
pathOpen,
149+
currentMatrix,
150+
camMatrix
151+
);
152+
}
153+
break;
154+
155+
case Commands.LINE_STYLE:
156+
lineWidth = commands[++cmdIndex];
157+
lineColor = commands[++cmdIndex];
158+
lineAlpha = commands[++cmdIndex];
159+
pipeline.strokeTint = Utils.getTintAppendFloatAlphaAndSwap(lineColor, lineAlpha * alpha);
160+
break;
161+
162+
case Commands.FILL_STYLE:
163+
fillColor = commands[++cmdIndex];
164+
fillAlpha = commands[++cmdIndex];
165+
pipeline.fillTint = Utils.getTintAppendFloatAlphaAndSwap(fillColor, fillAlpha * alpha);
166+
break;
167+
168+
case Commands.ARC:
169+
iteration = 0;
170+
x = commands[++cmdIndex];
171+
y = commands[++cmdIndex];
172+
radius = commands[++cmdIndex];
173+
startAngle = commands[++cmdIndex];
174+
endAngle = commands[++cmdIndex];
175+
var anticlockwise = commands[++cmdIndex];
176+
177+
if (lastPath === null)
178+
{
179+
lastPath = new Path(x + Math.cos(startAngle) * radius, y + Math.sin(startAngle) * radius, lineWidth);
180+
path.push(lastPath);
181+
iteration += iterStep;
182+
}
183+
184+
while (iteration < 1)
185+
{
186+
ta = endAngle * iteration + startAngle;
187+
tx = x + Math.cos(ta) * radius;
188+
ty = y + Math.sin(ta) * radius;
189+
190+
lastPath.points.push(new Point(tx, ty, lineWidth));
191+
192+
iteration += iterStep;
193+
}
194+
195+
ta = endAngle + startAngle;
196+
tx = x + Math.cos(ta) * radius;
197+
ty = y + Math.sin(ta) * radius;
198+
199+
lastPath.points.push(new Point(tx, ty, lineWidth));
200+
201+
break;
202+
203+
case Commands.FILL_RECT:
204+
pipeline.batchFillRect(
205+
commands[++cmdIndex],
206+
commands[++cmdIndex],
207+
commands[++cmdIndex],
208+
commands[++cmdIndex],
209+
currentMatrix,
210+
camMatrix
211+
);
212+
break;
213+
214+
case Commands.FILL_TRIANGLE:
215+
pipeline.batchFillTriangle(
216+
commands[++cmdIndex],
217+
commands[++cmdIndex],
218+
commands[++cmdIndex],
219+
commands[++cmdIndex],
220+
commands[++cmdIndex],
221+
commands[++cmdIndex],
222+
currentMatrix,
223+
camMatrix
224+
);
225+
break;
226+
227+
case Commands.STROKE_TRIANGLE:
228+
pipeline.batchStrokeTriangle(
229+
commands[++cmdIndex],
230+
commands[++cmdIndex],
231+
commands[++cmdIndex],
232+
commands[++cmdIndex],
233+
commands[++cmdIndex],
234+
commands[++cmdIndex],
235+
lineWidth,
236+
currentMatrix,
237+
camMatrix
238+
);
239+
break;
240+
241+
case Commands.LINE_TO:
242+
if (lastPath !== null)
243+
{
244+
lastPath.points.push(new Point(commands[cmdIndex + 1], commands[cmdIndex + 2], lineWidth));
245+
}
246+
else
247+
{
248+
lastPath = new Path(commands[cmdIndex + 1], commands[cmdIndex + 2], lineWidth);
249+
path.push(lastPath);
250+
}
251+
cmdIndex += 2;
252+
break;
253+
254+
case Commands.MOVE_TO:
255+
lastPath = new Path(commands[cmdIndex + 1], commands[cmdIndex + 2], lineWidth);
256+
path.push(lastPath);
257+
cmdIndex += 2;
258+
break;
259+
260+
case Commands.SAVE:
261+
matrixStack.push(currentMatrix.copyToArray());
262+
break;
263+
264+
case Commands.RESTORE:
265+
currentMatrix.copyFromArray(matrixStack.pop());
266+
break;
267+
268+
case Commands.TRANSLATE:
269+
x = commands[++cmdIndex];
270+
y = commands[++cmdIndex];
271+
currentMatrix.translate(x, y);
272+
break;
273+
274+
case Commands.SCALE:
275+
x = commands[++cmdIndex];
276+
y = commands[++cmdIndex];
277+
currentMatrix.scale(x, y);
278+
break;
279+
280+
case Commands.ROTATE:
281+
var r = commands[++cmdIndex];
282+
currentMatrix.rotate(r);
283+
break;
284+
285+
case Commands.SET_TEXTURE:
286+
var frame = commands[++cmdIndex];
287+
var mode = commands[++cmdIndex];
288+
289+
pipeline.currentFrame = frame;
290+
renderer.setTexture2D(frame.glTexture, 0);
291+
pipeline.tintEffect = mode;
292+
293+
break;
294+
295+
case Commands.CLEAR_TEXTURE:
296+
pipeline.currentFrame = renderer.blankTexture;
297+
renderer.setTexture2D(renderer.blankTexture.glTexture, 0);
298+
pipeline.tintEffect = 2;
299+
300+
break;
301+
302+
}
303+
}
33304
};
34305

35306
module.exports = GraphicsWebGLRenderer;

0 commit comments

Comments
 (0)