From 90fe288f6606234859793fd92c29f4c997ec6eb7 Mon Sep 17 00:00:00 2001 From: routersys Date: Mon, 29 Jun 2026 08:11:26 +0900 Subject: [PATCH] =?UTF-8?q?obj.draw=E3=81=AE=E8=93=84=E7=A9=8D=E6=8F=8F?= =?UTF-8?q?=E7=94=BB=E5=90=88=E6=88=90=E3=82=92=E5=BE=93=E6=9D=A5=E3=82=A8?= =?UTF-8?q?=E3=83=B3=E3=82=B8=E3=83=B3=E3=81=A7=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript/AviUtlScriptContext.cs | 8 ++ LuaScript/DrawCommand.cs | 10 ++ LuaScript/DrawCompositor.cs | 126 ++++++++++++++++++++++++++ LuaScript/LuaScriptEffectProcessor.cs | 14 +++ LuaScript/LuaScriptEngine.cs | 17 ++++ 5 files changed, 175 insertions(+) create mode 100644 LuaScript/DrawCommand.cs create mode 100644 LuaScript/DrawCompositor.cs diff --git a/LuaScript/AviUtlScriptContext.cs b/LuaScript/AviUtlScriptContext.cs index fd36bcf..974dd58 100644 --- a/LuaScript/AviUtlScriptContext.cs +++ b/LuaScript/AviUtlScriptContext.cs @@ -66,6 +66,14 @@ namespace LuaScript internal void ClearEffects() => _effectRequests.Clear(); + private readonly List _drawCommands = []; + + public IReadOnlyList DrawCommands => _drawCommands; + + internal void AddDraw(DrawCommand command) => _drawCommands.Add(command); + + internal void ClearDraws() => _drawCommands.Clear(); + public double RxRad => Rx * Math.PI / 180d; public double RyRad => Ry * Math.PI / 180d; public double RzRad => Rz * Math.PI / 180d; diff --git a/LuaScript/DrawCommand.cs b/LuaScript/DrawCommand.cs new file mode 100644 index 0000000..41df818 --- /dev/null +++ b/LuaScript/DrawCommand.cs @@ -0,0 +1,10 @@ +namespace LuaScript +{ + internal readonly record struct DrawCommand( + double Ox, + double Oy, + double Oz, + double Zoom, + double Alpha, + double Aspect); +} diff --git a/LuaScript/DrawCompositor.cs b/LuaScript/DrawCompositor.cs new file mode 100644 index 0000000..b211e5a --- /dev/null +++ b/LuaScript/DrawCompositor.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Numerics; +using Vortice.Direct2D1; +using Vortice.Direct2D1.Effects; +using YukkuriMovieMaker.Commons; + +namespace LuaScript +{ + internal sealed class DrawCompositor : IDisposable + { + private const int MaxDimension = 8192; + + private readonly GraphicsDevicesAndContext _ctx; + private ID2D1Bitmap1? _source; + private ID2D1Bitmap1? _target; + private AffineTransform2D? _placement; + private int _sourceWidth; + private int _sourceHeight; + private int _targetWidth; + private int _targetHeight; + + public DrawCompositor(GraphicsDevicesAndContext ctx) + { + _ctx = ctx; + } + + public unsafe ID2D1Image Compose(byte[] pixels, int width, int height, IReadOnlyList commands) + { + EnsureSource(width, height); + fixed (byte* ptr = pixels) + _source!.CopyFromMemory(new nint(ptr), width * 4); + + double minX = double.MaxValue, minY = double.MaxValue; + double maxX = double.MinValue, maxY = double.MinValue; + for (int i = 0; i < commands.Count; i++) + { + var cmd = commands[i]; + double aspect = Math.Clamp(cmd.Aspect, -1d, 1d); + double zx = cmd.Zoom * (1d + aspect); + double zy = cmd.Zoom * (1d - aspect); + double halfW = width * 0.5 * zx; + double halfH = height * 0.5 * zy; + minX = Math.Min(minX, cmd.Ox - halfW); + maxX = Math.Max(maxX, cmd.Ox + halfW); + minY = Math.Min(minY, cmd.Oy - halfH); + maxY = Math.Max(maxY, cmd.Oy + halfH); + } + + int originX = (int)Math.Floor(minX); + int originY = (int)Math.Floor(minY); + int targetW = Math.Clamp((int)Math.Ceiling(maxX) - originX, 1, MaxDimension); + int targetH = Math.Clamp((int)Math.Ceiling(maxY) - originY, 1, MaxDimension); + + EnsureTarget(targetW, targetH); + + var dc = _ctx.DeviceContext; + var rt = (ID2D1RenderTarget)dc; + var savedTarget = dc.Target; + + dc.Target = _target; + dc.BeginDraw(); + dc.Clear(null); + for (int i = 0; i < commands.Count; i++) + { + var cmd = commands[i]; + double aspect = Math.Clamp(cmd.Aspect, -1d, 1d); + float zx = (float)(cmd.Zoom * (1d + aspect)); + float zy = (float)(cmd.Zoom * (1d - aspect)); + float opacity = (float)Math.Clamp(cmd.Alpha, 0d, 1d); + + var matrix = + Matrix3x2.CreateTranslation(-width * 0.5f, -height * 0.5f) * + Matrix3x2.CreateScale(zx, zy) * + Matrix3x2.CreateTranslation((float)(cmd.Ox - originX), (float)(cmd.Oy - originY)); + + rt.Transform = matrix; + rt.DrawBitmap(_source, opacity, BitmapInterpolationMode.Linear); + } + rt.Transform = Matrix3x2.Identity; + dc.EndDraw(); + dc.Target = savedTarget; + + _placement!.TransformMatrix = Matrix3x2.CreateTranslation(originX, originY); + return _placement.Output; + } + + private void EnsureSource(int width, int height) + { + if (_source is not null && _sourceWidth == width && _sourceHeight == height) + return; + + _source?.Dispose(); + _source = _ctx.DeviceContext.CreateEmptyBitmap(width, height, BitmapOptions.Target); + _sourceWidth = width; + _sourceHeight = height; + } + + private void EnsureTarget(int width, int height) + { + if (_target is not null && _targetWidth == width && _targetHeight == height) + return; + + _placement?.Dispose(); + _target?.Dispose(); + _placement = null; + + _target = _ctx.DeviceContext.CreateEmptyBitmap(width, height, BitmapOptions.Target); + _targetWidth = width; + _targetHeight = height; + + _placement = new AffineTransform2D(_ctx.DeviceContext); + _placement.SetInput(0, _target, true); + } + + public void Dispose() + { + _placement?.Dispose(); + _source?.Dispose(); + _target?.Dispose(); + _placement = null; + _source = null; + _target = null; + } + } +} diff --git a/LuaScript/LuaScriptEffectProcessor.cs b/LuaScript/LuaScriptEffectProcessor.cs index e907f06..6e65ce1 100644 --- a/LuaScript/LuaScriptEffectProcessor.cs +++ b/LuaScript/LuaScriptEffectProcessor.cs @@ -88,6 +88,7 @@ namespace LuaScript private ID2D1Image? _cachedEffectOutput; private VideoEffectChain? _effectChain; + private DrawCompositor? _drawCompositor; protected override ID2D1Image? CreateEffect(IGraphicsDevicesAndContext devices) { @@ -232,6 +233,17 @@ namespace LuaScript } } + if (ctx.DrawCommands.Count > 0) + { + ctx.EnsurePixelBuffer(); + var drawBuffer = ctx.GetPixelBuffer(); + if (drawBuffer is not null) + { + _drawCompositor ??= new DrawCompositor(_ownCtx); + effectOutput = _drawCompositor.Compose(drawBuffer, ctx.ImageWidth, ctx.ImageHeight, ctx.DrawCommands); + } + } + outDesc = BuildOutputDesc(inDesc, ctx); if (ctx.EffectRequests.Count > 0) @@ -340,6 +352,7 @@ namespace LuaScript ctx.ClearQueries(); ctx.ClearEffects(); + ctx.ClearDraws(); ctx.ImageWidth = imgW; ctx.ImageHeight = imgH; @@ -502,6 +515,7 @@ namespace LuaScript _engine.Dispose(); _nativeWorker?.Dispose(); _effectChain?.Dispose(); + _drawCompositor?.Dispose(); _pixelLoaderSemaphore.Dispose(); _pixelManager?.Dispose(); _pixelManager = null; diff --git a/LuaScript/LuaScriptEngine.cs b/LuaScript/LuaScriptEngine.cs index b00ba47..24f55e1 100644 --- a/LuaScript/LuaScriptEngine.cs +++ b/LuaScript/LuaScriptEngine.cs @@ -514,6 +514,23 @@ namespace LuaScript _activeContext.AddEffect(new AviUtlEffectRequest(name, arguments)); return DynValue.Void; }); + + obj["draw"] = DynValue.NewCallback((_, args) => + { + _activeCancellation.ThrowIfCancellationRequested(); + if (_activeContext is null) + return DynValue.Void; + + double ox = args.Count > 0 ? args[0].CastToNumber() ?? 0d : 0d; + double oy = args.Count > 1 ? args[1].CastToNumber() ?? 0d : 0d; + double oz = args.Count > 2 ? args[2].CastToNumber() ?? 0d : 0d; + double zoom = args.Count > 3 ? args[3].CastToNumber() ?? 1d : 1d; + double alpha = args.Count > 4 ? args[4].CastToNumber() ?? 1d : 1d; + double aspect = args.Count > 5 ? args[5].CastToNumber() ?? 0d : 0d; + + _activeContext.AddDraw(new DrawCommand(ox, oy, oz, zoom, alpha, aspect)); + return DynValue.Void; + }); } private void LoadFigure(string name, int color, double size, double line, double aspect)