@@ -7,40 +7,20 @@ precision mediump float;
77#include < flutter/ runtime_effect.glsl>
88
99uniform vec2 iResolution;
10- uniform float iTime;
1110uniform float filmGrainIntensity;
12- uniform float noiseOpacity;
1311uniform sampler2D iChannel0;
1412
1513out vec4 fragColor;
1614
1715// Inspired by https://www.shadertoy.com/view/wdyczG
1816// Licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License:
1917// https://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
20- mat2 Rot(float a) {
21- float s = sin (a);
22- float c = cos (a);
23- return mat2 (c, - s, s, c);
24- }
2518
2619vec2 hash(vec2 p) {
2720 p = vec2 (dot (p, vec2 (2127.1 , 81.17 )), dot (p, vec2 (1269.5 , 283.37 )));
2821 return fract (sin (p)* 43758.5453 );
2922}
3023
31- float noise(in vec2 p) {
32- vec2 i = floor (p);
33- vec2 f = fract (p);
34-
35- vec2 u = f* f* (3.0 - 2.0 * f);
36-
37- float n = mix (mix (dot (- 1.0 + 2.0 * hash(i + vec2 (0.0 , 0.0 )), f - vec2 (0.0 , 0.0 )),
38- dot (- 1.0 + 2.0 * hash(i + vec2 (1.0 , 0.0 )), f - vec2 (1.0 , 0.0 )), u.x),
39- mix (dot (- 1.0 + 2.0 * hash(i + vec2 (0.0 , 1.0 )), f - vec2 (0.0 , 1.0 )),
40- dot (- 1.0 + 2.0 * hash(i + vec2 (1.0 , 1.0 )), f - vec2 (1.0 , 1.0 )), u.x), u.y);
41- return 0.5 + 0.5 * n;
42- }
43-
4424float filmGrainNoise(in vec2 uv) {
4525 return length (hash(vec2 (uv.x, uv.y)));
4626}
@@ -49,73 +29,15 @@ void main()
4929{
5030 vec2 fragCoord = FlutterFragCoord().xy;
5131 vec2 uv = fragCoord / iResolution.xy;
52- float aspectRatio = iResolution.x / iResolution.y;
5332
5433 // Get the original image/widget content
5534 vec3 originalColor = texture(iChannel0, uv).rgb;
5635
57- // Transformed uv for noise generation
58- vec2 tuv = uv - .5 ;
59-
60- // Rotate with noise
61- float degree = noise(vec2 (iTime*.05 , tuv.x* tuv.y));
62-
63- tuv.y *= 1 ./aspectRatio;
64- tuv *= Rot(radians ((degree-.5 )* 720 .+180 .));
65- tuv.y *= aspectRatio;
66-
67- // Wave warp with sine
68- float frequency = 5 .;
69- float amplitude = 30 .;
70- float speed = iTime * 2 .;
71- tuv.x += sin (tuv.y* frequency+ speed)/ amplitude;
72- tuv.y += sin (tuv.x* frequency* 1.5 + speed)/ (amplitude*.5 );
73-
74- // Light gradient colors
75- vec3 amberYellow = vec3 (299 , 186 , 137 ) / vec3 (255 );
76- vec3 deepBlue = vec3 (49 , 98 , 238 ) / vec3 (255 );
77- vec3 pink = vec3 (246 , 146 , 146 ) / vec3 (255 );
78- vec3 blue = vec3 (89 , 181 , 243 ) / vec3 (255 );
79-
80- // Dark gradient colors
81- vec3 purpleHaze = vec3 (105 , 49 , 245 ) / vec3 (255 );
82- vec3 swampyBlack = vec3 (32 , 42 , 50 ) / vec3 (255 );
83- vec3 persimmonOrange = vec3 (233 , 51 , 52 ) / vec3 (255 );
84- vec3 darkAmber = vec3 (233 , 160 , 75 ) / vec3 (255 );
85-
86- // Interpolate between light and dark gradient
87- float cycle = sin (iTime * 0.5 );
88- float t = (sign (cycle) * pow (abs (cycle), 0.6 ) + 1 .) / 2 .;
89- vec3 color1 = mix (amberYellow, purpleHaze, t);
90- vec3 color2 = mix (deepBlue, swampyBlack, t);
91- vec3 color3 = mix (pink, persimmonOrange, t);
92- vec3 color4 = mix (blue, darkAmber, t);
93-
94- // Blend the gradient colors and apply transformations
95- vec3 layer1 = mix (color3, color2, smoothstep (-.3 , .2 , (tuv* Rot(radians (- 5 .))).x));
96- vec3 layer2 = mix (color4, color1, smoothstep (-.3 , .2 , (tuv* Rot(radians (- 5 .))).x));
97-
98- vec3 noiseColor = mix (layer1, layer2, smoothstep (.5 , -.3 , tuv.y));
99-
100- // Apply film grain to noise
101- noiseColor = noiseColor - filmGrainNoise(uv) * filmGrainIntensity;
102-
103- // Blend the noise effect with the original image
104- // Use various blend modes for different effects
105- vec3 finalColor = mix (originalColor, noiseColor, noiseOpacity);
106-
107- // Alternative blend modes (uncomment to try different effects):
108- // Overlay blend mode:
109- // vec3 finalColor = mix(originalColor,
110- // originalColor < 0.5 ? 2.0 * originalColor * noiseColor
111- // : 1.0 - 2.0 * (1.0 - originalColor) * (1.0 - noiseColor),
112- // noiseOpacity);
113-
114- // Screen blend mode:
115- // vec3 finalColor = mix(originalColor, 1.0 - (1.0 - originalColor) * (1.0 - noiseColor), noiseOpacity);
116-
117- // Multiply blend mode:
118- // vec3 finalColor = mix(originalColor, originalColor * noiseColor, noiseOpacity);
36+ // Apply film grain
37+ float grain = filmGrainNoise(uv) * filmGrainIntensity;
38+ // Mix the grain with the original color instead of subtracting
39+ // This creates a more natural film grain effect
40+ vec3 finalColor = originalColor * (1.0 - grain * 0.5 ) + grain * 0.1 ;
11941
12042 fragColor = vec4 (finalColor, texture(iChannel0, uv).a);
12143}
0 commit comments