增加精确命中检测和插槽输出
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Spine.Interfaces.Attachments;
|
||||
using SkiaSharp;
|
||||
using Spine.Interfaces.Attachments;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -56,18 +57,6 @@ namespace Spine.Interfaces
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 命中测试, 当插槽全透明或者处于禁用或者骨骼处于未激活则无法命中
|
||||
/// </summary>
|
||||
public static bool HitTest(this ISlot self, float x, float y)
|
||||
{
|
||||
if (self.A <= 0 || !self.Bone.Active || self.Disabled)
|
||||
return false;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前状态包围盒
|
||||
/// </summary>
|
||||
@@ -112,11 +101,130 @@ namespace Spine.Interfaces
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逐插槽的命中测试, 不会计算处于禁用或者骨骼未激活的插槽, 比整体包围盒稍微精确一些
|
||||
/// 命中测试, 当插槽全透明或者处于禁用或者骨骼处于未激活则无法命中
|
||||
/// </summary>
|
||||
public static bool HitTest(this ISkeleton self, float x, float y)
|
||||
/// <param name="precise">是否精确命中检测, 否则仅使用包围盒进行命中检测</param>
|
||||
/// <param name="cache">调用方管理的缓存表</param>
|
||||
public static bool HitTest(this ISlot self, float x, float y, bool precise = false, Dictionary<SFML.Graphics.Texture, SFML.Graphics.Image> cache = null)
|
||||
{
|
||||
return self.IterDrawOrder().Any(st => st.HitTest(x, y));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逐插槽的命中测试, 命中后会提前返回结果中止计算
|
||||
/// </summary>
|
||||
public static bool HitTest(this ISkeleton self, float x, float y, bool precise = false)
|
||||
{
|
||||
var cache = new Dictionary<SFML.Graphics.Texture, SFML.Graphics.Image>();
|
||||
bool hit = self.IterDrawOrder().Any(st => st.HitTest(x, y, precise, cache));
|
||||
foreach (var img in cache.Values) img.Dispose();
|
||||
return hit;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逐插槽的命中测试, 会完整计算所有插槽的命中情况并按顶层至底层的顺序返回命中的插槽
|
||||
/// /// <param name="precise">是否精确命中检测, 否则仅使用每个插槽的包围盒进行命中检测</param>
|
||||
/// </summary>
|
||||
public static ISlot[] HitTestFull(this ISkeleton self, float x, float y, bool precise = false)
|
||||
{
|
||||
var cache = new Dictionary<SFML.Graphics.Texture, SFML.Graphics.Image>();
|
||||
var hitSlots = self.IterDrawOrder().Where(st => st.HitTest(x, y, precise, cache)).Reverse().ToArray();
|
||||
foreach (var img in cache.Values) img.Dispose();
|
||||
return hitSlots;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向量叉积
|
||||
/// </summary>
|
||||
private static float Cross(float x0, float y0, float x1, float y1) => x0 * y1 - y0 * x1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user