Skip to content

Commit

Permalink
[monogame] Added Z attribute at SkeletonDebugRenderer, added optional…
Browse files Browse the repository at this point in the history
… z parameters at ShapeRenderer methods.
  • Loading branch information
HaraldCsaszar committed Jan 9, 2023
1 parent 13dbc48 commit 2b8d70a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
54 changes: 27 additions & 27 deletions spine-monogame/spine-monogame/src/ShapeRenderer.cs
Expand Up @@ -65,18 +65,18 @@ public class ShapeRenderer {
device.BlendState = BlendState.AlphaBlend;
}

public void Line (float x1, float y1, float x2, float y2) {
vertices.Add(new VertexPositionColor(new Vector3(x1, y1, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x2, y2, 0), color));
public void Line (float x1, float y1, float x2, float y2, float z = 0f) {
vertices.Add(new VertexPositionColor(new Vector3(x1, y1, z), color));
vertices.Add(new VertexPositionColor(new Vector3(x2, y2, z), color));
}

/** Calls {@link #circle(float, float, float, int)} by estimating the number of segments needed for a smooth circle. */
public void Circle (float x, float y, float radius) {
Circle(x, y, radius, Math.Max(1, (int)(6 * (float)Math.Pow(radius, 1.0f / 3.0f))));
public void Circle (float x, float y, float radius, float z = 0f) {
Circle(x, y, radius, Math.Max(1, (int)(6 * (float)Math.Pow(radius, 1.0f / 3.0f))), z);
}

/** Draws a circle using {@link ShapeType#Line} or {@link ShapeType#Filled}. */
public void Circle (float x, float y, float radius, int segments) {
public void Circle (float x, float y, float radius, int segments, float z = 0f) {
if (segments <= 0) throw new ArgumentException("segments must be > 0.");
float angle = 2 * MathUtils.PI / segments;
float cos = MathUtils.Cos(angle);
Expand All @@ -85,37 +85,37 @@ public class ShapeRenderer {
float temp = 0;

for (int i = 0; i < segments; i++) {
vertices.Add(new VertexPositionColor(new Vector3(x + cx, y + cy, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x + cx, y + cy, z), color));
temp = cx;
cx = cos * cx - sin * cy;
cy = sin * temp + cos * cy;
vertices.Add(new VertexPositionColor(new Vector3(x + cx, y + cy, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x + cx, y + cy, z), color));
}
vertices.Add(new VertexPositionColor(new Vector3(x + cx, y + cy, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x + cx, y + cy, z), color));

temp = cx;
cx = radius;
cy = 0;
vertices.Add(new VertexPositionColor(new Vector3(x + cx, y + cy, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x + cx, y + cy, z), color));
}

public void Triangle (float x1, float y1, float x2, float y2, float x3, float y3) {
vertices.Add(new VertexPositionColor(new Vector3(x1, y1, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x2, y2, 0), color));
public void Triangle (float x1, float y1, float x2, float y2, float x3, float y3, float z = 0f) {
vertices.Add(new VertexPositionColor(new Vector3(x1, y1, z), color));
vertices.Add(new VertexPositionColor(new Vector3(x2, y2, z), color));

vertices.Add(new VertexPositionColor(new Vector3(x2, y2, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x3, y3, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x2, y2, z), color));
vertices.Add(new VertexPositionColor(new Vector3(x3, y3, z), color));

vertices.Add(new VertexPositionColor(new Vector3(x3, y3, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x1, y1, 0), color));
vertices.Add(new VertexPositionColor(new Vector3(x3, y3, z), color));
vertices.Add(new VertexPositionColor(new Vector3(x1, y1, z), color));
}

public void X (float x, float y, float len) {
Line(x + len, y + len, x - len, y - len);
Line(x - len, y + len, x + len, y - len);
public void X (float x, float y, float len, float z = 0f) {
Line(x + len, y + len, x - len, y - len, z);
Line(x - len, y + len, x + len, y - len, z);
}

public void Polygon (float[] polygonVertices, int offset, int count) {
public void Polygon (float[] polygonVertices, int offset, int count, float z = 0f) {
if (count < 3) throw new ArgumentException("Polygon must contain at least 3 vertices");

offset <<= 1;
Expand All @@ -139,15 +139,15 @@ public class ShapeRenderer {
y2 = polygonVertices[i + 3];
}

Line(x1, y1, x2, y2);
Line(x1, y1, x2, y2, z);
}
}

public void Rect (float x, float y, float width, float height) {
Line(x, y, x + width, y);
Line(x + width, y, x + width, y + height);
Line(x + width, y + height, x, y + height);
Line(x, y + height, x, y);
public void Rect (float x, float y, float width, float height, float z = 0f) {
Line(x, y, x + width, y, z);
Line(x + width, y, x + width, y + height, z);
Line(x + width, y + height, x, y + height, z);
Line(x, y + height, x, y, z);
}

public void End () {
Expand Down
32 changes: 19 additions & 13 deletions spine-monogame/spine-monogame/src/SkeletonDebugRenderer.cs
Expand Up @@ -48,6 +48,11 @@ public class SkeletonDebugRenderer {
public static Color aabbColor = new Color(0f, 1f, 0f, 0.5f);

public BasicEffect Effect { get { return renderer.Effect; } set { renderer.Effect = value; } }

/// <summary>A Z position offset added at each vertex.</summary>
private float z = 0.0f;
public float Z { get { return z; } set { z = value; } }

public bool DrawBones { get; set; }
public bool DrawRegionAttachments { get; set; }
public bool DrawBoundingBoxes { get; set; }
Expand Down Expand Up @@ -104,9 +109,9 @@ public class SkeletonDebugRenderer {
if (bone.Parent == null) continue;
var x = bone.Data.Length * bone.A + bone.WorldX;
var y = bone.Data.Length * bone.C + bone.WorldY;
renderer.Line(bone.WorldX, bone.WorldY, x, y);
renderer.Line(bone.WorldX, bone.WorldY, x, y, z);
}
if (DrawSkeletonXY) renderer.X(skeletonX, skeletonY, 4);
if (DrawSkeletonXY) renderer.X(skeletonX, skeletonY, 4, z);
}

if (DrawRegionAttachments) {
Expand All @@ -119,10 +124,10 @@ public class SkeletonDebugRenderer {
var regionAttachment = (RegionAttachment)attachment;
var vertices = this.vertices;
regionAttachment.ComputeWorldVertices(slot, vertices, 0, 2);
renderer.Line(vertices[0], vertices[1], vertices[2], vertices[3]);
renderer.Line(vertices[2], vertices[3], vertices[4], vertices[5]);
renderer.Line(vertices[4], vertices[5], vertices[6], vertices[7]);
renderer.Line(vertices[6], vertices[7], vertices[0], vertices[1]);
renderer.Line(vertices[0], vertices[1], vertices[2], vertices[3], z);
renderer.Line(vertices[2], vertices[3], vertices[4], vertices[5], z);
renderer.Line(vertices[4], vertices[5], vertices[6], vertices[7], z);
renderer.Line(vertices[6], vertices[7], vertices[0], vertices[1], z);
}
}
}
Expand All @@ -144,7 +149,8 @@ public class SkeletonDebugRenderer {
int v1 = triangles[ii] * 2, v2 = triangles[ii + 1] * 2, v3 = triangles[ii + 2] * 2;
renderer.Triangle(world[v1], world[v1 + 1], //
world[v2], world[v2 + 1], //
world[v3], world[v3 + 1] //
world[v3], world[v3 + 1], //
z
);
}
}
Expand All @@ -154,7 +160,7 @@ public class SkeletonDebugRenderer {
float lastX = vertices[hullLength - 2], lastY = vertices[hullLength - 1];
for (int ii = 0, nn = hullLength; ii < nn; ii += 2) {
float x = vertices[ii], y = vertices[ii + 1];
renderer.Line(x, y, lastX, lastY);
renderer.Line(x, y, lastX, lastY, z);
lastX = x;
lastY = y;
}
Expand All @@ -166,20 +172,20 @@ public class SkeletonDebugRenderer {
var bounds = this.bounds;
bounds.Update(skeleton, true);
renderer.SetColor(aabbColor);
renderer.Rect(bounds.MinX, bounds.MinY, bounds.Width, bounds.Height);
renderer.Rect(bounds.MinX, bounds.MinY, bounds.Width, bounds.Height, z);
var polygons = bounds.Polygons;
var boxes = bounds.BoundingBoxes;
for (int i = 0, n = polygons.Count; i < n; i++) {
var polygon = polygons.Items[i];
renderer.Polygon(polygon.Vertices, 0, polygon.Count);
renderer.Polygon(polygon.Vertices, 0, polygon.Count, z);
}
}

if (DrawBones) {
renderer.SetColor(boneOriginColor);
for (int i = 0, n = bones.Count; i < n; i++) {
var bone = bones.Items[i];
renderer.Circle(bone.WorldX, bone.WorldY, 3);
renderer.Circle(bone.WorldX, bone.WorldY, 3, z);
}
}

Expand All @@ -200,7 +206,7 @@ public class SkeletonDebugRenderer {
var y = world[ii + 1];
var x2 = world[(ii + 2) % nn];
var y2 = world[(ii + 3) % nn];
renderer.Line(x, y, x2, y2);
renderer.Line(x, y, x2, y2, z);
clippingPolygon.Add(x);
clippingPolygon.Add(y);
}
Expand All @@ -214,7 +220,7 @@ public class SkeletonDebugRenderer {
SkeletonClipping.MakeClockwise(polygon);
polygon.Add(polygon.Items[0]);
polygon.Add(polygon.Items[1]);
renderer.Polygon(polygon.Items, 0, polygon.Count >> 1);
renderer.Polygon(polygon.Items, 0, polygon.Count >> 1, z);
}
}
}
Expand Down

0 comments on commit 2b8d70a

Please sign in to comment.