-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from sirdoombox/main
Effect rendering improvements.
- Loading branch information
Showing
20 changed files
with
845 additions
and
584 deletions.
There are no files selected for viewing
This file contains 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,69 @@ | ||
const float pi = 3.14159265358979323846; | ||
|
||
float smoothstep(float a, float b, float x) { | ||
float t = clamp((x - a) / (b - a), 0.0, 1.0); | ||
return t * t * (3.0 - 2.0 * t); | ||
} | ||
|
||
float atan(float y, float x) { | ||
// Constants for the series expansion | ||
const float pi_2 = pi / 2.0; | ||
|
||
// Handle special cases | ||
if (x == 0.0) { | ||
if (y > 0.0) return pi_2; | ||
if (y < 0.0) return -pi_2; | ||
return 0.0; // Undefined, but return 0 | ||
} | ||
|
||
float abs_y = abs(y) + 1e-10; // Avoid division by zero | ||
|
||
// Compute the arctangent of y/x | ||
float angle; | ||
if (abs(x) > abs_y) { | ||
float z = y / x; | ||
float zz = z * z; | ||
angle = z * (0.999866 + zz * (-0.3302995 + zz * (0.180141 + zz * (-0.085133 + zz * 0.020835)))); | ||
if (x < 0.0) { | ||
if (y < 0.0) { | ||
angle -= pi; | ||
} else { | ||
angle += pi; | ||
} | ||
} | ||
} else { | ||
float z = x / y; | ||
float zz = z * z; | ||
angle = pi_2 - z * (0.999866 + zz * (-0.3302995 + zz * (0.180141 + zz * (-0.085133 + zz * 0.020835)))); | ||
if (y < 0.0) { | ||
angle -= pi; | ||
} | ||
} | ||
|
||
return angle; | ||
} | ||
|
||
vec4 main(vec2 fragCoord) { | ||
float radius = 0.3; | ||
float lineWidth = 2.0; // in pixels | ||
float glowSize = 3.0; // in pixels | ||
|
||
float pixelSize = 1.0 / min(iResolution.x, iResolution.y); | ||
lineWidth *= pixelSize; | ||
glowSize *= pixelSize; | ||
glowSize *= 2.0; | ||
|
||
vec2 uv = (fragCoord.xy / iResolution.xy) - 0.5; | ||
uv.x *= iResolution.x / iResolution.y; | ||
|
||
float len = length(uv); | ||
float angle = atan(uv.y, uv.x); | ||
|
||
float fallOff = fract(-0.5 * (angle / pi) - iTime * 0.5); | ||
|
||
lineWidth = (lineWidth - pixelSize) * 0.5 * fallOff; | ||
float color = smoothstep(pixelSize, 0.0, abs(radius - len) - lineWidth) * fallOff; | ||
color += smoothstep(glowSize * fallOff, 0.0, abs(radius - len) - lineWidth) * fallOff * 0.5; | ||
|
||
return vec4(color) * vec4(iAccent, iAlpha); | ||
} |
This file contains 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,28 @@ | ||
float smoothstep(float a, float b, float x) { | ||
float t = clamp((x - a) / (b - a), 0.0, 1.0); | ||
return t * t * (3.0 - 2.0 * t); | ||
} | ||
|
||
float A(vec2 p, float a) { | ||
a *= 3.14159;\ | ||
vec2 s = vec2(sin(a), cos(a)); | ||
p.x = abs(p.x); | ||
return ((s.y * p.x > s.x * p.y) ? length(p - s * .7) : | ||
abs(length(p) - .7)) - .13; | ||
} | ||
|
||
mat2 D(float a) { | ||
a *= 3.14159;\ | ||
vec2 s = vec2(sin(a), cos(a)); | ||
return mat2(s.y, -s.x, s.x, s.y); | ||
} | ||
|
||
vec4 main(vec2 fragCoord) { | ||
vec2 r = iResolution.xy, p = (2. * fragCoord - r) / r.y; | ||
float T = iTime * 1., | ||
d = A(p * D(1. - .125 * floor(T)), .4375), // distance to longest arc | ||
i; | ||
for (i = 0.; i < 1.; i += .5) | ||
d = min(A(p * D(mix(-.5, .625, fract(T / 2. + i)) - .125 * T), .0625), d); // distance to shorter arcs ("pellets") | ||
return vec4(smoothstep(.01, .0, d)) * vec4(iAccent, iAlpha); | ||
} |
This file contains 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,74 @@ | ||
using System.Collections.Generic; | ||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Media; | ||
using SkiaSharp; | ||
using SukiUI.Utilities.Effects; | ||
|
||
namespace SukiUI.Demo.Controls | ||
{ | ||
public class LoadingTest : Control | ||
{ | ||
public static readonly StyledProperty<LoadingStyle> LoadingStyleProperty = | ||
AvaloniaProperty.Register<LoadingTest, LoadingStyle>(nameof(LoadingStyle)); | ||
|
||
public LoadingStyle LoadingStyle | ||
{ | ||
get => GetValue(LoadingStyleProperty); | ||
set => SetValue(LoadingStyleProperty, value); | ||
} | ||
|
||
private static readonly IReadOnlyDictionary<LoadingStyle, SukiEffect> Effects = | ||
new Dictionary<LoadingStyle, SukiEffect>() | ||
{ | ||
{ LoadingStyle.Glow, SukiEffect.FromEmbeddedResource("glow") }, | ||
{ LoadingStyle.Pellets, SukiEffect.FromEmbeddedResource("pellets") } | ||
}; | ||
|
||
private readonly LoadingEffectDraw _draw; | ||
|
||
public LoadingTest() | ||
{ | ||
Width = 50; | ||
Height = 50; | ||
_draw = new LoadingEffectDraw(Bounds); | ||
} | ||
|
||
public override void Render(DrawingContext context) | ||
{ | ||
_draw.Bounds = Bounds; | ||
_draw.Effect = Effects[LoadingStyle]; | ||
context.Custom(_draw); | ||
} | ||
|
||
public class LoadingEffectDraw : EffectDrawBase | ||
{ | ||
public LoadingEffectDraw(Rect bounds) : base(bounds) | ||
{ | ||
AnimationEnabled = true; | ||
AnimationSpeedScale = 2f; | ||
} | ||
|
||
protected override void Render(SKCanvas canvas, SKRect rect) | ||
{ | ||
canvas.Scale(1,-1); | ||
canvas.Translate(0, (float)-Bounds.Height); | ||
using var mainShaderPaint = new SKPaint(); | ||
|
||
if (Effect is not null) | ||
{ | ||
using var shader = EffectWithUniforms(); | ||
mainShaderPaint.Shader = shader; | ||
canvas.DrawRect(rect, mainShaderPaint); | ||
} | ||
canvas.Restore(); | ||
} | ||
} | ||
} | ||
|
||
public enum LoadingStyle | ||
{ | ||
Glow, | ||
Pellets | ||
} | ||
} |
This file contains 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 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 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 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 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 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.