diff --git a/SpectrumColorShift/DispersionLaw.cs b/SpectrumColorShift/DispersionLaw.cs index c775dc8..bebfd13 100644 --- a/SpectrumColorShift/DispersionLaw.cs +++ b/SpectrumColorShift/DispersionLaw.cs @@ -1,13 +1,12 @@ using System.ComponentModel.DataAnnotations; -namespace SpectrumColorShift -{ - public enum DispersionLaw - { - [Display(Name = nameof(Texts.DispersionGrating), Description = nameof(Texts.DispersionGratingDescription), ResourceType = typeof(Texts))] - Grating = 0, +namespace SpectrumColorShift; - [Display(Name = nameof(Texts.DispersionPrism), Description = nameof(Texts.DispersionPrismDescription), ResourceType = typeof(Texts))] - Prism = 1, - } +public enum DispersionLaw +{ + [Display(Name = nameof(Texts.DispersionGrating), Description = nameof(Texts.DispersionGratingDescription), ResourceType = typeof(Texts))] + Grating = 0, + + [Display(Name = nameof(Texts.DispersionPrism), Description = nameof(Texts.DispersionPrismDescription), ResourceType = typeof(Texts))] + Prism = 1, } diff --git a/SpectrumColorShift/ShiftGeometry.cs b/SpectrumColorShift/ShiftGeometry.cs index 09011d2..575b623 100644 --- a/SpectrumColorShift/ShiftGeometry.cs +++ b/SpectrumColorShift/ShiftGeometry.cs @@ -1,129 +1,128 @@ using System.Numerics; using Vortice; -namespace SpectrumColorShift +namespace SpectrumColorShift; + +internal static class ShiftGeometry { - internal static class ShiftGeometry + public const float MaxRadialShift = 0.5f; + public const int Guard = 2; + + private const double RoundingTolerance = 1e-3; + + public static Vector2 Center(RawRect rect, Vector2 offset) + => new((rect.Left + rect.Right) * 0.5f + offset.X, (rect.Top + rect.Bottom) * 0.5f + offset.Y); + + public static float Radius(RawRect rect, Vector2 center) => Math.Max(Farthest(rect, center), 1f); + + public static RawRect Output(RawRect rect, ShiftMode mode, float distance, Vector2 direction, Vector2 center, float radius) { - public const float MaxRadialShift = 0.5f; - public const int Guard = 2; + var reach = Math.Abs(distance); + if (reach <= 0f) + return rect; - private const double RoundingTolerance = 1e-3; - - public static Vector2 Center(RawRect rect, Vector2 offset) - => new((rect.Left + rect.Right) * 0.5f + offset.X, (rect.Top + rect.Bottom) * 0.5f + offset.Y); - - public static float Radius(RawRect rect, Vector2 center) => Math.Max(Farthest(rect, center), 1f); - - public static RawRect Output(RawRect rect, ShiftMode mode, float distance, Vector2 direction, Vector2 center, float radius) + return mode switch { - var reach = Math.Abs(distance); - if (reach <= 0f) - return rect; - - return mode switch - { - ShiftMode.Radial => Scaled(rect, center, Math.Min(reach / radius, MaxRadialShift)), - ShiftMode.Rotational => Swept(rect, center, Math.Min(reach / radius, MathF.PI)), - _ => Straight(rect, reach, direction), - }; - } - - public static RawRect Input(RawRect output, ShiftMode mode, float distance, Vector2 direction, Vector2 center, float radius) - { - var reach = Math.Abs(distance); - if (reach <= 0f) - return output; - - switch (mode) - { - case ShiftMode.Radial: - var ratio = Math.Min(reach / radius, MaxRadialShift); - return Inflate( - output, - Margin(ratio * Math.Max(Math.Abs(output.Left - center.X), Math.Abs(output.Right - center.X))), - Margin(ratio * Math.Max(Math.Abs(output.Top - center.Y), Math.Abs(output.Bottom - center.Y)))); - case ShiftMode.Rotational: - var chord = 2f * MathF.Sin(Math.Min(reach / radius, MathF.PI) * 0.5f) * Farthest(output, center); - return Inflate(output, Margin(chord), Margin(chord)); - default: - return Straight(output, reach, direction); - } - } - - private static RawRect Straight(RawRect rect, float reach, Vector2 direction) - => Inflate(rect, Margin(reach * Math.Abs(direction.X)), Margin(reach * Math.Abs(direction.Y))); - - private static RawRect Scaled(RawRect rect, Vector2 center, float ratio) - { - var near = 1f / (1f + ratio); - var far = 1f / (1f - ratio); - var left = rect.Left - center.X; - var top = rect.Top - center.Y; - var right = rect.Right - center.X; - var bottom = rect.Bottom - center.Y; - - return Outward( - center.X + Math.Min(left * near, left * far), - center.Y + Math.Min(top * near, top * far), - center.X + Math.Max(right * near, right * far), - center.Y + Math.Max(bottom * near, bottom * far)); - } - - private static RawRect Swept(RawRect rect, Vector2 center, float angle) - { - var right = float.NegativeInfinity; - var bottom = float.NegativeInfinity; - var left = float.NegativeInfinity; - var top = float.NegativeInfinity; - - Extend(rect.Left - center.X, rect.Top - center.Y, angle, ref right, ref bottom, ref left, ref top); - Extend(rect.Right - center.X, rect.Top - center.Y, angle, ref right, ref bottom, ref left, ref top); - Extend(rect.Left - center.X, rect.Bottom - center.Y, angle, ref right, ref bottom, ref left, ref top); - Extend(rect.Right - center.X, rect.Bottom - center.Y, angle, ref right, ref bottom, ref left, ref top); - - return Outward(center.X - left, center.Y - top, center.X + right, center.Y + bottom); - } - - private static void Extend(float x, float y, float angle, ref float right, ref float bottom, ref float left, ref float top) - { - var length = MathF.Sqrt(x * x + y * y); - var heading = MathF.Atan2(y, x); - right = Math.Max(right, Reach(length, heading, 0f, angle)); - bottom = Math.Max(bottom, Reach(length, heading, MathF.PI * 0.5f, angle)); - left = Math.Max(left, Reach(length, heading, MathF.PI, angle)); - top = Math.Max(top, Reach(length, heading, -MathF.PI * 0.5f, angle)); - } - - private static float Reach(float length, float heading, float axis, float angle) - { - var difference = Math.Abs(MathF.IEEERemainder(heading - axis, MathF.Tau)); - return length * MathF.Cos(Math.Max(difference - angle, 0f)); - } - - private static float Farthest(RawRect rect, Vector2 center) - { - var dx = Math.Max(Math.Abs(rect.Left - center.X), Math.Abs(rect.Right - center.X)); - var dy = Math.Max(Math.Abs(rect.Top - center.Y), Math.Abs(rect.Bottom - center.Y)); - return MathF.Sqrt(dx * dx + dy * dy); - } - - private static int Margin(float distance) => Saturate((long)Math.Ceiling(distance - RoundingTolerance) + Guard); - - private static RawRect Inflate(RawRect rect, int marginX, int marginY) - => new( - Saturate((long)rect.Left - marginX), - Saturate((long)rect.Top - marginY), - Saturate((long)rect.Right + marginX), - Saturate((long)rect.Bottom + marginY)); - - private static RawRect Outward(float left, float top, float right, float bottom) - => new( - Saturate((long)Math.Floor(left) - Guard), - Saturate((long)Math.Floor(top) - Guard), - Saturate((long)Math.Ceiling(right) + Guard), - Saturate((long)Math.Ceiling(bottom) + Guard)); - - private static int Saturate(long value) => (int)Math.Clamp(value, int.MinValue, int.MaxValue); + ShiftMode.Radial => Scaled(rect, center, Math.Min(reach / radius, MaxRadialShift)), + ShiftMode.Rotational => Swept(rect, center, Math.Min(reach / radius, MathF.PI)), + _ => Straight(rect, reach, direction), + }; } + + public static RawRect Input(RawRect output, ShiftMode mode, float distance, Vector2 direction, Vector2 center, float radius) + { + var reach = Math.Abs(distance); + if (reach <= 0f) + return output; + + switch (mode) + { + case ShiftMode.Radial: + var ratio = Math.Min(reach / radius, MaxRadialShift); + return Inflate( + output, + Margin(ratio * Math.Max(Math.Abs(output.Left - center.X), Math.Abs(output.Right - center.X))), + Margin(ratio * Math.Max(Math.Abs(output.Top - center.Y), Math.Abs(output.Bottom - center.Y)))); + case ShiftMode.Rotational: + var chord = 2f * MathF.Sin(Math.Min(reach / radius, MathF.PI) * 0.5f) * Farthest(output, center); + return Inflate(output, Margin(chord), Margin(chord)); + default: + return Straight(output, reach, direction); + } + } + + private static RawRect Straight(RawRect rect, float reach, Vector2 direction) + => Inflate(rect, Margin(reach * Math.Abs(direction.X)), Margin(reach * Math.Abs(direction.Y))); + + private static RawRect Scaled(RawRect rect, Vector2 center, float ratio) + { + var near = 1f / (1f + ratio); + var far = 1f / (1f - ratio); + var left = rect.Left - center.X; + var top = rect.Top - center.Y; + var right = rect.Right - center.X; + var bottom = rect.Bottom - center.Y; + + return Outward( + center.X + Math.Min(left * near, left * far), + center.Y + Math.Min(top * near, top * far), + center.X + Math.Max(right * near, right * far), + center.Y + Math.Max(bottom * near, bottom * far)); + } + + private static RawRect Swept(RawRect rect, Vector2 center, float angle) + { + var right = float.NegativeInfinity; + var bottom = float.NegativeInfinity; + var left = float.NegativeInfinity; + var top = float.NegativeInfinity; + + Extend(rect.Left - center.X, rect.Top - center.Y, angle, ref right, ref bottom, ref left, ref top); + Extend(rect.Right - center.X, rect.Top - center.Y, angle, ref right, ref bottom, ref left, ref top); + Extend(rect.Left - center.X, rect.Bottom - center.Y, angle, ref right, ref bottom, ref left, ref top); + Extend(rect.Right - center.X, rect.Bottom - center.Y, angle, ref right, ref bottom, ref left, ref top); + + return Outward(center.X - left, center.Y - top, center.X + right, center.Y + bottom); + } + + private static void Extend(float x, float y, float angle, ref float right, ref float bottom, ref float left, ref float top) + { + var length = MathF.Sqrt(x * x + y * y); + var heading = MathF.Atan2(y, x); + right = Math.Max(right, Reach(length, heading, 0f, angle)); + bottom = Math.Max(bottom, Reach(length, heading, MathF.PI * 0.5f, angle)); + left = Math.Max(left, Reach(length, heading, MathF.PI, angle)); + top = Math.Max(top, Reach(length, heading, -MathF.PI * 0.5f, angle)); + } + + private static float Reach(float length, float heading, float axis, float angle) + { + var difference = Math.Abs(MathF.IEEERemainder(heading - axis, MathF.Tau)); + return length * MathF.Cos(Math.Max(difference - angle, 0f)); + } + + private static float Farthest(RawRect rect, Vector2 center) + { + var dx = Math.Max(Math.Abs(rect.Left - center.X), Math.Abs(rect.Right - center.X)); + var dy = Math.Max(Math.Abs(rect.Top - center.Y), Math.Abs(rect.Bottom - center.Y)); + return MathF.Sqrt(dx * dx + dy * dy); + } + + private static int Margin(float distance) => Saturate((long)Math.Ceiling(distance - RoundingTolerance) + Guard); + + private static RawRect Inflate(RawRect rect, int marginX, int marginY) + => new( + Saturate((long)rect.Left - marginX), + Saturate((long)rect.Top - marginY), + Saturate((long)rect.Right + marginX), + Saturate((long)rect.Bottom + marginY)); + + private static RawRect Outward(float left, float top, float right, float bottom) + => new( + Saturate((long)Math.Floor(left) - Guard), + Saturate((long)Math.Floor(top) - Guard), + Saturate((long)Math.Ceiling(right) + Guard), + Saturate((long)Math.Ceiling(bottom) + Guard)); + + private static int Saturate(long value) => (int)Math.Clamp(value, int.MinValue, int.MaxValue); } diff --git a/SpectrumColorShift/ShiftMode.cs b/SpectrumColorShift/ShiftMode.cs index 974d6c3..c684bdc 100644 --- a/SpectrumColorShift/ShiftMode.cs +++ b/SpectrumColorShift/ShiftMode.cs @@ -1,16 +1,15 @@ using System.ComponentModel.DataAnnotations; -namespace SpectrumColorShift +namespace SpectrumColorShift; + +public enum ShiftMode { - public enum ShiftMode - { - [Display(Name = nameof(Texts.ModeLinear), Description = nameof(Texts.ModeLinearDescription), ResourceType = typeof(Texts))] - Linear = 1, + [Display(Name = nameof(Texts.ModeLinear), Description = nameof(Texts.ModeLinearDescription), ResourceType = typeof(Texts))] + Linear = 1, - [Display(Name = nameof(Texts.ModeRadial), Description = nameof(Texts.ModeRadialDescription), ResourceType = typeof(Texts))] - Radial = 2, + [Display(Name = nameof(Texts.ModeRadial), Description = nameof(Texts.ModeRadialDescription), ResourceType = typeof(Texts))] + Radial = 2, - [Display(Name = nameof(Texts.ModeRotational), Description = nameof(Texts.ModeRotationalDescription), ResourceType = typeof(Texts))] - Rotational = 4, - } + [Display(Name = nameof(Texts.ModeRotational), Description = nameof(Texts.ModeRotationalDescription), ResourceType = typeof(Texts))] + Rotational = 4, } diff --git a/SpectrumColorShift/Spectrum.cs b/SpectrumColorShift/Spectrum.cs index a7a200e..7a45368 100644 --- a/SpectrumColorShift/Spectrum.cs +++ b/SpectrumColorShift/Spectrum.cs @@ -1,81 +1,80 @@ using System.Numerics; -namespace SpectrumColorShift +namespace SpectrumColorShift; + +internal static class Spectrum { - internal static class Spectrum + public const int MinSteps = 8; + public const int MaxSteps = 64; + public const int DefaultSteps = 32; + public const float ShortestWavelength = 400f; + public const float LongestWavelength = 700f; + public const float DefaultReferenceWavelength = 550f; + + public static int ClampSteps(int steps) => Math.Clamp(steps, MinSteps, MaxSteps); + + public static float ClampReference(float wavelength) + => float.IsNaN(wavelength) ? DefaultReferenceWavelength : Math.Clamp(wavelength, ShortestWavelength, LongestWavelength); + + public static (double X, double Y, double Z) ColorMatching(double wavelength) { - public const int MinSteps = 8; - public const int MaxSteps = 64; - public const int DefaultSteps = 32; - public const float ShortestWavelength = 400f; - public const float LongestWavelength = 700f; - public const float DefaultReferenceWavelength = 550f; + var x = 0.362 * Lobe(wavelength, 442.0, 0.0624, 0.0374) + + 1.056 * Lobe(wavelength, 599.8, 0.0264, 0.0323) + - 0.065 * Lobe(wavelength, 501.1, 0.0490, 0.0382); + var y = 0.821 * Lobe(wavelength, 568.8, 0.0213, 0.0247) + + 0.286 * Lobe(wavelength, 530.9, 0.0613, 0.0322); + var z = 1.217 * Lobe(wavelength, 437.0, 0.0845, 0.0278) + + 0.681 * Lobe(wavelength, 459.0, 0.0385, 0.0725); - public static int ClampSteps(int steps) => Math.Clamp(steps, MinSteps, MaxSteps); + return (x, y, z); + } - public static float ClampReference(float wavelength) - => float.IsNaN(wavelength) ? DefaultReferenceWavelength : Math.Clamp(wavelength, ShortestWavelength, LongestWavelength); + public static Vector3 Weights(double wavelength) + { + var (x, y, z) = ColorMatching(wavelength); - public static (double X, double Y, double Z) ColorMatching(double wavelength) + return new Vector3( + (float)Math.Max(3.2404542 * x - 1.5371385 * y - 0.4985314 * z, 0.0), + (float)Math.Max(-0.9692660 * x + 1.8760108 * y + 0.0415560 * z, 0.0), + (float)Math.Max(0.0556434 * x - 0.2040259 * y + 1.0572252 * z, 0.0)); + } + + public static void Fill(Span table, int steps, float referenceWavelength, DispersionLaw law) + { + steps = ClampSteps(steps); + var reference = ClampReference(referenceWavelength); + var norm = Math.Max( + Math.Abs(Displacement(law, ShortestWavelength, reference)), + Math.Abs(Displacement(law, LongestWavelength, reference))); + + double sumRed = 0.0, sumGreen = 0.0, sumBlue = 0.0; + for (var index = 0; index < steps; index++) { - var x = 0.362 * Lobe(wavelength, 442.0, 0.0624, 0.0374) - + 1.056 * Lobe(wavelength, 599.8, 0.0264, 0.0323) - - 0.065 * Lobe(wavelength, 501.1, 0.0490, 0.0382); - var y = 0.821 * Lobe(wavelength, 568.8, 0.0213, 0.0247) - + 0.286 * Lobe(wavelength, 530.9, 0.0613, 0.0322); - var z = 1.217 * Lobe(wavelength, 437.0, 0.0845, 0.0278) - + 0.681 * Lobe(wavelength, 459.0, 0.0385, 0.0725); - - return (x, y, z); + var wavelength = ShortestWavelength + (LongestWavelength - ShortestWavelength) * index / (steps - 1); + var weights = Weights(wavelength); + table[index] = new Vector4(weights, (float)(Displacement(law, wavelength, reference) / norm)); + sumRed += weights.X; + sumGreen += weights.Y; + sumBlue += weights.Z; } - public static Vector3 Weights(double wavelength) + for (var index = 0; index < steps; index++) { - var (x, y, z) = ColorMatching(wavelength); - - return new Vector3( - (float)Math.Max(3.2404542 * x - 1.5371385 * y - 0.4985314 * z, 0.0), - (float)Math.Max(-0.9692660 * x + 1.8760108 * y + 0.0415560 * z, 0.0), - (float)Math.Max(0.0556434 * x - 0.2040259 * y + 1.0572252 * z, 0.0)); + var entry = table[index]; + table[index] = new Vector4((float)(entry.X / sumRed), (float)(entry.Y / sumGreen), (float)(entry.Z / sumBlue), entry.W); } - public static void Fill(Span table, int steps, float referenceWavelength, DispersionLaw law) - { - steps = ClampSteps(steps); - var reference = ClampReference(referenceWavelength); - var norm = Math.Max( - Math.Abs(Displacement(law, ShortestWavelength, reference)), - Math.Abs(Displacement(law, LongestWavelength, reference))); + table[steps..].Clear(); + } - double sumRed = 0.0, sumGreen = 0.0, sumBlue = 0.0; - for (var index = 0; index < steps; index++) - { - var wavelength = ShortestWavelength + (LongestWavelength - ShortestWavelength) * index / (steps - 1); - var weights = Weights(wavelength); - table[index] = new Vector4(weights, (float)(Displacement(law, wavelength, reference) / norm)); - sumRed += weights.X; - sumGreen += weights.Y; - sumBlue += weights.Z; - } + private static double Displacement(DispersionLaw law, double wavelength, double reference) + => law == DispersionLaw.Prism + ? 1.0 / (wavelength * wavelength) - 1.0 / (reference * reference) + : reference - wavelength; - for (var index = 0; index < steps; index++) - { - var entry = table[index]; - table[index] = new Vector4((float)(entry.X / sumRed), (float)(entry.Y / sumGreen), (float)(entry.Z / sumBlue), entry.W); - } - - table[steps..].Clear(); - } - - private static double Displacement(DispersionLaw law, double wavelength, double reference) - => law == DispersionLaw.Prism - ? 1.0 / (wavelength * wavelength) - 1.0 / (reference * reference) - : reference - wavelength; - - private static double Lobe(double wavelength, double center, double lowScale, double highScale) - { - var t = (wavelength - center) * (wavelength < center ? lowScale : highScale); - return Math.Exp(-0.5 * t * t); - } + private static double Lobe(double wavelength, double center, double lowScale, double highScale) + { + var t = (wavelength - center) * (wavelength < center ? lowScale : highScale); + return Math.Exp(-0.5 * t * t); } } diff --git a/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs b/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs index 4aaf381..08d441a 100644 --- a/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs +++ b/SpectrumColorShift/SpectrumColorShiftCustomEffect.cs @@ -6,255 +6,254 @@ using Vortice.Direct2D1; using YukkuriMovieMaker.Commons; using YukkuriMovieMaker.Player.Video; -namespace SpectrumColorShift +namespace SpectrumColorShift; + +internal sealed class SpectrumColorShiftCustomEffect(IGraphicsDevicesAndContext devices) : D2D1CustomShaderEffectBase(Create(devices)) { - internal sealed class SpectrumColorShiftCustomEffect(IGraphicsDevicesAndContext devices) : D2D1CustomShaderEffectBase(Create(devices)) + internal enum PropertyIndex { - internal enum PropertyIndex + Distance = 0, + Mode, + Angle, + CenterX, + CenterY, + Strength, + Dispersion, + ReferenceWavelength, + Steps, + } + + public float Distance { set => SetValue((int)PropertyIndex.Distance, value); } + public int Mode { set => SetValue((int)PropertyIndex.Mode, value); } + public float Angle { set => SetValue((int)PropertyIndex.Angle, value); } + public float CenterX { set => SetValue((int)PropertyIndex.CenterX, value); } + public float CenterY { set => SetValue((int)PropertyIndex.CenterY, value); } + public float Strength { set => SetValue((int)PropertyIndex.Strength, value); } + public int Dispersion { set => SetValue((int)PropertyIndex.Dispersion, value); } + public float ReferenceWavelength { set => SetValue((int)PropertyIndex.ReferenceWavelength, value); } + public int Steps { set => SetValue((int)PropertyIndex.Steps, value); } + + [CustomEffect(1)] + internal sealed class EffectImpl : D2D1CustomShaderEffectImplBase + { + private ConstantBuffer _cb; + private ShiftMode _mode = ShiftMode.Linear; + private DispersionLaw _dispersion = DispersionLaw.Grating; + private Vector2 _centerOffset; + private float _referenceWavelength = Spectrum.DefaultReferenceWavelength; + private int _steps = Spectrum.DefaultSteps; + private bool _tableStale = true; + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.Distance)] + public float Distance { - Distance = 0, - Mode, - Angle, - CenterX, - CenterY, - Strength, - Dispersion, - ReferenceWavelength, - Steps, + get => _cb.Distance; + set + { + _cb.Distance = Sanitize(value, -MaxInputPixel, MaxInputPixel); + UpdateConstants(); + } } - public float Distance { set => SetValue((int)PropertyIndex.Distance, value); } - public int Mode { set => SetValue((int)PropertyIndex.Mode, value); } - public float Angle { set => SetValue((int)PropertyIndex.Angle, value); } - public float CenterX { set => SetValue((int)PropertyIndex.CenterX, value); } - public float CenterY { set => SetValue((int)PropertyIndex.CenterY, value); } - public float Strength { set => SetValue((int)PropertyIndex.Strength, value); } - public int Dispersion { set => SetValue((int)PropertyIndex.Dispersion, value); } - public float ReferenceWavelength { set => SetValue((int)PropertyIndex.ReferenceWavelength, value); } - public int Steps { set => SetValue((int)PropertyIndex.Steps, value); } - - [CustomEffect(1)] - internal sealed class EffectImpl : D2D1CustomShaderEffectImplBase + [CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.Mode)] + public int Mode { - private ConstantBuffer _cb; - private ShiftMode _mode = ShiftMode.Linear; - private DispersionLaw _dispersion = DispersionLaw.Grating; - private Vector2 _centerOffset; - private float _referenceWavelength = Spectrum.DefaultReferenceWavelength; - private int _steps = Spectrum.DefaultSteps; - private bool _tableStale = true; - - [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.Distance)] - public float Distance + get => (int)_mode; + set { - get => _cb.Distance; - set - { - _cb.Distance = Sanitize(value, -MaxInputPixel, MaxInputPixel); - UpdateConstants(); - } + _mode = value is (int)ShiftMode.Radial or (int)ShiftMode.Rotational ? (ShiftMode)value : ShiftMode.Linear; + _cb.Mode = (int)_mode; + UpdateConstants(); } + } - [CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.Mode)] - public int Mode + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.Angle)] + public float Angle + { + get => MathF.Atan2(_cb.Direction.Y, _cb.Direction.X) * 180f / MathF.PI; + set { - get => (int)_mode; - set - { - _mode = value is (int)ShiftMode.Radial or (int)ShiftMode.Rotational ? (ShiftMode)value : ShiftMode.Linear; - _cb.Mode = (int)_mode; - UpdateConstants(); - } + var radians = float.IsFinite(value) ? value * MathF.PI / 180f : 0f; + _cb.Direction = new Vector2(MathF.Cos(radians), MathF.Sin(radians)); + UpdateConstants(); } + } - [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.Angle)] - public float Angle + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.CenterX)] + public float CenterX + { + get => _centerOffset.X; + set { - get => MathF.Atan2(_cb.Direction.Y, _cb.Direction.X) * 180f / MathF.PI; - set - { - var radians = float.IsFinite(value) ? value * MathF.PI / 180f : 0f; - _cb.Direction = new Vector2(MathF.Cos(radians), MathF.Sin(radians)); - UpdateConstants(); - } - } - - [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.CenterX)] - public float CenterX - { - get => _centerOffset.X; - set - { - _centerOffset.X = Sanitize(value, -MaxInputPixel, MaxInputPixel); - RefreshGeometry(); - UpdateConstants(); - } - } - - [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.CenterY)] - public float CenterY - { - get => _centerOffset.Y; - set - { - _centerOffset.Y = Sanitize(value, -MaxInputPixel, MaxInputPixel); - RefreshGeometry(); - UpdateConstants(); - } - } - - [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.Strength)] - public float Strength - { - get => _cb.Strength; - set - { - _cb.Strength = Sanitize(value, 0f, 1f); - UpdateConstants(); - } - } - - [CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.Dispersion)] - public int Dispersion - { - get => (int)_dispersion; - set - { - _dispersion = value == (int)DispersionLaw.Prism ? DispersionLaw.Prism : DispersionLaw.Grating; - _tableStale = true; - UpdateConstants(); - } - } - - [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.ReferenceWavelength)] - public float ReferenceWavelength - { - get => _referenceWavelength; - set - { - _referenceWavelength = Spectrum.ClampReference(value); - _tableStale = true; - UpdateConstants(); - } - } - - [CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.Steps)] - public int Steps - { - get => _steps; - set - { - _steps = Spectrum.ClampSteps(value); - _cb.Steps = _steps; - _tableStale = true; - UpdateConstants(); - } - } - - public EffectImpl() : base(ShaderResourceUri.Get("SpectrumColorShift")) - { - _cb.Direction = Vector2.UnitX; - _cb.Radius = 1f; - _cb.Strength = 1f; - _cb.Mode = (int)ShiftMode.Linear; - _cb.Steps = Spectrum.DefaultSteps; - } - - protected override void UpdateConstants() - { - if (drawInformation is null) - return; - - try - { - if (_tableStale) - { - Spectrum.Fill(_cb.Samples, _steps, _referenceWavelength, _dispersion); - _tableStale = false; - } - - drawInformation.SetPixelShaderConstantBuffer(_cb); - } - catch (Exception exception) - { - SpectrumColorShiftTelemetry.Report(exception); - throw; - } - } - - public override void MapInputRectsToOutputRect( - RawRect[] inputRects, - RawRect[] inputOpaqueSubRects, - out RawRect outputRect, - out RawRect outputOpaqueSubRect) - { - inputRect = ClampInputRect(inputRects[0]); - if (inputRect.Right <= inputRect.Left || inputRect.Bottom <= inputRect.Top) - { - outputRect = inputRect; - outputOpaqueSubRect = default; - return; - } - - _cb.InputBounds = new Vector4(inputRect.Left, inputRect.Top, inputRect.Right, inputRect.Bottom); + _centerOffset.X = Sanitize(value, -MaxInputPixel, MaxInputPixel); RefreshGeometry(); UpdateConstants(); - - outputRect = ShiftGeometry.Output(inputRect, _mode, _cb.Distance, _cb.Direction, _cb.Center, _cb.Radius); - outputOpaqueSubRect = default; } + } - public override void MapOutputRectToInputRects(RawRect outputRect, RawRect[] inputRects) + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.CenterY)] + public float CenterY + { + get => _centerOffset.Y; + set { - if (inputRects.Length == 0) - return; + _centerOffset.Y = Sanitize(value, -MaxInputPixel, MaxInputPixel); + RefreshGeometry(); + UpdateConstants(); + } + } - if (inputRect.Right <= inputRect.Left || inputRect.Bottom <= inputRect.Top) + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.Strength)] + public float Strength + { + get => _cb.Strength; + set + { + _cb.Strength = Sanitize(value, 0f, 1f); + UpdateConstants(); + } + } + + [CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.Dispersion)] + public int Dispersion + { + get => (int)_dispersion; + set + { + _dispersion = value == (int)DispersionLaw.Prism ? DispersionLaw.Prism : DispersionLaw.Grating; + _tableStale = true; + UpdateConstants(); + } + } + + [CustomEffectProperty(PropertyType.Float, (int)PropertyIndex.ReferenceWavelength)] + public float ReferenceWavelength + { + get => _referenceWavelength; + set + { + _referenceWavelength = Spectrum.ClampReference(value); + _tableStale = true; + UpdateConstants(); + } + } + + [CustomEffectProperty(PropertyType.Int32, (int)PropertyIndex.Steps)] + public int Steps + { + get => _steps; + set + { + _steps = Spectrum.ClampSteps(value); + _cb.Steps = _steps; + _tableStale = true; + UpdateConstants(); + } + } + + public EffectImpl() : base(ShaderResourceUri.Get("SpectrumColorShift")) + { + _cb.Direction = Vector2.UnitX; + _cb.Radius = 1f; + _cb.Strength = 1f; + _cb.Mode = (int)ShiftMode.Linear; + _cb.Steps = Spectrum.DefaultSteps; + } + + protected override void UpdateConstants() + { + if (drawInformation is null) + return; + + try + { + if (_tableStale) { - inputRects[0] = inputRect; - return; + Spectrum.Fill(_cb.Samples, _steps, _referenceWavelength, _dispersion); + _tableStale = false; } - inputRects[0] = ShiftGeometry.Input(outputRect, _mode, _cb.Distance, _cb.Direction, _cb.Center, _cb.Radius); + drawInformation.SetPixelShaderConstantBuffer(_cb); } - - public override RawRect MapInvalidRect(int inputIndex, RawRect invalidInputRect) - => ShiftGeometry.Output(invalidInputRect, _mode, _cb.Distance, _cb.Direction, _cb.Center, _cb.Radius); - - private static float Sanitize(float value, float minimum, float maximum) - => float.IsNaN(value) ? 0f : Math.Clamp(value, minimum, maximum); - - private void RefreshGeometry() + catch (Exception exception) { - if (inputRect.Right <= inputRect.Left || inputRect.Bottom <= inputRect.Top) - return; - - _cb.Center = ShiftGeometry.Center(inputRect, _centerOffset); - _cb.Radius = ShiftGeometry.Radius(inputRect, _cb.Center); + SpectrumColorShiftTelemetry.Report(exception); + throw; } + } - [InlineArray(Spectrum.MaxSteps)] - private struct SampleTable + public override void MapInputRectsToOutputRect( + RawRect[] inputRects, + RawRect[] inputOpaqueSubRects, + out RawRect outputRect, + out RawRect outputOpaqueSubRect) + { + inputRect = ClampInputRect(inputRects[0]); + if (inputRect.Right <= inputRect.Left || inputRect.Bottom <= inputRect.Top) { - private Vector4 _element; + outputRect = inputRect; + outputOpaqueSubRect = default; + return; } - [StructLayout(LayoutKind.Sequential)] - private struct ConstantBuffer + _cb.InputBounds = new Vector4(inputRect.Left, inputRect.Top, inputRect.Right, inputRect.Bottom); + RefreshGeometry(); + UpdateConstants(); + + outputRect = ShiftGeometry.Output(inputRect, _mode, _cb.Distance, _cb.Direction, _cb.Center, _cb.Radius); + outputOpaqueSubRect = default; + } + + public override void MapOutputRectToInputRects(RawRect outputRect, RawRect[] inputRects) + { + if (inputRects.Length == 0) + return; + + if (inputRect.Right <= inputRect.Left || inputRect.Bottom <= inputRect.Top) { - public Vector4 InputBounds; - public Vector2 Direction; - public Vector2 Center; - public float Distance; - public float Radius; - public float Strength; - public int Mode; - public int Steps; - public float Pad0; - public float Pad1; - public float Pad2; - public SampleTable Samples; + inputRects[0] = inputRect; + return; } + + inputRects[0] = ShiftGeometry.Input(outputRect, _mode, _cb.Distance, _cb.Direction, _cb.Center, _cb.Radius); + } + + public override RawRect MapInvalidRect(int inputIndex, RawRect invalidInputRect) + => ShiftGeometry.Output(invalidInputRect, _mode, _cb.Distance, _cb.Direction, _cb.Center, _cb.Radius); + + private static float Sanitize(float value, float minimum, float maximum) + => float.IsNaN(value) ? 0f : Math.Clamp(value, minimum, maximum); + + private void RefreshGeometry() + { + if (inputRect.Right <= inputRect.Left || inputRect.Bottom <= inputRect.Top) + return; + + _cb.Center = ShiftGeometry.Center(inputRect, _centerOffset); + _cb.Radius = ShiftGeometry.Radius(inputRect, _cb.Center); + } + + [InlineArray(Spectrum.MaxSteps)] + private struct SampleTable + { + private Vector4 _element; + } + + [StructLayout(LayoutKind.Sequential)] + private struct ConstantBuffer + { + public Vector4 InputBounds; + public Vector2 Direction; + public Vector2 Center; + public float Distance; + public float Radius; + public float Strength; + public int Mode; + public int Steps; + public float Pad0; + public float Pad1; + public float Pad2; + public SampleTable Samples; } } } diff --git a/SpectrumColorShift/SpectrumColorShiftEffect.cs b/SpectrumColorShift/SpectrumColorShiftEffect.cs index 5a3caaa..04beb18 100644 --- a/SpectrumColorShift/SpectrumColorShiftEffect.cs +++ b/SpectrumColorShift/SpectrumColorShiftEffect.cs @@ -8,96 +8,95 @@ using YukkuriMovieMaker.Player.Video; using YukkuriMovieMaker.Plugin; using YukkuriMovieMaker.Plugin.Effects; -namespace SpectrumColorShift +namespace SpectrumColorShift; + +[PluginDetails(AuthorName = "routersys")] +[VideoEffect(nameof(Texts.SpectrumColorShift), [VideoEffectCategories.Filtering], [nameof(Texts.TagColorShift), nameof(Texts.TagChromaticAberration), nameof(Texts.TagSpectrum), nameof(Texts.TagDispersion), nameof(Texts.TagPrism), nameof(Texts.TagRgbSplit)], IsAviUtlSupported = false, ResourceType = typeof(Texts))] +public sealed class SpectrumColorShiftEffect : VideoEffectBase { - [PluginDetails(AuthorName = "routersys")] - [VideoEffect(nameof(Texts.SpectrumColorShift), [VideoEffectCategories.Filtering], [nameof(Texts.TagColorShift), nameof(Texts.TagChromaticAberration), nameof(Texts.TagSpectrum), nameof(Texts.TagDispersion), nameof(Texts.TagPrism), nameof(Texts.TagRgbSplit)], IsAviUtlSupported = false, ResourceType = typeof(Texts))] - public sealed class SpectrumColorShiftEffect : VideoEffectBase + public SpectrumColorShiftEffect() { - public SpectrumColorShiftEffect() - { - SpectrumColorShiftTelemetry.EnsureStartedOnce(); - SpectrumColorShiftUpdateNotifier.EnsureCheckedOnce(); - } - - public override string Label => Texts.SpectrumColorShift; - - [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.Distance), Description = nameof(Texts.DistanceDescription), Order = 0, ResourceType = typeof(Texts))] - [AnimationSlider("F1", "px", -100, 100)] - public Animation Distance { get; } = new Animation(8, -4096, 4096); - - [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.Mode), Description = nameof(Texts.ModeDescription), Order = 1, ResourceType = typeof(Texts))] - [EnumComboBox] - public ShiftMode Mode - { - get => _mode; - set => Set(ref _mode, value); - } - private ShiftMode _mode = ShiftMode.Linear; - - [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.Angle), Description = nameof(Texts.AngleDescription), Order = 2, ResourceType = typeof(Texts))] - [AnimationSlider("F1", "°", -360, 360)] - [ShowPropertyEditorWhen(nameof(Mode), ShiftMode.Linear)] - public Animation Angle { get; } = new Animation(0, YMM4Constants.VerySmallValue, YMM4Constants.VeryLargeValue); - - [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.CenterX), Description = nameof(Texts.CenterXDescription), Order = 3, ResourceType = typeof(Texts))] - [AnimationSlider("F1", "px", -500, 500)] - [ShowPropertyEditorWhen(nameof(Mode), ShiftMode.Radial | ShiftMode.Rotational)] - public Animation CenterX { get; } = new Animation(0, -4096, 4096); - - [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.CenterY), Description = nameof(Texts.CenterYDescription), Order = 4, ResourceType = typeof(Texts))] - [AnimationSlider("F1", "px", -500, 500)] - [ShowPropertyEditorWhen(nameof(Mode), ShiftMode.Radial | ShiftMode.Rotational)] - public Animation CenterY { get; } = new Animation(0, -4096, 4096); - - [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.Strength), Description = nameof(Texts.StrengthDescription), Order = 5, ResourceType = typeof(Texts))] - [AnimationSlider("F1", "%", 0, 100)] - public Animation Strength { get; } = new Animation(100, 0, 100); - - [Display(GroupName = nameof(Texts.SpectrumGroup), Name = nameof(Texts.Dispersion), Description = nameof(Texts.DispersionDescription), Order = 10, ResourceType = typeof(Texts))] - [EnumComboBox] - public DispersionLaw Dispersion - { - get => _dispersion; - set => Set(ref _dispersion, value); - } - private DispersionLaw _dispersion = DispersionLaw.Grating; - - [Display(GroupName = nameof(Texts.SpectrumGroup), Name = nameof(Texts.ReferenceWavelength), Description = nameof(Texts.ReferenceWavelengthDescription), Order = 11, ResourceType = typeof(Texts))] - [AnimationSlider("F0", "nm", Spectrum.ShortestWavelength, Spectrum.LongestWavelength)] - public Animation ReferenceWavelength { get; } = new Animation(Spectrum.DefaultReferenceWavelength, Spectrum.ShortestWavelength, Spectrum.LongestWavelength); - - [Display(GroupName = nameof(Texts.SpectrumGroup), Name = nameof(Texts.Steps), Description = nameof(Texts.StepsDescription), Order = 12, ResourceType = typeof(Texts))] - [Range(Spectrum.MinSteps, Spectrum.MaxSteps)] - [DefaultValue(Spectrum.DefaultSteps)] - [TextBoxSlider("F0", "", Spectrum.MinSteps, Spectrum.MaxSteps)] - public int Steps - { - get => _steps; - set => Set(ref _steps, Spectrum.ClampSteps(value)); - } - private int _steps = Spectrum.DefaultSteps; - - private IAnimatable[]? _animatables; - - public override IEnumerable CreateExoVideoFilters( - int keyFrameIndex, - ExoOutputDescription exoOutputDescription) => []; - - public override IVideoEffectProcessor CreateVideoEffect(IGraphicsDevicesAndContext devices) - { - try - { - return new SpectrumColorShiftEffectProcessor(devices, this); - } - catch (Exception exception) - { - SpectrumColorShiftTelemetry.Report(exception); - throw; - } - } - - protected override IEnumerable GetAnimatables() - => _animatables ??= [Distance, Angle, CenterX, CenterY, Strength, ReferenceWavelength]; + SpectrumColorShiftTelemetry.EnsureStartedOnce(); + SpectrumColorShiftUpdateNotifier.EnsureCheckedOnce(); } + + public override string Label => Texts.SpectrumColorShift; + + [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.Distance), Description = nameof(Texts.DistanceDescription), Order = 0, ResourceType = typeof(Texts))] + [AnimationSlider("F1", "px", -100, 100)] + public Animation Distance { get; } = new Animation(8, -4096, 4096); + + [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.Mode), Description = nameof(Texts.ModeDescription), Order = 1, ResourceType = typeof(Texts))] + [EnumComboBox] + public ShiftMode Mode + { + get => _mode; + set => Set(ref _mode, value); + } + private ShiftMode _mode = ShiftMode.Linear; + + [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.Angle), Description = nameof(Texts.AngleDescription), Order = 2, ResourceType = typeof(Texts))] + [AnimationSlider("F1", "°", -360, 360)] + [ShowPropertyEditorWhen(nameof(Mode), ShiftMode.Linear)] + public Animation Angle { get; } = new Animation(0, YMM4Constants.VerySmallValue, YMM4Constants.VeryLargeValue); + + [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.CenterX), Description = nameof(Texts.CenterXDescription), Order = 3, ResourceType = typeof(Texts))] + [AnimationSlider("F1", "px", -500, 500)] + [ShowPropertyEditorWhen(nameof(Mode), ShiftMode.Radial | ShiftMode.Rotational)] + public Animation CenterX { get; } = new Animation(0, -4096, 4096); + + [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.CenterY), Description = nameof(Texts.CenterYDescription), Order = 4, ResourceType = typeof(Texts))] + [AnimationSlider("F1", "px", -500, 500)] + [ShowPropertyEditorWhen(nameof(Mode), ShiftMode.Radial | ShiftMode.Rotational)] + public Animation CenterY { get; } = new Animation(0, -4096, 4096); + + [Display(GroupName = nameof(Texts.ShiftGroup), Name = nameof(Texts.Strength), Description = nameof(Texts.StrengthDescription), Order = 5, ResourceType = typeof(Texts))] + [AnimationSlider("F1", "%", 0, 100)] + public Animation Strength { get; } = new Animation(100, 0, 100); + + [Display(GroupName = nameof(Texts.SpectrumGroup), Name = nameof(Texts.Dispersion), Description = nameof(Texts.DispersionDescription), Order = 10, ResourceType = typeof(Texts))] + [EnumComboBox] + public DispersionLaw Dispersion + { + get => _dispersion; + set => Set(ref _dispersion, value); + } + private DispersionLaw _dispersion = DispersionLaw.Grating; + + [Display(GroupName = nameof(Texts.SpectrumGroup), Name = nameof(Texts.ReferenceWavelength), Description = nameof(Texts.ReferenceWavelengthDescription), Order = 11, ResourceType = typeof(Texts))] + [AnimationSlider("F0", "nm", Spectrum.ShortestWavelength, Spectrum.LongestWavelength)] + public Animation ReferenceWavelength { get; } = new Animation(Spectrum.DefaultReferenceWavelength, Spectrum.ShortestWavelength, Spectrum.LongestWavelength); + + [Display(GroupName = nameof(Texts.SpectrumGroup), Name = nameof(Texts.Steps), Description = nameof(Texts.StepsDescription), Order = 12, ResourceType = typeof(Texts))] + [Range(Spectrum.MinSteps, Spectrum.MaxSteps)] + [DefaultValue(Spectrum.DefaultSteps)] + [TextBoxSlider("F0", "", Spectrum.MinSteps, Spectrum.MaxSteps)] + public int Steps + { + get => _steps; + set => Set(ref _steps, Spectrum.ClampSteps(value)); + } + private int _steps = Spectrum.DefaultSteps; + + private IAnimatable[]? _animatables; + + public override IEnumerable CreateExoVideoFilters( + int keyFrameIndex, + ExoOutputDescription exoOutputDescription) => []; + + public override IVideoEffectProcessor CreateVideoEffect(IGraphicsDevicesAndContext devices) + { + try + { + return new SpectrumColorShiftEffectProcessor(devices, this); + } + catch (Exception exception) + { + SpectrumColorShiftTelemetry.Report(exception); + throw; + } + } + + protected override IEnumerable GetAnimatables() + => _animatables ??= [Distance, Angle, CenterX, CenterY, Strength, ReferenceWavelength]; } diff --git a/SpectrumColorShift/SpectrumColorShiftEffectProcessor.cs b/SpectrumColorShift/SpectrumColorShiftEffectProcessor.cs index 8e73469..b8d7817 100644 --- a/SpectrumColorShift/SpectrumColorShiftEffectProcessor.cs +++ b/SpectrumColorShift/SpectrumColorShiftEffectProcessor.cs @@ -3,125 +3,124 @@ using YukkuriMovieMaker.Commons; using YukkuriMovieMaker.Player.Video; using YukkuriMovieMaker.Player.Video.Effects; -namespace SpectrumColorShift +namespace SpectrumColorShift; + +internal sealed class SpectrumColorShiftEffectProcessor( + IGraphicsDevicesAndContext devices, + SpectrumColorShiftEffect item) : VideoEffectProcessorBase(devices) { - internal sealed class SpectrumColorShiftEffectProcessor( - IGraphicsDevicesAndContext devices, - SpectrumColorShiftEffect item) : VideoEffectProcessorBase(devices) + private readonly SpectrumColorShiftEffect _item = item; + private SpectrumColorShiftCustomEffect? _effect; + + private bool _isFirst = true; + private Parameters _parameters; + + public override DrawDescription Update(EffectDescription effectDescription) { - private readonly SpectrumColorShiftEffect _item = item; - private SpectrumColorShiftCustomEffect? _effect; - - private bool _isFirst = true; - private Parameters _parameters; - - public override DrawDescription Update(EffectDescription effectDescription) + try { - try - { - return UpdateCore(effectDescription); - } - catch (Exception exception) - { - SpectrumColorShiftTelemetry.Report(exception); - throw; - } + return UpdateCore(effectDescription); } - - private DrawDescription UpdateCore(EffectDescription effectDescription) + catch (Exception exception) { - if (IsPassThroughEffect || _effect is null) - return effectDescription.DrawDescription; - - var frame = effectDescription.ItemPosition.Frame; - var length = effectDescription.ItemDuration.Frame; - var fps = effectDescription.FPS; - - var parameters = new Parameters( - (float)_item.Distance.GetValue(frame, length, fps), - (int)_item.Mode, - (float)_item.Angle.GetValue(frame, length, fps), - (float)_item.CenterX.GetValue(frame, length, fps), - (float)_item.CenterY.GetValue(frame, length, fps), - (float)(_item.Strength.GetValue(frame, length, fps) / 100.0), - (int)_item.Dispersion, - (float)_item.ReferenceWavelength.GetValue(frame, length, fps), - _item.Steps); - - if (_isFirst || _parameters.Distance != parameters.Distance) - _effect.Distance = parameters.Distance; - if (_isFirst || _parameters.Mode != parameters.Mode) - _effect.Mode = parameters.Mode; - if (_isFirst || _parameters.Angle != parameters.Angle) - _effect.Angle = parameters.Angle; - if (_isFirst || _parameters.CenterX != parameters.CenterX) - _effect.CenterX = parameters.CenterX; - if (_isFirst || _parameters.CenterY != parameters.CenterY) - _effect.CenterY = parameters.CenterY; - if (_isFirst || _parameters.Strength != parameters.Strength) - _effect.Strength = parameters.Strength; - if (_isFirst || _parameters.Dispersion != parameters.Dispersion) - _effect.Dispersion = parameters.Dispersion; - if (_isFirst || _parameters.ReferenceWavelength != parameters.ReferenceWavelength) - _effect.ReferenceWavelength = parameters.ReferenceWavelength; - if (_isFirst || _parameters.Steps != parameters.Steps) - _effect.Steps = parameters.Steps; - - _parameters = parameters; - _isFirst = false; - - return effectDescription.DrawDescription; + SpectrumColorShiftTelemetry.Report(exception); + throw; } - - protected override ID2D1Image? CreateEffect(IGraphicsDevicesAndContext devices) - { - _effect = new SpectrumColorShiftCustomEffect(devices); - if (!_effect.IsEnabled) - { - _effect.Dispose(); - _effect = null; - return null; - } - disposer.Collect(_effect); - - var output = _effect.Output; - disposer.Collect(output); - return output; - } - - protected override void setInput(ID2D1Image? input) - { - _effect?.SetInput(0, input, true); - } - - protected override void ClearEffectChain() - { - try - { - ClearEffectChainCore(); - } - catch (Exception exception) - { - SpectrumColorShiftTelemetry.Report(exception); - throw; - } - } - - private void ClearEffectChainCore() - { - _effect?.SetInput(0, null, true); - _isFirst = true; - } - - private readonly record struct Parameters( - float Distance, - int Mode, - float Angle, - float CenterX, - float CenterY, - float Strength, - int Dispersion, - float ReferenceWavelength, - int Steps); } + + private DrawDescription UpdateCore(EffectDescription effectDescription) + { + if (IsPassThroughEffect || _effect is null) + return effectDescription.DrawDescription; + + var frame = effectDescription.ItemPosition.Frame; + var length = effectDescription.ItemDuration.Frame; + var fps = effectDescription.FPS; + + var parameters = new Parameters( + (float)_item.Distance.GetValue(frame, length, fps), + (int)_item.Mode, + (float)_item.Angle.GetValue(frame, length, fps), + (float)_item.CenterX.GetValue(frame, length, fps), + (float)_item.CenterY.GetValue(frame, length, fps), + (float)(_item.Strength.GetValue(frame, length, fps) / 100.0), + (int)_item.Dispersion, + (float)_item.ReferenceWavelength.GetValue(frame, length, fps), + _item.Steps); + + if (_isFirst || _parameters.Distance != parameters.Distance) + _effect.Distance = parameters.Distance; + if (_isFirst || _parameters.Mode != parameters.Mode) + _effect.Mode = parameters.Mode; + if (_isFirst || _parameters.Angle != parameters.Angle) + _effect.Angle = parameters.Angle; + if (_isFirst || _parameters.CenterX != parameters.CenterX) + _effect.CenterX = parameters.CenterX; + if (_isFirst || _parameters.CenterY != parameters.CenterY) + _effect.CenterY = parameters.CenterY; + if (_isFirst || _parameters.Strength != parameters.Strength) + _effect.Strength = parameters.Strength; + if (_isFirst || _parameters.Dispersion != parameters.Dispersion) + _effect.Dispersion = parameters.Dispersion; + if (_isFirst || _parameters.ReferenceWavelength != parameters.ReferenceWavelength) + _effect.ReferenceWavelength = parameters.ReferenceWavelength; + if (_isFirst || _parameters.Steps != parameters.Steps) + _effect.Steps = parameters.Steps; + + _parameters = parameters; + _isFirst = false; + + return effectDescription.DrawDescription; + } + + protected override ID2D1Image? CreateEffect(IGraphicsDevicesAndContext devices) + { + _effect = new SpectrumColorShiftCustomEffect(devices); + if (!_effect.IsEnabled) + { + _effect.Dispose(); + _effect = null; + return null; + } + disposer.Collect(_effect); + + var output = _effect.Output; + disposer.Collect(output); + return output; + } + + protected override void setInput(ID2D1Image? input) + { + _effect?.SetInput(0, input, true); + } + + protected override void ClearEffectChain() + { + try + { + ClearEffectChainCore(); + } + catch (Exception exception) + { + SpectrumColorShiftTelemetry.Report(exception); + throw; + } + } + + private void ClearEffectChainCore() + { + _effect?.SetInput(0, null, true); + _isFirst = true; + } + + private readonly record struct Parameters( + float Distance, + int Mode, + float Angle, + float CenterX, + float CenterY, + float Strength, + int Dispersion, + float ReferenceWavelength, + int Steps); }