Skip to content

Commit 6027b65

Browse files
committed
Added fill gradient and line gradient methods
1 parent 0c50b0e commit 6027b65

3 files changed

Lines changed: 129 additions & 37 deletions

File tree

src/gameobjects/graphics/Commands.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ module.exports = {
3030
SCALE: 17,
3131
ROTATE: 18,
3232
SET_TEXTURE: 19,
33-
CLEAR_TEXTURE: 20
33+
CLEAR_TEXTURE: 20,
34+
GRADIENT_FILL_STYLE: 21,
35+
GRADIENT_LINE_STYLE: 22
3436

3537
};

src/gameobjects/graphics/Graphics.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,79 @@ var Graphics = new Class({
314314
return this;
315315
},
316316

317+
/**
318+
* Sets a gradient fill style. This is a WebGL only feature.
319+
*
320+
* The gradient color values represent the 4 corners of an untransformed rectangle.
321+
* The gradient is used to color all filled shapes and paths drawn after calling this method.
322+
* If you wish to turn a gradient off, call `fillStyle` and provide a new single fill color.
323+
*
324+
* When filling a triangle only the first 3 color values provided are used for the 3 points of a triangle.
325+
*
326+
* Note that for objects such as arcs or ellipses, or anything which is made out of triangles, each triangle used
327+
* will be filled with a gradient on its own. There is no ability to gradient fill a shape or path as a single
328+
* item at this time.
329+
*
330+
* @method Phaser.GameObjects.Graphics#fillGradientStyle
331+
* @webglOnly
332+
* @since 3.12.0
333+
*
334+
* @param {integer} topLeft - The tint being applied to the top-left of the Game Object.
335+
* @param {integer} topRight - The tint being applied to the top-right of the Game Object.
336+
* @param {integer} bottomLeft - The tint being applied to the bottom-left of the Game Object.
337+
* @param {integer} bottomRight - The tint being applied to the bottom-right of the Game Object.
338+
* @param {number} [alpha=1] - The fill alpha.
339+
*
340+
* @return {Phaser.GameObjects.Graphics} This Game Object.
341+
*/
342+
fillGradientStyle: function (topLeft, topRight, bottomLeft, bottomRight, alpha)
343+
{
344+
if (alpha === undefined) { alpha = 1; }
345+
346+
this.commandBuffer.push(
347+
Commands.GRADIENT_FILL_STYLE,
348+
alpha, topLeft, topRight, bottomLeft, bottomRight
349+
);
350+
351+
return this;
352+
},
353+
354+
/**
355+
* Sets a gradient line style. This is a WebGL only feature.
356+
*
357+
* The gradient color values represent the 4 corners of an untransformed rectangle.
358+
* The gradient is used to color all stroked shapes and paths drawn after calling this method.
359+
* If you wish to turn a gradient off, call `lineStyle` and provide a new single line color.
360+
*
361+
* Note that for objects such as arcs or ellipses, or anything which is made out of triangles, each triangle used
362+
* will be filled with a gradient on its own. There is no ability to gradient stroke a shape or path as a single
363+
* item at this time.
364+
*
365+
* @method Phaser.GameObjects.Graphics#lineGradientStyle
366+
* @webglOnly
367+
* @since 3.12.0
368+
*
369+
* @param {number} lineWidth - The stroke width.
370+
* @param {integer} topLeft - The tint being applied to the top-left of the Game Object.
371+
* @param {integer} topRight - The tint being applied to the top-right of the Game Object.
372+
* @param {integer} bottomLeft - The tint being applied to the bottom-left of the Game Object.
373+
* @param {integer} bottomRight - The tint being applied to the bottom-right of the Game Object.
374+
* @param {number} [alpha=1] - The fill alpha.
375+
*
376+
* @return {Phaser.GameObjects.Graphics} This Game Object.
377+
*/
378+
lineGradientStyle: function (lineWidth, topLeft, topRight, bottomLeft, bottomRight, alpha)
379+
{
380+
if (alpha === undefined) { alpha = 1; }
381+
382+
this.commandBuffer.push(
383+
Commands.GRADIENT_LINE_STYLE,
384+
lineWidth, alpha, topLeft, topRight, bottomLeft, bottomRight
385+
);
386+
387+
return this;
388+
},
389+
317390
/**
318391
* Sets the texture and frame this Graphics Object will use when texturing the shapes it renders.
319392
*

src/gameobjects/graphics/GraphicsWebGLRenderer.js

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,24 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
8484

8585
var commands = src.commandBuffer;
8686
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;
87+
88+
var lineWidth = 1;
89+
var fillTint = pipeline.fillTint;
90+
var strokeTint = pipeline.strokeTint;
91+
9592
var tx = 0;
9693
var ty = 0;
9794
var ta = 0;
98-
var x = 0;
99-
var y = 0;
100-
var radius = 0;
101-
var startAngle = 0;
102-
var endAngle = 0;
95+
var iterStep = 0.01;
96+
10397
var cmd;
98+
10499
var path = [];
105100
var pathIndex = 0;
106101
var pathOpen = false;
102+
var lastPath = null;
103+
104+
var getTint = Utils.getTintAppendFloatAlphaAndSwap;
107105

108106
for (var cmdIndex = 0; cmdIndex < commands.length; cmdIndex++)
109107
{
@@ -154,27 +152,51 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
154152

155153
case Commands.LINE_STYLE:
156154
lineWidth = commands[++cmdIndex];
157-
lineColor = commands[++cmdIndex];
158-
lineAlpha = commands[++cmdIndex];
159-
pipeline.strokeTint = Utils.getTintAppendFloatAlphaAndSwap(lineColor, lineAlpha * alpha);
155+
var strokeColor = commands[++cmdIndex];
156+
var strokeAlpha = commands[++cmdIndex] * alpha;
157+
var strokeTintColor = getTint(strokeColor, strokeAlpha);
158+
strokeTint.TL = strokeTintColor;
159+
strokeTint.TR = strokeTintColor;
160+
strokeTint.BL = strokeTintColor;
161+
strokeTint.BR = strokeTintColor;
160162
break;
161163

162164
case Commands.FILL_STYLE:
163-
fillColor = commands[++cmdIndex];
164-
fillAlpha = commands[++cmdIndex];
165-
pipeline.fillTint = Utils.getTintAppendFloatAlphaAndSwap(fillColor, fillAlpha * alpha);
165+
var fillColor = commands[++cmdIndex];
166+
var fillAlpha = commands[++cmdIndex] * alpha;
167+
var fillTintColor = getTint(fillColor, fillAlpha);
168+
fillTint.TL = fillTintColor;
169+
fillTint.TR = fillTintColor;
170+
fillTint.BL = fillTintColor;
171+
fillTint.BR = fillTintColor;
172+
break;
173+
174+
case Commands.GRADIENT_FILL_STYLE:
175+
var gradientFillAlpha = commands[++cmdIndex] * alpha;
176+
fillTint.TL = getTint(commands[++cmdIndex], gradientFillAlpha);
177+
fillTint.TR = getTint(commands[++cmdIndex], gradientFillAlpha);
178+
fillTint.BL = getTint(commands[++cmdIndex], gradientFillAlpha);
179+
fillTint.BR = getTint(commands[++cmdIndex], gradientFillAlpha);
180+
break;
181+
182+
case Commands.GRADIENT_LINE_STYLE:
183+
lineWidth = commands[++cmdIndex];
184+
var gradientLineAlpha = commands[++cmdIndex] * alpha;
185+
strokeTint.TL = getTint(commands[++cmdIndex], gradientLineAlpha);
186+
strokeTint.TR = getTint(commands[++cmdIndex], gradientLineAlpha);
187+
strokeTint.BL = getTint(commands[++cmdIndex], gradientLineAlpha);
188+
strokeTint.BR = getTint(commands[++cmdIndex], gradientLineAlpha);
166189
break;
167190

168191
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];
192+
var iteration = 0;
193+
var x = commands[++cmdIndex];
194+
var y = commands[++cmdIndex];
195+
var radius = commands[++cmdIndex];
196+
var startAngle = commands[++cmdIndex];
197+
var endAngle = commands[++cmdIndex];
175198

176-
// var anticlockwise
177-
cmdIndex++;
199+
cmdIndex++; // anticlockwise (canvas only)
178200

179201
if (lastPath === null)
180202
{
@@ -243,20 +265,18 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
243265
case Commands.LINE_TO:
244266
if (lastPath !== null)
245267
{
246-
lastPath.points.push(new Point(commands[cmdIndex + 1], commands[cmdIndex + 2], lineWidth));
268+
lastPath.points.push(new Point(commands[++cmdIndex], commands[++cmdIndex], lineWidth));
247269
}
248270
else
249271
{
250-
lastPath = new Path(commands[cmdIndex + 1], commands[cmdIndex + 2], lineWidth);
272+
lastPath = new Path(commands[++cmdIndex], commands[++cmdIndex], lineWidth);
251273
path.push(lastPath);
252274
}
253-
cmdIndex += 2;
254275
break;
255276

256277
case Commands.MOVE_TO:
257-
lastPath = new Path(commands[cmdIndex + 1], commands[cmdIndex + 2], lineWidth);
278+
lastPath = new Path(commands[++cmdIndex], commands[++cmdIndex], lineWidth);
258279
path.push(lastPath);
259-
cmdIndex += 2;
260280
break;
261281

262282
case Commands.SAVE:
@@ -280,8 +300,7 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
280300
break;
281301

282302
case Commands.ROTATE:
283-
var r = commands[++cmdIndex];
284-
currentMatrix.rotate(r);
303+
currentMatrix.rotate(commands[++cmdIndex]);
285304
break;
286305

287306
case Commands.SET_TEXTURE:
@@ -291,14 +310,12 @@ var GraphicsWebGLRenderer = function (renderer, src, interpolationPercentage, ca
291310
pipeline.currentFrame = frame;
292311
renderer.setTexture2D(frame.glTexture, 0);
293312
pipeline.tintEffect = mode;
294-
295313
break;
296314

297315
case Commands.CLEAR_TEXTURE:
298316
pipeline.currentFrame = renderer.blankTexture;
299317
renderer.setTexture2D(renderer.blankTexture.glTexture, 0);
300318
pipeline.tintEffect = 2;
301-
302319
break;
303320

304321
}

0 commit comments

Comments
 (0)