Skip to content

Commit ddf535c

Browse files
committed
Rope can now draw vertex debug to a Graphics instance
1 parent c357b5e commit ddf535c

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

src/gameobjects/rope/Rope.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,39 @@ var Rope = new Class({
230230
*/
231231
this._perp = new Vector2();
232232

233+
/**
234+
* You can optionally choose to render the vertices of this Rope to a Graphics instance.
235+
*
236+
* Achieve this by setting the `debugCallback` and the `debugGraphic` properties.
237+
*
238+
* You can do this in a single call via the `Rope.setDebug` method, which will use the
239+
* built-in debug function. You can also set it to your own callback. The callback
240+
* will be invoked _once per render_ and sent the following parameters:
241+
*
242+
* `debugCallback(src, meshLength, verts)`
243+
*
244+
* `src` is the Rope instance being debugged.
245+
* `meshLength` is the number of mesh vertices in total.
246+
* `verts` is an array of the translated vertex coordinates.
247+
*
248+
* To disable rendering, set this property back to `null`.
249+
*
250+
* @name Phaser.GameObjects.Rope#debugCallback
251+
* @type {function}
252+
* @since 3.23.0
253+
*/
254+
this.debugCallback = null;
255+
256+
/**
257+
* The Graphics instance that the debug vertices will be drawn to, if `setDebug` has
258+
* been called.
259+
*
260+
* @name Phaser.GameObjects.Rope#debugGraphic
261+
* @type {Phaser.GameObjects.Graphics}
262+
* @since 3.23.0
263+
*/
264+
this.debugGraphic = null;
265+
233266
this.setTexture(texture, frame);
234267
this.setPosition(x, y);
235268
this.setSizeToFrame();
@@ -890,6 +923,95 @@ var Rope = new Class({
890923
return this;
891924
},
892925

926+
/**
927+
* This method enables rendering of the Rope vertices to the given Graphics instance.
928+
*
929+
* If you enable this feature, you must call `Graphics.clear()` in your Scene `update`,
930+
* otherwise the Graphics instance will fill-in with draw calls. This is not done automatically
931+
* to allow for you to debug render multiple Rope objects to a single Graphics instance.
932+
*
933+
* The Rope class has a built-in debug rendering callback `Rope.renderDebugVerts`, however
934+
* you can also provide your own callback to be used instead. Do this by setting the `callback` parameter.
935+
*
936+
* The callback is invoked _once per render_ and sent the following parameters:
937+
*
938+
* `callback(src, meshLength, verts)`
939+
*
940+
* `src` is the Rope instance being debugged.
941+
* `meshLength` is the number of mesh vertices in total.
942+
* `verts` is an array of the translated vertex coordinates.
943+
*
944+
* If using your own callback you do not have to provide a Graphics instance to this method.
945+
*
946+
* To disable debug rendering, to either your own callback or the built-in one, call this method
947+
* with no arguments.
948+
*
949+
* @method Phaser.GameObjects.Rope#setDebug
950+
* @since 3.23.0
951+
*
952+
* @param {Phaser.GameObjects.Graphics} [graphic] - The Graphic instance to render to if using the built-in callback.
953+
* @param {function} [callback] - The callback to invoke during debug render. Leave as undefined to use the built-in callback.
954+
*
955+
* @return {this} This Game Object instance.
956+
*/
957+
setDebug: function (graphic, callback)
958+
{
959+
this.debugGraphic = graphic;
960+
961+
if (!graphic && !callback)
962+
{
963+
this.debugCallback = null;
964+
}
965+
else if (callback)
966+
{
967+
this.debugCallback = this.renderDebugVerts;
968+
}
969+
970+
return this;
971+
},
972+
973+
/**
974+
* The built-in Rope vertices debug rendering method.
975+
*
976+
* See `Rope.setDebug` for more details.
977+
*
978+
* @method Phaser.GameObjects.Rope#renderDebugVerts
979+
* @since 3.23.0
980+
*
981+
* @param {Phaser.GameObjects.Rope} src - The Rope object being rendered.
982+
* @param {integer} meshLength - The number of vertices in the mesh.
983+
* @param {number[]} verts - An array of translated vertex coordinates.
984+
*/
985+
renderDebugVerts: function (src, meshLength, verts)
986+
{
987+
var graphic = src.debugGraphic;
988+
989+
var px0 = verts[0];
990+
var py0 = verts[1];
991+
var px1 = verts[2];
992+
var py1 = verts[3];
993+
994+
graphic.lineBetween(px0, py0, px1, py1);
995+
996+
for (var i = 4; i < meshLength; i += 4)
997+
{
998+
var x0 = verts[i + 0];
999+
var y0 = verts[i + 1];
1000+
var x1 = verts[i + 2];
1001+
var y1 = verts[i + 3];
1002+
1003+
graphic.lineBetween(px0, py0, x0, y0);
1004+
graphic.lineBetween(px1, py1, x1, y1);
1005+
graphic.lineBetween(px1, py1, x0, y0);
1006+
graphic.lineBetween(x0, y0, x1, y1);
1007+
1008+
px0 = x0;
1009+
py0 = y0;
1010+
px1 = x1;
1011+
py1 = y1;
1012+
}
1013+
},
1014+
8931015
/**
8941016
* Handles the pre-destroy step for the Rope, which removes the Animation component and typed arrays.
8951017
*
@@ -908,6 +1030,9 @@ var Rope = new Class({
9081030
this.uv = null;
9091031
this.colors = null;
9101032
this.alphas = null;
1033+
1034+
this.debugCallback = null;
1035+
this.debugGraphic = null;
9111036
},
9121037

9131038
/**

src/gameobjects/rope/RopeWebGLRenderer.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ var RopeWebGLRenderer = function (renderer, src, interpolationPercentage, camera
8989
src.updateVertices();
9090
}
9191

92+
var debugCallback = src.debugCallback;
93+
var debugVerts = [];
94+
9295
for (var i = 0; i < meshVerticesLength; i += 2)
9396
{
9497
var x = vertices[i + 0];
@@ -111,6 +114,17 @@ var RopeWebGLRenderer = function (renderer, src, interpolationPercentage, camera
111114
vertexViewU32[++vertexOffset] = getTint(colors[colorIndex], camera.alpha * (alphas[colorIndex] * alpha));
112115

113116
colorIndex++;
117+
118+
if (debugCallback)
119+
{
120+
debugVerts[i + 0] = tx;
121+
debugVerts[i + 1] = ty;
122+
}
123+
}
124+
125+
if (debugCallback)
126+
{
127+
debugCallback.call(src, src, meshVerticesLength, debugVerts);
114128
}
115129

116130
pipeline.vertexCount += vertexCount;

0 commit comments

Comments
 (0)