增加调试骨骼

This commit is contained in:
ww-rm
2025-04-12 20:58:56 +08:00
parent 168f7a8173
commit 3b73aea5c0
8 changed files with 412 additions and 144 deletions

View File

@@ -260,7 +260,7 @@ namespace SpineViewer.Spine.Implementations.SpineObject
protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states) protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
{ {
vertexArray.Clear(); triangleVertices.Clear();
states.Texture = null; states.Texture = null;
states.Shader = SFMLShader.GetSpineShader(usePma); states.Shader = SFMLShader.GetSpineShader(usePma);
@@ -329,13 +329,10 @@ namespace SpineViewer.Spine.Implementations.SpineObject
states.Texture ??= texture; states.Texture ??= texture;
if (states.BlendMode != blendMode || states.Texture != texture) if (states.BlendMode != blendMode || states.Texture != texture)
{ {
if (vertexArray.VertexCount > 0) if (triangleVertices.VertexCount > 0)
{ {
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) triangleVertices.Clear();
target.Draw(vertexArray, states);
vertexArray.Clear();
} }
states.BlendMode = blendMode; states.BlendMode = blendMode;
states.Texture = texture; states.Texture = texture;
@@ -369,26 +366,56 @@ namespace SpineViewer.Spine.Implementations.SpineObject
vertex.Position.Y = worldVertices[index + 1]; vertex.Position.Y = worldVertices[index + 1];
vertex.TexCoords.X = uvs[index] * textureSizeX; vertex.TexCoords.X = uvs[index] * textureSizeX;
vertex.TexCoords.Y = uvs[index + 1] * textureSizeY; vertex.TexCoords.Y = uvs[index + 1] * textureSizeY;
vertexArray.Append(vertex); triangleVertices.Append(vertex);
} }
//clipping.ClipEnd(slot); //clipping.ClipEnd(slot);
} }
//clipping.ClipEnd(); //clipping.ClipEnd();
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) }
target.Draw(vertexArray, states);
// 包围盒 protected override void debugDraw(SFML.Graphics.RenderTarget target)
if (isDebug && isSelected && debugBounds) {
lineVertices.Clear();
rectLineVertices.Clear();
// 调试包围盒
if (debugBounds)
{ {
var b = bounds; var b = bounds;
boundsVertices[0] = boundsVertices[4] = new(new(b.Left, b.Top), BoundsColor); var v = new SFML.Graphics.Vertex() { Color = BoundsColor };
boundsVertices[1] = new(new(b.Right, b.Top), BoundsColor); v.Position = new(b.Left, b.Top); lineVertices.Append(v);
boundsVertices[2] = new(new(b.Right, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Top); lineVertices.Append(v); lineVertices.Append(v);
boundsVertices[3] = new(new(b.Left, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
target.Draw(boundsVertices); v.Position = new(b.Left, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
v.Position = new(b.Left, b.Top); lineVertices.Append(v);
}
if (debugBones)
{
var width = scale;
foreach (var bone in skeleton.Bones)
{
var boneLength = bone.Data.Length;
var p1 = new SFML.System.Vector2f(bone.WorldX, bone.WorldY);
var p2 = new SFML.System.Vector2f(bone.WorldX + boneLength * bone.M00, bone.WorldY + boneLength * bone.M10);
AddRectLine(p1, p2, BoneLineColor, width);
}
}
target.Draw(lineVertices);
target.Draw(rectLineVertices);
// 骨骼的点最后画, 层级处于上面
if (debugBones)
{
var radius = scale;
foreach (var bone in skeleton.Bones)
{
DrawCirclePoint(target, new(bone.WorldX, bone.WorldY), BonePointColor, radius);
}
} }
} }
} }

View File

@@ -219,7 +219,7 @@ namespace SpineViewer.Spine.Implementations.SpineObject
protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states) protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
{ {
vertexArray.Clear(); triangleVertices.Clear();
states.Texture = null; states.Texture = null;
states.Shader = SFMLShader.GetSpineShader(usePma); states.Shader = SFMLShader.GetSpineShader(usePma);
@@ -286,13 +286,10 @@ namespace SpineViewer.Spine.Implementations.SpineObject
states.Texture ??= texture; states.Texture ??= texture;
if (states.BlendMode != blendMode || states.Texture != texture) if (states.BlendMode != blendMode || states.Texture != texture)
{ {
if (vertexArray.VertexCount > 0) if (triangleVertices.VertexCount > 0)
{ {
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) triangleVertices.Clear();
target.Draw(vertexArray, states);
vertexArray.Clear();
} }
states.BlendMode = blendMode; states.BlendMode = blendMode;
states.Texture = texture; states.Texture = texture;
@@ -326,26 +323,56 @@ namespace SpineViewer.Spine.Implementations.SpineObject
vertex.Position.Y = worldVertices[index + 1]; vertex.Position.Y = worldVertices[index + 1];
vertex.TexCoords.X = uvs[index] * textureSizeX; vertex.TexCoords.X = uvs[index] * textureSizeX;
vertex.TexCoords.Y = uvs[index + 1] * textureSizeY; vertex.TexCoords.Y = uvs[index + 1] * textureSizeY;
vertexArray.Append(vertex); triangleVertices.Append(vertex);
} }
clipping.ClipEnd(slot); clipping.ClipEnd(slot);
} }
clipping.ClipEnd(); clipping.ClipEnd();
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) }
target.Draw(vertexArray, states);
// 包围盒 protected override void debugDraw(SFML.Graphics.RenderTarget target)
if (isDebug && isSelected && debugBounds) {
lineVertices.Clear();
rectLineVertices.Clear();
// 调试包围盒
if (debugBounds)
{ {
var b = bounds; var b = bounds;
boundsVertices[0] = boundsVertices[4] = new(new(b.Left, b.Top), BoundsColor); var v = new SFML.Graphics.Vertex() { Color = BoundsColor };
boundsVertices[1] = new(new(b.Right, b.Top), BoundsColor); v.Position = new(b.Left, b.Top); lineVertices.Append(v);
boundsVertices[2] = new(new(b.Right, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Top); lineVertices.Append(v); lineVertices.Append(v);
boundsVertices[3] = new(new(b.Left, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
target.Draw(boundsVertices); v.Position = new(b.Left, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
v.Position = new(b.Left, b.Top); lineVertices.Append(v);
}
if (debugBones)
{
var width = scale;
foreach (var bone in skeleton.Bones)
{
var boneLength = bone.Data.Length;
var p1 = new SFML.System.Vector2f(bone.WorldX, bone.WorldY);
var p2 = new SFML.System.Vector2f(bone.WorldX + boneLength * bone.A, bone.WorldY + boneLength * bone.C);
AddRectLine(p1, p2, BoneLineColor, width);
}
}
target.Draw(lineVertices);
target.Draw(rectLineVertices);
// 骨骼的点最后画, 层级处于上面
if (debugBones)
{
var radius = scale;
foreach (var bone in skeleton.Bones)
{
DrawCirclePoint(target, new(bone.WorldX, bone.WorldY), BonePointColor, radius);
}
} }
} }
} }

View File

@@ -191,7 +191,7 @@ namespace SpineViewer.Spine.Implementations.SpineObject
protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states) protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
{ {
vertexArray.Clear(); triangleVertices.Clear();
states.Texture = null; states.Texture = null;
states.Shader = SFMLShader.GetSpineShader(usePma); states.Shader = SFMLShader.GetSpineShader(usePma);
@@ -258,13 +258,10 @@ namespace SpineViewer.Spine.Implementations.SpineObject
states.Texture ??= texture; states.Texture ??= texture;
if (states.BlendMode != blendMode || states.Texture != texture) if (states.BlendMode != blendMode || states.Texture != texture)
{ {
if (vertexArray.VertexCount > 0) if (triangleVertices.VertexCount > 0)
{ {
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) triangleVertices.Clear();
target.Draw(vertexArray, states);
vertexArray.Clear();
} }
states.BlendMode = blendMode; states.BlendMode = blendMode;
states.Texture = texture; states.Texture = texture;
@@ -298,26 +295,56 @@ namespace SpineViewer.Spine.Implementations.SpineObject
vertex.Position.Y = worldVertices[index + 1]; vertex.Position.Y = worldVertices[index + 1];
vertex.TexCoords.X = uvs[index] * textureSizeX; vertex.TexCoords.X = uvs[index] * textureSizeX;
vertex.TexCoords.Y = uvs[index + 1] * textureSizeY; vertex.TexCoords.Y = uvs[index + 1] * textureSizeY;
vertexArray.Append(vertex); triangleVertices.Append(vertex);
} }
clipping.ClipEnd(slot); clipping.ClipEnd(slot);
} }
clipping.ClipEnd(); clipping.ClipEnd();
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) }
target.Draw(vertexArray, states);
// 包围盒 protected override void debugDraw(SFML.Graphics.RenderTarget target)
if (isDebug && isSelected && debugBounds) {
lineVertices.Clear();
rectLineVertices.Clear();
// 调试包围盒
if (debugBounds)
{ {
var b = bounds; var b = bounds;
boundsVertices[0] = boundsVertices[4] = new(new(b.Left, b.Top), BoundsColor); var v = new SFML.Graphics.Vertex() { Color = BoundsColor };
boundsVertices[1] = new(new(b.Right, b.Top), BoundsColor); v.Position = new(b.Left, b.Top); lineVertices.Append(v);
boundsVertices[2] = new(new(b.Right, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Top); lineVertices.Append(v); lineVertices.Append(v);
boundsVertices[3] = new(new(b.Left, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
target.Draw(boundsVertices); v.Position = new(b.Left, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
v.Position = new(b.Left, b.Top); lineVertices.Append(v);
}
if (debugBones)
{
var width = scale;
foreach (var bone in skeleton.Bones)
{
var boneLength = bone.Data.Length;
var p1 = new SFML.System.Vector2f(bone.WorldX, bone.WorldY);
var p2 = new SFML.System.Vector2f(bone.WorldX + boneLength * bone.A, bone.WorldY + boneLength * bone.C);
AddRectLine(p1, p2, BoneLineColor, width);
}
}
target.Draw(lineVertices);
target.Draw(rectLineVertices);
// 骨骼的点最后画, 层级处于上面
if (debugBones)
{
var radius = scale;
foreach (var bone in skeleton.Bones)
{
DrawCirclePoint(target, new(bone.WorldX, bone.WorldY), BonePointColor, radius);
}
} }
} }
} }

View File

@@ -199,7 +199,7 @@ namespace SpineViewer.Spine.Implementations.SpineObject
protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states) protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
{ {
vertexArray.Clear(); triangleVertices.Clear();
states.Texture = null; states.Texture = null;
states.Shader = SFMLShader.GetSpineShader(usePma); states.Shader = SFMLShader.GetSpineShader(usePma);
@@ -266,13 +266,10 @@ namespace SpineViewer.Spine.Implementations.SpineObject
states.Texture ??= texture; states.Texture ??= texture;
if (states.BlendMode != blendMode || states.Texture != texture) if (states.BlendMode != blendMode || states.Texture != texture)
{ {
if (vertexArray.VertexCount > 0) if (triangleVertices.VertexCount > 0)
{ {
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) triangleVertices.Clear();
target.Draw(vertexArray, states);
vertexArray.Clear();
} }
states.BlendMode = blendMode; states.BlendMode = blendMode;
states.Texture = texture; states.Texture = texture;
@@ -306,26 +303,56 @@ namespace SpineViewer.Spine.Implementations.SpineObject
vertex.Position.Y = worldVertices[index + 1]; vertex.Position.Y = worldVertices[index + 1];
vertex.TexCoords.X = uvs[index] * textureSizeX; vertex.TexCoords.X = uvs[index] * textureSizeX;
vertex.TexCoords.Y = uvs[index + 1] * textureSizeY; vertex.TexCoords.Y = uvs[index + 1] * textureSizeY;
vertexArray.Append(vertex); triangleVertices.Append(vertex);
} }
clipping.ClipEnd(slot); clipping.ClipEnd(slot);
} }
clipping.ClipEnd(); clipping.ClipEnd();
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) }
target.Draw(vertexArray, states);
protected override void debugDraw(SFML.Graphics.RenderTarget target)
{
lineVertices.Clear();
rectLineVertices.Clear();
// 调试包围盒 // 调试包围盒
if (isDebug && isSelected && debugBounds) if (debugBounds)
{ {
var b = bounds; var b = bounds;
boundsVertices[0] = boundsVertices[4] = new(new(b.Left, b.Top), BoundsColor); var v = new SFML.Graphics.Vertex() { Color = BoundsColor };
boundsVertices[1] = new(new(b.Right, b.Top), BoundsColor); v.Position = new(b.Left, b.Top); lineVertices.Append(v);
boundsVertices[2] = new(new(b.Right, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Top); lineVertices.Append(v); lineVertices.Append(v);
boundsVertices[3] = new(new(b.Left, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
target.Draw(boundsVertices); v.Position = new(b.Left, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
v.Position = new(b.Left, b.Top); lineVertices.Append(v);
}
if (debugBones)
{
var width = scale;
foreach (var bone in skeleton.Bones)
{
var boneLength = bone.Data.Length;
var p1 = new SFML.System.Vector2f(bone.WorldX, bone.WorldY);
var p2 = new SFML.System.Vector2f(bone.WorldX + boneLength * bone.A, bone.WorldY + boneLength * bone.C);
AddRectLine(p1, p2, BoneLineColor, width);
}
}
target.Draw(lineVertices);
target.Draw(rectLineVertices);
// 骨骼的点最后画, 层级处于上面
if (debugBones)
{
var radius = scale;
foreach (var bone in skeleton.Bones)
{
DrawCirclePoint(target, new(bone.WorldX, bone.WorldY), BonePointColor, radius);
}
} }
} }
} }

View File

@@ -195,7 +195,7 @@ namespace SpineViewer.Spine.Implementations.SpineObject
protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states) protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
{ {
vertexArray.Clear(); triangleVertices.Clear();
states.Texture = null; states.Texture = null;
states.Shader = SFMLShader.GetSpineShader(usePma); states.Shader = SFMLShader.GetSpineShader(usePma);
@@ -262,13 +262,10 @@ namespace SpineViewer.Spine.Implementations.SpineObject
states.Texture ??= texture; states.Texture ??= texture;
if (states.BlendMode != blendMode || states.Texture != texture) if (states.BlendMode != blendMode || states.Texture != texture)
{ {
if (vertexArray.VertexCount > 0) if (triangleVertices.VertexCount > 0)
{ {
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) triangleVertices.Clear();
target.Draw(vertexArray, states);
vertexArray.Clear();
} }
states.BlendMode = blendMode; states.BlendMode = blendMode;
states.Texture = texture; states.Texture = texture;
@@ -302,26 +299,56 @@ namespace SpineViewer.Spine.Implementations.SpineObject
vertex.Position.Y = worldVertices[index + 1]; vertex.Position.Y = worldVertices[index + 1];
vertex.TexCoords.X = uvs[index] * textureSizeX; vertex.TexCoords.X = uvs[index] * textureSizeX;
vertex.TexCoords.Y = uvs[index + 1] * textureSizeY; vertex.TexCoords.Y = uvs[index + 1] * textureSizeY;
vertexArray.Append(vertex); triangleVertices.Append(vertex);
} }
clipping.ClipEnd(slot); clipping.ClipEnd(slot);
} }
clipping.ClipEnd(); clipping.ClipEnd();
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) }
target.Draw(vertexArray, states);
// 包围盒 protected override void debugDraw(SFML.Graphics.RenderTarget target)
if (isDebug && isSelected && debugBounds) {
lineVertices.Clear();
rectLineVertices.Clear();
// 调试包围盒
if (debugBounds)
{ {
var b = bounds; var b = bounds;
boundsVertices[0] = boundsVertices[4] = new(new(b.Left, b.Top), BoundsColor); var v = new SFML.Graphics.Vertex() { Color = BoundsColor };
boundsVertices[1] = new(new(b.Right, b.Top), BoundsColor); v.Position = new(b.Left, b.Top); lineVertices.Append(v);
boundsVertices[2] = new(new(b.Right, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Top); lineVertices.Append(v); lineVertices.Append(v);
boundsVertices[3] = new(new(b.Left, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
target.Draw(boundsVertices); v.Position = new(b.Left, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
v.Position = new(b.Left, b.Top); lineVertices.Append(v);
}
if (debugBones)
{
var width = scale;
foreach (var bone in skeleton.Bones)
{
var boneLength = bone.Data.Length;
var p1 = new SFML.System.Vector2f(bone.WorldX, bone.WorldY);
var p2 = new SFML.System.Vector2f(bone.WorldX + boneLength * bone.A, bone.WorldY + boneLength * bone.C);
AddRectLine(p1, p2, BoneLineColor, width);
}
}
target.Draw(lineVertices);
target.Draw(rectLineVertices);
// 骨骼的点最后画, 层级处于上面
if (debugBones)
{
var radius = scale;
foreach (var bone in skeleton.Bones)
{
DrawCirclePoint(target, new(bone.WorldX, bone.WorldY), BonePointColor, radius);
}
} }
} }
} }

View File

@@ -195,7 +195,7 @@ namespace SpineViewer.Spine.Implementations.SpineObject
protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states) protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
{ {
vertexArray.Clear(); triangleVertices.Clear();
states.Texture = null; states.Texture = null;
states.Shader = SFMLShader.GetSpineShader(usePma); states.Shader = SFMLShader.GetSpineShader(usePma);
@@ -262,13 +262,10 @@ namespace SpineViewer.Spine.Implementations.SpineObject
states.Texture ??= texture; states.Texture ??= texture;
if (states.BlendMode != blendMode || states.Texture != texture) if (states.BlendMode != blendMode || states.Texture != texture)
{ {
if (vertexArray.VertexCount > 0) if (triangleVertices.VertexCount > 0)
{ {
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) triangleVertices.Clear();
target.Draw(vertexArray, states);
vertexArray.Clear();
} }
states.BlendMode = blendMode; states.BlendMode = blendMode;
states.Texture = texture; states.Texture = texture;
@@ -302,26 +299,56 @@ namespace SpineViewer.Spine.Implementations.SpineObject
vertex.Position.Y = worldVertices[index + 1]; vertex.Position.Y = worldVertices[index + 1];
vertex.TexCoords.X = uvs[index] * textureSizeX; vertex.TexCoords.X = uvs[index] * textureSizeX;
vertex.TexCoords.Y = uvs[index + 1] * textureSizeY; vertex.TexCoords.Y = uvs[index + 1] * textureSizeY;
vertexArray.Append(vertex); triangleVertices.Append(vertex);
} }
clipping.ClipEnd(slot); clipping.ClipEnd(slot);
} }
clipping.ClipEnd(); clipping.ClipEnd();
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) }
target.Draw(vertexArray, states);
// 包围盒 protected override void debugDraw(SFML.Graphics.RenderTarget target)
if (isDebug && isSelected && debugBounds) {
lineVertices.Clear();
rectLineVertices.Clear();
// 调试包围盒
if (debugBounds)
{ {
var b = bounds; var b = bounds;
boundsVertices[0] = boundsVertices[4] = new(new(b.Left, b.Top), BoundsColor); var v = new SFML.Graphics.Vertex() { Color = BoundsColor };
boundsVertices[1] = new(new(b.Right, b.Top), BoundsColor); v.Position = new(b.Left, b.Top); lineVertices.Append(v);
boundsVertices[2] = new(new(b.Right, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Top); lineVertices.Append(v); lineVertices.Append(v);
boundsVertices[3] = new(new(b.Left, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
target.Draw(boundsVertices); v.Position = new(b.Left, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
v.Position = new(b.Left, b.Top); lineVertices.Append(v);
}
if (debugBones)
{
var width = scale;
foreach (var bone in skeleton.Bones)
{
var boneLength = bone.Data.Length;
var p1 = new SFML.System.Vector2f(bone.WorldX, bone.WorldY);
var p2 = new SFML.System.Vector2f(bone.WorldX + boneLength * bone.A, bone.WorldY + boneLength * bone.C);
AddRectLine(p1, p2, BoneLineColor, width);
}
}
target.Draw(lineVertices);
target.Draw(rectLineVertices);
// 骨骼的点最后画, 层级处于上面
if (debugBones)
{
var radius = scale;
foreach (var bone in skeleton.Bones)
{
DrawCirclePoint(target, new(bone.WorldX, bone.WorldY), BonePointColor, radius);
}
} }
} }
} }

View File

@@ -195,7 +195,7 @@ namespace SpineViewer.Spine.Implementations.SpineObject
protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states) protected override void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
{ {
vertexArray.Clear(); triangleVertices.Clear();
states.Texture = null; states.Texture = null;
states.Shader = SFMLShader.GetSpineShader(usePma); states.Shader = SFMLShader.GetSpineShader(usePma);
@@ -262,13 +262,10 @@ namespace SpineViewer.Spine.Implementations.SpineObject
states.Texture ??= texture; states.Texture ??= texture;
if (states.BlendMode != blendMode || states.Texture != texture) if (states.BlendMode != blendMode || states.Texture != texture)
{ {
if (vertexArray.VertexCount > 0) if (triangleVertices.VertexCount > 0)
{ {
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) triangleVertices.Clear();
target.Draw(vertexArray, states);
vertexArray.Clear();
} }
states.BlendMode = blendMode; states.BlendMode = blendMode;
states.Texture = texture; states.Texture = texture;
@@ -302,26 +299,56 @@ namespace SpineViewer.Spine.Implementations.SpineObject
vertex.Position.Y = worldVertices[index + 1]; vertex.Position.Y = worldVertices[index + 1];
vertex.TexCoords.X = uvs[index] * textureSizeX; vertex.TexCoords.X = uvs[index] * textureSizeX;
vertex.TexCoords.Y = uvs[index + 1] * textureSizeY; vertex.TexCoords.Y = uvs[index + 1] * textureSizeY;
vertexArray.Append(vertex); triangleVertices.Append(vertex);
} }
clipping.ClipEnd(slot); clipping.ClipEnd(slot);
} }
clipping.ClipEnd(); clipping.ClipEnd();
// 调试纹理 target.Draw(triangleVertices, states);
if (!isDebug || debugTexture) }
target.Draw(vertexArray, states);
// 包围盒 protected override void debugDraw(SFML.Graphics.RenderTarget target)
if (isDebug && isSelected && debugBounds) {
lineVertices.Clear();
rectLineVertices.Clear();
// 调试包围盒
if (debugBounds)
{ {
var b = bounds; var b = bounds;
boundsVertices[0] = boundsVertices[4] = new(new(b.Left, b.Top), BoundsColor); var v = new SFML.Graphics.Vertex() { Color = BoundsColor };
boundsVertices[1] = new(new(b.Right, b.Top), BoundsColor); v.Position = new(b.Left, b.Top); lineVertices.Append(v);
boundsVertices[2] = new(new(b.Right, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Top); lineVertices.Append(v); lineVertices.Append(v);
boundsVertices[3] = new(new(b.Left, b.Bottom), BoundsColor); v.Position = new(b.Right, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
target.Draw(boundsVertices); v.Position = new(b.Left, b.Bottom); lineVertices.Append(v); lineVertices.Append(v);
v.Position = new(b.Left, b.Top); lineVertices.Append(v);
}
if (debugBones)
{
var width = scale;
foreach (var bone in skeleton.Bones)
{
var boneLength = bone.Data.Length;
var p1 = new SFML.System.Vector2f(bone.WorldX, bone.WorldY);
var p2 = new SFML.System.Vector2f(bone.WorldX + boneLength * bone.A, bone.WorldY + boneLength * bone.C);
AddRectLine(p1, p2, BoneLineColor, width);
}
}
target.Draw(lineVertices);
target.Draw(rectLineVertices);
// 骨骼的点最后画, 层级处于上面
if (debugBones)
{
var radius = scale;
foreach (var bone in skeleton.Bones)
{
DrawCirclePoint(target, new(bone.WorldX, bone.WorldY), BonePointColor, radius);
}
} }
} }
} }

View File

@@ -103,7 +103,13 @@ namespace SpineViewer.Spine
~SpineObject() { Dispose(false); } ~SpineObject() { Dispose(false); }
public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }
protected virtual void Dispose(bool disposing) { Preview?.Dispose(); } protected virtual void Dispose(bool disposing)
{
Preview?.Dispose();
triangleVertices.Dispose();
lineVertices.Dispose();
rectLineVertices.Dispose();
}
/// <summary> /// <summary>
/// 运行时唯一 ID /// 运行时唯一 ID
@@ -233,7 +239,7 @@ namespace SpineViewer.Spine
get { lock (_lock) return isDebug; } get { lock (_lock) return isDebug; }
set { lock (_lock) { isDebug = value; update(0); } } set { lock (_lock) { isDebug = value; update(0); } }
} }
protected bool isDebug = false; private bool isDebug = false;
/// <summary> /// <summary>
/// 显示纹理 /// 显示纹理
@@ -243,7 +249,7 @@ namespace SpineViewer.Spine
get { lock (_lock) return debugTexture; } get { lock (_lock) return debugTexture; }
set { lock (_lock) { debugTexture = value; update(0); } } set { lock (_lock) { debugTexture = value; update(0); } }
} }
protected bool debugTexture = true; private bool debugTexture = true;
/// <summary> /// <summary>
/// 显示包围盒 /// 显示包围盒
@@ -263,7 +269,7 @@ namespace SpineViewer.Spine
get { lock (_lock) return debugBones; } get { lock (_lock) return debugBones; }
set { lock (_lock) { debugBones = value; update(0); } } set { lock (_lock) { debugBones = value; update(0); } }
} }
protected bool debugBones = false; protected bool debugBones = true;
/// <summary> /// <summary>
/// 获取已加载的皮肤列表快照, 允许出现重复值 /// 获取已加载的皮肤列表快照, 允许出现重复值
@@ -379,32 +385,100 @@ namespace SpineViewer.Spine
#region SFML.Graphics.Drawable #region SFML.Graphics.Drawable
/// <summary>
/// 顶点坐标缓冲区
/// </summary>
protected float[] worldVerticesBuffer = new float[1024];
/// <summary>
/// 顶点缓冲区
/// </summary>
protected readonly SFML.Graphics.VertexArray vertexArray = new(SFML.Graphics.PrimitiveType.Triangles);
/// <summary> /// <summary>
/// 包围盒颜色 /// 包围盒颜色
/// </summary> /// </summary>
protected static readonly SFML.Graphics.Color BoundsColor = new(120, 200, 0); protected static readonly SFML.Graphics.Color BoundsColor = new(120, 200, 0);
/// <summary> /// <summary>
/// 包围盒顶点数组 /// 骨骼点颜色
/// </summary> /// </summary>
protected readonly SFML.Graphics.VertexArray boundsVertices = new(SFML.Graphics.PrimitiveType.LineStrip, 5); protected static readonly SFML.Graphics.Color BonePointColor = new(0, 255, 0);
/// <summary>
/// 骨骼线颜色
/// </summary>
protected static readonly SFML.Graphics.Color BoneLineColor = new(255, 0, 0);
/// <summary>
/// spine 顶点坐标缓冲区
/// </summary>
protected float[] worldVerticesBuffer = new float[1024];
/// <summary>
/// 三角形顶点缓冲区
/// </summary>
protected readonly SFML.Graphics.VertexArray triangleVertices = new(SFML.Graphics.PrimitiveType.Triangles);
/// <summary>
/// 无面积线条缓冲区
/// </summary>
protected readonly SFML.Graphics.VertexArray lineVertices = new(SFML.Graphics.PrimitiveType.Lines);
/// <summary>
/// 有半径圆点临时缓存对象
/// </summary>
private readonly SFML.Graphics.CircleShape circlePointShape = new();
/// <summary>
/// 有宽度线条缓冲区, 需要通过 <see cref="AddRectLine"/> 添加顶点
/// </summary>
protected readonly SFML.Graphics.VertexArray rectLineVertices = new(SFML.Graphics.PrimitiveType.Quads);
/// <summary>
/// 绘制有半径的实心圆点, 随模型一起缩放大小
/// </summary>
protected void DrawCirclePoint(SFML.Graphics.RenderTarget target, SFML.System.Vector2f p, SFML.Graphics.Color color, float radius = 1)
{
circlePointShape.Origin = new(radius, radius);
circlePointShape.Position = p;
circlePointShape.FillColor = color;
circlePointShape.Radius = radius;
target.Draw(circlePointShape);
}
/// <summary>
/// 绘制有宽度的实心线, 会随模型一起缩放粗细, 顶点被存储在 <see cref="rectLineVertices"/> 数组内
/// </summary>
protected void AddRectLine(SFML.System.Vector2f p1, SFML.System.Vector2f p2, SFML.Graphics.Color color, float width = 1)
{
var dx = p2.X - p1.X;
var dy = p2.Y - p1.Y;
var dt = (float)Math.Sqrt(dx * dx + dy * dy);
if (dt == 0) return;
var cosTheta = -dy / dt;
var sinTheta = dx / dt;
var halfWidth = width / 2;
var t = new SFML.System.Vector2f(halfWidth * cosTheta, halfWidth * sinTheta);
var v = new SFML.Graphics.Vertex() { Color = color };
v.Position = p1 + t; rectLineVertices.Append(v);
v.Position = p2 + t; rectLineVertices.Append(v);
v.Position = p2 - t; rectLineVertices.Append(v);
v.Position = p1 - t; rectLineVertices.Append(v);
}
/// <summary> /// <summary>
/// SFML.Graphics.Drawable 接口实现 /// SFML.Graphics.Drawable 接口实现
/// <para>这个渲染实现绘制出来的像素将是预乘的, 当渲染的背景透明度是 1 时, 则等价于非预乘的结果, 即正常画面, 否则画面偏暗</para> /// <para>这个渲染实现绘制出来的像素将是预乘的, 当渲染的背景透明度是 1 时, 则等价于非预乘的结果, 即正常画面, 否则画面偏暗</para>
/// <para>可以用于 <see cref="SFML.Graphics.RenderWindow"/> 的渲染, 因为直接在窗口上绘制时窗口始终是不透明的</para> /// <para>可以用于 <see cref="SFML.Graphics.RenderWindow"/> 的渲染, 因为直接在窗口上绘制时窗口始终是不透明的</para>
/// </summary> /// </summary>
public void Draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states) { lock (_lock) draw(target, states); } public void Draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states)
{
lock (_lock)
{
if (!isDebug)
{
draw(target, states);
}
else
{
if (debugTexture) draw(target, states);
debugDraw(target);
}
}
}
/// <summary> /// <summary>
/// 这个渲染实现绘制出来的像素将是预乘的, 当渲染的背景透明度是 1 时, 则等价于非预乘的结果, 即正常画面, 否则画面偏暗 /// 这个渲染实现绘制出来的像素将是预乘的, 当渲染的背景透明度是 1 时, 则等价于非预乘的结果, 即正常画面, 否则画面偏暗
@@ -412,6 +486,11 @@ namespace SpineViewer.Spine
/// </summary> /// </summary>
protected abstract void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states); protected abstract void draw(SFML.Graphics.RenderTarget target, SFML.Graphics.RenderStates states);
/// <summary>
/// 渲染调试内容
/// </summary>
protected abstract void debugDraw(SFML.Graphics.RenderTarget target);
#endregion #endregion
} }
} }