From 41906f81e998a81fbeb40e8d72c61e19fd79511b Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 25 Jun 2026 15:09:06 +0900 Subject: [PATCH] =?UTF-8?q?obj.getobject=E3=81=AB=E3=83=95=E3=83=AC?= =?UTF-8?q?=E3=83=BC=E3=83=A0=E6=8C=87=E5=AE=9A=E5=BC=95=E6=95=B0=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript/AviUtlScriptContext.cs | 29 ++++------- LuaScript/LuaScriptEffectProcessor.cs | 57 ++++++++++------------ LuaScript/LuaScriptEngine.cs | 10 +++- LuaScript/SceneObjectInfo.cs | 5 ++ LuaScript/SceneObjectResolver.cs | 69 +++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 52 deletions(-) create mode 100644 LuaScript/SceneObjectResolver.cs diff --git a/LuaScript/AviUtlScriptContext.cs b/LuaScript/AviUtlScriptContext.cs index 2e7f109..bfdac0a 100644 --- a/LuaScript/AviUtlScriptContext.cs +++ b/LuaScript/AviUtlScriptContext.cs @@ -42,7 +42,11 @@ namespace LuaScript public int Index { get; init; } public int Num { get; init; } - public IReadOnlyList SceneObjects { get; init; } = []; + public SceneObjectResolver? Resolver { get; init; } + + private readonly List _objectQueries = []; + + public IReadOnlyList ObjectQueries => _objectQueries; public double RxRad => Rx * Math.PI / 180d; public double RyRad => Ry * Math.PI / 180d; @@ -83,29 +87,16 @@ namespace LuaScript internal void MarkPixelsDirty() => _isPixelsDirty = true; - public bool TryGetObject(string tag, out SceneObjectInfo info) + public bool ResolveObject(string tag, int frame, out SceneObjectInfo info) { - SceneObjectInfo? fallback = null; - var objects = SceneObjects; - for (int i = 0; i < objects.Count; i++) + if (Resolver is not null && Resolver.TryResolve(tag, frame, out var resolved)) { - var candidate = objects[i]; - if (!string.Equals(candidate.Tag, tag, StringComparison.Ordinal)) - continue; - if (candidate.Exist) - { - info = candidate; - return true; - } - fallback ??= candidate; - } - - if (fallback is { } value) - { - info = value; + _objectQueries.Add(new SceneObjectQuery(tag, frame, resolved)); + info = resolved; return true; } + _objectQueries.Add(new SceneObjectQuery(tag, frame, null)); info = default; return false; } diff --git a/LuaScript/LuaScriptEffectProcessor.cs b/LuaScript/LuaScriptEffectProcessor.cs index 1434ccd..7981ed7 100644 --- a/LuaScript/LuaScriptEffectProcessor.cs +++ b/LuaScript/LuaScriptEffectProcessor.cs @@ -59,7 +59,7 @@ namespace LuaScript private bool _isFirst = true; private ID2D1Image? _cachedInput; private RenderKey _cachedKey; - private SceneObjectInfo[] _cachedSceneObjects = []; + private SceneObjectQuery[] _cachedQueries = []; private DrawDescription? _cachedOutputDesc; private bool _cachedPixelsModified; @@ -113,10 +113,10 @@ namespace LuaScript desc.TimelineDuration.Time.TotalSeconds, inDesc); - var sceneObjects = BuildSceneObjects(desc); + var resolver = BuildSceneObjectResolver(desc); if (!_isFirst && key == _cachedKey && - _cachedSceneObjects.AsSpan().SequenceEqual(sceneObjects)) + QueriesMatch(_cachedQueries, resolver)) { effectOutput = _cachedPixelsModified ? _transformEffect?.Output : null; return _cachedOutputDesc ?? inDesc; @@ -126,7 +126,7 @@ namespace LuaScript int imgW = Math.Max(1, (int)Math.Ceiling(bounds.Right - bounds.Left)); int imgH = Math.Max(1, (int)Math.Ceiling(bounds.Bottom - bounds.Top)); - var ctx = BuildContext(in key, imgW, imgH, sceneObjects); + var ctx = BuildContext(in key, imgW, imgH, resolver); ctx.SetPixelLoader(() => LoadInputPixels(bounds, imgW, imgH)); DrawDescription outDesc = inDesc; @@ -165,18 +165,18 @@ namespace LuaScript _isFirst = false; _cachedKey = key; - _cachedSceneObjects = sceneObjects; + _cachedQueries = [.. ctx.ObjectQueries]; _cachedOutputDesc = outDesc; _cachedPixelsModified = pixelsModified; return outDesc; } - private static SceneObjectInfo[] BuildSceneObjects(EffectDescription desc) + private static SceneObjectResolver BuildSceneObjectResolver(EffectDescription desc) { var scenes = desc.Scenes; if (scenes is null) - return []; + return new SceneObjectResolver([], desc.FPS); Scene? scene = null; foreach (var info in scenes) @@ -188,12 +188,10 @@ namespace LuaScript } } if (scene is null) - return []; + return new SceneObjectResolver([], desc.FPS); - int timelineFrame = desc.TimelinePosition.Frame; - int fps = desc.FPS; var items = scene.Timeline.Items; - var result = new List(items.Count); + var entries = new List(items.Count); foreach (var item in items) { @@ -203,30 +201,25 @@ namespace LuaScript if (string.IsNullOrEmpty(tag)) continue; - int length = item.Length; - int local = Math.Clamp(timelineFrame - item.Frame, 0, length); - bool exist = item.Frame <= timelineFrame && timelineFrame < item.Frame + length; - - double x = visual.X.GetValue(local, length, fps); - double y = visual.Y.GetValue(local, length, fps); - double z = visual.Z.GetValue(local, length, fps); - double zoom = visual.Zoom.GetValue(local, length, fps) / 100d; - double rz = visual.Rotation.GetValue(local, length, fps); - - double opacity = visual.Opacity.GetValue(local, length, fps) / 100d; - double timeSec = fps > 0 ? local / (double)fps : 0d; - double durationSec = fps > 0 ? length / (double)fps : 0d; - double fadeIn = visual.FadeIn <= 0d ? 1d : Math.Min(1d, timeSec / visual.FadeIn); - double fadeOut = visual.FadeOut <= 0d ? 1d : Math.Min(1d, (durationSec - timeSec) / visual.FadeOut); - double alpha = opacity * Math.Min(fadeIn, fadeOut) * 255d; - - result.Add(new SceneObjectInfo(tag, exist, x, y, z, zoom, rz, alpha, item.Layer)); + entries.Add(new SceneObjectResolver.Entry(tag, item.Frame, item.Length, item.Layer, visual)); } - return [.. result]; + return new SceneObjectResolver([.. entries], desc.FPS); } - private static AviUtlScriptContext BuildContext(in RenderKey key, int imgW, int imgH, SceneObjectInfo[] sceneObjects) + private static bool QueriesMatch(SceneObjectQuery[] queries, SceneObjectResolver resolver) + { + for (int i = 0; i < queries.Length; i++) + { + var query = queries[i]; + SceneObjectInfo? current = resolver.TryResolve(query.Tag, query.Frame, out var info) ? info : null; + if (!Nullable.Equals(current, query.Result)) + return false; + } + return true; + } + + private static AviUtlScriptContext BuildContext(in RenderKey key, int imgW, int imgH, SceneObjectResolver resolver) { var zoom = key.InputDesc.Zoom; double zoomAvg = (zoom.X + zoom.Y) / 2d; @@ -277,7 +270,7 @@ namespace LuaScript IsPaused = key.Usage == TimelineSourceUsage.Paused, SceneId = key.SceneId.ToString(), TimeRatio = key.Length > 0 ? key.Frame / (double)key.Length : 0d, - SceneObjects = sceneObjects, + Resolver = resolver, }; } diff --git a/LuaScript/LuaScriptEngine.cs b/LuaScript/LuaScriptEngine.cs index 1c9d9d6..c202628 100644 --- a/LuaScript/LuaScriptEngine.cs +++ b/LuaScript/LuaScriptEngine.cs @@ -407,7 +407,15 @@ namespace LuaScript var tag = args[0]; if (tag.Type != DataType.String) return DynValue.Nil; - if (!_activeContext.TryGetObject(tag.String, out var info)) + int frame = _activeContext.TimelineFrame; + if (args.Count >= 2) + { + var frameArg = args[1]; + if (frameArg.Type != DataType.Number) + return DynValue.Nil; + frame = (int)frameArg.Number; + } + if (!_activeContext.ResolveObject(tag.String, frame, out var info)) return DynValue.Nil; return BuildObjectTable(execCtx.GetScript(), info); }); diff --git a/LuaScript/SceneObjectInfo.cs b/LuaScript/SceneObjectInfo.cs index d0e804c..79c432c 100644 --- a/LuaScript/SceneObjectInfo.cs +++ b/LuaScript/SceneObjectInfo.cs @@ -10,4 +10,9 @@ namespace LuaScript double Rz, double Alpha, int Layer); + + internal readonly record struct SceneObjectQuery( + string Tag, + int Frame, + SceneObjectInfo? Result); } diff --git a/LuaScript/SceneObjectResolver.cs b/LuaScript/SceneObjectResolver.cs new file mode 100644 index 0000000..a1b1f81 --- /dev/null +++ b/LuaScript/SceneObjectResolver.cs @@ -0,0 +1,69 @@ +using YukkuriMovieMaker.Project.Items; + +namespace LuaScript +{ + internal sealed class SceneObjectResolver + { + internal readonly record struct Entry(string Tag, int Frame, int Length, int Layer, VisualItem Item); + + private readonly Entry[] _entries; + private readonly int _fps; + + internal SceneObjectResolver(Entry[] entries, int fps) + { + _entries = entries; + _fps = fps; + } + + internal bool TryResolve(string tag, int timelineFrame, out SceneObjectInfo info) + { + int fallback = -1; + for (int i = 0; i < _entries.Length; i++) + { + if (!string.Equals(_entries[i].Tag, tag, StringComparison.Ordinal)) + continue; + if (IsExist(_entries[i], timelineFrame)) + { + info = Evaluate(_entries[i], timelineFrame); + return true; + } + if (fallback < 0) + fallback = i; + } + + if (fallback >= 0) + { + info = Evaluate(_entries[fallback], timelineFrame); + return true; + } + + info = default; + return false; + } + + private static bool IsExist(in Entry entry, int timelineFrame) + => entry.Frame <= timelineFrame && timelineFrame < entry.Frame + entry.Length; + + private SceneObjectInfo Evaluate(in Entry entry, int timelineFrame) + { + var visual = entry.Item; + int length = entry.Length; + int local = Math.Clamp(timelineFrame - entry.Frame, 0, length); + + double x = visual.X.GetValue(local, length, _fps); + double y = visual.Y.GetValue(local, length, _fps); + double z = visual.Z.GetValue(local, length, _fps); + double zoom = visual.Zoom.GetValue(local, length, _fps) / 100d; + double rz = visual.Rotation.GetValue(local, length, _fps); + + double opacity = visual.Opacity.GetValue(local, length, _fps) / 100d; + double timeSec = _fps > 0 ? local / (double)_fps : 0d; + double durationSec = _fps > 0 ? length / (double)_fps : 0d; + double fadeIn = visual.FadeIn <= 0d ? 1d : Math.Min(1d, timeSec / visual.FadeIn); + double fadeOut = visual.FadeOut <= 0d ? 1d : Math.Min(1d, (durationSec - timeSec) / visual.FadeOut); + double alpha = opacity * Math.Min(fadeIn, fadeOut) * 255d; + + return new SceneObjectInfo(entry.Tag, IsExist(entry, timelineFrame), x, y, z, zoom, rz, alpha, entry.Layer); + } + } +}