Android log-in: Eyes among the patterns, ready to look at your finger

263b40b · gmorell · 2026-12-03 22:29

32 files +830 -12
Message
{commit_body(@commit)}

Files changed

added android/app/src/main/assets/eyes.frag
+196 −0
@@ -0,0 +1,196 @@
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/recourse/app/ui/login/LoginBackdrop.kt
+6 −3
@@ -11,6 +11,7 @@ import androidx.compose.ui.graphics.Brush
11 11 import androidx.compose.ui.platform.LocalLayoutDirection
12 12 import androidx.compose.ui.unit.LayoutDirection
13 13 import org.neiam.recourse.app.ui.shader.ShaderBackground
14 +import org.neiam.recourse.app.ui.shader.ShaderPointer
14 15 import org.neiam.recourse.app.ui.shader.ShaderSettings
15 16 import org.neiam.recourse.app.ui.shader.rememberShaderPalette
16 17
@@ -28,10 +29,12 @@ val LOGIN_SHADER = ShaderSettings(pattern = "oil", oilCoverage = 17)
28 29 * top band in portrait, the start column in landscape, as LoginLayout puts it)
29 30 * so the title, subtitle and ? stay readable. The page passes this a
30 31 * hazeSource modifier, so the method cards can blur it. Only the login page
31 − * draws it; leaving the page (or opening Help) stops the shader.
32 + * draws it; leaving the page (or opening Help) stops the shader. [pointer]
33 + * is where the Eyes, if they are the pattern, look now and then; the page
34 + * hears it (shaderPointer) on the box that holds this and the cards.
32 35 */
33 36 @Composable
34 −fun LoginBackdrop(modifier: Modifier = Modifier) {
37 +fun LoginBackdrop(modifier: Modifier = Modifier, pointer: ShaderPointer? = null) {
35 38 BoxWithConstraints(modifier) {
36 39 val page = MaterialTheme.colorScheme.background
37 40 val stops = arrayOf(
@@ -46,7 +49,7 @@ fun LoginBackdrop(modifier: Modifier = Modifier) {
46 49 Brush.horizontalGradient(*stops, startX = Float.POSITIVE_INFINITY, endX = 0f)
47 50 else -> Brush.horizontalGradient(*stops)
48 51 }
49 − ShaderBackground(LOGIN_SHADER, rememberShaderPalette(), Modifier.fillMaxSize())
52 + ShaderBackground(LOGIN_SHADER, rememberShaderPalette(), Modifier.fillMaxSize(), pointer)
50 53 Box(Modifier.fillMaxSize().background(veil))
51 54 }
52 55 }
modified android/app/src/main/java/org/neiam/recourse/app/ui/login/LoginScreen.kt
+7 −3
@@ -63,6 +63,8 @@ import org.neiam.recourse.app.data.serverHost
63 63 import org.neiam.recourse.app.data.normalizeServer
64 64 import org.neiam.recourse.app.data.oidcLoginUrl
65 65 import org.neiam.recourse.app.ui.PairPanel
66 +import org.neiam.recourse.app.ui.shader.ShaderPointer
67 +import org.neiam.recourse.app.ui.shader.shaderPointer
66 68
67 69 /**
68 70 * What differs between the apps' login pages. The page itself is shared.
@@ -182,10 +184,12 @@ fun LoginScreen(
182 184 val passwordOff = methods?.password == false
183 185
184 186 // The shader and its veil behind the page, the source the method
185 − // cards blur (LoginBackdrop, Glass).
187 + // cards blur (LoginBackdrop, Glass). Touches anywhere on the page are
188 + // heard, not taken, for the eyes (if they are the pattern) to look at.
186 189 val hazeState = remember { HazeState() }
187 − Box(Modifier.fillMaxSize()) {
188 − LoginBackdrop(Modifier.fillMaxSize().hazeSource(hazeState))
190 + val shaderPointer = remember { ShaderPointer() }
191 + Box(Modifier.fillMaxSize().shaderPointer(shaderPointer, enabled = LOGIN_SHADER.pattern == "eyes")) {
192 + LoginBackdrop(Modifier.fillMaxSize().hazeSource(hazeState), shaderPointer)
189 193 LoginLayout(
190 194 hazeState = hazeState,
191 195 header = { mod, compact -> LoginHeader(branding.appName, compact, onHelp = { showHelp = true }, modifier = mod) },
added android/app/src/main/java/org/neiam/recourse/app/ui/shader/Eyes.kt
+337 −0
@@ -0,0 +1,337 @@
1 +package org.neiam.recourse.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/recourse/app/ui/shader/FieldRenderer.kt
+7 −2
@@ -35,6 +35,9 @@ class FieldRenderer(private val source: (String) -> String) : GLSurfaceView.Rend
35 35 /** Animations are off system-wide: draw each frame as if settled. */
36 36 @Volatile var still = false
37 37
38 + /** Where the pointer is, for the eyes; read each frame. */
39 + @Volatile var pointer: ShaderPointer? = null
40 +
38 41 private val motion = Motion()
39 42
40 43 /** A linked shader program and its uniforms' locations. */
@@ -75,7 +78,9 @@ class FieldRenderer(private val source: (String) -> String) : GLSurfaceView.Rend
75 78 }
76 79 lastFrame = now
77 80 val s = settings
78 − motion.advance(dt, s, colours)
81 + // The pointer only while the pattern is eyes, and not held still.
82 + val at = if (s.pattern == "eyes" && !still) pointer?.inUnits(width, height) else null
83 + motion.advance(dt, s, colours, still, at)
79 84
80 85 val uniforms = motion.uniforms(width, height, s)
81 86 val shown = Motion.PROGRAMS.associateWith { motion.shown(it) }
@@ -96,7 +101,7 @@ class FieldRenderer(private val source: (String) -> String) : GLSurfaceView.Rend
96 101 GLES30.glEnable(GLES30.GL_BLEND)
97 102 GLES30.glBlendFunc(GLES30.GL_ONE, GLES30.GL_ONE_MINUS_SRC_ALPHA)
98 103 for ((name, amount) in shown) {
99 − val extra = if (name == "bubbles" || name == "spheres") motion.discUniforms(name, still) else emptyMap()
104 + val extra = motion.programUniforms(name, still)
100 105 drawWith(shader(name)!!, uniforms + extra + ("qt_Opacity" to floatArrayOf(amount.toFloat())))
101 106 }
102 107 GLES30.glDisable(GLES30.GL_BLEND)
modified android/app/src/main/java/org/neiam/recourse/app/ui/shader/Motion.kt
+30 −3

Click to load diff…

modified android/app/src/main/java/org/neiam/recourse/app/ui/shader/ShaderBackground.kt
+10 −1

Click to load diff…

added android/app/src/main/java/org/neiam/recourse/app/ui/shader/ShaderPointer.kt
+78 −0

Click to load diff…

modified android/app/src/main/java/org/neiam/recourse/app/ui/shader/ShaderSettings.kt
+5 −0

Click to load diff…

added android/app/src/test/java/org/neiam/recourse/app/ui/shader/EyesTest.kt
+144 −0

Click to load diff…

modified android/app/src/test/java/org/neiam/recourse/app/ui/shader/ShaderTest.kt
+10 −0

Click to load diff…

Parents: 8cd9941