neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Android log-in: Eyes, the default, looking now and then at your finger
6b9b29d · gmorell · 2026-12-03 22:22
Message
{commit_body(@commit)}
Files changed
added
android/app/src/main/assets/eyes.frag
+196
−0
| 1 | +// Eyes (their lives run on the CPU, in the site's shader_eyes.js): | |
| 2 | +// each opens from a line where it pleases, blinks and looks about, now | |
| 3 | +// and then at the pointer, then closes back to a line that fades. | |
| 4 | +// An almond of lids, a sclera, an iris with a darker ring and a pupil, | |
| 5 | +// and a glint. The shader only draws; openness, gaze and fade arrive | |
| 6 | +// per eye. | |
| 7 | +#version 440 | |
| 8 | + | |
| 9 | +layout(location = 0) in vec2 qt_TexCoord0; | |
| 10 | +layout(location = 0) out vec4 fragColor; | |
| 11 | + | |
| 12 | +layout(std140, binding = 0) uniform buf { | |
| 13 | + mat4 qt_Matrix; | |
| 14 | + float qt_Opacity; | |
| 15 | + | |
| 16 | + vec2 size; // pixels | |
| 17 | + // eye0-eye23: x, y, radius (half the eye's width, shorter side | |
| 18 | + // units, y down), openness 0-1 | |
| 19 | + vec4 eye0; | |
| 20 | + vec4 eye1; | |
| 21 | + vec4 eye2; | |
| 22 | + vec4 eye3; | |
| 23 | + vec4 eye4; | |
| 24 | + vec4 eye5; | |
| 25 | + vec4 eye6; | |
| 26 | + vec4 eye7; | |
| 27 | + vec4 eye8; | |
| 28 | + vec4 eye9; | |
| 29 | + vec4 eye10; | |
| 30 | + vec4 eye11; | |
| 31 | + vec4 eye12; | |
| 32 | + vec4 eye13; | |
| 33 | + vec4 eye14; | |
| 34 | + vec4 eye15; | |
| 35 | + vec4 eye16; | |
| 36 | + vec4 eye17; | |
| 37 | + vec4 eye18; | |
| 38 | + vec4 eye19; | |
| 39 | + vec4 eye20; | |
| 40 | + vec4 eye21; | |
| 41 | + vec4 eye22; | |
| 42 | + vec4 eye23; | |
| 43 | + // look0-look23: gaze x, y (-1 to 1, the iris's full travel), | |
| 44 | + // shade 0-1, fade 0-1 (the whole eye, line included) | |
| 45 | + vec4 look0; | |
| 46 | + vec4 look1; | |
| 47 | + vec4 look2; | |
| 48 | + vec4 look3; | |
| 49 | + vec4 look4; | |
| 50 | + vec4 look5; | |
| 51 | + vec4 look6; | |
| 52 | + vec4 look7; | |
| 53 | + vec4 look8; | |
| 54 | + vec4 look9; | |
| 55 | + vec4 look10; | |
| 56 | + vec4 look11; | |
| 57 | + vec4 look12; | |
| 58 | + vec4 look13; | |
| 59 | + vec4 look14; | |
| 60 | + vec4 look15; | |
| 61 | + vec4 look16; | |
| 62 | + vec4 look17; | |
| 63 | + vec4 look18; | |
| 64 | + vec4 look19; | |
| 65 | + vec4 look20; | |
| 66 | + vec4 look21; | |
| 67 | + vec4 look22; | |
| 68 | + vec4 look23; | |
| 69 | + float count; // how many of eyes are in use | |
| 70 | + float breath; // near 1; the pupils widen a little as it falls | |
| 71 | + float grain; // 0-1, film grain | |
| 72 | + float dither; // 0-1, dither | |
| 73 | + float seed; // 0-1, changes each frame to move the grain | |
| 74 | + | |
| 75 | + vec4 base; // the ground | |
| 76 | + vec4 deep; // the ground at the edges | |
| 77 | + vec4 ink; | |
| 78 | + vec4 tone; | |
| 79 | +}; | |
| 80 | + | |
| 81 | +const float UPPER = 0.55; // the open lids' heights, in radii | |
| 82 | +const float LOWER = 0.42; | |
| 83 | + | |
| 84 | +float hash12(vec2 p) | |
| 85 | +{ | |
| 86 | + vec3 p3 = fract(vec3(p.xyx) * 0.1031); | |
| 87 | + p3 += dot(p3, p3.yzx + 33.33); | |
| 88 | + return fract((p3.x + p3.y) * p3.z); | |
| 89 | +} | |
| 90 | + | |
| 91 | +// Distance from q to a lid: the curve y = h (1 - x^2) for |x| <= 1, | |
| 92 | +// its corners beyond. Signed across the curve, > 0 below it. | |
| 93 | +float lid(vec2 q, float h) | |
| 94 | +{ | |
| 95 | + float y = h * (1.0 - q.x * q.x); | |
| 96 | + float slope = -2.0 * h * q.x; | |
| 97 | + return (q.y - y) / sqrt(1.0 + slope * slope); | |
| 98 | +} | |
| 99 | + | |
| 100 | +float stroke(vec2 q, float h, float width, float pe) | |
| 101 | +{ | |
| 102 | + float d = abs(q.x) <= 1.0 ? abs(lid(q, h)) : length(q - vec2(sign(q.x), 0.0)); | |
| 103 | + float x = clamp(q.x, -1.0, 1.0); | |
| 104 | + // Tapering to the corners, never under a pixel or so. | |
| 105 | + float w = max(0.6 * pe, width * (0.3 + 0.7 * (1.0 - x * x))); | |
| 106 | + return clamp((w - d) / pe + 0.5, 0.0, 1.0); | |
| 107 | +} | |
| 108 | + | |
| 109 | +void main() | |
| 110 | +{ | |
| 111 | + float px = 1.0 / min(size.x, size.y); | |
| 112 | + vec2 extent = size * px; | |
| 113 | + vec2 p = qt_TexCoord0 * extent; | |
| 114 | + | |
| 115 | + // Gathered into arrays the loop can index; they arrive as separate | |
| 116 | + // uniforms because Qt's ShaderEffect has no array properties. | |
| 117 | + vec4 eyes[24] = vec4[24](eye0, eye1, eye2, eye3, eye4, eye5, eye6, eye7, eye8, eye9, eye10, eye11, eye12, eye13, eye14, eye15, eye16, eye17, eye18, eye19, eye20, eye21, eye22, eye23); | |
| 118 | + vec4 looks[24] = vec4[24](look0, look1, look2, look3, look4, look5, look6, look7, look8, look9, look10, look11, look12, look13, look14, look15, look16, look17, look18, look19, look20, look21, look22, look23); | |
| 119 | + | |
| 120 | + float v = smoothstep(0.35, 1.25, length(p - 0.5 * extent)); | |
| 121 | + vec3 colour = mix(base.rgb, deep.rgb, v); | |
| 122 | + | |
| 123 | + // On a light ground the eyes are drawn in ink, with white sclerae; | |
| 124 | + // on a dark one their whites are the ink and the lines go to tone. | |
| 125 | + vec3 lum = vec3(0.2126, 0.7152, 0.0722); | |
| 126 | + bool lightGround = dot(base.rgb, lum) > dot(ink.rgb, lum); | |
| 127 | + vec3 sclera = lightGround ? mix(base.rgb, vec3(1.0), 0.7) : mix(ink.rgb, base.rgb, 0.12); | |
| 128 | + vec3 dark = lightGround ? ink.rgb : deep.rgb * 0.25; | |
| 129 | + vec3 line = lightGround ? ink.rgb : mix(ink.rgb, tone.rgb, 0.4); | |
| 130 | + | |
| 131 | + for (int i = 0; i < 24; i++) { | |
| 132 | + if (float(i) >= count) | |
| 133 | + break; | |
| 134 | + vec4 e = eyes[i]; | |
| 135 | + vec4 l = looks[i]; | |
| 136 | + if (e.z <= 0.0 || l.w <= 0.0) | |
| 137 | + continue; | |
| 138 | + vec2 q = (p - e.xy) / e.z; | |
| 139 | + if (abs(q.x) > 1.3 || abs(q.y) > 1.0) | |
| 140 | + continue; | |
| 141 | + float pe = px / e.z; // a pixel, in this eye's units | |
| 142 | + float open = e.w; | |
| 143 | + | |
| 144 | + // Between the lids: the upper above, the lower below. | |
| 145 | + float top = lid(q, -UPPER * open); | |
| 146 | + float bottom = -lid(q, LOWER * open); | |
| 147 | + float inside = clamp(min(top, bottom) / pe + 0.5, 0.0, 1.0) * clamp(open * 30.0, 0.0, 1.0); | |
| 148 | + | |
| 149 | + if (inside > 0.0) { | |
| 150 | + // The white, shaded under the upper lid and into the corners. | |
| 151 | + vec3 c = sclera * (1.0 - 0.28 * exp(-max(top, 0.0) / 0.1)) * (1.0 - 0.18 * q.x * q.x); | |
| 152 | + | |
| 153 | + vec2 ic = l.xy * vec2(0.42, 0.2); | |
| 154 | + vec2 r = q - ic; | |
| 155 | + float d = length(r); | |
| 156 | + float ri = 0.4; | |
| 157 | + float rp = 0.15 * (2.0 - breath); | |
| 158 | + float ang = atan(r.y, r.x); | |
| 159 | + vec3 iris = mix(tone.rgb, ink.rgb, 0.08 + 0.3 * l.z); | |
| 160 | + // Kept well darker than the white, whatever the theme. | |
| 161 | + iris *= clamp((dot(sclera, lum) - 0.3) / max(dot(iris, lum), 0.01), 0.35, 1.0); | |
| 162 | + float fibres = 0.84 + 0.16 * sin(ang * 23.0 + 4.0 * sin(ang * 7.0 + 6.2831853 * l.z)); | |
| 163 | + vec3 ic3 = iris * mix(1.0, fibres, smoothstep(rp, ri, d)); | |
| 164 | + ic3 *= 1.0 - 0.3 * smoothstep(0.6 * ri, ri, d); | |
| 165 | + ic3 += 0.18 * iris * exp(-pow((d - rp * 1.45) / 0.05, 2.0)); | |
| 166 | + ic3 = mix(ic3, dark, 0.65 * smoothstep(ri - 0.08, ri, d)); | |
| 167 | + ic3 *= 1.0 - 0.3 * exp(-max(top, 0.0) / 0.1); | |
| 168 | + c = mix(c, ic3, clamp((ri - d) / pe + 0.5, 0.0, 1.0)); | |
| 169 | + c = mix(c, dark, clamp((rp - d) / pe + 0.5, 0.0, 1.0)); | |
| 170 | + | |
| 171 | + // A window's glint, up and to the left, and a faint one opposite. | |
| 172 | + float g = length(r - vec2(-0.13, -0.14)); | |
| 173 | + float g2 = length(r - vec2(0.11, 0.1)); | |
| 174 | + c = mix(c, vec3(1.0), 0.9 * clamp((0.055 - g) / pe + 0.5, 0.0, 1.0)); | |
| 175 | + c = mix(c, vec3(1.0), 0.35 * clamp((0.025 - g2) / pe + 0.5, 0.0, 1.0)); | |
| 176 | + | |
| 177 | + colour = mix(colour, c, inside * l.w); | |
| 178 | + } | |
| 179 | + | |
| 180 | + // The lids' edges, which meet in a line when shut; the upper heavier. | |
| 181 | + float w = 0.035; | |
| 182 | + float edge = max(stroke(q, -UPPER * open, w, pe), stroke(q, LOWER * open, 0.6 * w, pe)); | |
| 183 | + // And a faint crease above the open eye. | |
| 184 | + vec2 qc = vec2(q.x / 0.85, q.y); | |
| 185 | + float crease = abs(qc.x) < 1.0 ? stroke(qc, -(UPPER + 0.16) * open, 0.4 * w, pe) * (1.0 - qc.x * qc.x) : 0.0; | |
| 186 | + colour = mix(colour, line, max(edge, 0.35 * open * crease) * l.w); | |
| 187 | + } | |
| 188 | + | |
| 189 | + vec2 pixel = qt_TexCoord0 * size; | |
| 190 | + if (grain > 0.0) | |
| 191 | + colour += (hash12(pixel + seed * 1000.0) - 0.5) * 0.06 * grain; | |
| 192 | + if (dither > 0.0) | |
| 193 | + colour += (hash12(pixel + 0.5) + hash12(pixel + 91.3) - 1.0) / 255.0 * dither; | |
| 194 | + | |
| 195 | + fragColor = vec4(colour, 1.0) * qt_Opacity; | |
| 196 | +} |
modified
android/app/src/main/java/org/neiam/vigil/app/auth/login/LoginScreen.kt
+8
−3
| 62 | 62 | import dev.chrisbanes.haze.hazeSource |
| 63 | 63 | import org.neiam.vigil.app.ui.shader.ShaderBackground |
| 64 | 64 | import org.neiam.vigil.app.ui.shader.ShaderColours |
| 65 | +import org.neiam.vigil.app.ui.shader.ShaderPointer | |
| 65 | 66 | import org.neiam.vigil.app.ui.shader.ShaderSettings |
| 67 | +import org.neiam.vigil.app.ui.shader.shaderPointer | |
| 66 | 68 | |
| 67 | 69 | /** The header's share of the height (portrait) or width (landscape): 20–25%. */ |
| 68 | 70 | private const val HEADER_WEIGHT = 0.22f |
| 360 | 362 | /** |
| 361 | 363 | * The page behind the log-in page: the shader under a veil of the page |
| 362 | 364 | * colour, which haze blurs behind the buttons, then [content] over it on a |
| 363 | − * transparent Surface. | |
| 365 | + * transparent Surface. Touches anywhere on the page are heard, not taken, | |
| 366 | + * for the eyes to look at. | |
| 364 | 367 | */ |
| 365 | 368 | @Composable |
| 366 | 369 | private fun LoginBackdrop(haze: HazeState, content: @Composable () -> Unit) { |
| 368 | 371 | val colours = remember(scheme.background, scheme.primary, scheme.secondary, scheme.tertiary) { |
| 369 | 372 | ShaderColours.from(scheme) |
| 370 | 373 | } |
| 371 | − Box(Modifier.fillMaxSize()) { | |
| 374 | + val settings = remember { ShaderSettings() } | |
| 375 | + val pointer = remember { ShaderPointer() } | |
| 376 | + Box(Modifier.fillMaxSize().shaderPointer(pointer, enabled = settings.pattern == "eyes")) { | |
| 372 | 377 | BoxWithConstraints(Modifier.fillMaxSize().hazeSource(haze)) { |
| 373 | − ShaderBackground(ShaderSettings(), colours, Modifier.fillMaxSize()) | |
| 378 | + ShaderBackground(settings, colours, Modifier.fillMaxSize(), pointer) | |
| 374 | 379 | // The page's test for landscape, near enough: this box |
| 375 | 380 | // includes the insets. |
| 376 | 381 | val landscape = maxWidth > maxHeight && maxWidth >= 480.dp |
added
android/app/src/main/java/org/neiam/vigil/app/ui/shader/Eyes.kt
+337
−0
| 1 | +package org.neiam.vigil.app.ui.shader | |
| 2 | + | |
| 3 | +import kotlin.math.PI | |
| 4 | +import kotlin.math.cos | |
| 5 | +import kotlin.math.exp | |
| 6 | +import kotlin.math.hypot | |
| 7 | +import kotlin.math.max | |
| 8 | +import kotlin.math.min | |
| 9 | +import kotlin.math.pow | |
| 10 | +import kotlin.math.roundToInt | |
| 11 | +import kotlin.math.sin | |
| 12 | +import kotlin.math.sqrt | |
| 13 | +import java.util.Locale | |
| 14 | +import kotlin.random.Random | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * The moving parts of the Eyes pattern: neiam-org's assets/js/shader_eyes.js, | |
| 18 | + * ported line for line; keep the two in step (the TIMES, ATTENTION and | |
| 19 | + * [sizes] above all). Each eye lives a cycle: it picks a free spot and a | |
| 20 | + * size, appears there as a line (its closed lids' seam), opens, stays open | |
| 21 | + * 10 to 60 s blinking and looking about, slowly closes back to a line, and | |
| 22 | + * the line fades. Then it starts over somewhere else. While open it | |
| 23 | + * sometimes looks at the pointer, in spells, rather than always. | |
| 24 | + * | |
| 25 | + * Times are in Motion's clock (seconds times the speed setting), positions | |
| 26 | + * in units of the surface's shorter side, y down, as eyes.frag has them. | |
| 27 | + * They are handed over as separate vec4 uniforms (Qt's ShaderEffect can't | |
| 28 | + * pass arrays), eye0 to eye23 and look0 to look23: | |
| 29 | + * eyeN: x, y, radius (half the eye's width), openness 0-1 (blinks included) | |
| 30 | + * lookN: gaze x, gaze y (-1 to 1, the iris's full travel), shade 0-1, fade 0-1 | |
| 31 | + * Unused ones are zero. | |
| 32 | + */ | |
| 33 | +class Eyes(private val random: Random = Random.Default) { | |
| 34 | + | |
| 35 | + enum class State { WAIT, APPEAR, OPENING, OPEN, CLOSING, VANISH } | |
| 36 | + | |
| 37 | + /** Where the pointer is, in shader units, and whether it has moved | |
| 38 | + * lately (the web's `{ x, y, active }`). */ | |
| 39 | + data class Pointer(val x: Double, val y: Double, val active: Boolean) | |
| 40 | + | |
| 41 | + class Eye internal constructor( | |
| 42 | + var state: State, | |
| 43 | + var timer: Double, | |
| 44 | + var shade: Double, | |
| 45 | + var nextBlink: Double, | |
| 46 | + var nextGlance: Double, | |
| 47 | + ) { | |
| 48 | + var x = 0.0 | |
| 49 | + var y = 0.0 | |
| 50 | + var r = 0.0 | |
| 51 | + var open = 0.0 | |
| 52 | + var fade = 0.0 | |
| 53 | + var gazeX = 0.0 | |
| 54 | + var gazeY = 0.0 | |
| 55 | + var targetX = 0.0 | |
| 56 | + var targetY = 0.0 | |
| 57 | + /** Seconds into a blink, or -1 while not blinking. */ | |
| 58 | + var blink = -1.0 | |
| 59 | + /** Seconds left of a spell of looking at the pointer. */ | |
| 60 | + var attending = 0.0 | |
| 61 | + internal var drop = false | |
| 62 | + } | |
| 63 | + | |
| 64 | + var list = mutableListOf<Eye>() | |
| 65 | + private set | |
| 66 | + private var settled: String? = null | |
| 67 | + private var pointerWas = false | |
| 68 | + | |
| 69 | + private fun rand(lo: Double, hi: Double) = lo + random.nextDouble() * (hi - lo) | |
| 70 | + private fun rand(range: ClosedFloatingPointRange<Double>) = rand(range.start, range.endInclusive) | |
| 71 | + | |
| 72 | + /** A fresh eye, unseen, waiting [delay] (else a random wait) before it | |
| 73 | + * takes a spot. */ | |
| 74 | + private fun make(delay: Double? = null) = Eye( | |
| 75 | + state = State.WAIT, | |
| 76 | + timer = delay ?: rand(TIMES.wait), | |
| 77 | + shade = random.nextDouble(), | |
| 78 | + nextBlink = rand(TIMES.blinkEvery), | |
| 79 | + nextGlance = rand(TIMES.glance), | |
| 80 | + ) | |
| 81 | + | |
| 82 | + private fun reset(e: Eye, fresh: Eye) { | |
| 83 | + e.state = fresh.state; e.timer = fresh.timer; e.nextBlink = fresh.nextBlink; e.nextGlance = fresh.nextGlance | |
| 84 | + e.x = 0.0; e.y = 0.0; e.r = 0.0; e.open = 0.0; e.fade = 0.0 | |
| 85 | + e.gazeX = 0.0; e.gazeY = 0.0; e.targetX = 0.0; e.targetY = 0.0 | |
| 86 | + e.blink = -1.0; e.attending = 0.0 | |
| 87 | + // The shade is kept: `{ shade: e.shade }` on the web. | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** Somewhere free for an eye of a random size, or false if none was | |
| 91 | + * found (it waits a moment and tries again). */ | |
| 92 | + private fun place(e: Eye, w: Double, h: Double, n: Int, ratio: Double): Boolean { | |
| 93 | + val (lo, hi) = sizes(n, ratio) | |
| 94 | + repeat(40) { | |
| 95 | + // Log-uniform, so small and large are equally common by ratio. | |
| 96 | + val r = lo * (hi / lo).pow(random.nextDouble()) | |
| 97 | + val mx = r * 1.15 | |
| 98 | + val my = r * 0.75 | |
| 99 | + val x = rand(mx, max(mx, w - mx)) | |
| 100 | + val y = rand(my, max(my, h - my)) | |
| 101 | + val clear = list.all { o -> | |
| 102 | + o === e || o.state == State.WAIT || hypot(o.x - x, o.y - y) > (reach(o) + r * 1.15) * 1.1 | |
| 103 | + } | |
| 104 | + if (clear) { | |
| 105 | + e.x = x | |
| 106 | + e.y = y | |
| 107 | + e.r = r | |
| 108 | + return true | |
| 109 | + } | |
| 110 | + } | |
| 111 | + return false | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** One step of [dt] seconds (Motion's clock) on a surface [w] by [h] | |
| 115 | + * shader units, for [count] eyes, the largest [ratio] times the | |
| 116 | + * smallest. [pointer]: in shader units, or null. */ | |
| 117 | + fun step(dt: Double, w: Double, h: Double, count: Int, ratio: Double, pointer: Pointer? = null) { | |
| 118 | + val n = count.coerceIn(1, MAX) | |
| 119 | + settled = null | |
| 120 | + // The first ones staggered, so they don't all open together. | |
| 121 | + while (list.size < n) list += make(if (list.isNotEmpty()) rand(0.0, 4.0) else 0.3) | |
| 122 | + // Too many: the extra ones close now, and are dropped once gone. | |
| 123 | + for (i in n until list.size) { | |
| 124 | + val e = list[i] | |
| 125 | + if (e.state == State.OPEN) { | |
| 126 | + e.state = State.CLOSING | |
| 127 | + e.timer = TIMES.closing | |
| 128 | + } else if (e.state == State.WAIT) { | |
| 129 | + e.drop = true | |
| 130 | + } | |
| 131 | + } | |
| 132 | + | |
| 133 | + val now = if (pointer != null && pointer.active) pointer else null | |
| 134 | + val fresh = now != null && !pointerWas | |
| 135 | + pointerWas = now != null | |
| 136 | + | |
| 137 | + for (e in list) live(e, dt, w, h, n, ratio, now, fresh) | |
| 138 | + list.removeAll { it.drop } | |
| 139 | + } | |
| 140 | + | |
| 141 | + private fun live(e: Eye, dt: Double, w: Double, h: Double, n: Int, ratio: Double, pointer: Pointer?, fresh: Boolean) { | |
| 142 | + e.timer -= dt | |
| 143 | + when (e.state) { | |
| 144 | + State.WAIT -> run { | |
| 145 | + if (e.timer > 0) return@run | |
| 146 | + if (list.indexOf(e) >= n) { | |
| 147 | + e.drop = true | |
| 148 | + return@run | |
| 149 | + } | |
| 150 | + if (!place(e, w, h, n, ratio)) { | |
| 151 | + e.timer = 0.5 | |
| 152 | + return@run | |
| 153 | + } | |
| 154 | + e.state = State.APPEAR | |
| 155 | + e.timer = TIMES.appear | |
| 156 | + e.open = 0.0 | |
| 157 | + e.fade = 0.0 | |
| 158 | + e.shade = random.nextDouble() | |
| 159 | + e.gazeX = 0.0; e.gazeY = 0.0 | |
| 160 | + e.targetX = 0.0; e.targetY = 0.0 | |
| 161 | + } | |
| 162 | + State.APPEAR -> { | |
| 163 | + e.fade = smooth(min(1.0, 1 - e.timer / TIMES.appear)) | |
| 164 | + if (e.timer <= 0) { | |
| 165 | + e.state = State.OPENING | |
| 166 | + e.timer = TIMES.opening | |
| 167 | + e.fade = 1.0 | |
| 168 | + } | |
| 169 | + } | |
| 170 | + State.OPENING -> { | |
| 171 | + e.open = smooth(min(1.0, 1 - e.timer / TIMES.opening)) | |
| 172 | + if (e.timer <= 0) { | |
| 173 | + e.state = State.OPEN | |
| 174 | + e.timer = rand(TIMES.open) | |
| 175 | + e.open = 1.0 | |
| 176 | + } | |
| 177 | + } | |
| 178 | + State.OPEN -> { | |
| 179 | + if (e.timer <= 0) { | |
| 180 | + e.state = State.CLOSING | |
| 181 | + e.timer = TIMES.closing | |
| 182 | + } | |
| 183 | + } | |
| 184 | + State.CLOSING -> { | |
| 185 | + e.open = smooth(max(0.0, e.timer / TIMES.closing)) | |
| 186 | + if (e.timer <= 0) { | |
| 187 | + e.state = State.VANISH | |
| 188 | + e.timer = TIMES.vanish | |
| 189 | + e.open = 0.0 | |
| 190 | + } | |
| 191 | + } | |
| 192 | + State.VANISH -> { | |
| 193 | + e.fade = smooth(max(0.0, e.timer / TIMES.vanish)) | |
| 194 | + if (e.timer <= 0) { | |
| 195 | + if (list.indexOf(e) >= n) e.drop = true else reset(e, make()) | |
| 196 | + } | |
| 197 | + } | |
| 198 | + } | |
| 199 | + if (e.state == State.OPEN || e.state == State.OPENING || e.state == State.CLOSING) look(e, dt, pointer, fresh) | |
| 200 | + // Blinks while open, each its own time: shut and open in 150 ms, | |
| 201 | + // now and then twice. | |
| 202 | + if (e.state == State.OPEN) { | |
| 203 | + if (e.blink >= 0) { | |
| 204 | + e.blink += dt | |
| 205 | + if (e.blink >= TIMES.blink) { | |
| 206 | + e.blink = -1.0 | |
| 207 | + e.nextBlink = if (random.nextDouble() < 0.12) 0.12 else rand(TIMES.blinkEvery) | |
| 208 | + } | |
| 209 | + } else { | |
| 210 | + e.nextBlink -= dt | |
| 211 | + if (e.nextBlink <= 0) e.blink = 0.0 | |
| 212 | + } | |
| 213 | + } else { | |
| 214 | + e.blink = -1.0 | |
| 215 | + } | |
| 216 | + } | |
| 217 | + | |
| 218 | + private fun look(e: Eye, dt: Double, pointer: Pointer?, fresh: Boolean) { | |
| 219 | + // A pointer that has just turned up catches some eyes' attention. | |
| 220 | + if (fresh && e.state == State.OPEN && random.nextDouble() < 0.5) { | |
| 221 | + e.nextGlance = min(e.nextGlance, rand(0.1, 0.6)) | |
| 222 | + } | |
| 223 | + | |
| 224 | + if (e.attending > 0) { | |
| 225 | + e.attending -= dt | |
| 226 | + if (pointer == null) e.attending = 0.0 | |
| 227 | + } | |
| 228 | + e.nextGlance -= dt | |
| 229 | + if (e.nextGlance <= 0) { | |
| 230 | + e.nextGlance = rand(TIMES.glance) | |
| 231 | + if (pointer != null && e.attending <= 0 && random.nextDouble() < (if (fresh) 0.6 else ATTENTION)) { | |
| 232 | + e.attending = rand(TIMES.attention) | |
| 233 | + } | |
| 234 | + if (e.attending <= 0 || pointer == null) lookAround(e) | |
| 235 | + } | |
| 236 | + // Following the pointer smoothly while attending; otherwise quick | |
| 237 | + // saccades to each new target. | |
| 238 | + var tau = 0.035 | |
| 239 | + if (e.attending > 0 && pointer != null) { | |
| 240 | + val dx = pointer.x - e.x | |
| 241 | + val dy = pointer.y - e.y | |
| 242 | + val k = 1 / sqrt(dx * dx + dy * dy + 2.25 * e.r * e.r) | |
| 243 | + e.targetX = dx * k | |
| 244 | + e.targetY = dy * k | |
| 245 | + tau = 0.08 | |
| 246 | + } | |
| 247 | + val k = 1 - exp(-dt / tau) | |
| 248 | + e.gazeX += (e.targetX - e.gazeX) * k | |
| 249 | + e.gazeY += (e.targetY - e.gazeY) * k | |
| 250 | + } | |
| 251 | + | |
| 252 | + private fun lookAround(e: Eye) { | |
| 253 | + if (random.nextDouble() < 0.25) { | |
| 254 | + e.targetX = 0.0 | |
| 255 | + e.targetY = 0.0 | |
| 256 | + return | |
| 257 | + } | |
| 258 | + val a = random.nextDouble() * 2 * PI | |
| 259 | + val m = 0.35 + 0.6 * sqrt(random.nextDouble()) | |
| 260 | + e.targetX = m * cos(a) | |
| 261 | + e.targetY = m * sin(a) | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** Openness with any blink taken out. */ | |
| 265 | + fun openness(e: Eye): Double { | |
| 266 | + if (e.blink < 0) return e.open | |
| 267 | + val t = e.blink / TIMES.blink | |
| 268 | + return e.open * (1 - min(1.0, 2 * min(t, 1 - t) * 1.25)) | |
| 269 | + } | |
| 270 | + | |
| 271 | + /** For animations off: [count] eyes open and looking ahead, placed once | |
| 272 | + * and kept while the count, ratio and surface shape stay the same. */ | |
| 273 | + fun settle(w: Double, h: Double, count: Int, ratio: Double) { | |
| 274 | + val n = count.coerceIn(1, MAX) | |
| 275 | + val key = String.format(Locale.ROOT, "%d %s %.3f %.3f", n, ratio, w, h) | |
| 276 | + if (settled == key) return | |
| 277 | + settled = key | |
| 278 | + list = mutableListOf() | |
| 279 | + for (i in 0 until n) { | |
| 280 | + val e = make(0.0) | |
| 281 | + if (!place(e, w, h, n, ratio)) continue | |
| 282 | + e.state = State.OPEN | |
| 283 | + e.timer = rand(TIMES.open) | |
| 284 | + e.open = 1.0 | |
| 285 | + e.fade = 1.0 | |
| 286 | + list += e | |
| 287 | + } | |
| 288 | + } | |
| 289 | + | |
| 290 | + /** eye0-eye23, look0-look23 and count, as eyes.frag takes them. */ | |
| 291 | + fun pack(): Map<String, FloatArray> { | |
| 292 | + val out = HashMap<String, FloatArray>() | |
| 293 | + for (i in 0 until MAX) { | |
| 294 | + val e = list.getOrNull(i) | |
| 295 | + val shown = e != null && e.state != State.WAIT | |
| 296 | + out["eye$i"] = if (shown) f(e!!.x, e.y, e.r, openness(e)) else FloatArray(4) | |
| 297 | + out["look$i"] = if (shown) f(e!!.gazeX, e.gazeY, e.shade, e.fade) else FloatArray(4) | |
| 298 | + } | |
| 299 | + out["count"] = floatArrayOf(min(MAX, list.size).toFloat()) | |
| 300 | + return out | |
| 301 | + } | |
| 302 | + | |
| 303 | + /** The life cycle's times, in seconds, as the web's TIMES. */ | |
| 304 | + object TIMES { | |
| 305 | + val wait = 0.5..3.0 // unseen, before taking a spot | |
| 306 | + const val appear = 0.8 // the line fading in | |
| 307 | + const val opening = 1.6 | |
| 308 | + val open = 10.0..60.0 | |
| 309 | + const val closing = 3.2 // slowly | |
| 310 | + const val vanish = 1.5 // the line fading out | |
| 311 | + const val blink = 0.15 | |
| 312 | + val blinkEvery = 2.0..9.0 | |
| 313 | + val glance = 0.6..3.5 // between saccades | |
| 314 | + val attention = 1.5..6.0 // a spell of looking at the pointer | |
| 315 | + } | |
| 316 | + | |
| 317 | + companion object { | |
| 318 | + const val MAX = 24 | |
| 319 | + | |
| 320 | + /** The chance, at each saccade, of a spell of looking at the pointer. */ | |
| 321 | + const val ATTENTION = 0.3 | |
| 322 | + | |
| 323 | + /** The radius range for [n] eyes: the largest [ratio] times the | |
| 324 | + * smallest, both smaller the more eyes there are, so they have room. */ | |
| 325 | + fun sizes(n: Int, ratio: Double): Pair<Double, Double> { | |
| 326 | + val big = min(0.3, 0.55 / sqrt(max(1, n).toDouble())) | |
| 327 | + return big / max(1.0, ratio) to big | |
| 328 | + } | |
| 329 | + | |
| 330 | + /** The extent an eye takes up around its centre, to keep eyes apart. */ | |
| 331 | + internal fun reach(e: Eye) = e.r * 1.15 | |
| 332 | + | |
| 333 | + private fun smooth(t: Double) = t * t * (3 - 2 * t) | |
| 334 | + | |
| 335 | + private fun f(vararg v: Double) = FloatArray(v.size) { v[it].toFloat() } | |
| 336 | + } | |
| 337 | +} |
modified
android/app/src/main/java/org/neiam/vigil/app/ui/shader/FieldRenderer.kt
+7
−2
| 27 | 27 | /** Animations are off system-wide: draw each frame as if settled. */ |
| 28 | 28 | @Volatile var still = false |
| 29 | 29 | |
| 30 | + /** Where the pointer is, for the eyes; read each frame. */ | |
| 31 | + @Volatile var pointer: ShaderPointer? = null | |
| 32 | + | |
| 30 | 33 | private val motion = Motion() |
| 31 | 34 | |
| 32 | 35 | /** A linked shader program and its uniforms' locations. */ |
| 67 | 70 | } |
| 68 | 71 | lastFrame = now |
| 69 | 72 | val s = settings |
| 70 | − motion.advance(dt, s, colours) | |
| 73 | + // The pointer only while the pattern is eyes, and not held still. | |
| 74 | + val at = if (s.pattern == "eyes" && !still) pointer?.inUnits(width, height) else null | |
| 75 | + motion.advance(dt, s, colours, still, at) | |
| 71 | 76 | |
| 72 | 77 | val uniforms = motion.uniforms(width, height, s) |
| 73 | 78 | val shown = Motion.PROGRAMS.associateWith { motion.shown(it) } |
| 88 | 93 | GLES30.glEnable(GLES30.GL_BLEND) |
| 89 | 94 | GLES30.glBlendFunc(GLES30.GL_ONE, GLES30.GL_ONE_MINUS_SRC_ALPHA) |
| 90 | 95 | for ((name, amount) in shown) { |
| 91 | − val extra = if (name == "bubbles" || name == "spheres") motion.discUniforms(name, still) else emptyMap() | |
| 96 | + val extra = motion.programUniforms(name, still) | |
| 92 | 97 | drawWith(shader(name)!!, uniforms + extra + ("qt_Opacity" to floatArrayOf(amount.toFloat()))) |
| 93 | 98 | } |
| 94 | 99 | GLES30.glDisable(GLES30.GL_BLEND) |
modified
android/app/src/main/java/org/neiam/vigil/app/ui/shader/Motion.kt
+30
−3
| 22 | 22 | * pattern or theme fades rather than cuts. The patterns with shaders of |
| 23 | 23 | * their own ([PROGRAMS]) fade in over the layered one the same way, as |
| 24 | 24 | * [shown] amounts; Prism's turns and the Bubbles and Absorbing spheres |
| 25 | − * discs ([Balls]) move as neiam-device's Motion.kt moves them. | |
| 25 | + * discs ([Balls]) move as neiam-device's Motion.kt moves them, and the | |
| 26 | + * Eyes live their lives ([Eyes]) as neiam-org's shader_field.js has them. | |
| 26 | 27 | */ |
| 27 | 28 | class Motion { |
| 28 | 29 | /** Seconds of motion so far, scaled by speed; a random start. */ |
| 50 | 51 | // Each as last stepped, kept while its pattern fades out. |
| 51 | 52 | private var bubbleDiscs = Balls.Packed(emptyList(), 1.0) |
| 52 | 53 | private var sphereDiscs = Balls.Packed(emptyList(), 0.0) |
| 54 | + private val eyes = Eyes() | |
| 55 | + private var eyesPacked: Map<String, FloatArray> = eyes.pack() | |
| 53 | 56 | |
| 54 | 57 | // The surface's size in the shader's units, as last drawn. |
| 55 | 58 | private var extentX = 16.0 / 9 |
| 77 | 80 | eased[key] = if (abs(target - now) <= step) target else now + sign(target - now) * step |
| 78 | 81 | } |
| 79 | 82 | |
| 80 | − fun advance(dt: Double, s: ShaderSettings, target: List<FloatArray>) { | |
| 83 | + /** [still]: one settled frame, animations off. [pointer]: where the | |
| 84 | + * pointer is, in shader units, for the eyes. */ | |
| 85 | + fun advance( | |
| 86 | + dt: Double, | |
| 87 | + s: ShaderSettings, | |
| 88 | + target: List<FloatArray>, | |
| 89 | + still: Boolean = false, | |
| 90 | + pointer: Eyes.Pointer? = null, | |
| 91 | + ) { | |
| 81 | 92 | val step = s.speed * min(dt, 0.25) |
| 82 | 93 | clock += step |
| 83 | 94 | frame = (frame + 1) % 100000 |
| 108 | 119 | } else if (program == "spheres") { |
| 109 | 120 | spheres.step(step, extentX, extentY, s.sphereCount) |
| 110 | 121 | sphereDiscs = spheres.pack() |
| 122 | + } else if (program == "eyes") { | |
| 123 | + // Held still: open, looking ahead. | |
| 124 | + if (still) eyes.settle(extentX, extentY, s.eyeCount, s.eyeRatio) | |
| 125 | + else eyes.step(step, extentX, extentY, s.eyeCount, s.eyeRatio, pointer) | |
| 126 | + eyesPacked = eyes.pack() | |
| 111 | 127 | } |
| 112 | 128 | |
| 113 | 129 | val current = colours |
| 221 | 237 | return out |
| 222 | 238 | } |
| 223 | 239 | |
| 240 | + /** The eyes' uniforms for eyes.frag: eye0-eye23, look0-look23 and | |
| 241 | + * count, as last stepped (kept while the pattern fades out). */ | |
| 242 | + fun eyeUniforms(): Map<String, FloatArray> = eyesPacked | |
| 243 | + | |
| 244 | + /** What a pattern's own shader takes beyond [uniforms]. */ | |
| 245 | + fun programUniforms(program: String, still: Boolean): Map<String, FloatArray> = when (program) { | |
| 246 | + "bubbles", "spheres" -> discUniforms(program, still) | |
| 247 | + "eyes" -> eyeUniforms() | |
| 248 | + else -> emptyMap() | |
| 249 | + } | |
| 250 | + | |
| 224 | 251 | /** The settled ground colour, for clearing when the shader can't run. */ |
| 225 | 252 | val base: FloatArray get() = colours?.get(0) ?: floatArrayOf(0f, 0f, 0f) |
| 226 | 253 |
| 231 | 258 | val LAYERS = listOf("moire", "halftone", "bokeh", "stars") |
| 232 | 259 | |
| 233 | 260 | /** Patterns drawn by a shader of their own, over field.frag. */ |
| 234 | − val PROGRAMS = listOf("prism", "oil", "bubbles", "spheres") | |
| 261 | + val PROGRAMS = listOf("prism", "oil", "bubbles", "spheres", "eyes") | |
| 235 | 262 | |
| 236 | 263 | /** Which shader draws the pattern: "field" for the layered ones. */ |
| 237 | 264 | fun program(s: ShaderSettings) = if (s.pattern in PROGRAMS) s.pattern else "field" |
modified
android/app/src/main/java/org/neiam/vigil/app/ui/shader/ShaderBackground.kt
+10
−1
Click to load diff…
added
android/app/src/main/java/org/neiam/vigil/app/ui/shader/ShaderPointer.kt
+78
−0
Click to load diff…
modified
android/app/src/main/java/org/neiam/vigil/app/ui/shader/ShaderSettings.kt
+8
−2
Click to load diff…
added
android/app/src/test/java/org/neiam/vigil/app/ui/shader/EyesTest.kt
+144
−0
Click to load diff…
modified
android/app/src/test/java/org/neiam/vigil/app/ui/shader/MotionTest.kt
+2
−2
Click to load diff…
modified
android/app/src/test/java/org/neiam/vigil/app/ui/shader/ShaderTest.kt
+10
−0
Click to load diff…
Parents: 3a07995