「縁取り(内側・部分)」エフェクトを追加する

このコミットが含まれているのは:
routersys committed 2026-10-25 13:06:31 +09:00
1 親 3ebd92f276
コミット 25278a5b84
2ファイル変更
+527

検索条件に一致するファイルはありません

+124
ファイルの表示
@@ -0,0 +1,124 @@
using System.ComponentModel.DataAnnotations;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Controls;
using YukkuriMovieMaker.Exo;
using YukkuriMovieMaker.Player.Video;
using YukkuriMovieMaker.Plugin;
using YukkuriMovieMaker.Plugin.Effects;
using YukkuriMovieMaker.Project;
namespace PartialInnerOutline;
[PluginDetails(AuthorName = "routersys")]
[VideoEffect(
nameof(Texts.PartialInnerOutline),
[VideoEffectCategories.Decoration],
[nameof(Texts.TagPartialInnerOutline), nameof(Texts.TagInnerOutline), nameof(Texts.TagPartialOutline), nameof(Texts.TagOutline), nameof(Texts.TagBorder)],
IsAviUtlSupported = false,
ResourceType = typeof(Texts))]
public sealed class PartialInnerOutlineEffect : VideoEffectBase, IFileItem
{
public override string Label => $"{Texts.PartialInnerOutline} {Thickness.GetValue(0, 1, 30):F0}px";
public PartialInnerOutlineEffect()
{
PartialInnerOutlineTelemetry.EnsureStartedOnce();
PartialInnerOutlineUpdateNotifier.EnsureCheckedOnce();
}
[Display(GroupName = nameof(Texts.OutlineGroup), Name = nameof(Texts.Thickness), Description = nameof(Texts.ThicknessDescription), Order = 0, ResourceType = typeof(Texts))]
[AnimationSlider("F1", "px", 0, 10)]
public Animation Thickness { get; } = new Animation(3, 0, PartialInnerOutlineSettings.MaximumThickness);
[Display(GroupName = nameof(Texts.OutlineGroup), Name = nameof(Texts.Quality), Description = nameof(Texts.QualityDescription), Order = 1, ResourceType = typeof(Texts))]
[AnimationSlider("F0", "", PartialInnerOutlineSettings.MinimumQuality, 64)]
public Animation Quality { get; } = new Animation(64, PartialInnerOutlineSettings.MinimumQuality, PartialInnerOutlineSettings.MaximumQuality);
[Display(GroupName = nameof(Texts.OutlineGroup), Name = nameof(Texts.Smoothness), Description = nameof(Texts.SmoothnessDescription), Order = 2, ResourceType = typeof(Texts))]
[AnimationSlider("F1", "%", 0, 100)]
public Animation Smoothness { get; } = new Animation(100, 0, 100);
[Display(GroupName = nameof(Texts.OutlineGroup), Name = nameof(Texts.IsAngular), Description = nameof(Texts.IsAngularDescription), Order = 3, ResourceType = typeof(Texts))]
[ToggleSlider]
public bool IsAngular { get => _isAngular; set => Set(ref _isAngular, value); }
private bool _isAngular;
[Display(GroupName = nameof(Texts.BandGroup), Name = nameof(Texts.BandDirection), Description = nameof(Texts.BandDirectionDescription), Order = 10, ResourceType = typeof(Texts))]
[AnimationSlider("F1", "°", 0, 360)]
public Animation BandDirection { get; } = new Animation(90, 0, 360);
[Display(GroupName = nameof(Texts.BandGroup), Name = nameof(Texts.BandPosition), Description = nameof(Texts.BandPositionDescription), Order = 11, ResourceType = typeof(Texts))]
[AnimationSlider("F1", "%", -100, 100)]
public Animation BandPosition { get; } = new Animation(0, -200, 200);
[Display(GroupName = nameof(Texts.BandGroup), Name = nameof(Texts.BandWidth), Description = nameof(Texts.BandWidthDescription), Order = 12, ResourceType = typeof(Texts))]
[AnimationSlider("F1", "%", 0, 100)]
public Animation BandWidth { get; } = new Animation(100, 0, PartialInnerOutlineSettings.FullBandWidthPercent);
[Display(GroupName = nameof(Texts.BandGroup), Name = nameof(Texts.BandSoftness), Description = nameof(Texts.BandSoftnessDescription), Order = 13, ResourceType = typeof(Texts))]
[AnimationSlider("F1", "%", 0, 100)]
public Animation BandSoftness { get; } = new Animation(30, 0, 100);
[Display(GroupName = nameof(Texts.AppearanceGroup), Name = nameof(Texts.Opacity), Description = nameof(Texts.OpacityDescription), Order = 20, ResourceType = typeof(Texts))]
[AnimationSlider("F1", "%", 0, 100)]
public Animation Opacity { get; } = new Animation(100, 0, 100);
[Display(GroupName = nameof(Texts.AppearanceGroup), Name = nameof(Texts.Blur), Description = nameof(Texts.BlurDescription), Order = 21, ResourceType = typeof(Texts))]
[AnimationSlider("F1", "px", 0, 5)]
public Animation Blur { get; } = new Animation(0, 0, PartialInnerOutlineSettings.MaximumBlur);
[Display(GroupName = nameof(Texts.AppearanceGroup), Name = nameof(Texts.Blend), Description = nameof(Texts.BlendDescription), Order = 22, ResourceType = typeof(Texts))]
[EnumComboBox]
public Blend Blend { get => _blend; set => Set(ref _blend, value); }
private Blend _blend = Blend.Normal;
[Display(GroupName = nameof(Texts.AppearanceGroup), Name = nameof(Texts.IsOutlineOnly), Description = nameof(Texts.IsOutlineOnlyDescription), Order = 23, ResourceType = typeof(Texts))]
[ToggleSlider]
public bool IsOutlineOnly { get => _isOutlineOnly; set => Set(ref _isOutlineOnly, value); }
private bool _isOutlineOnly;
[Display(GroupName = nameof(Texts.BrushGroup), ResourceType = typeof(Texts), AutoGenerateField = true)]
public YukkuriMovieMaker.Plugin.Brush.Brush Brush { get; } = new YukkuriMovieMaker.Plugin.Brush.Brush();
private IAnimatable[]? _animatables;
public override IEnumerable<string> CreateExoVideoFilters(int keyFrameIndex, ExoOutputDescription exoOutputDescription) => [];
public override IVideoEffectProcessor CreateVideoEffect(IGraphicsDevicesAndContext devices)
{
try
{
return new PartialInnerOutlineEffectProcessor(devices, this);
}
catch (Exception exception)
{
PartialInnerOutlineTelemetry.Report(exception);
throw;
}
}
protected override IEnumerable<IAnimatable> GetAnimatables()
=> _animatables ??= [Thickness, Quality, Smoothness, BandDirection, BandPosition, BandWidth, BandSoftness, Opacity, Blur, Brush];
public override IEnumerable<string> GetFiles()
{
foreach (var file in base.GetFiles())
yield return file;
foreach (var file in Brush.GetFiles())
yield return file;
}
public override void ReplaceFile(string from, string to)
{
base.ReplaceFile(from, to);
Brush.ReplaceFile(from, to);
}
public override IEnumerable<TimelineResource> GetResources()
{
foreach (var resource in base.GetResources())
yield return resource;
foreach (var resource in Brush.GetResources())
yield return resource;
}
}
+403
ファイルの表示
@@ -0,0 +1,403 @@
using Vortice;
using Vortice.Direct2D1;
using Vortice.Direct2D1.Effects;
using YukkuriMovieMaker.Commons;
using YukkuriMovieMaker.Player;
using YukkuriMovieMaker.Player.Video;
using YukkuriMovieMaker.Player.Video.Effects;
using YukkuriMovieMaker.Plugin.Brush;
using AlphaMask = Vortice.Direct2D1.Effects.AlphaMask;
using Blend = Vortice.Direct2D1.Effects.Blend;
using ProjectBlend = YukkuriMovieMaker.Project.Blend;
namespace PartialInnerOutline;
internal sealed class PartialInnerOutlineEffectProcessor : VideoEffectProcessorBase
{
private readonly IGraphicsDevicesAndContext _devices;
private readonly PartialInnerOutlineEffect _item;
private readonly List<OutlineDilationCustomEffect> _dilations = [];
private readonly float[] _steps = new float[PartialInnerOutlineSettings.MaximumStepCount];
private readonly int[] _inflations = new int[PartialInnerOutlineSettings.MaximumStepCount];
private OutlineMaskCustomEffect? _mask;
private AlphaMask? _brushMask;
private GaussianBlur? _blur;
private AlphaMask? _clip;
private Composite? _composite;
private Blend? _blend;
private Opacity? _empty;
private AffineTransform2D? _sink;
private readonly IBrushSource _brushSource;
private ID2D1CommandList? _brushCommandList;
private bool _isBrushStale = true;
private RawRectF? _fillBounds;
private OutputKind? _outputKind;
private ProjectBlend _blendMode;
private PlanKey? _plan;
private int _passCount;
private int? _padding;
private double? _blurValue;
private PartialInnerOutlineSettings.FeatherRamp? _ramp;
private float? _opacity;
private PartialInnerOutlineSettings.BandGeometry? _band;
public PartialInnerOutlineEffectProcessor(IGraphicsDevicesAndContext devices, PartialInnerOutlineEffect item)
: base(devices)
{
_devices = devices;
_item = item;
_brushSource = item.Brush.CreateBrush(devices);
disposer.Collect(_brushSource);
}
public override DrawDescription Update(EffectDescription effectDescription)
{
try
{
return UpdateCore(effectDescription);
}
catch (Exception exception)
{
PartialInnerOutlineTelemetry.Report(exception);
throw;
}
}
private DrawDescription UpdateCore(EffectDescription effectDescription)
{
if (IsPassThroughEffect || _mask is null || _blur is null || _sink is null || input is null)
return effectDescription.DrawDescription;
var frame = effectDescription.ItemPosition.Frame;
var length = effectDescription.ItemDuration.Frame;
var fps = effectDescription.FPS;
var thickness = Math.Clamp(Math.Round(_item.Thickness.GetValue(frame, length, fps), PartialInnerOutlineSettings.ThicknessDecimals), 0.0, PartialInnerOutlineSettings.MaximumThickness);
var quality = (int)Math.Clamp(_item.Quality.GetValue(frame, length, fps), PartialInnerOutlineSettings.MinimumQuality, PartialInnerOutlineSettings.MaximumQuality);
var smoothness = _item.Smoothness.GetValue(frame, length, fps) / PartialInnerOutlineSettings.PercentScale;
var width = _item.BandWidth.GetValue(frame, length, fps);
var opacity = Math.Clamp(_item.Opacity.GetValue(frame, length, fps) / PartialInnerOutlineSettings.PercentScale, 0.0, 1.0);
var blur = Math.Clamp(_item.Blur.GetValue(frame, length, fps), 0.0, PartialInnerOutlineSettings.MaximumBlur);
var isAngular = _item.IsAngular;
var isOutlineOnly = _item.IsOutlineOnly;
var blendMode = _item.Blend;
var bounds = _devices.DeviceContext.GetImageLocalBounds(input);
var hasOutline = IsUsable(bounds) && thickness > 0.0 && width > 0.0 && opacity > 0.0;
var outputKind = !hasOutline ? (isOutlineOnly ? OutputKind.Empty : OutputKind.Input)
: isOutlineOnly ? OutputKind.Outline
: blendMode.IsCompositionEffect() ? OutputKind.Composite
: OutputKind.Blend;
if (_outputKind != outputKind || (outputKind >= OutputKind.Composite && _blendMode != blendMode))
ConnectSink(outputKind, blendMode);
if (!hasOutline)
return effectDescription.DrawDescription;
var blurPadding = PartialInnerOutlineSettings.GetBlurPadding(blur);
var plan = new PlanKey(thickness, quality, isAngular);
if (_plan != plan)
ApplyPlan(plan);
if (_padding != blurPadding)
{
_mask.OutputInflation = blurPadding;
_padding = blurPadding;
}
if (_blurValue != blur)
{
_blur.StandardDeviation = (float)blur;
_blurValue = blur;
}
var ramp = PartialInnerOutlineSettings.GetFeatherRamp(smoothness);
if (_ramp != ramp)
{
_mask.FeatherLow = ramp.Low;
_mask.FeatherInverseWidth = ramp.InverseWidth;
_ramp = ramp;
}
var opacityKey = (float)opacity;
if (_opacity != opacityKey)
{
_mask.Opacity = opacityKey;
_opacity = opacityKey;
}
var band = PartialInnerOutlineSettings.GetBand(
_item.BandDirection.GetValue(frame, length, fps),
_item.BandPosition.GetValue(frame, length, fps),
width,
_item.BandSoftness.GetValue(frame, length, fps),
bounds.Left,
bounds.Top,
bounds.Right,
bounds.Bottom);
if (_band != band)
{
_mask.BandDirectionX = band.DirectionX;
_mask.BandDirectionY = band.DirectionY;
_mask.BandOffset = band.Offset;
_mask.BandEdgeNear = band.EdgeNear;
_mask.BandEdgeFar = band.EdgeFar;
_band = band;
}
UpdateBrush(effectDescription, new RawRectF(bounds.Left - blurPadding, bounds.Top - blurPadding, bounds.Right + blurPadding, bounds.Bottom + blurPadding));
return effectDescription.DrawDescription;
}
private static bool IsUsable(RawRectF bounds)
=> float.IsFinite(bounds.Left) && float.IsFinite(bounds.Top) && float.IsFinite(bounds.Right) && float.IsFinite(bounds.Bottom)
&& bounds.Right > bounds.Left && bounds.Bottom > bounds.Top;
private void UpdateBrush(EffectDescription effectDescription, RawRectF fillBounds)
{
if (_brushMask is null)
return;
var isChanged = _brushSource.Update(new BrushSourceDescription(effectDescription, fillBounds));
var isSameBounds = _fillBounds is { } previous && previous.Equals(fillBounds);
if (!_isBrushStale && !isChanged && isSameBounds)
return;
disposer.RemoveAndDispose(ref _brushCommandList);
var commandList = _devices.DeviceContext.CreateCommandList();
disposer.Collect(commandList);
_brushCommandList = commandList;
var deviceContext = _devices.DeviceContext;
deviceContext.Target = commandList;
try
{
deviceContext.BeginDraw();
deviceContext.Clear(null);
deviceContext.FillRectangle(fillBounds, _brushSource.Brush);
deviceContext.EndDraw();
}
finally
{
deviceContext.Target = null;
}
commandList.Close();
_brushMask.SetInput(0, commandList, true);
_fillBounds = fillBounds;
_isBrushStale = false;
}
private void ApplyPlan(PlanKey plan)
{
var count = PartialInnerOutlineSettings.DecomposeThickness(plan.Thickness, _steps);
PartialInnerOutlineSettings.GetOutputInflations(_steps.AsSpan(0, count), _inflations);
EnsureDilations(count);
for (var index = 0; index < count; index++)
{
var step = _steps[index];
var dilation = _dilations[index];
dilation.StepPixels = step;
dilation.SampleCount = PartialInnerOutlineSettings.GetRingSampleCount(step, plan.IsAngular, plan.Quality);
dilation.IsAngular = plan.IsAngular;
dilation.OutputInflation = _inflations[index];
dilation.Cached = index == count - 1;
}
ConnectChain(count);
_passCount = count;
_plan = plan;
}
private void EnsureDilations(int count)
{
while (_dilations.Count < count)
{
var dilation = new OutlineDilationCustomEffect(_devices);
if (!dilation.IsEnabled)
{
dilation.Dispose();
throw new InvalidOperationException("The outline dilation effect could not be created.");
}
dilation.IsInverted = _dilations.Count == 0;
_dilations.Add(dilation);
disposer.Collect(dilation);
}
}
private void ConnectChain(int count)
{
for (var index = 0; index < _dilations.Count; index++)
{
if (index >= count)
_dilations[index].SetInput(0, null, true);
else if (index == 0)
_dilations[0].SetInput(0, input, true);
else
{
using var previous = _dilations[index - 1].Output;
_dilations[index].SetInput(0, previous, true);
}
}
if (count > 0)
{
using var last = _dilations[count - 1].Output;
_mask?.SetInput(0, last, true);
}
else
_mask?.SetInput(0, null, true);
}
private void ConnectSink(OutputKind kind, ProjectBlend blendMode)
{
if (_sink is null || _empty is null || _clip is null || _composite is null || _blend is null)
return;
switch (kind)
{
case OutputKind.Input:
_sink.SetInput(0, input, true);
break;
case OutputKind.Empty:
{
using var output = _empty.Output;
_sink.SetInput(0, output, true);
break;
}
case OutputKind.Outline:
{
using var output = _clip.Output;
_sink.SetInput(0, output, true);
break;
}
case OutputKind.Composite:
{
_composite.Mode = blendMode.ToD2DCompositionMode();
using var output = _composite.Output;
_sink.SetInput(0, output, true);
break;
}
default:
{
_blend.Mode = blendMode.ToD2DBlendMode();
using var output = _blend.Output;
_sink.SetInput(0, output, true);
break;
}
}
_outputKind = kind;
_blendMode = blendMode;
}
protected override ID2D1Image? CreateEffect(IGraphicsDevicesAndContext devices)
{
var mask = new OutlineMaskCustomEffect(devices);
if (!mask.IsEnabled)
{
mask.Dispose();
return null;
}
_mask = mask;
disposer.Collect(mask);
_brushMask = new AlphaMask(devices.DeviceContext);
disposer.Collect(_brushMask);
using (var maskOutput = mask.Output)
_brushMask.SetInput(1, maskOutput, true);
_blur = new GaussianBlur(devices.DeviceContext);
disposer.Collect(_blur);
using (var maskedOutput = _brushMask.Output)
_blur.SetInput(0, maskedOutput, true);
_clip = new AlphaMask(devices.DeviceContext);
disposer.Collect(_clip);
using (var blurredOutput = _blur.Output)
_clip.SetInput(0, blurredOutput, true);
_composite = new Composite(devices.DeviceContext);
disposer.Collect(_composite);
_blend = new Blend(devices.DeviceContext);
disposer.Collect(_blend);
using (var clippedOutput = _clip.Output)
{
_composite.SetInput(1, clippedOutput, true);
_blend.SetInput(1, clippedOutput, true);
}
_empty = new Opacity(devices.DeviceContext) { Value = 0f };
disposer.Collect(_empty);
_sink = new AffineTransform2D(devices.DeviceContext);
disposer.Collect(_sink);
var result = _sink.Output;
disposer.Collect(result);
return result;
}
protected override void setInput(ID2D1Image? input)
{
try
{
SetInputCore(input);
}
catch (Exception exception)
{
PartialInnerOutlineTelemetry.Report(exception);
throw;
}
}
private void SetInputCore(ID2D1Image? image)
{
if (_passCount > 0)
_dilations[0].SetInput(0, image, true);
_clip?.SetInput(1, image, true);
_composite?.SetInput(0, image, true);
_blend?.SetInput(0, image, true);
_empty?.SetInput(0, image, true);
if (_outputKind == OutputKind.Input)
_sink?.SetInput(0, image, true);
}
protected override void ClearEffectChain()
{
try
{
foreach (var dilation in _dilations)
dilation.SetInput(0, null, true);
_mask?.SetInput(0, null, true);
_brushMask?.SetInput(0, null, true);
_brushMask?.SetInput(1, null, true);
_blur?.SetInput(0, null, true);
_clip?.SetInput(0, null, true);
_clip?.SetInput(1, null, true);
_composite?.SetInput(0, null, true);
_composite?.SetInput(1, null, true);
_blend?.SetInput(0, null, true);
_blend?.SetInput(1, null, true);
_empty?.SetInput(0, null, true);
_sink?.SetInput(0, null, true);
}
catch (Exception exception)
{
PartialInnerOutlineTelemetry.Report(exception);
throw;
}
}
private enum OutputKind
{
Input,
Empty,
Outline,
Composite,
Blend,
}
private readonly record struct PlanKey(double Thickness, int Quality, bool IsAngular);
}