[monogame] Added Z attribute at SkeletonDebugRenderer, added optional z parameters at ShapeRenderer methods.

This commit is contained in:
Harald Csaszar 2023-01-09 15:54:58 +01:00
parent 13dbc48cff
commit 2b8d70afaa
2 changed files with 46 additions and 40 deletions

View File

@ -65,18 +65,18 @@ namespace Spine {
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);
@ -85,37 +85,37 @@ namespace Spine {
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;
@ -139,15 +139,15 @@ namespace Spine {
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 () {

View File

@ -48,6 +48,11 @@ namespace Spine {
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; }
@ -104,9 +109,9 @@ namespace Spine {
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) {
@ -119,10 +124,10 @@ namespace Spine {
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);
}
}
}
@ -144,7 +149,8 @@ namespace Spine {
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
);
}
}
@ -154,7 +160,7 @@ namespace Spine {
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;
}
@ -166,12 +172,12 @@ namespace Spine {
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);
}
}
@ -179,7 +185,7 @@ namespace Spine {
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);
}
}
@ -200,7 +206,7 @@ namespace Spine {
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);
}
@ -214,7 +220,7 @@ namespace Spine {
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);
}
}
}