From 94f7ad4f50fbb9177673dea5ca29a8571acbce5b Mon Sep 17 00:00:00 2001 From: routersys Date: Mon, 29 Jun 2026 08:26:00 +0900 Subject: [PATCH] =?UTF-8?q?obj.drawpoly=E3=81=AE=E3=82=A2=E3=83=95?= =?UTF-8?q?=E3=82=A3=E3=83=B3=E5=9B=9B=E8=A7=92=E5=BD=A2=E6=8F=8F=E7=94=BB?= =?UTF-8?q?=E3=82=92=E4=B8=A1=E3=82=A8=E3=83=B3=E3=82=B8=E3=83=B3=E3=81=A7?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/DrawPolyMathTests.cs | 63 +++++++++++++++++++ LuaScript.Tests/LuaScript.Tests.csproj | 1 + LuaScript.Tests/NativeWorkerRoundTripTests.cs | 45 +++++++++++++ LuaScript/DrawCommand.cs | 3 +- LuaScript/DrawCompositor.cs | 19 ++++++ LuaScript/DrawPolyMath.cs | 53 ++++++++++++++++ LuaScript/Engine/LuaJitWorker.cs | 14 +++++ LuaScript/Engine/NativeProtocol.cs | 1 + LuaScript/LuaScriptEngine.cs | 36 +++++++++++ native/worker.lua | 28 +++++++++ 10 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 LuaScript.Tests/DrawPolyMathTests.cs create mode 100644 LuaScript/DrawPolyMath.cs diff --git a/LuaScript.Tests/DrawPolyMathTests.cs b/LuaScript.Tests/DrawPolyMathTests.cs new file mode 100644 index 0000000..0451f8f --- /dev/null +++ b/LuaScript.Tests/DrawPolyMathTests.cs @@ -0,0 +1,63 @@ +using System.Numerics; +using LuaScript; + +namespace LuaScript.Tests +{ + public class DrawPolyMathTests + { + private static double[] Quad( + double x0, double y0, double x1, double y1, + double x2, double y2, double x3, double y3, + double u0, double v0, double u1, double v1, + double u2, double v2, double u3, double v3, + double alpha) + => [x0, y0, 0, x1, y1, 0, x2, y2, 0, x3, y3, 0, u0, v0, u1, v1, u2, v2, u3, v3, alpha]; + + [Fact] + public void TrySolveAffine_MapsUvCornersToQuad() + { + var poly = Quad( + 10, 20, 18, 20, 18, 32, 10, 32, + 0, 0, 4, 0, 4, 4, 0, 4, 1); + + Assert.True(DrawPolyMath.TrySolveAffine(poly, out var m)); + + AssertMaps(m, 0, 0, 10, 20); + AssertMaps(m, 4, 0, 18, 20); + AssertMaps(m, 4, 4, 18, 32); + AssertMaps(m, 0, 4, 10, 32); + AssertMaps(m, 2, 2, 14, 26); + } + + [Fact] + public void TrySolveAffine_HandlesRotatedQuad() + { + var poly = Quad( + 0, 0, 0, 10, -10, 10, -10, 0, + 0, 0, 10, 0, 10, 10, 0, 10, 1); + + Assert.True(DrawPolyMath.TrySolveAffine(poly, out var m)); + + AssertMaps(m, 0, 0, 0, 0); + AssertMaps(m, 10, 0, 0, 10); + AssertMaps(m, 0, 10, -10, 0); + } + + [Fact] + public void TrySolveAffine_ReturnsFalse_OnDegenerateUv() + { + var poly = Quad( + 0, 0, 10, 0, 10, 10, 0, 10, + 0, 0, 0, 0, 0, 0, 0, 0, 1); + + Assert.False(DrawPolyMath.TrySolveAffine(poly, out _)); + } + + private static void AssertMaps(Matrix3x2 m, float u, float v, float x, float y) + { + var p = Vector2.Transform(new Vector2(u, v), m); + Assert.Equal(x, p.X, 3); + Assert.Equal(y, p.Y, 3); + } + } +} diff --git a/LuaScript.Tests/LuaScript.Tests.csproj b/LuaScript.Tests/LuaScript.Tests.csproj index d8d70a9..539d9a2 100644 --- a/LuaScript.Tests/LuaScript.Tests.csproj +++ b/LuaScript.Tests/LuaScript.Tests.csproj @@ -32,6 +32,7 @@ + diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs index 87f13a2..ffca592 100644 --- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs +++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs @@ -267,6 +267,51 @@ namespace LuaScript.Tests Assert.Equal(new DrawCommand(10d, 20d, 30d, 2d, 0.5d, 0.25d), commands[1]); } + [Fact] + public void DrawPoly_RecordsDefaultUvAndAlpha() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var pixels = new byte[4 * 4 * 4]; + var fields = Fields(4, 4, 0d); + var commands = new System.Collections.Generic.List(); + Action addDraw = commands.Add; + + bool ok = _worker.Execute( + "obj.drawpoly(0,0,0, 10,0,0, 10,10,0, 0,10,0)", + fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoAddEffect, addDraw, + out _, out _, out _, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.Single(commands); + var poly = commands[0].Poly; + Assert.NotNull(poly); + Assert.Equal(new double[] { 0, 0, 0, 10, 0, 0, 10, 10, 0, 0, 10, 0, 0, 0, 4, 0, 4, 4, 0, 4, 1 }, poly); + } + + [Fact] + public void DrawPoly_RecordsExplicitUvAndAlpha() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var pixels = new byte[4 * 4 * 4]; + var fields = Fields(4, 4, 0d); + var commands = new System.Collections.Generic.List(); + Action addDraw = commands.Add; + + bool ok = _worker.Execute( + "obj.drawpoly(0,0,0, 8,0,0, 8,8,0, 0,8,0, 1,1, 3,1, 3,3, 1,3, 0.5)", + fields, pixels, 4, 4, 5000, NoResolver, NoLoadFigure, NoAddEffect, addDraw, + out _, out _, out _, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.Single(commands); + var poly = commands[0].Poly; + Assert.NotNull(poly); + Assert.Equal(new double[] { 0, 0, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 1, 1, 3, 1, 3, 3, 1, 3, 0.5 }, poly); + Assert.Equal(0.5d, commands[0].Alpha); + } + [Fact] public void RuntimeError_IsReported_AndWorkerSurvives() { diff --git a/LuaScript/DrawCommand.cs b/LuaScript/DrawCommand.cs index 41df818..56d307b 100644 --- a/LuaScript/DrawCommand.cs +++ b/LuaScript/DrawCommand.cs @@ -6,5 +6,6 @@ namespace LuaScript double Oz, double Zoom, double Alpha, - double Aspect); + double Aspect, + double[]? Poly = null); } diff --git a/LuaScript/DrawCompositor.cs b/LuaScript/DrawCompositor.cs index b211e5a..422a71f 100644 --- a/LuaScript/DrawCompositor.cs +++ b/LuaScript/DrawCompositor.cs @@ -36,6 +36,15 @@ namespace LuaScript for (int i = 0; i < commands.Count; i++) { var cmd = commands[i]; + if (cmd.Poly is { } poly) + { + DrawPolyMath.Bounds(poly, out double pMinX, out double pMinY, out double pMaxX, out double pMaxY); + minX = Math.Min(minX, pMinX); + maxX = Math.Max(maxX, pMaxX); + minY = Math.Min(minY, pMinY); + maxY = Math.Max(maxY, pMaxY); + continue; + } double aspect = Math.Clamp(cmd.Aspect, -1d, 1d); double zx = cmd.Zoom * (1d + aspect); double zy = cmd.Zoom * (1d - aspect); @@ -61,9 +70,19 @@ namespace LuaScript dc.Target = _target; dc.BeginDraw(); dc.Clear(null); + var toTarget = Matrix3x2.CreateTranslation(-originX, -originY); for (int i = 0; i < commands.Count; i++) { var cmd = commands[i]; + if (cmd.Poly is { } poly) + { + if (!DrawPolyMath.TrySolveAffine(poly, out var affine)) + continue; + rt.Transform = affine * toTarget; + rt.DrawBitmap(_source, (float)Math.Clamp(poly[20], 0d, 1d), BitmapInterpolationMode.Linear); + continue; + } + double aspect = Math.Clamp(cmd.Aspect, -1d, 1d); float zx = (float)(cmd.Zoom * (1d + aspect)); float zy = (float)(cmd.Zoom * (1d - aspect)); diff --git a/LuaScript/DrawPolyMath.cs b/LuaScript/DrawPolyMath.cs new file mode 100644 index 0000000..966723f --- /dev/null +++ b/LuaScript/DrawPolyMath.cs @@ -0,0 +1,53 @@ +using System; +using System.Numerics; + +namespace LuaScript +{ + internal static class DrawPolyMath + { + public const int Length = 21; + + public static bool TrySolveAffine(double[] poly, out Matrix3x2 matrix) + { + double x0 = poly[0], y0 = poly[1]; + double x1 = poly[3], y1 = poly[4]; + double x3 = poly[9], y3 = poly[10]; + double u0 = poly[12], v0 = poly[13]; + double u1 = poly[14], v1 = poly[15]; + double u3 = poly[18], v3 = poly[19]; + + double det = u0 * (v1 - v3) - v0 * (u1 - u3) + (u1 * v3 - v1 * u3); + if (Math.Abs(det) < 1e-9) + { + matrix = Matrix3x2.Identity; + return false; + } + + double inv = 1d / det; + double a = (x0 * (v1 - v3) - v0 * (x1 - x3) + (x1 * v3 - v1 * x3)) * inv; + double b = (u0 * (x1 - x3) - x0 * (u1 - u3) + (u1 * x3 - x1 * u3)) * inv; + double c = (u0 * (v1 * x3 - x1 * v3) - v0 * (u1 * x3 - x1 * u3) + x0 * (u1 * v3 - v1 * u3)) * inv; + double d = (y0 * (v1 - v3) - v0 * (y1 - y3) + (y1 * v3 - v1 * y3)) * inv; + double e = (u0 * (y1 - y3) - y0 * (u1 - u3) + (u1 * y3 - y1 * u3)) * inv; + double f = (u0 * (v1 * y3 - y1 * v3) - v0 * (u1 * y3 - y1 * u3) + y0 * (u1 * v3 - v1 * u3)) * inv; + + matrix = new Matrix3x2((float)a, (float)d, (float)b, (float)e, (float)c, (float)f); + return true; + } + + public static void Bounds(double[] poly, out double minX, out double minY, out double maxX, out double maxY) + { + minX = minY = double.MaxValue; + maxX = maxY = double.MinValue; + for (int i = 0; i < 4; i++) + { + double x = poly[i * 3]; + double y = poly[i * 3 + 1]; + if (x < minX) minX = x; + if (x > maxX) maxX = x; + if (y < minY) minY = y; + if (y > maxY) maxY = y; + } + } + } +} diff --git a/LuaScript/Engine/LuaJitWorker.cs b/LuaScript/Engine/LuaJitWorker.cs index 7dfcca7..a11e7c3 100644 --- a/LuaScript/Engine/LuaJitWorker.cs +++ b/LuaScript/Engine/LuaJitWorker.cs @@ -166,6 +166,9 @@ namespace LuaScript.Engine case NativeProtocol.CbKindDraw: ResolveDrawCallback(view, addDraw); break; + case NativeProtocol.CbKindDrawPoly: + ResolveDrawPolyCallback(view, addDraw); + break; } } @@ -184,6 +187,17 @@ namespace LuaScript.Engine view.Write(NativeProtocol.OffCallbackFound, 1); } + private static void ResolveDrawPolyCallback(MemoryMappedViewAccessor view, Action addDraw) + { + var poly = new double[DrawPolyMath.Length]; + for (int i = 0; i < DrawPolyMath.Length; i++) + poly[i] = view.ReadDouble(NativeProtocol.CallbackTagOffset + i * 8); + + try { addDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly)); } + catch { } + view.Write(NativeProtocol.OffCallbackFound, 1); + } + private void ResolveGetObjectCallback(MemoryMappedViewAccessor view, Func resolveObject) { int frame = view.ReadInt32(NativeProtocol.OffCallbackFrame); diff --git a/LuaScript/Engine/NativeProtocol.cs b/LuaScript/Engine/NativeProtocol.cs index aae5c5c..f2d3cb1 100644 --- a/LuaScript/Engine/NativeProtocol.cs +++ b/LuaScript/Engine/NativeProtocol.cs @@ -47,6 +47,7 @@ namespace LuaScript.Engine public const int CbKindLoadFigure = 1; public const int CbKindEffect = 2; public const int CbKindDraw = 3; + public const int CbKindDrawPoly = 4; public const int CbExist = 0; public const int CbX = 1; diff --git a/LuaScript/LuaScriptEngine.cs b/LuaScript/LuaScriptEngine.cs index 24f55e1..763b4ce 100644 --- a/LuaScript/LuaScriptEngine.cs +++ b/LuaScript/LuaScriptEngine.cs @@ -531,6 +531,42 @@ namespace LuaScript _activeContext.AddDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect)); return DynValue.Void; }); + + obj["drawpoly"] = DynValue.NewCallback((_, args) => + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null || args.Count < 12) + return DynValue.Void; + + var poly = new double[DrawPolyMath.Length]; + for (int i = 0; i < 12; i++) + poly[i] = args[i].CastToNumber() ?? 0d; + + if (args.Count >= 20) + { + for (int i = 0; i < 8; i++) + poly[12 + i] = args[12 + i].CastToNumber() ?? 0d; + } + else + { + double w = _activeContext.ImageWidth; + double h = _activeContext.ImageHeight; + poly[12] = 0d; poly[13] = 0d; + poly[14] = w; poly[15] = 0d; + poly[16] = w; poly[17] = h; + poly[18] = 0d; poly[19] = h; + } + + poly[20] = args.Count switch + { + 13 => args[12].CastToNumber() ?? 1d, + >= 21 => args[20].CastToNumber() ?? 1d, + _ => 1d, + }; + + _activeContext.AddDraw(new DrawCommand(0d, 0d, 0d, 1d, poly[20], 0d, poly)); + return DynValue.Void; + }); } private void LoadFigure(string name, int color, double size, double line, double aspect) diff --git a/native/worker.lua b/native/worker.lua index ffc6b7f..cb7973d 100644 --- a/native/worker.lua +++ b/native/worker.lua @@ -62,6 +62,7 @@ local CB_KIND_GETOBJECT = 0 local CB_KIND_LOADFIGURE = 1 local CB_KIND_EFFECT = 2 local CB_KIND_DRAW = 3 +local CB_KIND_DRAWPOLY = 4 assert(loadfile(shimPath))() @@ -74,6 +75,7 @@ end local width, height = 0, 0 local pixels = ffi.cast("uint8_t*", base + PIXEL_OFFSET) local cbResult = ffi.cast("double*", base + CB_RESULT_OFFSET) +local cbTagD = ffi.cast("double*", base + CB_TAG_OFFSET) local dirty = false local obj = {} @@ -241,6 +243,32 @@ function obj.draw(ox, oy, oz, zoom, alpha, aspect) k32.WaitForSingleObject(workEvent, INFINITE) end +function obj.drawpoly(...) + local n = select("#", ...) + if n < 12 then return end + local a = { ... } + for i = 0, 11 do cbTagD[i] = a[i + 1] or 0 end + if n >= 20 then + for i = 0, 7 do cbTagD[12 + i] = a[13 + i] or 0 end + else + cbTagD[12] = 0; cbTagD[13] = 0 + cbTagD[14] = width; cbTagD[15] = 0 + cbTagD[16] = width; cbTagD[17] = height + cbTagD[18] = 0; cbTagD[19] = height + end + local alpha = 1 + if n == 13 then + alpha = a[13] or 1 + elseif n >= 21 then + alpha = a[21] or 1 + end + cbTagD[20] = alpha + i32[OFF_CB_KIND] = CB_KIND_DRAWPOLY + i32[OFF_STATUS] = STATUS_CALLBACK + k32.SetEvent(doneEvent) + k32.WaitForSingleObject(workEvent, INFINITE) +end + local cacheTag, cacheFrame, cacheFound local cacheExist, cacheX, cacheY, cacheZ, cacheZoom, cacheRz, cacheAlpha, cacheLayer