Page Curl
A page curl as a SwiftUI layer effect. The view's bottom right corner is pulled to a grab point; the paper past the fold rolls round a cylinder and lies back over the page, its back a cream paper with the print showing through, mirrored. The grab point is the touch, a press and drag on the view that eases back to the corner when let go, plus a pull of the effect's own on the clock, so the page turns by itself too.
· SwiftUI Effects · SwiftUI Effect shader
Teaches A distortion effect that follows a drag: the page lifts where you pull
Builds on Flowing Color Field (hash21, value noise), Heat Haze, Frosted Glass
Watch
The idea
A SwiftUI layer effect is a Metal function marked [[ stitchable ]] that SwiftUI runs once for every point of a view, handed the view itself as layer. Heat Haze showed each point from a little way off, and Frosted Glass from sixteen places at once. A page curl shows each point from much further away: the part of the page that has been lifted and turned over lies on top of a different part of the view, so the color a point shows comes from the other side of a fold.
Take a sheet of paper by its bottom right corner and pull the corner up and to the left. Wherever you hold it, the fold is the line halfway between where the corner started and where your hand is: every point of the fold is as far from one as from the other, which is why the corner lands on your hand. Past the fold the page has gone. Short of it, the turned-over part lies over the page, mirrored across the fold, and you see its back.
The subject is the card Ray Kiln previews SwiftUI effects on, 320 by 200 points, measured from its top left as SwiftUI measures it. The effect takes one more argument than the earlier ones: touch, a point in the view that follows a press and drag. You can pull this page yourself, under Run it live on any stage from the second on. It is built in five steps, each a whole effect you can run in the playground or in an app.
Stage 1 · A fold
The corner is size, the view's bottom right. Here it is pulled toward the top left along the diagonal, n = normalize(size) being the way back to it, and far is how far it has gone: up to three quarters of the diagonal times turn, times pageLift. That is the clock's part. Every twelve seconds the page lifts for about five seconds, holds, and falls back flat by the end of the turn, and the clock starts three seconds into one so the page is already lifting at the start.
The fold is the line through the point halfway, corner - n * far * 0.5, across n, and d = dot(position - fold, n) is how far a point is past it toward the corner. Past the fold the effect returns half4(0.0h), clear, so the dark backdrop shows where the page was. Short of it, the point shows the page, and over it the point as far past the fold as this one is short of it: layer.sample(position - 2.0 * d * n), the view mirrored across the line. Where that point is off the card, the sample is clear and the page shows through. The two are stacked as premultiplied colors are, over + below * (1 - over.a): the top one, plus what it lets by.
stage-1-fold.metal
// Stage 1 · A fold
// The page's bottom right corner is pulled in along the diagonal and folded
// over. The fold is the line halfway between the corner and where it is
// pulled to: past it the page is gone, and the part that was there lies
// over the page, mirrored across the fold.
// @subject card
// @backdrop dark
// @maxSampleOffset 40 40
#include <metal_stdlib>
#include <SwiftUI/SwiftUI_Metal.h>
using namespace metal;
// @param turn 0.7 0.0 1.0 How far the page turns by itself
// @param volume 0.7 0.0 1.0 How loud the paper is
// 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);
}
// A turn every 12 seconds: how far through it, 0 to 1.
float pageCycle(float t) {
return fract((t + 3.0) / 12.0);
}
// How far the page has lifted: up, held, then down.
float pageLift(float t) {
float c = pageCycle(t);
return smoothstep(0.0, 0.45, c) * smoothstep(1.0, 0.65, c);
}
[[ stitchable ]] half4 pageCurl(float2 position, SwiftUI::Layer layer,
float2 size, float time, float turn) {
// The corner, pulled in along the diagonal.
float2 corner = size;
float2 n = normalize(size);
float far = length(size) * 0.75 * turn * pageLift(time);
if (far < 0.01) return layer.sample(position);
// The fold, halfway; d is how far past it.
float2 fold = corner - n * far * 0.5;
float d = dot(position - fold, n);
// Past it, the page is gone.
if (d > 0.0) return half4(0.0h);
// Short of it, the page, mirrored over itself.
half4 below = layer.sample(position);
half4 over = layer.sample(position - 2.0 * d * n);
return over + below * (1.0h - over.a);
}
float2 sound(float t, constant Params& p) {
// How fast the page is moving, from its lift a little either side.
float move = abs(pageLift(t + 0.02) - pageLift(t - 0.02)) / 0.04;
move = clamp(move * 3.0, 0.0, 1.0) * p.turn;
// Paper rustling: fast value noise read along the turn's own time, a
// different noise in each ear and each turn, fluttering on a slower
// noise as the sheet crinkles.
float since = pageCycle(t) * 12.0;
float seed = floor((t + 3.0) / 12.0) * 7.0;
float2 rustle = 0.0;
for (int ch = 0; ch < 2; ch++) {
float s = seed + float(ch) * 41.0;
float hiss = (vnoise(float2(since * 9000.0, s + 1.0)) - 0.5)
+ 0.7 * (vnoise(float2(since * 4100.0, s + 2.0)) - 0.5)
- 0.6 * (vnoise(float2(since * 900.0, s + 3.0)) - 0.5);
float flutter = vnoise(float2(since * 28.0, s + 4.0));
rustle[ch] = hiss * (0.25 + 1.2 * flutter * flutter);
}
float2 v = rustle * move * 0.9;
// tanh rounds off the loudest moments instead of letting them clip.
float2 o = tanh(v * 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.5, t);
return o * p.volume * fade;
}Stage 2 · The page follows a drag
Now the effect takes float2 touch, and the corner goes to a grab point. The header line // @touch 1 1 says where touch rests: the bottom right corner, as fractions of the view. A press on the picture puts it under the pointer, it follows the drag, and let go, it eases back to the corner. At rest it is the corner itself, so a still or a video draws the page as the clock alone has it.
The clock's pull is pagePull: up and to the left at about 11° up from leftward, swinging a little lower as each turn goes on, as a hand turning a page does, as far as three quarters of the view's width times turn and the lift. The grab point is touch plus that pull, and the pull gives way as the drag takes over: it fades out as touch gets away from the corner, and is gone by 30% of the view's width, so a press in the middle of the page puts the corner under the pointer rather than somewhere past it. A grab point to the right of the corner or below it stops at it, min(touch + pull, corner), so a drag out past the right edge lifts the bottom edge instead, and it is never further from the corner than the view's diagonal, so the most the page can do is fold in half.
The fold is still the line halfway, but n is now the way from the grab point to the corner, so the fold turns to face wherever the page is pulled. Set turn to 0 and the page lies still until you drag it.
stage-2-drag.metal22 new lines since stage 1
// Stage 2 · The page follows a drag
// Now the corner goes where it is pulled. The effect takes touch, which
// follows a press and drag on the view and rests at the corner, as the
// // @touch line says; the clock adds a pull of its own, so the page turns
// by itself too. The fold turns to face the grab point.
// @subject card
// @backdrop dark
// @touch 1 1 The page's bottom right corner
// @maxSampleOffset 40 40
#include <metal_stdlib>
#include <SwiftUI/SwiftUI_Metal.h>
using namespace metal;
// @param turn 0.7 0.0 1.0 How far the page turns alone; at 0 only a drag does
// @param volume 0.7 0.0 1.0 How loud the paper is
// 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);
}
// A turn every 12 seconds: how far through it, 0 to 1.
float pageCycle(float t) {
return fract((t + 3.0) / 12.0);
}
// How far the page has lifted: up, held, then down.
float pageLift(float t) {
float c = pageCycle(t);
return smoothstep(0.0, 0.45, c) * smoothstep(1.0, 0.65, c);
}
// The clock's own pull on the corner, in points: up and to the left,
// swinging lower as the turn goes on, as a hand does.
float2 pagePull(float t, float2 size, float turn) {
float a = 0.2 + 0.2 * (0.5 - pageCycle(t));
float far = size.x * 0.75 * turn * pageLift(t);
return -float2(cos(a), sin(a)) * far;
}
[[ stitchable ]] half4 pageCurl(float2 position, SwiftUI::Layer layer,
float2 size, float2 touch, float time,
float turn) {
float2 corner = size;
// The grab point: the drag, plus the clock's pull, which gives way
// as the drag takes the corner away from it.
float held = length(touch - corner) / size.x;
float2 pull = pagePull(time, size, turn);
pull *= 1.0 - smoothstep(0.0, 0.3, held);
float2 grab = min(touch + pull, corner);
float far = length(corner - grab);
if (far < 0.01) return layer.sample(position);
float2 n = (corner - grab) / far;
// At most, the page folds in half.
far = min(far, length(size));
// The fold, halfway; d is how far this point is past it.
float2 fold = corner - n * far * 0.5;
float d = dot(position - fold, n);
if (d > 0.0) return half4(0.0h);
// The page, and over it the part past the fold, mirrored.
half4 below = layer.sample(position);
half4 over = layer.sample(position - 2.0 * d * n);
return over + below * (1.0h - over.a);
}
float2 sound(float t, constant Params& p) {
// How fast the page is moving, from its lift a little either side.
float lift = pageLift(t);
float move = abs(pageLift(t + 0.02) - pageLift(t - 0.02)) / 0.04;
move = clamp(move * 3.0, 0.0, 1.0) * p.turn;
// Paper rustling: fast value noise read along the turn's own time, a
// different noise in each ear and each turn, fluttering on a slower
// noise as the sheet crinkles.
float since = pageCycle(t) * 12.0;
float seed = floor((t + 3.0) / 12.0) * 7.0;
float2 rustle = 0.0;
for (int ch = 0; ch < 2; ch++) {
float s = seed + float(ch) * 41.0;
float hiss = (vnoise(float2(since * 9000.0, s + 1.0)) - 0.5)
+ 0.7 * (vnoise(float2(since * 4100.0, s + 2.0)) - 0.5)
- 0.6 * (vnoise(float2(since * 900.0, s + 3.0)) - 0.5);
float flutter = vnoise(float2(since * 28.0, s + 4.0));
rustle[ch] = hiss * (0.25 + 1.2 * flutter * flutter);
}
// As the corner travels left it takes the rustle to the left.
float pan = -0.5 * lift * p.turn;
rustle *= move * float2(1.0 - pan, 1.0 + pan);
// A soft low tone, A and E, that swells as the page lifts.
float2 tone = float2(sin(6.2832 * 110.0 * t), sin(6.2832 * 164.81 * t));
tone *= (0.06 + 0.12 * lift * p.turn) * float2(1.0, 0.8);
// A soft pat as the page lands flat again at the end of each turn.
float pat = sin(6.2832 * 70.0 * since) * exp(-since * 16.0);
pat *= smoothstep(0.0, 0.004, since) * p.turn;
float2 v = (rustle * 0.9 + tone + pat * 0.5) * 1.25;
// tanh rounds off the loudest moments instead of letting them clip.
float2 o = tanh(v * 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.5, t);
return o * p.volume * fade;
}4 lines from stage 1 that are gone
float2 size, float time, float turn) {
⋯
float2 n = normalize(size);
float far = length(size) * 0.75 * turn * pageLift(time);
⋯
float2 v = rustle * move * 0.9;Stage 3 · A roll
Paper doesn't crease when it turns: it rolls. Picture a cylinder of radius points lying on the page along the fold. The paper lies flat up to the cylinder's foot, climbs its near side, crosses the top, and comes back down over the page upside down. pageAbove works out what lies over a point. d is now how far the point is past the cylinder's axis. Past d = r, the far side of the roll, there is nothing. Short of the axis, a point is under the paper that has come over the top, and a point of the page s past the axis ends up pi r - s past it: the mirror of stage 1, moved back by half the roll.
Over the roll itself, between the axis and r past it, there are two layers of paper. At angle a round the cylinder from its foot the paper is r sin a past the axis, so a = asin(d / r), and that is the paper r a along from the foot, front up, climbing. The other layer is pi - a round, on its way back over the top with its back up, and it is on top. The roll's outer edge fades over one point so it isn't jagged.
The roll takes up paper, so the axis sits further back than the halfway line, at (far + pi r) / 2 from the corner: once the paper has gone round, the corner still lands on the grab point. The radius is never more than a quarter of far, so a page that has barely lifted has a small roll. Lifted off the page, the paper casts a shadow: the effect asks how much paper lies over 24 points on a spiral 9 points out, a little toward the corner, which is where the light comes from, and darkens the page by up to 0.6 * shadow. The golden-angle spiral, as in Frosted Glass, softens the shadow's edge, and each pixel turns it by its own random angle from hash21, so the edge fades in a fine grain instead of in steps.
stage-3-roll.metal27 new lines since stage 2
// Stage 3 · A roll
// Paper doesn't crease when it turns: it rolls. Here it rolls round a
// cylinder of radius points along the fold, climbs it, crosses the top
// and lies back over the page. Lifted off the page, the paper casts a
// soft shadow on it.
// @subject card
// @backdrop dark
// @touch 1 1 The page's bottom right corner
// @maxSampleOffset 40 40
#include <metal_stdlib>
#include <SwiftUI/SwiftUI_Metal.h>
using namespace metal;
// @param turn 0.7 0.0 1.0 How far the page turns alone; at 0 only a drag does
// @param radius 20.0 0.0 40.0 The radius of the roll of paper, in points
// @param shadow 0.6 0.0 1.0 How dark the shadow of the lifted page is
// @param volume 0.7 0.0 1.0 How loud the paper is
// 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);
}
// A turn every 12 seconds: how far through it, 0 to 1.
float pageCycle(float t) {
return fract((t + 3.0) / 12.0);
}
// How far the page has lifted: up, held, then down.
float pageLift(float t) {
float c = pageCycle(t);
return smoothstep(0.0, 0.45, c) * smoothstep(1.0, 0.65, c);
}
// The clock's own pull on the corner, in points: up and to the left,
// swinging lower as the turn goes on, as a hand does.
float2 pagePull(float t, float2 size, float turn) {
float a = 0.2 + 0.2 * (0.5 - pageCycle(t));
float far = size.x * 0.75 * turn * pageLift(t);
return -float2(cos(a), sin(a)) * far;
}
// The paper lifted over p, or clear; d is how far p is past the axis.
half4 pageAbove(SwiftUI::Layer layer, float2 p, float2 axis, float2 n,
float r) {
const float pi = 3.14159265;
float d = dot(p - axis, n);
if (d > r) return half4(0.0h);
// Over the roll and back: s past the axis lands pi r - s past.
if (d <= 0.0) return layer.sample(p + (pi * r - 2.0 * d) * n);
// On the roll at angle a: the back on top, pi - a round, the front a.
float a = asin(d / r);
half4 back = layer.sample(p + (r * (pi - a) - d) * n);
half4 front = layer.sample(p + (r * a - d) * n);
// The roll's edge fades over a point.
return (back + front * (1.0h - back.a)) * half(clamp(r - d, 0.0, 1.0));
}
[[ stitchable ]] half4 pageCurl(float2 position, SwiftUI::Layer layer,
float2 size, float2 touch, float time,
float turn, float radius, float shadow) {
float2 corner = size;
// The grab point: the drag, plus the clock's pull, which gives way
// as the drag takes the corner away from it.
float held = length(touch - corner) / size.x;
float2 pull = pagePull(time, size, turn);
pull *= 1.0 - smoothstep(0.0, 0.3, held);
float2 grab = min(touch + pull, corner);
float far = length(corner - grab);
if (far < 0.01) return layer.sample(position);
float2 n = (corner - grab) / far;
// At most, the page folds in half.
far = min(far, length(size));
float r = min(radius, far * 0.25);
// The axis, far enough back that the corner lands on the grab point.
float2 axis = corner - n * (far + 3.14159265 * r) * 0.5;
half4 top = pageAbove(layer, position, axis, n, r);
// The page beneath, gone past the axis.
half4 below = layer.sample(position);
if (dot(position - axis, n) > 0.0) below = half4(0.0h);
// Its shadow: how much paper lies over 24 points nearby, a little
// toward the corner, the spiral turned at random for each pixel.
float cover = 0.0;
float2 toward = position + n * (r * 0.5 + 6.0);
float spin = 6.2832 * hash21(position * 8.0);
for (int i = 0; i < 24; i++) {
float s = 9.0 * sqrt((float(i) + 0.5) / 24.0);
float a = float(i) * 2.39996 + spin;
float2 q = toward + s * float2(cos(a), sin(a));
cover += float(pageAbove(layer, q, axis, n, r).a);
}
below.rgb *= half(1.0 - shadow * 0.6 * cover / 24.0);
// Paper over page, as premultiplied colors stack.
return top + below * (1.0h - top.a);
}
float2 sound(float t, constant Params& p) {
// How fast the page is moving, from its lift a little either side.
float lift = pageLift(t);
float move = abs(pageLift(t + 0.02) - pageLift(t - 0.02)) / 0.04;
move = clamp(move * 3.0, 0.0, 1.0) * p.turn;
// Paper rustling: fast value noise read along the turn's own time, a
// different noise in each ear and each turn, fluttering on a slower
// noise as the sheet crinkles.
float since = pageCycle(t) * 12.0;
float seed = floor((t + 3.0) / 12.0) * 7.0;
float2 rustle = 0.0;
for (int ch = 0; ch < 2; ch++) {
float s = seed + float(ch) * 41.0;
float hiss = (vnoise(float2(since * 9000.0, s + 1.0)) - 0.5)
+ 0.7 * (vnoise(float2(since * 4100.0, s + 2.0)) - 0.5)
- 0.6 * (vnoise(float2(since * 900.0, s + 3.0)) - 0.5);
float flutter = vnoise(float2(since * 28.0, s + 4.0));
rustle[ch] = hiss * (0.25 + 1.2 * flutter * flutter);
}
// As the corner travels left it takes the rustle to the left.
float pan = -0.5 * lift * p.turn;
rustle *= move * float2(1.0 - pan, 1.0 + pan);
// A soft low tone, A and E, that swells as the page lifts.
float2 tone = float2(sin(6.2832 * 110.0 * t), sin(6.2832 * 164.81 * t));
tone *= (0.06 + 0.12 * lift * p.turn) * float2(1.0, 0.8);
// A soft pat as the page lands flat again at the end of each turn.
float pat = sin(6.2832 * 70.0 * since) * exp(-since * 16.0);
pat *= smoothstep(0.0, 0.004, since) * p.turn;
float2 v = (rustle * 0.9 + tone + pat * 0.5) * 1.25;
// tanh rounds off the loudest moments instead of letting them clip.
float2 o = tanh(v * 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.5, t);
return o * p.volume * fade;
}6 lines from stage 2 that are gone
float turn) {
⋯
float2 fold = corner - n * far * 0.5;
float d = dot(position - fold, n);
if (d > 0.0) return half4(0.0h);
⋯
half4 over = layer.sample(position - 2.0 * d * n);
return over + below * (1.0h - over.a);Stage 4 · Paper and light
The back of a page is paper. pageBack takes the color of the paper's front and returns cream, half3(1.0, 0.953, 0.871), times its alpha, mixed with the print by bleed, so 30% of the mirrored print shows through, as it does through thin paper held up to a light. The alpha stays the paper's own, so the card's rounded corners stay rounded on its back.
And the roll is lit. Light comes from above and a little toward the corner. pageLight takes the way the paper's visible side faces across the fold, up, and returns 0.3 + 0.7 times how squarely it faces the light, scaled so paper lying flat is exactly 1: the page and the paper lying back over it are untouched. Round the roll the back faces (sin a2, -cos a2) and the front (-sin a, cos a), so the paper darkens where it turns away from the light, at the roll's outer edge, which is what makes it read as round. shading blends from no shading to all of it.
stage-4-paper.metal23 new lines since stage 3
// Stage 4 · Paper and light
// The back of the page is paper now: cream, with the print showing through
// it a little, mirrored. And the roll is lit from above, so it darkens
// where it turns away from the light, which is what shows it is round.
// @subject card
// @backdrop dark
// @touch 1 1 The page's bottom right corner
// @maxSampleOffset 40 40
#include <metal_stdlib>
#include <SwiftUI/SwiftUI_Metal.h>
using namespace metal;
// @param turn 0.7 0.0 1.0 How far the page turns alone; at 0 only a drag does
// @param radius 20.0 0.0 40.0 The radius of the roll of paper, in points
// @param bleed 0.3 0.0 1.0 How much of the print shows on the page's back
// @param shading 0.7 0.0 1.0 How much the light falls off round the roll
// @param shadow 0.6 0.0 1.0 How dark the shadow of the lifted page is
// @param volume 0.7 0.0 1.0 How loud the paper is
// 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);
}
// A turn every 12 seconds: how far through it, 0 to 1.
float pageCycle(float t) {
return fract((t + 3.0) / 12.0);
}
// How far the page has lifted: up, held, then down.
float pageLift(float t) {
float c = pageCycle(t);
return smoothstep(0.0, 0.45, c) * smoothstep(1.0, 0.65, c);
}
// The clock's own pull on the corner, in points: up and to the left,
// swinging lower as the turn goes on, as a hand does.
float2 pagePull(float t, float2 size, float turn) {
float a = 0.2 + 0.2 * (0.5 - pageCycle(t));
float far = size.x * 0.75 * turn * pageLift(t);
return -float2(cos(a), sin(a)) * far;
}
// How bright paper is, its seen side facing up (x toward the corner, y
// off the page): lit from above, and exactly 1 lying flat.
float pageLight(float2 up) {
float lit = max(dot(up, float2(0.447, 0.894)), 0.0);
return 0.3 + 0.7 * lit / 0.894;
}
// The back of the page: cream paper, the print showing through by bleed.
half4 pageBack(half4 c, float bleed) {
half3 paper = half3(1.0h, 0.953h, 0.871h) * c.a;
return half4(mix(paper, c.rgb, half(bleed)), c.a);
}
// The paper lifted over p, or clear; d is how far p is past the axis.
half4 pageAbove(SwiftUI::Layer layer, float2 p, float2 axis, float2 n,
float r, float bleed, float shading) {
const float pi = 3.14159265;
float d = dot(p - axis, n);
if (d > r) return half4(0.0h);
if (d <= 0.0) {
// Over the roll and back: s past the axis lands pi r - s past.
float2 from = p + (pi * r - 2.0 * d) * n;
return pageBack(layer.sample(from), bleed);
}
// On the roll, at angle a from its foot: two layers of paper.
float a = asin(d / r);
// On top, the back, pi - a round.
float a2 = pi - a;
half4 back = pageBack(layer.sample(p + (r * a2 - d) * n), bleed);
float2 up2 = float2(sin(a2), -cos(a2));
back.rgb *= half(mix(1.0, pageLight(up2), shading));
// Beneath it, the front, a round.
half4 front = layer.sample(p + (r * a - d) * n);
float2 up1 = float2(-sin(a), cos(a));
front.rgb *= half(mix(1.0, pageLight(up1), shading));
// The roll's edge fades over a point.
return (back + front * (1.0h - back.a)) * half(clamp(r - d, 0.0, 1.0));
}
[[ stitchable ]] half4 pageCurl(float2 position, SwiftUI::Layer layer,
float2 size, float2 touch, float time,
float turn, float radius, float bleed,
float shading, float shadow) {
float2 corner = size;
// The grab point: the drag, plus the clock's pull, which gives way
// as the drag takes the corner away from it.
float held = length(touch - corner) / size.x;
float2 pull = pagePull(time, size, turn);
pull *= 1.0 - smoothstep(0.0, 0.3, held);
float2 grab = min(touch + pull, corner);
float far = length(corner - grab);
if (far < 0.01) return layer.sample(position);
float2 n = (corner - grab) / far;
// At most, the page folds in half.
far = min(far, length(size));
float r = min(radius, far * 0.25);
// The axis, far enough back that the corner lands on the grab point.
float2 axis = corner - n * (far + 3.14159265 * r) * 0.5;
half4 top = pageAbove(layer, position, axis, n, r, bleed, shading);
// The page beneath, gone past the axis.
half4 below = layer.sample(position);
if (dot(position - axis, n) > 0.0) below = half4(0.0h);
// Its shadow: how much paper lies over 24 points nearby, a little
// toward the corner, the spiral turned at random for each pixel.
float cover = 0.0;
float2 toward = position + n * (r * 0.5 + 6.0);
float spin = 6.2832 * hash21(position * 8.0);
for (int i = 0; i < 24; i++) {
float s = 9.0 * sqrt((float(i) + 0.5) / 24.0);
float a = float(i) * 2.39996 + spin;
float2 q = toward + s * float2(cos(a), sin(a));
cover += float(pageAbove(layer, q, axis, n, r, 0.0, 0.0).a);
}
below.rgb *= half(1.0 - shadow * 0.6 * cover / 24.0);
// Paper over page, as premultiplied colors stack.
return top + below * (1.0h - top.a);
}
float2 sound(float t, constant Params& p) {
// How fast the page is moving, from its lift a little either side.
float lift = pageLift(t);
float move = abs(pageLift(t + 0.02) - pageLift(t - 0.02)) / 0.04;
move = clamp(move * 3.0, 0.0, 1.0) * p.turn;
// Paper rustling: fast value noise read along the turn's own time, a
// different noise in each ear and each turn, fluttering on a slower
// noise as the sheet crinkles.
float since = pageCycle(t) * 12.0;
float seed = floor((t + 3.0) / 12.0) * 7.0;
float2 rustle = 0.0;
for (int ch = 0; ch < 2; ch++) {
float s = seed + float(ch) * 41.0;
float hiss = (vnoise(float2(since * 9000.0, s + 1.0)) - 0.5)
+ 0.7 * (vnoise(float2(since * 4100.0, s + 2.0)) - 0.5)
- 0.6 * (vnoise(float2(since * 900.0, s + 3.0)) - 0.5);
float flutter = vnoise(float2(since * 28.0, s + 4.0));
rustle[ch] = hiss * (0.25 + 1.2 * flutter * flutter);
}
// As the corner travels left it takes the rustle to the left.
float pan = -0.5 * lift * p.turn;
rustle *= move * float2(1.0 - pan, 1.0 + pan);
// A soft low tone, A and E, that swells as the page lifts.
float2 tone = float2(sin(6.2832 * 110.0 * t), sin(6.2832 * 164.81 * t));
tone *= (0.06 + 0.12 * lift * p.turn) * float2(1.0, 0.8);
// A soft pat as the page lands flat again at the end of each turn.
float pat = sin(6.2832 * 70.0 * since) * exp(-since * 16.0);
pat *= smoothstep(0.0, 0.004, since) * p.turn;
float2 v = (rustle * 0.9 + tone + pat * 0.5) * 1.25;
// tanh rounds off the loudest moments instead of letting them clip.
float2 o = tanh(v * 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.5, t);
return o * p.volume * fade;
}6 lines from stage 3 that are gone
float r) {
⋯
if (d <= 0.0) return layer.sample(p + (pi * r - 2.0 * d) * n);
⋯
half4 back = layer.sample(p + (r * (pi - a) - d) * n);
⋯
float turn, float radius, float shadow) {
⋯
half4 top = pageAbove(layer, position, axis, n, r);
⋯
cover += float(pageAbove(layer, q, axis, n, r).a);Stage 5 · Page Curl
Two things. The clock's pull no longer goes the same way every time: each of the five turns of a minute heads its own way, at 7°, 36°, 49°, 17° and 26° up from leftward, and as far as 1, 0.9, 0.95, 0.75 and 0.8 times the view's width. They were chosen so that even at turn 1 the lifted paper stays within the 40 points around the view that // @maxSampleOffset 40 40 gives it, every stage keeping the same number. And the roll shines: where its back faces halfway between the light and the eye, pow(..., 120.0) makes a narrow highlight along it, gloss deciding how bright.
shade(), from the finished shader
page-curl.metal, the whole file13 new lines since stage 4
// Ray Kiln · Page Curl
//
// @episode Page Curl
// @length 60
// @variation 30
// @variations 3,4,6,7,8,9,11,12
// @still 16.0
// @teaches A distortion effect that follows a drag: the page lifts where you pull
// @category SwiftUI Effect
// @tags swiftui, layer effect, page curl, touch, drag gesture, cylinder, premultiplied alpha, procedural sound
// @final Page curl | The page turns by itself, five times a minute and each time a different way, with a shine along the roll; press and drag it and the corner follows the pointer, then falls back when let go.
//
// A page curl is a SwiftUI layer effect: a function SwiftUI runs once for
// every point of a view, which reads the view itself through layer.sample.
// The page's bottom right corner is pulled to a grab point. The fold is the
// line halfway between the two, and past it the paper rolls round a cylinder
// and lies back over the page, showing its back: the view mirrored, as paper
// shows print through it. Where the page has lifted the view is clear. The
// grab point is touch, which follows a drag on the view and rests at the
// corner, plus a pull of the effect's own on the clock.
//
// @subject card
// @backdrop dark
// @touch 1 1 The page's bottom right corner
// @maxSampleOffset 40 40
#include <metal_stdlib>
#include <SwiftUI/SwiftUI_Metal.h>
using namespace metal;
// @param turn 0.7 0.0 1.0 How far the page turns alone; at 0 only a drag does
// @param radius 20.0 0.0 40.0 The radius of the roll of paper, in points
// @param bleed 0.3 0.0 1.0 How much of the print shows on the page's back
// @param shading 0.7 0.0 1.0 How much the light falls off round the roll
// @param shadow 0.6 0.0 1.0 How dark the shadow of the lifted page is
// @param gloss 0.5 0.0 1.0 How much the roll shines where it faces the light
// @param volume 0.7 0.0 1.0 How loud the paper is
// 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);
}
// A turn every 12 seconds: how far through it, 0 to 1.
float pageCycle(float t) {
return fract((t + 3.0) / 12.0);
}
// How far the page has lifted: up, held, then down.
float pageLift(float t) {
float c = pageCycle(t);
return smoothstep(0.0, 0.45, c) * smoothstep(1.0, 0.65, c);
}
// The clock's own pull on the corner, in points.
float2 pagePull(float t, float2 size, float turn) {
// Each of the five turns of a minute has its own way and reach.
int k = int(floor((t + 3.0) / 12.0)) % 5;
float a = k == 0 ? 0.12 : k == 1 ? 0.62 : k == 2 ? 0.85
: k == 3 ? 0.3 : 0.45;
float reach = k == 0 ? 1.0 : k == 1 ? 0.9 : k == 2 ? 0.95
: k == 3 ? 0.75 : 0.8;
// It swings lower as the turn goes on, as a hand does.
a += 0.2 * (0.5 - pageCycle(t));
float far = size.x * reach * turn * pageLift(t);
return -float2(cos(a), sin(a)) * far;
}
// How bright paper is, its seen side facing up (x toward the corner, y
// off the page): lit from above, and exactly 1 lying flat.
float pageLight(float2 up) {
float lit = max(dot(up, float2(0.447, 0.894)), 0.0);
return 0.3 + 0.7 * lit / 0.894;
}
// The back of the page: cream paper, the print showing through by bleed.
half4 pageBack(half4 c, float bleed) {
half3 paper = half3(1.0h, 0.953h, 0.871h) * c.a;
return half4(mix(paper, c.rgb, half(bleed)), c.a);
}
// The paper lifted over p, or clear; d is how far p is past the axis.
half4 pageAbove(SwiftUI::Layer layer, float2 p, float2 axis, float2 n,
float r, float bleed, float shading, float gloss) {
const float pi = 3.14159265;
float d = dot(p - axis, n);
if (d > r) return half4(0.0h);
if (d <= 0.0) {
// Over the roll and back: s past the axis lands pi r - s past.
float2 from = p + (pi * r - 2.0 * d) * n;
return pageBack(layer.sample(from), bleed);
}
// On the roll, at angle a from its foot: two layers of paper.
float a = asin(d / r);
// On top, the back, pi - a round.
float a2 = pi - a;
half4 back = pageBack(layer.sample(p + (r * a2 - d) * n), bleed);
float2 up2 = float2(sin(a2), -cos(a2));
back.rgb *= half(mix(1.0, pageLight(up2), shading));
// A shine where the roll faces halfway between the light and the eye.
float spec = pow(max(dot(up2, float2(0.23, 0.973)), 0.0), 120.0);
back.rgb += half(gloss * 0.8 * spec) * back.a;
// Beneath it, the front, a round.
half4 front = layer.sample(p + (r * a - d) * n);
float2 up1 = float2(-sin(a), cos(a));
front.rgb *= half(mix(1.0, pageLight(up1), shading));
// The roll's edge fades over a point.
return (back + front * (1.0h - back.a)) * half(clamp(r - d, 0.0, 1.0));
}
[[ stitchable ]] half4 pageCurl(float2 position, SwiftUI::Layer layer,
float2 size, float2 touch, float time,
float turn, float radius, float bleed,
float shading, float shadow, float gloss) {
float2 corner = size;
// The grab point: the drag, plus the clock's pull, which gives way
// as the drag takes the corner away from it.
float held = length(touch - corner) / size.x;
float2 pull = pagePull(time, size, turn);
pull *= 1.0 - smoothstep(0.0, 0.3, held);
float2 grab = min(touch + pull, corner);
float far = length(corner - grab);
if (far < 0.01) return layer.sample(position);
float2 n = (corner - grab) / far;
// At most, the page folds in half.
far = min(far, length(size));
float r = min(radius, far * 0.25);
// The axis, far enough back that the corner lands on the grab point.
float2 axis = corner - n * (far + 3.14159265 * r) * 0.5;
half4 top = pageAbove(layer, position, axis, n, r, bleed, shading, gloss);
// The page beneath, gone past the axis.
half4 below = layer.sample(position);
if (dot(position - axis, n) > 0.0) below = half4(0.0h);
// Its shadow: how much paper lies over 24 points nearby, a little
// toward the corner, the spiral turned at random for each pixel.
float cover = 0.0;
float2 toward = position + n * (r * 0.5 + 6.0);
float spin = 6.2832 * hash21(position * 8.0);
for (int i = 0; i < 24; i++) {
float s = 9.0 * sqrt((float(i) + 0.5) / 24.0);
float a = float(i) * 2.39996 + spin;
float2 q = toward + s * float2(cos(a), sin(a));
cover += float(pageAbove(layer, q, axis, n, r, 0.0, 0.0, 0.0).a);
}
below.rgb *= half(1.0 - shadow * 0.6 * cover / 24.0);
// Paper over page, as premultiplied colors stack.
return top + below * (1.0h - top.a);
}
float2 sound(float t, constant Params& p) {
// How fast the page is moving, from its lift a little either side.
float lift = pageLift(t);
float move = abs(pageLift(t + 0.02) - pageLift(t - 0.02)) / 0.04;
move = clamp(move * 3.0, 0.0, 1.0) * p.turn;
// Paper rustling: fast value noise read along the turn's own time, a
// different noise in each ear and each turn, fluttering on a slower
// noise as the sheet crinkles.
float since = pageCycle(t) * 12.0;
float seed = floor((t + 3.0) / 12.0) * 7.0;
float2 rustle = 0.0;
for (int ch = 0; ch < 2; ch++) {
float s = seed + float(ch) * 41.0;
float hiss = (vnoise(float2(since * 9000.0, s + 1.0)) - 0.5)
+ 0.7 * (vnoise(float2(since * 4100.0, s + 2.0)) - 0.5)
- 0.6 * (vnoise(float2(since * 900.0, s + 3.0)) - 0.5);
float flutter = vnoise(float2(since * 28.0, s + 4.0));
rustle[ch] = hiss * (0.25 + 1.2 * flutter * flutter);
}
// As the corner travels left it takes the rustle to the left.
float pan = -0.5 * lift * p.turn;
rustle *= move * float2(1.0 - pan, 1.0 + pan);
// A soft low tone, A and E, that swells as the page lifts.
float2 tone = float2(sin(6.2832 * 110.0 * t), sin(6.2832 * 164.81 * t));
tone *= (0.06 + 0.12 * lift * p.turn) * float2(1.0, 0.8);
// A soft pat as the page lands flat again at the end of each turn.
float pat = sin(6.2832 * 70.0 * since) * exp(-since * 16.0);
pat *= smoothstep(0.0, 0.004, since) * p.turn;
float2 v = (rustle * 0.9 + tone + pat * 0.5) * 1.25;
// tanh rounds off the loudest moments instead of letting them clip.
float2 o = tanh(v * 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.5, t);
return o * p.volume * fade;
}6 lines from stage 4 that are gone
float a = 0.2 + 0.2 * (0.5 - pageCycle(t));
float far = size.x * 0.75 * turn * pageLift(t);
⋯
float r, float bleed, float shading) {
⋯
float shading, float shadow) {
⋯
half4 top = pageAbove(layer, position, axis, n, r, bleed, shading);
⋯
cover += float(pageAbove(layer, q, axis, n, r, 0.0, 0.0).a);The sound
The page's own turning, read from the same pageLift and turn as the picture. The rustle is three value noises read along the turn's own time, at 9000, 4100 and 900 values a second, the slowest taken away to leave the hiss, made with the integer hash21, a different noise in each ear and each turn. A slower noise at 28 values a second flutters it, as a sheet crinkles. It is as loud as the page is moving: the lift's change over 40 ms, so the rustle comes as the page rises and as it falls, and stops while it holds. As the corner travels left, it takes the rustle to the left.
Under it is a soft low tone, A at 110 Hz and E at 164.81 Hz, that swells as the page lifts, and as each turn starts again, the page just landed flat, a soft pat at 70 Hz that dies in about a tenth of a second. A tanh rounds off the loudest moments, and the sound fades in over the first half second and out over the last. Stage 1 has the rustle alone.
None of this is SwiftUI's: the standalone file leaves sound out, and it can't hear your drag, only the clock. At the defaults the peak is about −6.5 dBFS and the average (RMS) about −20.3 dBFS over the minute; with turn and volume at 1, −3.0 dBFS and −15.0 dBFS.
New in Ray Kiln
A drag for SwiftUI effects. An effect that takes float2 touch now answers a press and drag on its picture. A press puts touch under the pointer, in points from the view's top left; it follows the drag, out past the view's edges too; and let go, it eases back to its rest place, its distance from it shrinking by exp(-dt / 0.2) a frame, a time constant of 0.2 s. Hovering doesn't move it. The rest place is a header line, // @touch x y, as fractions of the view's size from its top left; without one it rests in the middle, so every effect that doesn't say is as it was. Everything made without a hand on it, the stills, the videos on this page and the exercises, draws touch at its rest place, so a frame at a given time is always the same picture.
It works in the playground and under Run it live on these pages, where the arrow keys or W A S D push it too once the picture has the focus, and in the Mac app with its next release, where SwiftUI's own preview (Ray Kiln Pro) draws with the same touch. In the Swift package that Download for Xcode makes (Ray Kiln Pro) the modifier, .pageCurl(), tracks a DragGesture of its own and eases back the same way: a simultaneous gesture, so a Button under it still gets its taps. Give it touch: and it uses that point instead, with no gesture; tvOS has no drag, so there it rests unless given one. The SwiftUI effects page has the rest.
In this episode it is the page itself: press anywhere on the card under Run it live and the corner comes to the pointer; drag it round and the fold follows; let go and the page falls back to the corner, while the clock goes on turning it.
To use it in an app, add the standalone file to your app's target and apply the effect with the code under it. With .layerEffect yourself, touch is a CGPoint you keep, for example in a @State set from .simultaneousGesture(DragGesture(minimumDistance: 0).onChanged { touch = $0.location }), and, when the drag ends, brought back to the view's bottom right corner, CGPoint(x: size.width, y: size.height), at once or eased over a TimelineView as the package's modifier does. The location of a DragGesture in its .local space is in the view's points from its top left, as the shader takes it.
Try this
- Set
radiusto 0: the roll goes, and the page folds along a sharp crease. Then 40: a fat roll that takes up a lot of the paper. - Set
bleedto 0 for plain cream paper, then 1: the back of the page is the card itself, mirrored, as it was before stage 4. - Set
turnto 0: the page lies flat until you press and drag it. - Set
shadowto 0 and the page floats with no shadow; setglossto 1 and the line of light along the roll brightens. - Change
// @touch 1 1to// @touch 0.5 0.5: nowtouchrests in the middle, so the page rests folded with its corner on the middle of the card, and it stops turning by itself, since the clock's pull gives way whentouchis that far from the corner. - In
pageCurl, delete the linepull *= 1.0 - smoothstep(0.0, 0.3, held);, then press the middle of the card while it is turning: the clock's pull is added to your drag, and the corner lands well past the pointer, toward the top left. - In
pagePull, change% 5to% 1: every turn takes the first one's way, nearly straight to the left, like a book's page.
Parameters
Pick a parameter and drag the slider. Each frame is the same moment of the shader, with only that one setting changed.
| Parameter | Default | Range | What it does |
|---|---|---|---|
turn | 0.7 | 0.0 to 1.0 | How far the page turns alone; at 0 only a drag does |
radius | 20.0 | 0.0 to 40.0 | The radius of the roll of paper, in points |
bleed | 0.3 | 0.0 to 1.0 | How much of the print shows on the page's back |
shading | 0.7 | 0.0 to 1.0 | How much the light falls off round the roll |
shadow | 0.6 | 0.0 to 1.0 | How dark the shadow of the lifted page is |
gloss | 0.5 | 0.0 to 1.0 | How much the roll shines where it faces the light |
volume | 0.7 | 0.0 to 1.0 | How loud the paper is |
More from this shader
The same shader 8 more ways: each from a different moment, with different settings, one after another. Each chapter below says which settings moved most, then lists them all.
Wallpapers
A still from the shader, rendered at full size for a desktop screen. Free to use as your wallpaper.
A clip to post
Fifteen seconds of the shader with its sound, sized for posting: square, for feeds. Free to post, ideally with a link to this page.
Made from this episode
The full source
This is the whole episode: the effect, the parameters and the sound.
page-curl.metal
// Ray Kiln · Page Curl
//
// @episode Page Curl
// @length 60
// @variation 30
// @variations 3,4,6,7,8,9,11,12
// @still 16.0
// @teaches A distortion effect that follows a drag: the page lifts where you pull
// @category SwiftUI Effect
// @tags swiftui, layer effect, page curl, touch, drag gesture, cylinder, premultiplied alpha, procedural sound
// @final Page curl | The page turns by itself, five times a minute and each time a different way, with a shine along the roll; press and drag it and the corner follows the pointer, then falls back when let go.
//
// A page curl is a SwiftUI layer effect: a function SwiftUI runs once for
// every point of a view, which reads the view itself through layer.sample.
// The page's bottom right corner is pulled to a grab point. The fold is the
// line halfway between the two, and past it the paper rolls round a cylinder
// and lies back over the page, showing its back: the view mirrored, as paper
// shows print through it. Where the page has lifted the view is clear. The
// grab point is touch, which follows a drag on the view and rests at the
// corner, plus a pull of the effect's own on the clock.
//
// @subject card
// @backdrop dark
// @touch 1 1 The page's bottom right corner
// @maxSampleOffset 40 40
#include <metal_stdlib>
#include <SwiftUI/SwiftUI_Metal.h>
using namespace metal;
// @param turn 0.7 0.0 1.0 How far the page turns alone; at 0 only a drag does
// @param radius 20.0 0.0 40.0 The radius of the roll of paper, in points
// @param bleed 0.3 0.0 1.0 How much of the print shows on the page's back
// @param shading 0.7 0.0 1.0 How much the light falls off round the roll
// @param shadow 0.6 0.0 1.0 How dark the shadow of the lifted page is
// @param gloss 0.5 0.0 1.0 How much the roll shines where it faces the light
// @param volume 0.7 0.0 1.0 How loud the paper is
// 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);
}
// A turn every 12 seconds: how far through it, 0 to 1.
float pageCycle(float t) {
return fract((t + 3.0) / 12.0);
}
// How far the page has lifted: up, held, then down.
float pageLift(float t) {
float c = pageCycle(t);
return smoothstep(0.0, 0.45, c) * smoothstep(1.0, 0.65, c);
}
// The clock's own pull on the corner, in points.
float2 pagePull(float t, float2 size, float turn) {
// Each of the five turns of a minute has its own way and reach.
int k = int(floor((t + 3.0) / 12.0)) % 5;
float a = k == 0 ? 0.12 : k == 1 ? 0.62 : k == 2 ? 0.85
: k == 3 ? 0.3 : 0.45;
float reach = k == 0 ? 1.0 : k == 1 ? 0.9 : k == 2 ? 0.95
: k == 3 ? 0.75 : 0.8;
// It swings lower as the turn goes on, as a hand does.
a += 0.2 * (0.5 - pageCycle(t));
float far = size.x * reach * turn * pageLift(t);
return -float2(cos(a), sin(a)) * far;
}
// How bright paper is, its seen side facing up (x toward the corner, y
// off the page): lit from above, and exactly 1 lying flat.
float pageLight(float2 up) {
float lit = max(dot(up, float2(0.447, 0.894)), 0.0);
return 0.3 + 0.7 * lit / 0.894;
}
// The back of the page: cream paper, the print showing through by bleed.
half4 pageBack(half4 c, float bleed) {
half3 paper = half3(1.0h, 0.953h, 0.871h) * c.a;
return half4(mix(paper, c.rgb, half(bleed)), c.a);
}
// The paper lifted over p, or clear; d is how far p is past the axis.
half4 pageAbove(SwiftUI::Layer layer, float2 p, float2 axis, float2 n,
float r, float bleed, float shading, float gloss) {
const float pi = 3.14159265;
float d = dot(p - axis, n);
if (d > r) return half4(0.0h);
if (d <= 0.0) {
// Over the roll and back: s past the axis lands pi r - s past.
float2 from = p + (pi * r - 2.0 * d) * n;
return pageBack(layer.sample(from), bleed);
}
// On the roll, at angle a from its foot: two layers of paper.
float a = asin(d / r);
// On top, the back, pi - a round.
float a2 = pi - a;
half4 back = pageBack(layer.sample(p + (r * a2 - d) * n), bleed);
float2 up2 = float2(sin(a2), -cos(a2));
back.rgb *= half(mix(1.0, pageLight(up2), shading));
// A shine where the roll faces halfway between the light and the eye.
float spec = pow(max(dot(up2, float2(0.23, 0.973)), 0.0), 120.0);
back.rgb += half(gloss * 0.8 * spec) * back.a;
// Beneath it, the front, a round.
half4 front = layer.sample(p + (r * a - d) * n);
float2 up1 = float2(-sin(a), cos(a));
front.rgb *= half(mix(1.0, pageLight(up1), shading));
// The roll's edge fades over a point.
return (back + front * (1.0h - back.a)) * half(clamp(r - d, 0.0, 1.0));
}
[[ stitchable ]] half4 pageCurl(float2 position, SwiftUI::Layer layer,
float2 size, float2 touch, float time,
float turn, float radius, float bleed,
float shading, float shadow, float gloss) {
float2 corner = size;
// The grab point: the drag, plus the clock's pull, which gives way
// as the drag takes the corner away from it.
float held = length(touch - corner) / size.x;
float2 pull = pagePull(time, size, turn);
pull *= 1.0 - smoothstep(0.0, 0.3, held);
float2 grab = min(touch + pull, corner);
float far = length(corner - grab);
if (far < 0.01) return layer.sample(position);
float2 n = (corner - grab) / far;
// At most, the page folds in half.
far = min(far, length(size));
float r = min(radius, far * 0.25);
// The axis, far enough back that the corner lands on the grab point.
float2 axis = corner - n * (far + 3.14159265 * r) * 0.5;
half4 top = pageAbove(layer, position, axis, n, r, bleed, shading, gloss);
// The page beneath, gone past the axis.
half4 below = layer.sample(position);
if (dot(position - axis, n) > 0.0) below = half4(0.0h);
// Its shadow: how much paper lies over 24 points nearby, a little
// toward the corner, the spiral turned at random for each pixel.
float cover = 0.0;
float2 toward = position + n * (r * 0.5 + 6.0);
float spin = 6.2832 * hash21(position * 8.0);
for (int i = 0; i < 24; i++) {
float s = 9.0 * sqrt((float(i) + 0.5) / 24.0);
float a = float(i) * 2.39996 + spin;
float2 q = toward + s * float2(cos(a), sin(a));
cover += float(pageAbove(layer, q, axis, n, r, 0.0, 0.0, 0.0).a);
}
below.rgb *= half(1.0 - shadow * 0.6 * cover / 24.0);
// Paper over page, as premultiplied colors stack.
return top + below * (1.0h - top.a);
}
float2 sound(float t, constant Params& p) {
// How fast the page is moving, from its lift a little either side.
float lift = pageLift(t);
float move = abs(pageLift(t + 0.02) - pageLift(t - 0.02)) / 0.04;
move = clamp(move * 3.0, 0.0, 1.0) * p.turn;
// Paper rustling: fast value noise read along the turn's own time, a
// different noise in each ear and each turn, fluttering on a slower
// noise as the sheet crinkles.
float since = pageCycle(t) * 12.0;
float seed = floor((t + 3.0) / 12.0) * 7.0;
float2 rustle = 0.0;
for (int ch = 0; ch < 2; ch++) {
float s = seed + float(ch) * 41.0;
float hiss = (vnoise(float2(since * 9000.0, s + 1.0)) - 0.5)
+ 0.7 * (vnoise(float2(since * 4100.0, s + 2.0)) - 0.5)
- 0.6 * (vnoise(float2(since * 900.0, s + 3.0)) - 0.5);
float flutter = vnoise(float2(since * 28.0, s + 4.0));
rustle[ch] = hiss * (0.25 + 1.2 * flutter * flutter);
}
// As the corner travels left it takes the rustle to the left.
float pan = -0.5 * lift * p.turn;
rustle *= move * float2(1.0 - pan, 1.0 + pan);
// A soft low tone, A and E, that swells as the page lifts.
float2 tone = float2(sin(6.2832 * 110.0 * t), sin(6.2832 * 164.81 * t));
tone *= (0.06 + 0.12 * lift * p.turn) * float2(1.0, 0.8);
// A soft pat as the page lands flat again at the end of each turn.
float pat = sin(6.2832 * 70.0 * since) * exp(-since * 16.0);
pat *= smoothstep(0.0, 0.004, since) * p.turn;
float2 v = (rustle * 0.9 + tone + pat * 0.5) * 1.25;
// tanh rounds off the loudest moments instead of letting them clip.
float2 o = tanh(v * 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.5, t);
return o * p.volume * fade;
}Running it in your own project
This shader is a SwiftUI effect: a [[ stitchable ]] function SwiftUI runs on a view’s pixels. The standalone download above is the file as SwiftUI takes it. Add it to your app’s target, and Xcode compiles it into the app’s Metal library, where ShaderLibrary.pageCurl finds it. Its sound is Ray Kiln’s alone: SwiftUI has no use for it, so the standalone file leaves it out.
Then apply it to any view with .layerEffect. Flatten the view first with .compositingGroup(), so the effect sees one picture rather than each layer of the view on its own. The numbers below are the sliders’ defaults; start is when the view appeared, a Date you keep, and size the view’s size, from a GeometryReader or .visualEffect. touch is a CGPoint you keep, in the view’s points from its top left: a DragGesture’s location while a finger is down, and the place the effect rests at otherwise.
Using it, in SwiftUI
TimelineView(.animation) { context in
let time = context.date.timeIntervalSince(start)
yourView
.compositingGroup()
.layerEffect(
ShaderLibrary.pageCurl(.float2(size), .float2(touch), .float(time), .float(0.7), .float(20.0), .float(0.3), .float(0.7), .float(0.6), .float(0.5)),
maxSampleOffset: CGSize(width: 40, height: 40)
)
}Positions are in points from the view’s top left, as SwiftUI measures them, and maxSampleOffset is how far past its edges the effect may reach. The SwiftUI effects page has more of them, and the playground runs any on SwiftUI’s own pictures.
More in SwiftUI Effects
Press ? for keyboard shortcuts.

