// Ray Kiln ยท Frosted Glass // // @episode Frosted Glass // @length 60 // @variation 30 // @variations 1,2,3,4,8,9,10,11 // @sweeps blur, grain, tinting // @still 21.0 // @teaches A layer effect that scatters what is behind it, tinted by a color SwiftUI passes // @category SwiftUI Effect // @tags swiftui, layer effect, blur, frosted glass, premultiplied alpha, golden angle, signed distance fields, procedural sound // @final Frosted glass | The frost lies thick in some places and thin in others, and drifts; where it is thin the view shows through almost clear, and here and there a facet catches the light. // // A pane of frosted glass is a SwiftUI layer effect: a function SwiftUI runs // once for every point of a view, which reads the view itself through // `layer.sample`. Rough glass sends the light from each point behind it many // ways at once, so a point of the pane shows the average of the view around // it: sixteen samples on a spiral. Each facet of the surface leans its own // way, which is the grain; the pane has an edge, a rim and a shadow; and the // glass has a color, which SwiftUI passes in as an argument. // // @subject card // @backdrop dark // @maxSampleOffset 24 24 #include #include using namespace metal; // @param blur 10.0 0.0 16.0 How far the glass spreads the light, in points // @param grain 0.8 0.4 6.0 The size of a facet of the rough surface, in points // @param rough 0.6 0.0 1.0 How much each facet throws the light its own way // @param tinting 0.5 0.0 1.0 How much of the color of the glass shows // @param frost 0.7 0.0 1.0 How uneven the frost is, thick in places and thin // @param slide 1.0 0.0 3.0 How fast the pane slides across the view // @param volume 0.7 0.0 1.0 How loud the glass sings // @color tint 0.74 0.9 1 The color of the glass // A whole-number point in, a random 0 to 1 out, the same on every GPU. float hash21(float2 p) { int2 i = int2(floor(p)); uint h = uint(i.x) * 1597334677u ^ uint(i.y) * 3812015801u; h ^= h >> 16; h *= 2246822519u; h ^= h >> 13; return float(h & 0xFFFFFFu) / 16777216.0; } // Value noise: random values at grid corners, blended smoothly. float vnoise(float2 p) { float2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f); float a = hash21(i), b = hash21(i + float2(1, 0)); float c = hash21(i + float2(0, 1)), d = hash21(i + float2(1, 1)); return mix(mix(a, b, f.x), mix(c, d, f.x), f.y); } // How far the pane's middle is from the view's, as a share of its width: // it slides side to side, once in about eighteen seconds. float paneSlide(float t, float slide) { return 0.2 * sin(t * 0.35 * slide); } // The distance to the edge of the pane, in points: a rounded rectangle // 56% of the view wide and 80% of it high. Negative inside. float paneEdge(float2 position, float2 size, float t, float slide) { float2 middle = size * float2(0.5 + paneSlide(t, slide), 0.5); float2 q = abs(position - middle) - size * float2(0.28, 0.4) + 14.0; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - 14.0; } // How thick the frost is at a point, 0 to 1: patches about 50 points // across that drift up the pane. `frost` is how thin the thin places get. float frostAt(float2 position, float t, float frost) { float2 p = position / 50.0 + float2(0.0, t * 0.05); float n = vnoise(p) * 0.65 + vnoise(p * 2.3 + 9.0) * 0.35; return 1.0 - frost * smoothstep(0.62, 0.34, n); } // The view through rough glass at one point: sixteen samples on a spiral // around `at`, each the golden angle on from the last and a little further // out, as far as `radius` points. `turn` turns the whole spiral. half4 through(SwiftUI::Layer layer, float2 at, float radius, float turn) { half4 sum = half4(0.0h); for (int i = 0; i < 16; i++) { float r = radius * sqrt((float(i) + 0.5) / 16.0); float a = float(i) * 2.39996 + turn; sum += layer.sample(at + r * float2(cos(a), sin(a))); } return sum / 16.0h; } [[ stitchable ]] half4 frostedGlass(float2 position, SwiftUI::Layer layer, float time, float2 size, half4 tint, float blur, float grain, float rough, float tinting, float frost, float slide) { half4 clear = layer.sample(position); float d = paneEdge(position, size, time, slide); if (d > 0.0) { // Outside the pane: the view as it is, in the pane's soft shadow. clear.rgb *= half(1.0 - 0.35 * exp(-d / 5.0)); return clear; } // The frost here: thin frost spreads the light less far. float thick = frostAt(position, time, frost); float radius = blur * thick; // The facet of the surface this point is on, and two random numbers // for it: which way it leans, and how its spiral is turned. float2 facet = floor(position / grain); float h1 = hash21(facet), h2 = hash21(facet + 57.0); float lean = 6.2832 * h1; float2 off = float2(cos(lean), sin(lean)) * h2 * 0.5 * radius * rough; half4 c = through(layer, position + off, radius, 6.2832 * h2); // The color of the glass, as SwiftUI passes it: premultiplied. Light // going through is multiplied by it, and the frost adds some of its own. half3 glass = c.rgb * tint.rgb + tint.rgb * 0.22h * c.a; c.rgb = mix(c.rgb, glass, half(tinting * thick)); // A facet now and then catches the light behind it and throws it // straight at the eye: a glint, brighter where the frost is thick. float glint = smoothstep(0.97, 1.0, sin(time * 1.7 + 6.2832 * h1)); c.rgb += c.rgb * half(glint * 0.7 * thick * rough); // The rim: a bright line just inside the edge. float rim = smoothstep(2.4, 0.0, abs(d + 1.6)); c.rgb += tint.rgb * half(0.3 * rim) * c.a; return c; } float2 sound(float t, constant Params& p) { // Four glasses rubbed at their rims, a chord on D: each a pure tone // that wavers a little, with a faint second tone two octaves up. float2 v = 0.0; for (int n = 0; n < 4; n++) { float hz = n == 0 ? 146.83 : n == 1 ? 220.0 : n == 2 ? 369.99 : 440.0; // Each swells and fades on its own slow noise, the frost's own. float swell = vnoise(float2(t * 0.22 + float(n) * 13.0, float(n))); swell = smoothstep(0.25, 0.8, swell); // More blur dulls the high glasses, as it dulls the picture. float dull = 1.0 / (1.0 + float(n) * p.blur * 0.06); // Four copies of each, detuned as the facets spread the light: // more `rough`, a wider, rougher tone. float note = 0.0; for (int k = 0; k < 4; k++) { float seed = float(n) * 7.0; float off = hash21(float2(float(k), seed)) - 0.5; float f = hz * (1.0 + 0.024 * off * p.rough); float ph = 6.2832 * hash21(float2(seed, float(k) + 9.0)); ph += 6.2832 * f * t + 0.6 * sin(t * (4.3 + float(k))); note += 0.25 * (sin(ph) + 0.18 * sin(4.21 * ph)); } float side = float(n) * 0.5 - 0.75; v += note * swell * dull * float2(0.5 - 0.4 * side, 0.5 + 0.4 * side); } // The pane's slide leans the chord the way the pane has gone. float pan = clamp(paneSlide(t, p.slide) * 2.5, -0.6, 0.6); v *= float2(1.0 - pan, 1.0 + pan); // Frost ticking: on each of eight ticks a second, a chance of a small // high ping that dies in a few hundredths of a second. float beat = floor(t * 8.0); for (int k = 0; k < 3; k++) { float n = beat - float(k); if (hash21(float2(n, 3.0)) > 0.16 * (0.3 + p.frost)) continue; float since = t - (n + hash21(float2(n, 5.0))) / 8.0; if (since < 0.0) continue; float hz = 2400.0 + 3600.0 * hash21(float2(n, 7.0)); float ping = sin(6.2832 * hz * since) * exp(-since * 55.0); ping *= smoothstep(0.0, 0.002, since); float side = hash21(float2(n, 11.0)) - 0.5; v += ping * float2(0.5 - side, 0.5 + side) * 0.4; } // tanh rounds off the loudest moments instead of letting them clip. float2 o = tanh(v * 0.9 * 1.4) / 1.4; // Fade in over the first half second and out over the last, so the // sound starts from silence and ends in it. float fade = smoothstep(0.0, 0.5, t) * smoothstep(60.0, 59.0, t); return o * p.volume * fade; }