using SkiaSharp; using Spine.Interfaces.Attachments; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Spine.Interfaces { public static class SpineExtension { /// /// 获取当前状态包围盒 /// public static void GetBounds(this ISlot self, out float x, out float y, out float w, out float h) { float[] vertices = new float[8]; int verticesLength = 0; var attachment = self.Attachment; switch (attachment) { case IRegionAttachment: case IMeshAttachment: verticesLength = attachment.ComputeWorldVertices(self, ref vertices); break; default: break; } if (verticesLength > 0) { float minX = int.MaxValue; float minY = int.MaxValue; float maxX = int.MinValue; float maxY = int.MinValue; for (int ii = 0; ii + 1 < verticesLength; ii += 2) { float vx = vertices[ii]; float vy = vertices[ii + 1]; minX = Math.Min(minX, vx); minY = Math.Min(minY, vy); maxX = Math.Max(maxX, vx); maxY = Math.Max(maxY, vy); } x = minX; y = minY; w = maxX - minX; h = maxY - minY; } else { x = self.Bone.WorldX; y = self.Bone.WorldY; w = 0; h = 0; } } /// /// 获取当前状态包围盒 /// public static void GetBounds(this ISkeleton self, out float x, out float y, out float w, out float h) { float minX = int.MaxValue; float minY = int.MaxValue; float maxX = int.MinValue; float maxY = int.MinValue; foreach (var slot in self.IterDrawOrder()) { if (slot.A <= 0 || !slot.Bone.Active || slot.Disabled) continue; float[] vertices = new float[8]; int verticesLength = 0; var attachment = slot.Attachment; switch (attachment) { case IRegionAttachment: case IMeshAttachment: verticesLength = attachment.ComputeWorldVertices(slot, ref vertices); break; default: break; } for (int ii = 0; ii + 1 < verticesLength; ii += 2) { float vx = vertices[ii]; float vy = vertices[ii + 1]; minX = Math.Min(minX, vx); minY = Math.Min(minY, vy); maxX = Math.Max(maxX, vx); maxY = Math.Max(maxY, vy); } } x = minX; y = minY; w = maxX - minX; h = maxY - minY; } /// /// 命中测试, 当插槽全透明或者处于禁用或者骨骼处于未激活则无法命中 /// /// 是否精确命中检测, 否则仅使用包围盒进行命中检测 /// 调用方管理的缓存表 public static bool HitTest(this ISlot self, float x, float y, bool precise = false, Dictionary cache = null) { if (self.A <= 0 || !self.Bone.Active || self.Disabled) return false; if (!precise) { self.GetBounds(out var bx, out var by, out var bw, out var bh); return x >= bx && x <= (bx + bw) && y >= by && y <= (by + bh); } else { float[] vertices = new float[8]; int[] triangles; float[] uvs; SFML.Graphics.Texture tex; switch (self.Attachment) { case IRegionAttachment regionAttachment: _ = regionAttachment.ComputeWorldVertices(self, ref vertices); triangles = regionAttachment.Triangles; uvs = regionAttachment.UVs; tex = regionAttachment.RendererObject; break; case IMeshAttachment meshAttachment: _ = meshAttachment.ComputeWorldVertices(self, ref vertices); triangles = meshAttachment.Triangles; uvs = meshAttachment.UVs; tex = meshAttachment.RendererObject; break; default: return false; } SFML.Graphics.Image img = null; if (cache is not null) { if (!cache.TryGetValue(tex, out img)) { img = cache[tex] = tex.CopyToImage(); } } else { img = tex.CopyToImage(); } bool hit = false; var trianglesLength = triangles.Length; var texSize = img.Size; for (int i = 0; i + 2 < trianglesLength; i += 3) { var idx0 = triangles[i] << 1; var idx1 = triangles[i + 1] << 1; var idx2 = triangles[i + 2] << 1; float x0 = vertices[idx0] - x, y0 = vertices[idx0 + 1] - y; float x1 = vertices[idx1] - x, y1 = vertices[idx1 + 1] - y; float x2 = vertices[idx2] - x, y2 = vertices[idx2 + 1] - y; float c0 = Cross(x0, y0, x1, y1); float c1 = Cross(x1, y1, x2, y2); float c2 = Cross(x2, y2, x0, y0); // 判断是否全部同号 (或为 0, 点在边上) if ((c0 >= 0 && c1 >= 0 && c2 >= 0) || (c0 <= 0 && c1 <= 0 && c2 <= 0)) { float u0 = uvs[idx0], v0 = uvs[idx0 + 1]; float u1 = uvs[idx1], v1 = uvs[idx1 + 1]; float u2 = uvs[idx2], v2 = uvs[idx2 + 1]; float inv = 1 / (c0 + c1 + c2); float w0 = c1 * inv; float w1 = c2 * inv; float w2 = c0 * inv; float u = u0 * w0 + u1 * w1 + u2 * w2; float v = v0 * w0 + v1 * w1 + v2 * w2; var pixel = img.GetPixel((uint)(u * texSize.X), (uint)(v * texSize.Y)); hit = pixel.A > 0; break; } } // 无缓存需要立即释放资源 if (cache is null) { img.Dispose(); } return hit; } } /// /// 逐插槽的命中测试, 命中后会提前返回结果中止计算 /// public static bool HitTest(this ISkeleton self, float x, float y, bool precise = false) { var cache = new Dictionary(); bool hit = self.IterDrawOrder().Any(st => st.HitTest(x, y, precise, cache)); foreach (var img in cache.Values) img.Dispose(); return hit; } /// /// 逐插槽的命中测试, 会完整计算所有插槽的命中情况并按顶层至底层的顺序返回命中的插槽 /// /// 是否精确命中检测, 否则仅使用每个插槽的包围盒进行命中检测 /// public static ISlot[] HitTestFull(this ISkeleton self, float x, float y, bool precise = false) { var cache = new Dictionary(); var hitSlots = self.IterDrawOrder().Where(st => st.HitTest(x, y, precise, cache)).Reverse().ToArray(); foreach (var img in cache.Values) img.Dispose(); return hitSlots; } /// /// 向量叉积 /// private static float Cross(float x0, float y0, float x1, float y1) => x0 * y1 - y0 * x1; } }