-
-
Notifications
You must be signed in to change notification settings - Fork 51
Implement the PathFollowEffect #1427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/Beutl.Engine/Graphics/FilterEffects/PathFollowEffect.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| using System.ComponentModel.DataAnnotations; | ||
| using Beutl.Engine; | ||
| using Beutl.Language; | ||
| using Beutl.Media; | ||
| using SkiaSharp; | ||
|
|
||
| namespace Beutl.Graphics.Effects; | ||
|
|
||
| public sealed partial class PathFollowEffect : FilterEffect | ||
| { | ||
| public PathFollowEffect() | ||
| { | ||
| ScanProperties<PathFollowEffect>(); | ||
| Geometry.CurrentValue = new PathGeometry(); | ||
| } | ||
|
|
||
| [Display(Name = nameof(Strings.Geometry), ResourceType = typeof(Strings))] | ||
| public IProperty<Geometry?> Geometry { get; } = Property.Create<Geometry?>(); | ||
|
|
||
| [Display(Name = nameof(Strings.Progress), ResourceType = typeof(Strings))] | ||
| public IProperty<float> Progress { get; } = Property.CreateAnimatable(0f); | ||
|
|
||
| [Display(Name = nameof(Strings.FollowRotation), ResourceType = typeof(Strings))] | ||
| public IProperty<bool> FollowRotation { get; } = Property.CreateAnimatable(false); | ||
|
|
||
| public override void ApplyTo(FilterEffectContext context, FilterEffect.Resource resource) | ||
| { | ||
| var r = (Resource)resource; | ||
| if (r.Geometry == null) | ||
| return; | ||
|
|
||
| SKPath skPath = r.Geometry.GetCachedPath(); | ||
| if (skPath.IsEmpty) | ||
| return; | ||
|
|
||
| float progress = Math.Clamp(r.Progress, 0f, 100f) / 100f; | ||
|
|
||
| using var pathMeasure = new SKPathMeasure(skPath); | ||
| float totalLength = pathMeasure.Length; | ||
| if (totalLength <= 0) | ||
| return; | ||
|
|
||
| float distance = totalLength * progress; | ||
|
|
||
| if (!pathMeasure.GetPositionAndTangent(distance, out SKPoint position, out SKPoint tangent)) | ||
| return; | ||
|
|
||
| if (!pathMeasure.GetPosition(0, out SKPoint startPosition)) | ||
| return; | ||
|
|
||
| float offsetX = position.X - startPosition.X; | ||
| float offsetY = position.Y - startPosition.Y; | ||
|
|
||
| float rotationAngle = 0f; | ||
| if (r.FollowRotation) | ||
| { | ||
| rotationAngle = MathF.Atan2(tangent.Y, tangent.X); | ||
| } | ||
|
|
||
| context.CustomEffect((offsetX, offsetY, rotationAngle), static (data, effectContext) => | ||
| { | ||
| effectContext.ForEach((_, target) => | ||
| { | ||
|
|
||
| var translate = Matrix.CreateTranslation(data.offsetX, data.offsetY); | ||
| Matrix m1, m2; | ||
| if (data.rotationAngle != 0) | ||
| { | ||
| var center = new Vector(target.Bounds.Width / 2, target.Bounds.Height / 2); | ||
yuto-trd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var rotate = Matrix.CreateRotation(data.rotationAngle); | ||
|
|
||
| var offset1 = Matrix.CreateTranslation(center + target.Bounds.Position); | ||
| var offset2 = Matrix.CreateTranslation(center); | ||
| m1 = -offset1 * rotate * offset1 * translate; | ||
| m2 = -offset2 * rotate * offset2 * translate; | ||
| } | ||
| else | ||
| { | ||
| m1 = m2 = translate; | ||
| } | ||
|
|
||
| var newBounds = target.Bounds.TransformToAABB(m1); | ||
| var newTarget = effectContext.CreateTarget(newBounds); | ||
| using (var canvas = effectContext.Open(newTarget)) | ||
| using (canvas.PushTransform(Matrix.CreateTranslation(target.Bounds.Position - newTarget.Bounds.Position))) | ||
| using (canvas.PushTransform(m2)) | ||
| { | ||
| target.Draw(canvas); | ||
| } | ||
|
|
||
| target.Dispose(); | ||
| return newTarget; | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.