-
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 #223 from sirdoombox/main
Complete `SukiBackground` rewrite.
- Loading branch information
Showing
21 changed files
with
904 additions
and
672 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
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 |
---|---|---|
@@ -1,43 +1,52 @@ | ||
using Avalonia.Collections; | ||
using System; | ||
using Avalonia.Collections; | ||
using Avalonia.Styling; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using Material.Icons; | ||
using SukiUI.Enums; | ||
using SukiUI.Models; | ||
|
||
namespace SukiUI.Demo.Features.Theming; | ||
|
||
public partial class ThemingViewModel : DemoPageBase | ||
{ | ||
public Action<SukiBackgroundStyle> BackgroundStyleChanged { get; set; } | ||
public Action<bool> BackgroundAnimationsChanged { get; set; } | ||
|
||
public IAvaloniaReadOnlyList<SukiColorTheme> AvailableColors { get; } | ||
public IAvaloniaReadOnlyList<SukiBackgroundStyle> AvailableBackgroundStyles { get; } | ||
|
||
private readonly SukiTheme _theme = SukiTheme.GetInstance(); | ||
|
||
[ObservableProperty] private bool _isBackgroundAnimated; | ||
[ObservableProperty] private bool _isLightTheme; | ||
[ObservableProperty] private SukiBackgroundStyle _backgroundStyle; | ||
[ObservableProperty] private bool _backgroundAnimations; | ||
|
||
public ThemingViewModel() : base("Theming", MaterialIconKind.PaletteOutline, -200) | ||
{ | ||
AvailableBackgroundStyles = new AvaloniaList<SukiBackgroundStyle>(Enum.GetValues<SukiBackgroundStyle>()); | ||
AvailableColors = _theme.ColorThemes; | ||
IsLightTheme = _theme.ActiveBaseTheme == ThemeVariant.Light; | ||
IsBackgroundAnimated = _theme.IsBackgroundAnimated; | ||
_theme.OnBaseThemeChanged += variant => | ||
IsLightTheme = variant == ThemeVariant.Light; | ||
_theme.OnColorThemeChanged += theme => | ||
{ | ||
// TODO: Implement a way to make the correct, might need to wrap the thing in a VM, this isn't ideal. | ||
}; | ||
_theme.OnBackgroundAnimationChanged += value => | ||
IsBackgroundAnimated = value; | ||
} | ||
|
||
partial void OnIsLightThemeChanged(bool value) => | ||
_theme.ChangeBaseTheme(value ? ThemeVariant.Light : ThemeVariant.Dark); | ||
|
||
partial void OnIsBackgroundAnimatedChanged(bool value) => | ||
_theme.SetBackgroundAnimationsEnabled(value); | ||
|
||
[RelayCommand] | ||
public void SwitchToColorTheme(SukiColorTheme colorTheme) => | ||
_theme.ChangeColorTheme(colorTheme); | ||
|
||
partial void OnBackgroundStyleChanged(SukiBackgroundStyle value) => | ||
BackgroundStyleChanged?.Invoke(value); | ||
|
||
partial void OnBackgroundAnimationsChanged(bool value) => | ||
BackgroundAnimationsChanged?.Invoke(value); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
vec3 blendOverlay(vec3 base, vec3 blend) { | ||
return vec3( | ||
base.r < 0.5 ? (2.0 * base.r * blend.r) : (1.0 - 2.0 * (1.0 - base.r) * (1.0 - blend.r)), | ||
base.g < 0.5 ? (2.0 * base.g * blend.g) : (1.0 - 2.0 * (1.0 - base.g) * (1.0 - blend.g)), | ||
base.b < 0.5 ? (2.0 * base.b * blend.b) : (1.0 - 2.0 * (1.0 - base.b) * (1.0 - blend.b)) | ||
); | ||
} | ||
|
||
vec2 ran(vec2 uv) { | ||
uv *= vec2(dot(uv, vec2(127.1, 311.7)), dot(uv, vec2(227.1, 521.7))); | ||
return 1.0 - fract(tan(cos(uv) * 123.6) * 3533.3) * fract(tan(cos(uv) * 123.6) * 3533.3); | ||
} | ||
vec2 pt(vec2 id) { | ||
return sin(iTime * 0.5 * (ran(id + .5) - 0.5) + ran(id - 20.1) * 8.0) * 0.5; | ||
} | ||
|
||
vec4 main(vec2 fragCoord) | ||
{ | ||
float SIZE = 10.; | ||
vec2 uv = (fragCoord - .5 * iResolution.xy) / iResolution.x; | ||
vec2 off = iTime / vec2(200., 120.); | ||
uv += off; | ||
uv *= SIZE; | ||
|
||
vec2 gv = fract(uv) - .5; | ||
vec2 id = floor(uv); | ||
|
||
float mindist = 1e9; | ||
vec2 vorv = vec2(0); | ||
for (float i = -1.;i <= 1.; i++) { | ||
for (float j = -1.;j <= 1.; j++) { | ||
vec2 offv = vec2(i, j); | ||
float dist = length(gv + pt(id + offv) - offv); | ||
if (dist < mindist) { | ||
mindist = dist; | ||
vorv = (id + pt(id + offv) + offv) / SIZE - off; | ||
} | ||
} | ||
} | ||
|
||
vec3 col = mix(iPrimary, iAccent, clamp(vorv.x * 2.2 + vorv.y, -1., 1.) * 0.5 + 0.5); | ||
vec3 comp = blendOverlay(iBase, col); | ||
return vec4(comp, 1.); | ||
} |
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,3 @@ | ||
vec4 main(vec2 fragCoord) { | ||
return vec4(iBase, 1.0); | ||
} |
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,63 @@ | ||
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); | ||
} | ||
|
||
vec3 blendOverlay(vec3 base, vec3 blend) { | ||
return vec3( | ||
base.r < 0.5 ? (2.0 * base.r * blend.r) : (1.0 - 2.0 * (1.0 - base.r) * (1.0 - blend.r)), | ||
base.g < 0.5 ? (2.0 * base.g * blend.g) : (1.0 - 2.0 * (1.0 - base.g) * (1.0 - blend.g)), | ||
base.b < 0.5 ? (2.0 * base.b * blend.b) : (1.0 - 2.0 * (1.0 - base.b) * (1.0 - blend.b)) | ||
); | ||
} | ||
|
||
mat2 Rot(float a) { | ||
float s = sin(a); | ||
float c = cos(a); | ||
return mat2(c, -s, s, c); | ||
} | ||
|
||
vec2 hash(vec2 p) { | ||
p = vec2(dot(p, vec2(2127.1, 81.17)), dot(p, vec2(1269.5, 283.37))); | ||
return fract(sin(p) * 43758.5453); | ||
} | ||
|
||
float noise(in vec2 p) { | ||
vec2 i = floor(p); | ||
vec2 f = fract(p); | ||
|
||
vec2 u = f * f * (3.0 - 2.0 * f); | ||
|
||
float n = mix(mix(dot(-1.0 + 2.0 * hash(i + vec2(0.0, 0.0)), f - vec2(0.0, 0.0)), | ||
dot(-1.0 + 2.0 * hash(i + vec2(1.0, 0.0)), f - vec2(1.0, 0.0)), u.x), | ||
mix(dot(-1.0 + 2.0 * hash(i + vec2(0.0, 1.0)), f - vec2(0.0, 1.0)), | ||
dot(-1.0 + 2.0 * hash(i + vec2(1.0, 1.0)), f - vec2(1.0, 1.0)), u.x), u.y); | ||
return 0.5 + 0.5 * n; | ||
} | ||
|
||
vec4 main(vec2 fragCoord) { | ||
vec2 uv = fragCoord / iResolution.xy; | ||
float ratio = iResolution.x / iResolution.y; | ||
|
||
vec2 tuv = uv; | ||
tuv -= .5; | ||
|
||
float degree = noise(vec2(iTime * .1, tuv.x * tuv.y)); | ||
|
||
tuv.y *= 1. / ratio; | ||
tuv *= Rot(radians((degree - .5) * 720. + 180.)); | ||
tuv.y *= ratio; | ||
|
||
float frequency = 10.; | ||
float amplitude = 15.; | ||
float speed = iTime * 0.1; | ||
tuv.x += sin(tuv.y * frequency + speed) / amplitude; | ||
tuv.y += sin(tuv.x * frequency * 1.5 + speed) / (amplitude * .5); | ||
|
||
vec3 layer1 = mix(iAccent, iPrimary * 0.85, smoothstep(-.3, .2, (tuv * Rot(radians(-5.))).x)); | ||
vec3 layer2 = mix(iPrimary, iAccent * 0.65, smoothstep(-.3, .2, (tuv * Rot(radians(-5.))).x)); | ||
vec3 finalComp = mix(layer1, layer2, smoothstep(.5, -.3, tuv.y)); | ||
vec3 col = blendOverlay(iBase, finalComp); | ||
|
||
return vec4(col, 1.0); | ||
} |
Oops, something went wrong.