@johnmorrisdotca/wakusei-planets 0.45.1 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ /** The uniforms of the pyramid every shader reads, and the one function that reads it: `terrainY(point, distance)`, metres over the site's ground, the Moon's curve taken off. */
2
+ export declare const HEIGHTS = "\nuniform sampler2D uH0;\nuniform sampler2D uH1;\nuniform sampler2D uH2;\nuniform sampler2D uH3;\nuniform sampler2D uA0; // the ground picture on the first three levels, 0 to 1 with the middle at 0.5\nuniform sampler2D uA1;\nuniform sampler2D uA2;\nuniform float uPicture; // 1 where the picture is there\nuniform vec4 uL0; // half width, metres a sample, samples across, the distance out to which this level is used\nuniform vec4 uL1;\nuniform vec4 uL2;\nuniform vec4 uL3;\nuniform float uHmin;\nuniform float uHrange;\nuniform float uGround;\nuniform float uRadius;\n\nuniform vec2 uFarHeight; // the range the coarse level is encoded over: its minimum and its span\nfloat dec(vec4 t, vec2 hr) { return hr.x + (t.r * 255.0 * 256.0 + t.g * 255.0) / 65535.0 * hr.y; }\n\nfloat lin(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 f = vec2((p.x + l.x) / l.y - 0.5, (l.x - p.y) / l.y - 0.5);\n vec2 i = floor(f);\n vec2 t = f - i;\n float st = 1.0 / l.z;\n vec2 uv = (i + 0.5) * st;\n float a = dec(texture2D(s, uv), hr);\n float b = dec(texture2D(s, uv + vec2(st, 0.0)), hr);\n float c = dec(texture2D(s, uv + vec2(0.0, st)), hr);\n float d = dec(texture2D(s, uv + vec2(st, st)), hr);\n return mix(mix(a, b, t.x), mix(c, d, t.x), t.y);\n}\n\nfloat nearest(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 uv = vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z));\n return dec(texture2D(s, uv), hr);\n}\n\nbool holds(vec2 p, vec4 l, float dist) { return dist < l.w && abs(p.x) < l.x - 2.0 * l.y && abs(p.y) < l.x - 2.0 * l.y; }\n\n// the ground's height over the site's, the sphere's fall taken off; `fine` reads four samples, otherwise one\nfloat terrainY(vec2 p, float dist, bool fine) {\n float h;\n if (holds(p, uL0, dist)) h = fine ? lin(uH0, p, uL0, vec2(uHmin, uHrange)) : nearest(uH0, p, uL0, vec2(uHmin, uHrange));\n else if (holds(p, uL1, dist)) h = fine ? lin(uH1, p, uL1, vec2(uHmin, uHrange)) : nearest(uH1, p, uL1, vec2(uHmin, uHrange));\n else if (holds(p, uL2, dist)) h = fine ? lin(uH2, p, uL2, vec2(uHmin, uHrange)) : nearest(uH2, p, uL2, vec2(uHmin, uHrange));\n else h = fine ? lin(uH3, p, uL3, uFarHeight) : nearest(uH3, p, uL3, uFarHeight);\n return h - uGround - dot(p, p) / (2.0 * uRadius);\n}\n\n// the ground's own brightness at a point, from the picture of the level that serves it, 1 where it is the middle\nfloat albedoAt(vec2 p, float dist) {\n if (uPicture < 0.5) return 1.0;\n vec4 l;\n if (holds(p, uL0, dist)) { l = uL0; return 2.0 * texture2D(uA0, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL1, dist)) { l = uL1; return 2.0 * texture2D(uA1, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL2, dist)) { l = uL2; return 2.0 * texture2D(uA2, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n return 1.0;\n}\n\n// the measured ground's shadows, baked (scripts/make-moon-terrain.py): for a point, the elevation angle in degrees of the horizon toward the Sun, read from the finest of the three horizon maps that holds the point\nuniform sampler2D uZ0;\nuniform sampler2D uZ1;\nuniform sampler2D uZ2;\nuniform float uHzOn; // 1 where the maps are there\nuniform vec3 uHzSun; // the two directions (of 18) the Sun's azimuth lies between, and the share of the second\nuniform vec2 uHzRange; // the degrees the codes 0 and 255 stand for\n\nfloat hzSector(sampler2D s, vec2 cell, float k) {\n float slab = floor(k / 3.0);\n float ch = k - slab * 3.0;\n vec3 v = texture2D(s, vec2((cell.x + slab) / 6.0, cell.y)).rgb;\n return ch < 0.5 ? v.r : (ch < 1.5 ? v.g : v.b);\n}\n\nfloat hzFrom(sampler2D s, vec2 p, float reach) {\n vec2 cell = clamp(vec2((p.x + reach) / (2.0 * reach), (reach - p.y) / (2.0 * reach)), vec2(0.5 / 512.0), vec2(1.0 - 0.5 / 512.0));\n float code = mix(hzSector(s, cell, uHzSun.x), hzSector(s, cell, uHzSun.y), uHzSun.z);\n return uHzRange.x + code * (uHzRange.y - uHzRange.x);\n}\n\nfloat horizonAt(vec2 p) {\n if (abs(p.x) < uL0.x && abs(p.y) < uL0.x) return hzFrom(uZ0, p, uL0.x);\n if (abs(p.x) < uL1.x && abs(p.y) < uL1.x) return hzFrom(uZ1, p, uL1.x);\n if (abs(p.x) < uL2.x && abs(p.y) < uL2.x) return hzFrom(uZ2, p, uL2.x);\n return -90.0;\n}\n\n// the spacing of the level that serves a point, for the step of a slope\nfloat spacing(vec2 p, float dist) {\n if (holds(p, uL0, dist)) return uL0.y;\n if (holds(p, uL1, dist)) return uL1.y;\n if (holds(p, uL2, dist)) return uL2.y;\n return uL3.y;\n}\n";
3
+ /** The made detail, in GLSL: the same formulas as `detail.ts` (craters on a seeded grid, by octave), and a fine roughness for the normals. */
4
+ export declare const DETAIL = "\nuniform float uPixel; // the angle one pixel takes, radians\nuniform float uDetail; // 1 for the made detail, 0 for none\n\nuvec2 pcg2d(uvec2 v) {\n v = v * 1664525u + 1013904223u;\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n return v;\n}\nfloat u24(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }\n\n// the made ground at a point: x the height in metres, yz the slope east and north, from every crater of every octave whose nominal diameter is over `fromD`\nvec3 madeGround(vec2 p, float fromD) {\n vec3 sum = vec3(0.0);\n for (int octave = 5; octave >= 0; octave -= 1) {\n float nominal = 0.5 * exp2(float(octave));\n float fade = clamp(nominal / fromD - 1.0, 0.0, 1.0);\n if (fade <= 0.0) break;\n float cell = 3.0 * nominal;\n ivec2 base = ivec2(floor(p / cell));\n vec2 fr = p / cell - vec2(base);\n float occ = (octave >= 3 ? 0.5 : 1.0) * 0.36 * exp2(-0.5 * float(octave));\n for (int j = -1; j <= 1; j += 1) {\n for (int i = -1; i <= 1; i += 1) {\n // a crater is kept to the middle of its cell, so a neighbour's reaches this cell only within a fifth of the edge\n if ((i == -1 && fr.x > 0.2) || (i == 1 && fr.x < 0.8) || (j == -1 && fr.y > 0.2) || (j == 1 && fr.y < 0.8)) continue;\n ivec2 c = base + ivec2(i, j);\n uvec2 ab = pcg2d(uvec2(uint(c.x), uint(c.y + octave * 7919)));\n if (u24(ab.x) >= occ) continue;\n uvec2 cd = pcg2d(uvec2(uint(c.x + 104729), uint(c.y + octave * 7919 + 15485863)));\n float diameter = nominal * (1.0 + (octave == 5 ? 0.25 : 1.0) * u24(cd.y));\n float r4 = float(ab.x & 255u) / 255.0;\n float age = 1.0 - r4 * r4 * r4 * r4;\n float radius = 0.5 * diameter;\n float vary = 0.6 + 0.4 * (float(cd.x & 255u) / 255.0);\n float depth = diameter * (0.2 - 0.17 * age) * vary;\n float rim = 0.045 * diameter * pow(1.0 - age, 1.5);\n float rimWidth = 0.22 + 0.5 * age;\n vec2 centre = vec2(float(c.x) + 0.25 + 0.5 * u24(ab.y), float(c.y) + 0.25 + 0.5 * u24(cd.x)) * cell;\n vec2 d = p - centre;\n float r = length(d);\n float x = r / radius;\n float h = 0.0;\n float dh = 0.0;\n if (x < 1.0) { float bowl = 1.0 - x * x; float pw = 1.0 + age; h = -depth * pow(bowl, pw); dh = 2.0 * depth * pw * x * pow(bowl, age); }\n float e = (x - 1.0) / rimWidth;\n float ex = exp(-e * e);\n h += rim * ex;\n dh += -2.0 * rim * e * ex / rimWidth;\n if (x > 1.0) { float t = 1.0 / x; h += 0.25 * rim * t * t * t; dh += -0.75 * rim * t * t * t * t; }\n float k = r > 1e-6 ? dh / (radius * r) : 0.0;\n sum += fade * vec3(h, k * d.x, k * d.y);\n }\n }\n }\n return sum;\n}\n\nfloat vnoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = p - i;\n vec2 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n uvec2 c = uvec2(ivec2(i));\n float a = u24(pcg2d(c).x);\n float b = u24(pcg2d(c + uvec2(1u, 0u)).x);\n float cc = u24(pcg2d(c + uvec2(0u, 1u)).x);\n float d = u24(pcg2d(c + uvec2(1u, 1u)).x);\n return mix(mix(a, b, w.x), mix(cc, d, w.x), w.y);\n}\n\n// a fine roughness of the dust: two scales, as a slope (east, north), faded out where a pixel covers more than a few of its grains\nvec2 roughness(vec2 p, float footprint) {\n vec2 g = vec2(0.0);\n for (int k = 0; k < 2; k += 1) {\n float s = k == 0 ? 0.07 : 0.3;\n float amp = k == 0 ? 0.3 : 0.28;\n float fade = smoothstep(footprint * 2.0, footprint * 7.0, s);\n if (fade <= 0.0) continue;\n float d = s * 0.25;\n vec2 q = (k == 0 ? vec2(p.x * 0.629 + p.y * 0.777, p.y * 0.629 - p.x * 0.777) : vec2(p.x * 0.857 - p.y * 0.515, p.x * 0.515 + p.y * 0.857)) / s;\n float dx = vnoise(q + vec2(0.25, 0.0)) - vnoise(q - vec2(0.25, 0.0));\n float dy = vnoise(q + vec2(0.0, 0.25)) - vnoise(q - vec2(0.0, 0.25));\n g += fade * amp * vec2(dx, dy) / (0.5 * s) * 0.012;\n }\n return g;\n}\n";
5
+ /** The ground's vertices: a ring index and an angle across the view, laid out as the viewer's own polar grid, then raised to the measured heights. */
6
+ export declare const GROUND_VERTEX = "\nprecision highp float;\nprecision highp sampler2D;\n// position (declared by three.js) holds x: 0 to 1 out along the rays (ring spacing grows with the distance), y: -1 to 1 across the view\nuniform mat4 uVP;\nuniform vec2 uEyeEN; // the eyes' place in the site's plane: east, north\nuniform float uYaw; // radians, clockwise from north\nuniform float uSpan; // half the angle drawn across the view, radians\nuniform float uRMin;\nuniform float uRMax;\n\nuniform sampler2D uH0;\nuniform sampler2D uH1;\nuniform sampler2D uH2;\nuniform sampler2D uH3;\nuniform sampler2D uA0; // the ground picture on the first three levels, 0 to 1 with the middle at 0.5\nuniform sampler2D uA1;\nuniform sampler2D uA2;\nuniform float uPicture; // 1 where the picture is there\nuniform vec4 uL0; // half width, metres a sample, samples across, the distance out to which this level is used\nuniform vec4 uL1;\nuniform vec4 uL2;\nuniform vec4 uL3;\nuniform float uHmin;\nuniform float uHrange;\nuniform float uGround;\nuniform float uRadius;\n\nuniform vec2 uFarHeight; // the range the coarse level is encoded over: its minimum and its span\nfloat dec(vec4 t, vec2 hr) { return hr.x + (t.r * 255.0 * 256.0 + t.g * 255.0) / 65535.0 * hr.y; }\n\nfloat lin(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 f = vec2((p.x + l.x) / l.y - 0.5, (l.x - p.y) / l.y - 0.5);\n vec2 i = floor(f);\n vec2 t = f - i;\n float st = 1.0 / l.z;\n vec2 uv = (i + 0.5) * st;\n float a = dec(texture2D(s, uv), hr);\n float b = dec(texture2D(s, uv + vec2(st, 0.0)), hr);\n float c = dec(texture2D(s, uv + vec2(0.0, st)), hr);\n float d = dec(texture2D(s, uv + vec2(st, st)), hr);\n return mix(mix(a, b, t.x), mix(c, d, t.x), t.y);\n}\n\nfloat nearest(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 uv = vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z));\n return dec(texture2D(s, uv), hr);\n}\n\nbool holds(vec2 p, vec4 l, float dist) { return dist < l.w && abs(p.x) < l.x - 2.0 * l.y && abs(p.y) < l.x - 2.0 * l.y; }\n\n// the ground's height over the site's, the sphere's fall taken off; `fine` reads four samples, otherwise one\nfloat terrainY(vec2 p, float dist, bool fine) {\n float h;\n if (holds(p, uL0, dist)) h = fine ? lin(uH0, p, uL0, vec2(uHmin, uHrange)) : nearest(uH0, p, uL0, vec2(uHmin, uHrange));\n else if (holds(p, uL1, dist)) h = fine ? lin(uH1, p, uL1, vec2(uHmin, uHrange)) : nearest(uH1, p, uL1, vec2(uHmin, uHrange));\n else if (holds(p, uL2, dist)) h = fine ? lin(uH2, p, uL2, vec2(uHmin, uHrange)) : nearest(uH2, p, uL2, vec2(uHmin, uHrange));\n else h = fine ? lin(uH3, p, uL3, uFarHeight) : nearest(uH3, p, uL3, uFarHeight);\n return h - uGround - dot(p, p) / (2.0 * uRadius);\n}\n\n// the ground's own brightness at a point, from the picture of the level that serves it, 1 where it is the middle\nfloat albedoAt(vec2 p, float dist) {\n if (uPicture < 0.5) return 1.0;\n vec4 l;\n if (holds(p, uL0, dist)) { l = uL0; return 2.0 * texture2D(uA0, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL1, dist)) { l = uL1; return 2.0 * texture2D(uA1, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL2, dist)) { l = uL2; return 2.0 * texture2D(uA2, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n return 1.0;\n}\n\n// the measured ground's shadows, baked (scripts/make-moon-terrain.py): for a point, the elevation angle in degrees of the horizon toward the Sun, read from the finest of the three horizon maps that holds the point\nuniform sampler2D uZ0;\nuniform sampler2D uZ1;\nuniform sampler2D uZ2;\nuniform float uHzOn; // 1 where the maps are there\nuniform vec3 uHzSun; // the two directions (of 18) the Sun's azimuth lies between, and the share of the second\nuniform vec2 uHzRange; // the degrees the codes 0 and 255 stand for\n\nfloat hzSector(sampler2D s, vec2 cell, float k) {\n float slab = floor(k / 3.0);\n float ch = k - slab * 3.0;\n vec3 v = texture2D(s, vec2((cell.x + slab) / 6.0, cell.y)).rgb;\n return ch < 0.5 ? v.r : (ch < 1.5 ? v.g : v.b);\n}\n\nfloat hzFrom(sampler2D s, vec2 p, float reach) {\n vec2 cell = clamp(vec2((p.x + reach) / (2.0 * reach), (reach - p.y) / (2.0 * reach)), vec2(0.5 / 512.0), vec2(1.0 - 0.5 / 512.0));\n float code = mix(hzSector(s, cell, uHzSun.x), hzSector(s, cell, uHzSun.y), uHzSun.z);\n return uHzRange.x + code * (uHzRange.y - uHzRange.x);\n}\n\nfloat horizonAt(vec2 p) {\n if (abs(p.x) < uL0.x && abs(p.y) < uL0.x) return hzFrom(uZ0, p, uL0.x);\n if (abs(p.x) < uL1.x && abs(p.y) < uL1.x) return hzFrom(uZ1, p, uL1.x);\n if (abs(p.x) < uL2.x && abs(p.y) < uL2.x) return hzFrom(uZ2, p, uL2.x);\n return -90.0;\n}\n\n// the spacing of the level that serves a point, for the step of a slope\nfloat spacing(vec2 p, float dist) {\n if (holds(p, uL0, dist)) return uL0.y;\n if (holds(p, uL1, dist)) return uL1.y;\n if (holds(p, uL2, dist)) return uL2.y;\n return uL3.y;\n}\n\n\nuniform float uPixel; // the angle one pixel takes, radians\nuniform float uDetail; // 1 for the made detail, 0 for none\n\nuvec2 pcg2d(uvec2 v) {\n v = v * 1664525u + 1013904223u;\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n return v;\n}\nfloat u24(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }\n\n// the made ground at a point: x the height in metres, yz the slope east and north, from every crater of every octave whose nominal diameter is over `fromD`\nvec3 madeGround(vec2 p, float fromD) {\n vec3 sum = vec3(0.0);\n for (int octave = 5; octave >= 0; octave -= 1) {\n float nominal = 0.5 * exp2(float(octave));\n float fade = clamp(nominal / fromD - 1.0, 0.0, 1.0);\n if (fade <= 0.0) break;\n float cell = 3.0 * nominal;\n ivec2 base = ivec2(floor(p / cell));\n vec2 fr = p / cell - vec2(base);\n float occ = (octave >= 3 ? 0.5 : 1.0) * 0.36 * exp2(-0.5 * float(octave));\n for (int j = -1; j <= 1; j += 1) {\n for (int i = -1; i <= 1; i += 1) {\n // a crater is kept to the middle of its cell, so a neighbour's reaches this cell only within a fifth of the edge\n if ((i == -1 && fr.x > 0.2) || (i == 1 && fr.x < 0.8) || (j == -1 && fr.y > 0.2) || (j == 1 && fr.y < 0.8)) continue;\n ivec2 c = base + ivec2(i, j);\n uvec2 ab = pcg2d(uvec2(uint(c.x), uint(c.y + octave * 7919)));\n if (u24(ab.x) >= occ) continue;\n uvec2 cd = pcg2d(uvec2(uint(c.x + 104729), uint(c.y + octave * 7919 + 15485863)));\n float diameter = nominal * (1.0 + (octave == 5 ? 0.25 : 1.0) * u24(cd.y));\n float r4 = float(ab.x & 255u) / 255.0;\n float age = 1.0 - r4 * r4 * r4 * r4;\n float radius = 0.5 * diameter;\n float vary = 0.6 + 0.4 * (float(cd.x & 255u) / 255.0);\n float depth = diameter * (0.2 - 0.17 * age) * vary;\n float rim = 0.045 * diameter * pow(1.0 - age, 1.5);\n float rimWidth = 0.22 + 0.5 * age;\n vec2 centre = vec2(float(c.x) + 0.25 + 0.5 * u24(ab.y), float(c.y) + 0.25 + 0.5 * u24(cd.x)) * cell;\n vec2 d = p - centre;\n float r = length(d);\n float x = r / radius;\n float h = 0.0;\n float dh = 0.0;\n if (x < 1.0) { float bowl = 1.0 - x * x; float pw = 1.0 + age; h = -depth * pow(bowl, pw); dh = 2.0 * depth * pw * x * pow(bowl, age); }\n float e = (x - 1.0) / rimWidth;\n float ex = exp(-e * e);\n h += rim * ex;\n dh += -2.0 * rim * e * ex / rimWidth;\n if (x > 1.0) { float t = 1.0 / x; h += 0.25 * rim * t * t * t; dh += -0.75 * rim * t * t * t * t; }\n float k = r > 1e-6 ? dh / (radius * r) : 0.0;\n sum += fade * vec3(h, k * d.x, k * d.y);\n }\n }\n }\n return sum;\n}\n\nfloat vnoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = p - i;\n vec2 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n uvec2 c = uvec2(ivec2(i));\n float a = u24(pcg2d(c).x);\n float b = u24(pcg2d(c + uvec2(1u, 0u)).x);\n float cc = u24(pcg2d(c + uvec2(0u, 1u)).x);\n float d = u24(pcg2d(c + uvec2(1u, 1u)).x);\n return mix(mix(a, b, w.x), mix(cc, d, w.x), w.y);\n}\n\n// a fine roughness of the dust: two scales, as a slope (east, north), faded out where a pixel covers more than a few of its grains\nvec2 roughness(vec2 p, float footprint) {\n vec2 g = vec2(0.0);\n for (int k = 0; k < 2; k += 1) {\n float s = k == 0 ? 0.07 : 0.3;\n float amp = k == 0 ? 0.3 : 0.28;\n float fade = smoothstep(footprint * 2.0, footprint * 7.0, s);\n if (fade <= 0.0) continue;\n float d = s * 0.25;\n vec2 q = (k == 0 ? vec2(p.x * 0.629 + p.y * 0.777, p.y * 0.629 - p.x * 0.777) : vec2(p.x * 0.857 - p.y * 0.515, p.x * 0.515 + p.y * 0.857)) / s;\n float dx = vnoise(q + vec2(0.25, 0.0)) - vnoise(q - vec2(0.25, 0.0));\n float dy = vnoise(q + vec2(0.0, 0.25)) - vnoise(q - vec2(0.0, 0.25));\n g += fade * amp * vec2(dx, dy) / (0.5 * s) * 0.012;\n }\n return g;\n}\n\nvarying vec3 vP;\nvoid main() {\n float r = uRMin * pow(uRMax / uRMin, position.x);\n float a = uYaw + position.y * uSpan;\n vec2 p = uEyeEN + r * vec2(sin(a), cos(a));\n float y = terrainY(p, r, true);\n // the made craters, those the mesh can hold here: bigger than a few of its steps\n if (uDetail > 0.5 && r < 3000.0) y += madeGround(p, max(2.5 * r * uPixel, 0.06 * r)).x;\n vP = vec3(p.x, y, p.y);\n gl_Position = uVP * vec4(p.x, y, -p.y, 1.0);\n}\n";
7
+ /** What the ground, the rocks and their shadows all need to be lit: the Sun, the eyes, the heights and the made detail, the ground's height with the craters on it, and a point's light (a hard shadow by a march toward the Sun). */
8
+ export declare const LIGHT = "\nuniform vec3 uSun;\nuniform vec3 uEye;\nuniform vec2 uEyeEN;\nuniform float uExposure;\nuniform float uAlbedo;\nuniform float uMaxY; // the highest ground over the site's, for the end of a shadow's march\nuniform float uEarthshine; // the light of the sunlit ground round the shade, a small share of a lit slope's\nuniform float uShadows; // 1 for the ground to shadow itself (the default), 0 for none (a try-out)\nuniform float uSunDegrees; // the Sun's elevation in degrees\nuniform vec3 uNear; // the near march: steps, the distance it reaches in metres, the distance out to which the made craters are in it (the quality of the draw)\n\nuniform sampler2D uH0;\nuniform sampler2D uH1;\nuniform sampler2D uH2;\nuniform sampler2D uH3;\nuniform sampler2D uA0; // the ground picture on the first three levels, 0 to 1 with the middle at 0.5\nuniform sampler2D uA1;\nuniform sampler2D uA2;\nuniform float uPicture; // 1 where the picture is there\nuniform vec4 uL0; // half width, metres a sample, samples across, the distance out to which this level is used\nuniform vec4 uL1;\nuniform vec4 uL2;\nuniform vec4 uL3;\nuniform float uHmin;\nuniform float uHrange;\nuniform float uGround;\nuniform float uRadius;\n\nuniform vec2 uFarHeight; // the range the coarse level is encoded over: its minimum and its span\nfloat dec(vec4 t, vec2 hr) { return hr.x + (t.r * 255.0 * 256.0 + t.g * 255.0) / 65535.0 * hr.y; }\n\nfloat lin(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 f = vec2((p.x + l.x) / l.y - 0.5, (l.x - p.y) / l.y - 0.5);\n vec2 i = floor(f);\n vec2 t = f - i;\n float st = 1.0 / l.z;\n vec2 uv = (i + 0.5) * st;\n float a = dec(texture2D(s, uv), hr);\n float b = dec(texture2D(s, uv + vec2(st, 0.0)), hr);\n float c = dec(texture2D(s, uv + vec2(0.0, st)), hr);\n float d = dec(texture2D(s, uv + vec2(st, st)), hr);\n return mix(mix(a, b, t.x), mix(c, d, t.x), t.y);\n}\n\nfloat nearest(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 uv = vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z));\n return dec(texture2D(s, uv), hr);\n}\n\nbool holds(vec2 p, vec4 l, float dist) { return dist < l.w && abs(p.x) < l.x - 2.0 * l.y && abs(p.y) < l.x - 2.0 * l.y; }\n\n// the ground's height over the site's, the sphere's fall taken off; `fine` reads four samples, otherwise one\nfloat terrainY(vec2 p, float dist, bool fine) {\n float h;\n if (holds(p, uL0, dist)) h = fine ? lin(uH0, p, uL0, vec2(uHmin, uHrange)) : nearest(uH0, p, uL0, vec2(uHmin, uHrange));\n else if (holds(p, uL1, dist)) h = fine ? lin(uH1, p, uL1, vec2(uHmin, uHrange)) : nearest(uH1, p, uL1, vec2(uHmin, uHrange));\n else if (holds(p, uL2, dist)) h = fine ? lin(uH2, p, uL2, vec2(uHmin, uHrange)) : nearest(uH2, p, uL2, vec2(uHmin, uHrange));\n else h = fine ? lin(uH3, p, uL3, uFarHeight) : nearest(uH3, p, uL3, uFarHeight);\n return h - uGround - dot(p, p) / (2.0 * uRadius);\n}\n\n// the ground's own brightness at a point, from the picture of the level that serves it, 1 where it is the middle\nfloat albedoAt(vec2 p, float dist) {\n if (uPicture < 0.5) return 1.0;\n vec4 l;\n if (holds(p, uL0, dist)) { l = uL0; return 2.0 * texture2D(uA0, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL1, dist)) { l = uL1; return 2.0 * texture2D(uA1, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL2, dist)) { l = uL2; return 2.0 * texture2D(uA2, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n return 1.0;\n}\n\n// the measured ground's shadows, baked (scripts/make-moon-terrain.py): for a point, the elevation angle in degrees of the horizon toward the Sun, read from the finest of the three horizon maps that holds the point\nuniform sampler2D uZ0;\nuniform sampler2D uZ1;\nuniform sampler2D uZ2;\nuniform float uHzOn; // 1 where the maps are there\nuniform vec3 uHzSun; // the two directions (of 18) the Sun's azimuth lies between, and the share of the second\nuniform vec2 uHzRange; // the degrees the codes 0 and 255 stand for\n\nfloat hzSector(sampler2D s, vec2 cell, float k) {\n float slab = floor(k / 3.0);\n float ch = k - slab * 3.0;\n vec3 v = texture2D(s, vec2((cell.x + slab) / 6.0, cell.y)).rgb;\n return ch < 0.5 ? v.r : (ch < 1.5 ? v.g : v.b);\n}\n\nfloat hzFrom(sampler2D s, vec2 p, float reach) {\n vec2 cell = clamp(vec2((p.x + reach) / (2.0 * reach), (reach - p.y) / (2.0 * reach)), vec2(0.5 / 512.0), vec2(1.0 - 0.5 / 512.0));\n float code = mix(hzSector(s, cell, uHzSun.x), hzSector(s, cell, uHzSun.y), uHzSun.z);\n return uHzRange.x + code * (uHzRange.y - uHzRange.x);\n}\n\nfloat horizonAt(vec2 p) {\n if (abs(p.x) < uL0.x && abs(p.y) < uL0.x) return hzFrom(uZ0, p, uL0.x);\n if (abs(p.x) < uL1.x && abs(p.y) < uL1.x) return hzFrom(uZ1, p, uL1.x);\n if (abs(p.x) < uL2.x && abs(p.y) < uL2.x) return hzFrom(uZ2, p, uL2.x);\n return -90.0;\n}\n\n// the spacing of the level that serves a point, for the step of a slope\nfloat spacing(vec2 p, float dist) {\n if (holds(p, uL0, dist)) return uL0.y;\n if (holds(p, uL1, dist)) return uL1.y;\n if (holds(p, uL2, dist)) return uL2.y;\n return uL3.y;\n}\n\n\nuniform float uPixel; // the angle one pixel takes, radians\nuniform float uDetail; // 1 for the made detail, 0 for none\n\nuvec2 pcg2d(uvec2 v) {\n v = v * 1664525u + 1013904223u;\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n return v;\n}\nfloat u24(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }\n\n// the made ground at a point: x the height in metres, yz the slope east and north, from every crater of every octave whose nominal diameter is over `fromD`\nvec3 madeGround(vec2 p, float fromD) {\n vec3 sum = vec3(0.0);\n for (int octave = 5; octave >= 0; octave -= 1) {\n float nominal = 0.5 * exp2(float(octave));\n float fade = clamp(nominal / fromD - 1.0, 0.0, 1.0);\n if (fade <= 0.0) break;\n float cell = 3.0 * nominal;\n ivec2 base = ivec2(floor(p / cell));\n vec2 fr = p / cell - vec2(base);\n float occ = (octave >= 3 ? 0.5 : 1.0) * 0.36 * exp2(-0.5 * float(octave));\n for (int j = -1; j <= 1; j += 1) {\n for (int i = -1; i <= 1; i += 1) {\n // a crater is kept to the middle of its cell, so a neighbour's reaches this cell only within a fifth of the edge\n if ((i == -1 && fr.x > 0.2) || (i == 1 && fr.x < 0.8) || (j == -1 && fr.y > 0.2) || (j == 1 && fr.y < 0.8)) continue;\n ivec2 c = base + ivec2(i, j);\n uvec2 ab = pcg2d(uvec2(uint(c.x), uint(c.y + octave * 7919)));\n if (u24(ab.x) >= occ) continue;\n uvec2 cd = pcg2d(uvec2(uint(c.x + 104729), uint(c.y + octave * 7919 + 15485863)));\n float diameter = nominal * (1.0 + (octave == 5 ? 0.25 : 1.0) * u24(cd.y));\n float r4 = float(ab.x & 255u) / 255.0;\n float age = 1.0 - r4 * r4 * r4 * r4;\n float radius = 0.5 * diameter;\n float vary = 0.6 + 0.4 * (float(cd.x & 255u) / 255.0);\n float depth = diameter * (0.2 - 0.17 * age) * vary;\n float rim = 0.045 * diameter * pow(1.0 - age, 1.5);\n float rimWidth = 0.22 + 0.5 * age;\n vec2 centre = vec2(float(c.x) + 0.25 + 0.5 * u24(ab.y), float(c.y) + 0.25 + 0.5 * u24(cd.x)) * cell;\n vec2 d = p - centre;\n float r = length(d);\n float x = r / radius;\n float h = 0.0;\n float dh = 0.0;\n if (x < 1.0) { float bowl = 1.0 - x * x; float pw = 1.0 + age; h = -depth * pow(bowl, pw); dh = 2.0 * depth * pw * x * pow(bowl, age); }\n float e = (x - 1.0) / rimWidth;\n float ex = exp(-e * e);\n h += rim * ex;\n dh += -2.0 * rim * e * ex / rimWidth;\n if (x > 1.0) { float t = 1.0 / x; h += 0.25 * rim * t * t * t; dh += -0.75 * rim * t * t * t * t; }\n float k = r > 1e-6 ? dh / (radius * r) : 0.0;\n sum += fade * vec3(h, k * d.x, k * d.y);\n }\n }\n }\n return sum;\n}\n\nfloat vnoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = p - i;\n vec2 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n uvec2 c = uvec2(ivec2(i));\n float a = u24(pcg2d(c).x);\n float b = u24(pcg2d(c + uvec2(1u, 0u)).x);\n float cc = u24(pcg2d(c + uvec2(0u, 1u)).x);\n float d = u24(pcg2d(c + uvec2(1u, 1u)).x);\n return mix(mix(a, b, w.x), mix(cc, d, w.x), w.y);\n}\n\n// a fine roughness of the dust: two scales, as a slope (east, north), faded out where a pixel covers more than a few of its grains\nvec2 roughness(vec2 p, float footprint) {\n vec2 g = vec2(0.0);\n for (int k = 0; k < 2; k += 1) {\n float s = k == 0 ? 0.07 : 0.3;\n float amp = k == 0 ? 0.3 : 0.28;\n float fade = smoothstep(footprint * 2.0, footprint * 7.0, s);\n if (fade <= 0.0) continue;\n float d = s * 0.25;\n vec2 q = (k == 0 ? vec2(p.x * 0.629 + p.y * 0.777, p.y * 0.629 - p.x * 0.777) : vec2(p.x * 0.857 - p.y * 0.515, p.x * 0.515 + p.y * 0.857)) / s;\n float dx = vnoise(q + vec2(0.25, 0.0)) - vnoise(q - vec2(0.25, 0.0));\n float dy = vnoise(q + vec2(0.0, 0.25)) - vnoise(q - vec2(0.0, 0.25));\n g += fade * amp * vec2(dx, dy) / (0.5 * s) * 0.012;\n }\n return g;\n}\n\n\n// the ground's height at a point: the measured heights and, near the eyes, the made craters (kept to the ones a pixel here can show)\nfloat groundY(vec2 p, float dist, bool fine, float fromD) {\n float y = terrainY(p, dist, fine);\n if (uDetail > 0.5 && fromD < 24.0) y += madeGround(p, fromD).x;\n return y;\n}\n\n// 1 where the Sun reaches a point, 0 where the ground is in its way. The measured ground's shadow is read from the baked horizon; the march toward the Sun over the heights (and the made craters) is the near\n// field's only, a few tens of metres with a fixed number of steps, as sharp as the Sun's edge. Where there is no baked horizon the march goes all the way, as it did before the maps.\nfloat lit(vec3 P, float dist0) {\n vec2 dir = normalize(uSun.xz);\n float slope = uSun.y / max(length(uSun.xz), 1e-4);\n if (uHzOn > 0.5 && horizonAt(P.xz) - 0.3 > uSunDegrees) return 0.0;\n // the ground at the point from the heights themselves, not the mesh's slope between its corners, which can be a long way off on a far mountain\n P.y = groundY(P.xz, dist0, true, 2.5 * dist0 * uPixel);\n float t = 0.5 * spacing(P.xz, dist0) + 0.25;\n if (uDetail > 0.5) t = min(t, 0.2 + 0.02 * dist0);\n // a pixel's own start along the ray: the edge of a rim's shadow is a line of steps, and a line of steps is jagged; with a start of its own each pixel it becomes a fine grain instead\n float jitter = fract(sin(dot(P.xz, vec2(12.9898, 78.233))) * 43758.5453);\n float range = uHzOn > 0.5 ? uNear.y : 80000.0;\n for (int i = 0; i < 260; i += 1) {\n if (float(i) >= uNear.x) break;\n vec2 q = P.xz + dir * t;\n float y = P.y + slope * t;\n float dist = max(t, distance(q, uEyeEN));\n float res = spacing(q, dist);\n float h = groundY(q, dist, t < 600.0, t < uNear.z ? max(0.2 * t, 2.5 * dist * uPixel) : 99.0);\n float bias = (t < 40.0 ? 0.01 : 0.3 * res) + 0.002 * t;\n if (h > y + bias) return 0.0;\n if (y > uMaxY || t > range) break;\n float step = t < 30.0 ? t * 0.045 + 0.04 : t * 0.06 + 0.5;\n t += step * (0.65 + 0.7 * jitter);\n }\n return 1.0;\n}\n";
9
+ /** The ground's light. */
10
+ export declare const GROUND_FRAGMENT = "\nprecision highp float;\nprecision highp sampler2D;\n\nuniform vec3 uSun;\nuniform vec3 uEye;\nuniform vec2 uEyeEN;\nuniform float uExposure;\nuniform float uAlbedo;\nuniform float uMaxY; // the highest ground over the site's, for the end of a shadow's march\nuniform float uEarthshine; // the light of the sunlit ground round the shade, a small share of a lit slope's\nuniform float uShadows; // 1 for the ground to shadow itself (the default), 0 for none (a try-out)\nuniform float uSunDegrees; // the Sun's elevation in degrees\nuniform vec3 uNear; // the near march: steps, the distance it reaches in metres, the distance out to which the made craters are in it (the quality of the draw)\n\nuniform sampler2D uH0;\nuniform sampler2D uH1;\nuniform sampler2D uH2;\nuniform sampler2D uH3;\nuniform sampler2D uA0; // the ground picture on the first three levels, 0 to 1 with the middle at 0.5\nuniform sampler2D uA1;\nuniform sampler2D uA2;\nuniform float uPicture; // 1 where the picture is there\nuniform vec4 uL0; // half width, metres a sample, samples across, the distance out to which this level is used\nuniform vec4 uL1;\nuniform vec4 uL2;\nuniform vec4 uL3;\nuniform float uHmin;\nuniform float uHrange;\nuniform float uGround;\nuniform float uRadius;\n\nuniform vec2 uFarHeight; // the range the coarse level is encoded over: its minimum and its span\nfloat dec(vec4 t, vec2 hr) { return hr.x + (t.r * 255.0 * 256.0 + t.g * 255.0) / 65535.0 * hr.y; }\n\nfloat lin(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 f = vec2((p.x + l.x) / l.y - 0.5, (l.x - p.y) / l.y - 0.5);\n vec2 i = floor(f);\n vec2 t = f - i;\n float st = 1.0 / l.z;\n vec2 uv = (i + 0.5) * st;\n float a = dec(texture2D(s, uv), hr);\n float b = dec(texture2D(s, uv + vec2(st, 0.0)), hr);\n float c = dec(texture2D(s, uv + vec2(0.0, st)), hr);\n float d = dec(texture2D(s, uv + vec2(st, st)), hr);\n return mix(mix(a, b, t.x), mix(c, d, t.x), t.y);\n}\n\nfloat nearest(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 uv = vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z));\n return dec(texture2D(s, uv), hr);\n}\n\nbool holds(vec2 p, vec4 l, float dist) { return dist < l.w && abs(p.x) < l.x - 2.0 * l.y && abs(p.y) < l.x - 2.0 * l.y; }\n\n// the ground's height over the site's, the sphere's fall taken off; `fine` reads four samples, otherwise one\nfloat terrainY(vec2 p, float dist, bool fine) {\n float h;\n if (holds(p, uL0, dist)) h = fine ? lin(uH0, p, uL0, vec2(uHmin, uHrange)) : nearest(uH0, p, uL0, vec2(uHmin, uHrange));\n else if (holds(p, uL1, dist)) h = fine ? lin(uH1, p, uL1, vec2(uHmin, uHrange)) : nearest(uH1, p, uL1, vec2(uHmin, uHrange));\n else if (holds(p, uL2, dist)) h = fine ? lin(uH2, p, uL2, vec2(uHmin, uHrange)) : nearest(uH2, p, uL2, vec2(uHmin, uHrange));\n else h = fine ? lin(uH3, p, uL3, uFarHeight) : nearest(uH3, p, uL3, uFarHeight);\n return h - uGround - dot(p, p) / (2.0 * uRadius);\n}\n\n// the ground's own brightness at a point, from the picture of the level that serves it, 1 where it is the middle\nfloat albedoAt(vec2 p, float dist) {\n if (uPicture < 0.5) return 1.0;\n vec4 l;\n if (holds(p, uL0, dist)) { l = uL0; return 2.0 * texture2D(uA0, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL1, dist)) { l = uL1; return 2.0 * texture2D(uA1, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL2, dist)) { l = uL2; return 2.0 * texture2D(uA2, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n return 1.0;\n}\n\n// the measured ground's shadows, baked (scripts/make-moon-terrain.py): for a point, the elevation angle in degrees of the horizon toward the Sun, read from the finest of the three horizon maps that holds the point\nuniform sampler2D uZ0;\nuniform sampler2D uZ1;\nuniform sampler2D uZ2;\nuniform float uHzOn; // 1 where the maps are there\nuniform vec3 uHzSun; // the two directions (of 18) the Sun's azimuth lies between, and the share of the second\nuniform vec2 uHzRange; // the degrees the codes 0 and 255 stand for\n\nfloat hzSector(sampler2D s, vec2 cell, float k) {\n float slab = floor(k / 3.0);\n float ch = k - slab * 3.0;\n vec3 v = texture2D(s, vec2((cell.x + slab) / 6.0, cell.y)).rgb;\n return ch < 0.5 ? v.r : (ch < 1.5 ? v.g : v.b);\n}\n\nfloat hzFrom(sampler2D s, vec2 p, float reach) {\n vec2 cell = clamp(vec2((p.x + reach) / (2.0 * reach), (reach - p.y) / (2.0 * reach)), vec2(0.5 / 512.0), vec2(1.0 - 0.5 / 512.0));\n float code = mix(hzSector(s, cell, uHzSun.x), hzSector(s, cell, uHzSun.y), uHzSun.z);\n return uHzRange.x + code * (uHzRange.y - uHzRange.x);\n}\n\nfloat horizonAt(vec2 p) {\n if (abs(p.x) < uL0.x && abs(p.y) < uL0.x) return hzFrom(uZ0, p, uL0.x);\n if (abs(p.x) < uL1.x && abs(p.y) < uL1.x) return hzFrom(uZ1, p, uL1.x);\n if (abs(p.x) < uL2.x && abs(p.y) < uL2.x) return hzFrom(uZ2, p, uL2.x);\n return -90.0;\n}\n\n// the spacing of the level that serves a point, for the step of a slope\nfloat spacing(vec2 p, float dist) {\n if (holds(p, uL0, dist)) return uL0.y;\n if (holds(p, uL1, dist)) return uL1.y;\n if (holds(p, uL2, dist)) return uL2.y;\n return uL3.y;\n}\n\n\nuniform float uPixel; // the angle one pixel takes, radians\nuniform float uDetail; // 1 for the made detail, 0 for none\n\nuvec2 pcg2d(uvec2 v) {\n v = v * 1664525u + 1013904223u;\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n return v;\n}\nfloat u24(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }\n\n// the made ground at a point: x the height in metres, yz the slope east and north, from every crater of every octave whose nominal diameter is over `fromD`\nvec3 madeGround(vec2 p, float fromD) {\n vec3 sum = vec3(0.0);\n for (int octave = 5; octave >= 0; octave -= 1) {\n float nominal = 0.5 * exp2(float(octave));\n float fade = clamp(nominal / fromD - 1.0, 0.0, 1.0);\n if (fade <= 0.0) break;\n float cell = 3.0 * nominal;\n ivec2 base = ivec2(floor(p / cell));\n vec2 fr = p / cell - vec2(base);\n float occ = (octave >= 3 ? 0.5 : 1.0) * 0.36 * exp2(-0.5 * float(octave));\n for (int j = -1; j <= 1; j += 1) {\n for (int i = -1; i <= 1; i += 1) {\n // a crater is kept to the middle of its cell, so a neighbour's reaches this cell only within a fifth of the edge\n if ((i == -1 && fr.x > 0.2) || (i == 1 && fr.x < 0.8) || (j == -1 && fr.y > 0.2) || (j == 1 && fr.y < 0.8)) continue;\n ivec2 c = base + ivec2(i, j);\n uvec2 ab = pcg2d(uvec2(uint(c.x), uint(c.y + octave * 7919)));\n if (u24(ab.x) >= occ) continue;\n uvec2 cd = pcg2d(uvec2(uint(c.x + 104729), uint(c.y + octave * 7919 + 15485863)));\n float diameter = nominal * (1.0 + (octave == 5 ? 0.25 : 1.0) * u24(cd.y));\n float r4 = float(ab.x & 255u) / 255.0;\n float age = 1.0 - r4 * r4 * r4 * r4;\n float radius = 0.5 * diameter;\n float vary = 0.6 + 0.4 * (float(cd.x & 255u) / 255.0);\n float depth = diameter * (0.2 - 0.17 * age) * vary;\n float rim = 0.045 * diameter * pow(1.0 - age, 1.5);\n float rimWidth = 0.22 + 0.5 * age;\n vec2 centre = vec2(float(c.x) + 0.25 + 0.5 * u24(ab.y), float(c.y) + 0.25 + 0.5 * u24(cd.x)) * cell;\n vec2 d = p - centre;\n float r = length(d);\n float x = r / radius;\n float h = 0.0;\n float dh = 0.0;\n if (x < 1.0) { float bowl = 1.0 - x * x; float pw = 1.0 + age; h = -depth * pow(bowl, pw); dh = 2.0 * depth * pw * x * pow(bowl, age); }\n float e = (x - 1.0) / rimWidth;\n float ex = exp(-e * e);\n h += rim * ex;\n dh += -2.0 * rim * e * ex / rimWidth;\n if (x > 1.0) { float t = 1.0 / x; h += 0.25 * rim * t * t * t; dh += -0.75 * rim * t * t * t * t; }\n float k = r > 1e-6 ? dh / (radius * r) : 0.0;\n sum += fade * vec3(h, k * d.x, k * d.y);\n }\n }\n }\n return sum;\n}\n\nfloat vnoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = p - i;\n vec2 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n uvec2 c = uvec2(ivec2(i));\n float a = u24(pcg2d(c).x);\n float b = u24(pcg2d(c + uvec2(1u, 0u)).x);\n float cc = u24(pcg2d(c + uvec2(0u, 1u)).x);\n float d = u24(pcg2d(c + uvec2(1u, 1u)).x);\n return mix(mix(a, b, w.x), mix(cc, d, w.x), w.y);\n}\n\n// a fine roughness of the dust: two scales, as a slope (east, north), faded out where a pixel covers more than a few of its grains\nvec2 roughness(vec2 p, float footprint) {\n vec2 g = vec2(0.0);\n for (int k = 0; k < 2; k += 1) {\n float s = k == 0 ? 0.07 : 0.3;\n float amp = k == 0 ? 0.3 : 0.28;\n float fade = smoothstep(footprint * 2.0, footprint * 7.0, s);\n if (fade <= 0.0) continue;\n float d = s * 0.25;\n vec2 q = (k == 0 ? vec2(p.x * 0.629 + p.y * 0.777, p.y * 0.629 - p.x * 0.777) : vec2(p.x * 0.857 - p.y * 0.515, p.x * 0.515 + p.y * 0.857)) / s;\n float dx = vnoise(q + vec2(0.25, 0.0)) - vnoise(q - vec2(0.25, 0.0));\n float dy = vnoise(q + vec2(0.0, 0.25)) - vnoise(q - vec2(0.0, 0.25));\n g += fade * amp * vec2(dx, dy) / (0.5 * s) * 0.012;\n }\n return g;\n}\n\n\n// the ground's height at a point: the measured heights and, near the eyes, the made craters (kept to the ones a pixel here can show)\nfloat groundY(vec2 p, float dist, bool fine, float fromD) {\n float y = terrainY(p, dist, fine);\n if (uDetail > 0.5 && fromD < 24.0) y += madeGround(p, fromD).x;\n return y;\n}\n\n// 1 where the Sun reaches a point, 0 where the ground is in its way. The measured ground's shadow is read from the baked horizon; the march toward the Sun over the heights (and the made craters) is the near\n// field's only, a few tens of metres with a fixed number of steps, as sharp as the Sun's edge. Where there is no baked horizon the march goes all the way, as it did before the maps.\nfloat lit(vec3 P, float dist0) {\n vec2 dir = normalize(uSun.xz);\n float slope = uSun.y / max(length(uSun.xz), 1e-4);\n if (uHzOn > 0.5 && horizonAt(P.xz) - 0.3 > uSunDegrees) return 0.0;\n // the ground at the point from the heights themselves, not the mesh's slope between its corners, which can be a long way off on a far mountain\n P.y = groundY(P.xz, dist0, true, 2.5 * dist0 * uPixel);\n float t = 0.5 * spacing(P.xz, dist0) + 0.25;\n if (uDetail > 0.5) t = min(t, 0.2 + 0.02 * dist0);\n // a pixel's own start along the ray: the edge of a rim's shadow is a line of steps, and a line of steps is jagged; with a start of its own each pixel it becomes a fine grain instead\n float jitter = fract(sin(dot(P.xz, vec2(12.9898, 78.233))) * 43758.5453);\n float range = uHzOn > 0.5 ? uNear.y : 80000.0;\n for (int i = 0; i < 260; i += 1) {\n if (float(i) >= uNear.x) break;\n vec2 q = P.xz + dir * t;\n float y = P.y + slope * t;\n float dist = max(t, distance(q, uEyeEN));\n float res = spacing(q, dist);\n float h = groundY(q, dist, t < 600.0, t < uNear.z ? max(0.2 * t, 2.5 * dist * uPixel) : 99.0);\n float bias = (t < 40.0 ? 0.01 : 0.3 * res) + 0.002 * t;\n if (h > y + bias) return 0.0;\n if (y > uMaxY || t > range) break;\n float step = t < 30.0 ? t * 0.045 + 0.04 : t * 0.06 + 0.5;\n t += step * (0.65 + 0.7 * jitter);\n }\n return 1.0;\n}\n\nvarying vec3 vP;\n\nvoid main() {\n float dist = distance(vP.xz, uEyeEN);\n float fromD = 2.5 * dist * uPixel;\n float e = max(spacing(vP.xz, dist), dist * 0.001);\n float hE = terrainY(vP.xz + vec2(e, 0.0), dist, true);\n float hW = terrainY(vP.xz - vec2(e, 0.0), dist, true);\n float hN = terrainY(vP.xz + vec2(0.0, e), dist, true);\n float hS = terrainY(vP.xz - vec2(0.0, e), dist, true);\n vec2 slope = vec2((hE - hW) / (2.0 * e), (hN - hS) / (2.0 * e));\n if (uDetail > 0.5) {\n if (fromD < 24.0) slope += madeGround(vP.xz, fromD).yz;\n slope += roughness(vP.xz, dist * uPixel);\n }\n vec3 n = normalize(vec3(-slope.x, 1.0, -slope.y));\n float mu0 = dot(n, uSun);\n float mu = max(dot(n, normalize(uEye - vP)), 0.03);\n float shine = 0.0;\n if (mu0 > 0.0 && uSun.y > -0.01) shine = (uShadows > 0.5 ? lit(vP, dist) : 1.0) * 2.0 * mu0 / (mu0 + mu);\n float light = uAlbedo * albedoAt(vP.xz, dist) * (shine + uEarthshine * max(n.y, 0.0)) * uExposure;\n gl_FragColor = vec4(vec3(pow(clamp(light, 0.0, 1.0), 1.0 / 2.2)), 1.0);\n}\n";
11
+ /** The rocks: pieces of ground standing on it. Their corners come from the page already in the site's plane (east, up, north). */
12
+ export declare const ROCK_VERTEX = "\nprecision highp float;\n// position, normal and uv are declared by three.js\nattribute float tone;\nuniform mat4 uVP;\nvarying vec3 vP;\nvarying vec3 vN;\nvarying float vTone;\nvoid main() {\n vP = position;\n vN = normal;\n vTone = tone;\n gl_Position = uVP * vec4(position.x, position.y, -position.z, 1.0);\n}\n";
13
+ export declare const ROCK_FRAGMENT = "\nprecision highp float;\nprecision highp sampler2D;\n\nuniform vec3 uSun;\nuniform vec3 uEye;\nuniform vec2 uEyeEN;\nuniform float uExposure;\nuniform float uAlbedo;\nuniform float uMaxY; // the highest ground over the site's, for the end of a shadow's march\nuniform float uEarthshine; // the light of the sunlit ground round the shade, a small share of a lit slope's\nuniform float uShadows; // 1 for the ground to shadow itself (the default), 0 for none (a try-out)\nuniform float uSunDegrees; // the Sun's elevation in degrees\nuniform vec3 uNear; // the near march: steps, the distance it reaches in metres, the distance out to which the made craters are in it (the quality of the draw)\n\nuniform sampler2D uH0;\nuniform sampler2D uH1;\nuniform sampler2D uH2;\nuniform sampler2D uH3;\nuniform sampler2D uA0; // the ground picture on the first three levels, 0 to 1 with the middle at 0.5\nuniform sampler2D uA1;\nuniform sampler2D uA2;\nuniform float uPicture; // 1 where the picture is there\nuniform vec4 uL0; // half width, metres a sample, samples across, the distance out to which this level is used\nuniform vec4 uL1;\nuniform vec4 uL2;\nuniform vec4 uL3;\nuniform float uHmin;\nuniform float uHrange;\nuniform float uGround;\nuniform float uRadius;\n\nuniform vec2 uFarHeight; // the range the coarse level is encoded over: its minimum and its span\nfloat dec(vec4 t, vec2 hr) { return hr.x + (t.r * 255.0 * 256.0 + t.g * 255.0) / 65535.0 * hr.y; }\n\nfloat lin(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 f = vec2((p.x + l.x) / l.y - 0.5, (l.x - p.y) / l.y - 0.5);\n vec2 i = floor(f);\n vec2 t = f - i;\n float st = 1.0 / l.z;\n vec2 uv = (i + 0.5) * st;\n float a = dec(texture2D(s, uv), hr);\n float b = dec(texture2D(s, uv + vec2(st, 0.0)), hr);\n float c = dec(texture2D(s, uv + vec2(0.0, st)), hr);\n float d = dec(texture2D(s, uv + vec2(st, st)), hr);\n return mix(mix(a, b, t.x), mix(c, d, t.x), t.y);\n}\n\nfloat nearest(sampler2D s, vec2 p, vec4 l, vec2 hr) {\n vec2 uv = vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z));\n return dec(texture2D(s, uv), hr);\n}\n\nbool holds(vec2 p, vec4 l, float dist) { return dist < l.w && abs(p.x) < l.x - 2.0 * l.y && abs(p.y) < l.x - 2.0 * l.y; }\n\n// the ground's height over the site's, the sphere's fall taken off; `fine` reads four samples, otherwise one\nfloat terrainY(vec2 p, float dist, bool fine) {\n float h;\n if (holds(p, uL0, dist)) h = fine ? lin(uH0, p, uL0, vec2(uHmin, uHrange)) : nearest(uH0, p, uL0, vec2(uHmin, uHrange));\n else if (holds(p, uL1, dist)) h = fine ? lin(uH1, p, uL1, vec2(uHmin, uHrange)) : nearest(uH1, p, uL1, vec2(uHmin, uHrange));\n else if (holds(p, uL2, dist)) h = fine ? lin(uH2, p, uL2, vec2(uHmin, uHrange)) : nearest(uH2, p, uL2, vec2(uHmin, uHrange));\n else h = fine ? lin(uH3, p, uL3, uFarHeight) : nearest(uH3, p, uL3, uFarHeight);\n return h - uGround - dot(p, p) / (2.0 * uRadius);\n}\n\n// the ground's own brightness at a point, from the picture of the level that serves it, 1 where it is the middle\nfloat albedoAt(vec2 p, float dist) {\n if (uPicture < 0.5) return 1.0;\n vec4 l;\n if (holds(p, uL0, dist)) { l = uL0; return 2.0 * texture2D(uA0, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL1, dist)) { l = uL1; return 2.0 * texture2D(uA1, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n if (holds(p, uL2, dist)) { l = uL2; return 2.0 * texture2D(uA2, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }\n return 1.0;\n}\n\n// the measured ground's shadows, baked (scripts/make-moon-terrain.py): for a point, the elevation angle in degrees of the horizon toward the Sun, read from the finest of the three horizon maps that holds the point\nuniform sampler2D uZ0;\nuniform sampler2D uZ1;\nuniform sampler2D uZ2;\nuniform float uHzOn; // 1 where the maps are there\nuniform vec3 uHzSun; // the two directions (of 18) the Sun's azimuth lies between, and the share of the second\nuniform vec2 uHzRange; // the degrees the codes 0 and 255 stand for\n\nfloat hzSector(sampler2D s, vec2 cell, float k) {\n float slab = floor(k / 3.0);\n float ch = k - slab * 3.0;\n vec3 v = texture2D(s, vec2((cell.x + slab) / 6.0, cell.y)).rgb;\n return ch < 0.5 ? v.r : (ch < 1.5 ? v.g : v.b);\n}\n\nfloat hzFrom(sampler2D s, vec2 p, float reach) {\n vec2 cell = clamp(vec2((p.x + reach) / (2.0 * reach), (reach - p.y) / (2.0 * reach)), vec2(0.5 / 512.0), vec2(1.0 - 0.5 / 512.0));\n float code = mix(hzSector(s, cell, uHzSun.x), hzSector(s, cell, uHzSun.y), uHzSun.z);\n return uHzRange.x + code * (uHzRange.y - uHzRange.x);\n}\n\nfloat horizonAt(vec2 p) {\n if (abs(p.x) < uL0.x && abs(p.y) < uL0.x) return hzFrom(uZ0, p, uL0.x);\n if (abs(p.x) < uL1.x && abs(p.y) < uL1.x) return hzFrom(uZ1, p, uL1.x);\n if (abs(p.x) < uL2.x && abs(p.y) < uL2.x) return hzFrom(uZ2, p, uL2.x);\n return -90.0;\n}\n\n// the spacing of the level that serves a point, for the step of a slope\nfloat spacing(vec2 p, float dist) {\n if (holds(p, uL0, dist)) return uL0.y;\n if (holds(p, uL1, dist)) return uL1.y;\n if (holds(p, uL2, dist)) return uL2.y;\n return uL3.y;\n}\n\n\nuniform float uPixel; // the angle one pixel takes, radians\nuniform float uDetail; // 1 for the made detail, 0 for none\n\nuvec2 pcg2d(uvec2 v) {\n v = v * 1664525u + 1013904223u;\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n v.x += v.y * 1664525u;\n v.y += v.x * 1664525u;\n v = v ^ (v >> 16u);\n return v;\n}\nfloat u24(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }\n\n// the made ground at a point: x the height in metres, yz the slope east and north, from every crater of every octave whose nominal diameter is over `fromD`\nvec3 madeGround(vec2 p, float fromD) {\n vec3 sum = vec3(0.0);\n for (int octave = 5; octave >= 0; octave -= 1) {\n float nominal = 0.5 * exp2(float(octave));\n float fade = clamp(nominal / fromD - 1.0, 0.0, 1.0);\n if (fade <= 0.0) break;\n float cell = 3.0 * nominal;\n ivec2 base = ivec2(floor(p / cell));\n vec2 fr = p / cell - vec2(base);\n float occ = (octave >= 3 ? 0.5 : 1.0) * 0.36 * exp2(-0.5 * float(octave));\n for (int j = -1; j <= 1; j += 1) {\n for (int i = -1; i <= 1; i += 1) {\n // a crater is kept to the middle of its cell, so a neighbour's reaches this cell only within a fifth of the edge\n if ((i == -1 && fr.x > 0.2) || (i == 1 && fr.x < 0.8) || (j == -1 && fr.y > 0.2) || (j == 1 && fr.y < 0.8)) continue;\n ivec2 c = base + ivec2(i, j);\n uvec2 ab = pcg2d(uvec2(uint(c.x), uint(c.y + octave * 7919)));\n if (u24(ab.x) >= occ) continue;\n uvec2 cd = pcg2d(uvec2(uint(c.x + 104729), uint(c.y + octave * 7919 + 15485863)));\n float diameter = nominal * (1.0 + (octave == 5 ? 0.25 : 1.0) * u24(cd.y));\n float r4 = float(ab.x & 255u) / 255.0;\n float age = 1.0 - r4 * r4 * r4 * r4;\n float radius = 0.5 * diameter;\n float vary = 0.6 + 0.4 * (float(cd.x & 255u) / 255.0);\n float depth = diameter * (0.2 - 0.17 * age) * vary;\n float rim = 0.045 * diameter * pow(1.0 - age, 1.5);\n float rimWidth = 0.22 + 0.5 * age;\n vec2 centre = vec2(float(c.x) + 0.25 + 0.5 * u24(ab.y), float(c.y) + 0.25 + 0.5 * u24(cd.x)) * cell;\n vec2 d = p - centre;\n float r = length(d);\n float x = r / radius;\n float h = 0.0;\n float dh = 0.0;\n if (x < 1.0) { float bowl = 1.0 - x * x; float pw = 1.0 + age; h = -depth * pow(bowl, pw); dh = 2.0 * depth * pw * x * pow(bowl, age); }\n float e = (x - 1.0) / rimWidth;\n float ex = exp(-e * e);\n h += rim * ex;\n dh += -2.0 * rim * e * ex / rimWidth;\n if (x > 1.0) { float t = 1.0 / x; h += 0.25 * rim * t * t * t; dh += -0.75 * rim * t * t * t * t; }\n float k = r > 1e-6 ? dh / (radius * r) : 0.0;\n sum += fade * vec3(h, k * d.x, k * d.y);\n }\n }\n }\n return sum;\n}\n\nfloat vnoise(vec2 p) {\n vec2 i = floor(p);\n vec2 f = p - i;\n vec2 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);\n uvec2 c = uvec2(ivec2(i));\n float a = u24(pcg2d(c).x);\n float b = u24(pcg2d(c + uvec2(1u, 0u)).x);\n float cc = u24(pcg2d(c + uvec2(0u, 1u)).x);\n float d = u24(pcg2d(c + uvec2(1u, 1u)).x);\n return mix(mix(a, b, w.x), mix(cc, d, w.x), w.y);\n}\n\n// a fine roughness of the dust: two scales, as a slope (east, north), faded out where a pixel covers more than a few of its grains\nvec2 roughness(vec2 p, float footprint) {\n vec2 g = vec2(0.0);\n for (int k = 0; k < 2; k += 1) {\n float s = k == 0 ? 0.07 : 0.3;\n float amp = k == 0 ? 0.3 : 0.28;\n float fade = smoothstep(footprint * 2.0, footprint * 7.0, s);\n if (fade <= 0.0) continue;\n float d = s * 0.25;\n vec2 q = (k == 0 ? vec2(p.x * 0.629 + p.y * 0.777, p.y * 0.629 - p.x * 0.777) : vec2(p.x * 0.857 - p.y * 0.515, p.x * 0.515 + p.y * 0.857)) / s;\n float dx = vnoise(q + vec2(0.25, 0.0)) - vnoise(q - vec2(0.25, 0.0));\n float dy = vnoise(q + vec2(0.0, 0.25)) - vnoise(q - vec2(0.0, 0.25));\n g += fade * amp * vec2(dx, dy) / (0.5 * s) * 0.012;\n }\n return g;\n}\n\n\n// the ground's height at a point: the measured heights and, near the eyes, the made craters (kept to the ones a pixel here can show)\nfloat groundY(vec2 p, float dist, bool fine, float fromD) {\n float y = terrainY(p, dist, fine);\n if (uDetail > 0.5 && fromD < 24.0) y += madeGround(p, fromD).x;\n return y;\n}\n\n// 1 where the Sun reaches a point, 0 where the ground is in its way. The measured ground's shadow is read from the baked horizon; the march toward the Sun over the heights (and the made craters) is the near\n// field's only, a few tens of metres with a fixed number of steps, as sharp as the Sun's edge. Where there is no baked horizon the march goes all the way, as it did before the maps.\nfloat lit(vec3 P, float dist0) {\n vec2 dir = normalize(uSun.xz);\n float slope = uSun.y / max(length(uSun.xz), 1e-4);\n if (uHzOn > 0.5 && horizonAt(P.xz) - 0.3 > uSunDegrees) return 0.0;\n // the ground at the point from the heights themselves, not the mesh's slope between its corners, which can be a long way off on a far mountain\n P.y = groundY(P.xz, dist0, true, 2.5 * dist0 * uPixel);\n float t = 0.5 * spacing(P.xz, dist0) + 0.25;\n if (uDetail > 0.5) t = min(t, 0.2 + 0.02 * dist0);\n // a pixel's own start along the ray: the edge of a rim's shadow is a line of steps, and a line of steps is jagged; with a start of its own each pixel it becomes a fine grain instead\n float jitter = fract(sin(dot(P.xz, vec2(12.9898, 78.233))) * 43758.5453);\n float range = uHzOn > 0.5 ? uNear.y : 80000.0;\n for (int i = 0; i < 260; i += 1) {\n if (float(i) >= uNear.x) break;\n vec2 q = P.xz + dir * t;\n float y = P.y + slope * t;\n float dist = max(t, distance(q, uEyeEN));\n float res = spacing(q, dist);\n float h = groundY(q, dist, t < 600.0, t < uNear.z ? max(0.2 * t, 2.5 * dist * uPixel) : 99.0);\n float bias = (t < 40.0 ? 0.01 : 0.3 * res) + 0.002 * t;\n if (h > y + bias) return 0.0;\n if (y > uMaxY || t > range) break;\n float step = t < 30.0 ? t * 0.045 + 0.04 : t * 0.06 + 0.5;\n t += step * (0.65 + 0.7 * jitter);\n }\n return 1.0;\n}\n\nvarying vec3 vP;\nvarying vec3 vN;\nvarying float vTone;\nvoid main() {\n vec3 n = normalize(vN);\n float mu0 = dot(n, uSun);\n float mu = max(dot(n, normalize(uEye - vP)), 0.03);\n float dist = distance(vP.xz, uEyeEN);\n float shine = 0.0;\n if (mu0 > 0.0 && uSun.y > -0.01) shine = (uShadows > 0.5 ? lit(vP + n * 0.02, dist) : 1.0) * 2.0 * mu0 / (mu0 + mu);\n float light = uAlbedo * 1.1 * vTone * (shine + 0.12 * (n.y * 0.5 + 0.5)) * uExposure; // the shade of a stone is lit by the ground round it\n gl_FragColor = vec4(vec3(pow(clamp(light, 0.0, 1.0), 1.0 / 2.2)), 1.0);\n}\n";
14
+ /** A rock's shadow on the ground: a quad laid along the way the Sun's light goes, a soft dark shape, drawn over the ground. */
15
+ export declare const SHADOW_VERTEX = "\nprecision highp float;\nuniform mat4 uVP;\nvarying vec2 vUv;\nvoid main() {\n vUv = uv;\n gl_Position = uVP * vec4(position.x, position.y, -position.z, 1.0);\n}\n";
16
+ export declare const SHADOW_FRAGMENT = "\nprecision highp float;\nvarying vec2 vUv;\nvoid main() {\n float u = vUv.x;\n float across = smoothstep(1.0, 0.5, abs(vUv.y));\n float along = smoothstep(0.0, 0.06, u) * (1.0 - smoothstep(0.78, 1.0, u));\n float a = across * along * 0.85;\n if (a < 0.01) discard;\n gl_FragColor = vec4(0.0, 0.0, 0.0, a);\n}\n";
17
+ /** A flat disc that always faces the viewer, set at a direction and a size: the Sun, and the Earth with its lit side toward the Sun. */
18
+ export declare const DISC_VERTEX = "\nprecision highp float;\n// position (declared by three.js) holds the corners of a square, -1 to 1\nuniform mat4 uVP;\nuniform vec3 uEyeWorld;\nuniform vec3 uDirWorld; // unit, scene frame\nuniform vec3 uRightWorld;\nuniform vec3 uUpWorld;\nuniform float uHalf; // half the square's side as a share of the distance\nvarying vec2 vUv;\nvoid main() {\n vUv = position.xy;\n float d = 400000.0;\n vec3 w = uEyeWorld + uDirWorld * d + (uRightWorld * position.x + uUpWorld * position.y) * uHalf * d;\n gl_Position = uVP * vec4(w, 1.0);\n}\n";
19
+ /** The Sun as a disc, or the Earth as a lit sphere: the same shader, `uKind` 0 for the Sun and 1 for the Earth. */
20
+ export declare const DISC_FRAGMENT = "\nprecision highp float;\nuniform float uKind;\nuniform vec3 uSunEnu; // the Sun's direction in the sky's own frame (east, north, up)\nuniform vec3 uDirEnu; // the disc's direction in the same frame\nuniform vec3 uRightEnu;\nuniform vec3 uUpEnu;\nuniform float uExposure;\nvarying vec2 vUv;\nvoid main() {\n float r2 = dot(vUv, vUv);\n if (r2 > 1.0) discard;\n if (uKind < 0.5) { gl_FragColor = vec4(vec3(1.0), 1.0); return; }\n // the sphere's normal toward the viewer, in the sky's frame: the viewer looks along uDirEnu, so the face toward it points the other way\n vec3 nrm = normalize(-uDirEnu * sqrt(1.0 - r2) + uRightEnu * vUv.x + uUpEnu * vUv.y);\n float d = dot(nrm, uSunEnu);\n float day = smoothstep(-0.02, 0.06, d);\n vec3 ocean = vec3(0.10, 0.27, 0.62);\n vec3 cloud = vec3(0.85, 0.88, 0.92);\n float band = 0.5 + 0.5 * sin(nrm.z * 9.0 + nrm.x * 5.0);\n vec3 c = mix(ocean, cloud, smoothstep(0.55, 0.95, band) * 0.6);\n float limb = 1.0 - 0.35 * r2;\n vec3 colour = c * (0.04 + 0.96 * max(d, 0.0)) * day * limb;\n colour += c * 0.012; // the night side's own faint light\n gl_FragColor = vec4(pow(clamp(colour, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}\n";
@@ -0,0 +1,403 @@
1
+ // The shaders of the standing view. The ground is one mesh of rings and rays round the viewer's feet, its heights read in the vertex shader from the levels of the height pyramid (the finest
2
+ // that holds the point and is near enough), its normal worked out from the same heights in the fragment shader, and its light from the Sun's direction: Lommel-Seeliger, the law the Moon's
3
+ // dust follows, and a hard shadow found by walking from the point toward the Sun over the same heights (a horizon-angle march) rather than from a shadow map, which cannot hold a
4
+ // stone's shadow and a mountain's at once. No air: no haze, a black sky, and what the Sun does not reach is black.
5
+ // The scene's frame is x east, y up, z south; the maths here is in the site's own plane (east, up, north), so a z is flipped on the way to the screen.
6
+ /** The uniforms of the pyramid every shader reads, and the one function that reads it: `terrainY(point, distance)`, metres over the site's ground, the Moon's curve taken off. */
7
+ export const HEIGHTS = /* glsl */ `
8
+ uniform sampler2D uH0;
9
+ uniform sampler2D uH1;
10
+ uniform sampler2D uH2;
11
+ uniform sampler2D uH3;
12
+ uniform sampler2D uA0; // the ground picture on the first three levels, 0 to 1 with the middle at 0.5
13
+ uniform sampler2D uA1;
14
+ uniform sampler2D uA2;
15
+ uniform float uPicture; // 1 where the picture is there
16
+ uniform vec4 uL0; // half width, metres a sample, samples across, the distance out to which this level is used
17
+ uniform vec4 uL1;
18
+ uniform vec4 uL2;
19
+ uniform vec4 uL3;
20
+ uniform float uHmin;
21
+ uniform float uHrange;
22
+ uniform float uGround;
23
+ uniform float uRadius;
24
+
25
+ uniform vec2 uFarHeight; // the range the coarse level is encoded over: its minimum and its span
26
+ float dec(vec4 t, vec2 hr) { return hr.x + (t.r * 255.0 * 256.0 + t.g * 255.0) / 65535.0 * hr.y; }
27
+
28
+ float lin(sampler2D s, vec2 p, vec4 l, vec2 hr) {
29
+ vec2 f = vec2((p.x + l.x) / l.y - 0.5, (l.x - p.y) / l.y - 0.5);
30
+ vec2 i = floor(f);
31
+ vec2 t = f - i;
32
+ float st = 1.0 / l.z;
33
+ vec2 uv = (i + 0.5) * st;
34
+ float a = dec(texture2D(s, uv), hr);
35
+ float b = dec(texture2D(s, uv + vec2(st, 0.0)), hr);
36
+ float c = dec(texture2D(s, uv + vec2(0.0, st)), hr);
37
+ float d = dec(texture2D(s, uv + vec2(st, st)), hr);
38
+ return mix(mix(a, b, t.x), mix(c, d, t.x), t.y);
39
+ }
40
+
41
+ float nearest(sampler2D s, vec2 p, vec4 l, vec2 hr) {
42
+ vec2 uv = vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z));
43
+ return dec(texture2D(s, uv), hr);
44
+ }
45
+
46
+ bool holds(vec2 p, vec4 l, float dist) { return dist < l.w && abs(p.x) < l.x - 2.0 * l.y && abs(p.y) < l.x - 2.0 * l.y; }
47
+
48
+ // the ground's height over the site's, the sphere's fall taken off; \`fine\` reads four samples, otherwise one
49
+ float terrainY(vec2 p, float dist, bool fine) {
50
+ float h;
51
+ if (holds(p, uL0, dist)) h = fine ? lin(uH0, p, uL0, vec2(uHmin, uHrange)) : nearest(uH0, p, uL0, vec2(uHmin, uHrange));
52
+ else if (holds(p, uL1, dist)) h = fine ? lin(uH1, p, uL1, vec2(uHmin, uHrange)) : nearest(uH1, p, uL1, vec2(uHmin, uHrange));
53
+ else if (holds(p, uL2, dist)) h = fine ? lin(uH2, p, uL2, vec2(uHmin, uHrange)) : nearest(uH2, p, uL2, vec2(uHmin, uHrange));
54
+ else h = fine ? lin(uH3, p, uL3, uFarHeight) : nearest(uH3, p, uL3, uFarHeight);
55
+ return h - uGround - dot(p, p) / (2.0 * uRadius);
56
+ }
57
+
58
+ // the ground's own brightness at a point, from the picture of the level that serves it, 1 where it is the middle
59
+ float albedoAt(vec2 p, float dist) {
60
+ if (uPicture < 0.5) return 1.0;
61
+ vec4 l;
62
+ if (holds(p, uL0, dist)) { l = uL0; return 2.0 * texture2D(uA0, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }
63
+ if (holds(p, uL1, dist)) { l = uL1; return 2.0 * texture2D(uA1, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }
64
+ if (holds(p, uL2, dist)) { l = uL2; return 2.0 * texture2D(uA2, vec2((p.x + l.x) / (l.y * l.z), (l.x - p.y) / (l.y * l.z))).r; }
65
+ return 1.0;
66
+ }
67
+
68
+ // the measured ground's shadows, baked (scripts/make-moon-terrain.py): for a point, the elevation angle in degrees of the horizon toward the Sun, read from the finest of the three horizon maps that holds the point
69
+ uniform sampler2D uZ0;
70
+ uniform sampler2D uZ1;
71
+ uniform sampler2D uZ2;
72
+ uniform float uHzOn; // 1 where the maps are there
73
+ uniform vec3 uHzSun; // the two directions (of 18) the Sun's azimuth lies between, and the share of the second
74
+ uniform vec2 uHzRange; // the degrees the codes 0 and 255 stand for
75
+
76
+ float hzSector(sampler2D s, vec2 cell, float k) {
77
+ float slab = floor(k / 3.0);
78
+ float ch = k - slab * 3.0;
79
+ vec3 v = texture2D(s, vec2((cell.x + slab) / 6.0, cell.y)).rgb;
80
+ return ch < 0.5 ? v.r : (ch < 1.5 ? v.g : v.b);
81
+ }
82
+
83
+ float hzFrom(sampler2D s, vec2 p, float reach) {
84
+ vec2 cell = clamp(vec2((p.x + reach) / (2.0 * reach), (reach - p.y) / (2.0 * reach)), vec2(0.5 / 512.0), vec2(1.0 - 0.5 / 512.0));
85
+ float code = mix(hzSector(s, cell, uHzSun.x), hzSector(s, cell, uHzSun.y), uHzSun.z);
86
+ return uHzRange.x + code * (uHzRange.y - uHzRange.x);
87
+ }
88
+
89
+ float horizonAt(vec2 p) {
90
+ if (abs(p.x) < uL0.x && abs(p.y) < uL0.x) return hzFrom(uZ0, p, uL0.x);
91
+ if (abs(p.x) < uL1.x && abs(p.y) < uL1.x) return hzFrom(uZ1, p, uL1.x);
92
+ if (abs(p.x) < uL2.x && abs(p.y) < uL2.x) return hzFrom(uZ2, p, uL2.x);
93
+ return -90.0;
94
+ }
95
+
96
+ // the spacing of the level that serves a point, for the step of a slope
97
+ float spacing(vec2 p, float dist) {
98
+ if (holds(p, uL0, dist)) return uL0.y;
99
+ if (holds(p, uL1, dist)) return uL1.y;
100
+ if (holds(p, uL2, dist)) return uL2.y;
101
+ return uL3.y;
102
+ }
103
+ `;
104
+ /** The made detail, in GLSL: the same formulas as `detail.ts` (craters on a seeded grid, by octave), and a fine roughness for the normals. */
105
+ export const DETAIL = /* glsl */ `
106
+ uniform float uPixel; // the angle one pixel takes, radians
107
+ uniform float uDetail; // 1 for the made detail, 0 for none
108
+
109
+ uvec2 pcg2d(uvec2 v) {
110
+ v = v * 1664525u + 1013904223u;
111
+ v.x += v.y * 1664525u;
112
+ v.y += v.x * 1664525u;
113
+ v = v ^ (v >> 16u);
114
+ v.x += v.y * 1664525u;
115
+ v.y += v.x * 1664525u;
116
+ v = v ^ (v >> 16u);
117
+ return v;
118
+ }
119
+ float u24(uint h) { return float(h >> 8u) * (1.0 / 16777216.0); }
120
+
121
+ // the made ground at a point: x the height in metres, yz the slope east and north, from every crater of every octave whose nominal diameter is over \`fromD\`
122
+ vec3 madeGround(vec2 p, float fromD) {
123
+ vec3 sum = vec3(0.0);
124
+ for (int octave = 5; octave >= 0; octave -= 1) {
125
+ float nominal = 0.5 * exp2(float(octave));
126
+ float fade = clamp(nominal / fromD - 1.0, 0.0, 1.0);
127
+ if (fade <= 0.0) break;
128
+ float cell = 3.0 * nominal;
129
+ ivec2 base = ivec2(floor(p / cell));
130
+ vec2 fr = p / cell - vec2(base);
131
+ float occ = (octave >= 3 ? 0.5 : 1.0) * 0.36 * exp2(-0.5 * float(octave));
132
+ for (int j = -1; j <= 1; j += 1) {
133
+ for (int i = -1; i <= 1; i += 1) {
134
+ // a crater is kept to the middle of its cell, so a neighbour's reaches this cell only within a fifth of the edge
135
+ if ((i == -1 && fr.x > 0.2) || (i == 1 && fr.x < 0.8) || (j == -1 && fr.y > 0.2) || (j == 1 && fr.y < 0.8)) continue;
136
+ ivec2 c = base + ivec2(i, j);
137
+ uvec2 ab = pcg2d(uvec2(uint(c.x), uint(c.y + octave * 7919)));
138
+ if (u24(ab.x) >= occ) continue;
139
+ uvec2 cd = pcg2d(uvec2(uint(c.x + 104729), uint(c.y + octave * 7919 + 15485863)));
140
+ float diameter = nominal * (1.0 + (octave == 5 ? 0.25 : 1.0) * u24(cd.y));
141
+ float r4 = float(ab.x & 255u) / 255.0;
142
+ float age = 1.0 - r4 * r4 * r4 * r4;
143
+ float radius = 0.5 * diameter;
144
+ float vary = 0.6 + 0.4 * (float(cd.x & 255u) / 255.0);
145
+ float depth = diameter * (0.2 - 0.17 * age) * vary;
146
+ float rim = 0.045 * diameter * pow(1.0 - age, 1.5);
147
+ float rimWidth = 0.22 + 0.5 * age;
148
+ vec2 centre = vec2(float(c.x) + 0.25 + 0.5 * u24(ab.y), float(c.y) + 0.25 + 0.5 * u24(cd.x)) * cell;
149
+ vec2 d = p - centre;
150
+ float r = length(d);
151
+ float x = r / radius;
152
+ float h = 0.0;
153
+ float dh = 0.0;
154
+ if (x < 1.0) { float bowl = 1.0 - x * x; float pw = 1.0 + age; h = -depth * pow(bowl, pw); dh = 2.0 * depth * pw * x * pow(bowl, age); }
155
+ float e = (x - 1.0) / rimWidth;
156
+ float ex = exp(-e * e);
157
+ h += rim * ex;
158
+ dh += -2.0 * rim * e * ex / rimWidth;
159
+ if (x > 1.0) { float t = 1.0 / x; h += 0.25 * rim * t * t * t; dh += -0.75 * rim * t * t * t * t; }
160
+ float k = r > 1e-6 ? dh / (radius * r) : 0.0;
161
+ sum += fade * vec3(h, k * d.x, k * d.y);
162
+ }
163
+ }
164
+ }
165
+ return sum;
166
+ }
167
+
168
+ float vnoise(vec2 p) {
169
+ vec2 i = floor(p);
170
+ vec2 f = p - i;
171
+ vec2 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
172
+ uvec2 c = uvec2(ivec2(i));
173
+ float a = u24(pcg2d(c).x);
174
+ float b = u24(pcg2d(c + uvec2(1u, 0u)).x);
175
+ float cc = u24(pcg2d(c + uvec2(0u, 1u)).x);
176
+ float d = u24(pcg2d(c + uvec2(1u, 1u)).x);
177
+ return mix(mix(a, b, w.x), mix(cc, d, w.x), w.y);
178
+ }
179
+
180
+ // a fine roughness of the dust: two scales, as a slope (east, north), faded out where a pixel covers more than a few of its grains
181
+ vec2 roughness(vec2 p, float footprint) {
182
+ vec2 g = vec2(0.0);
183
+ for (int k = 0; k < 2; k += 1) {
184
+ float s = k == 0 ? 0.07 : 0.3;
185
+ float amp = k == 0 ? 0.3 : 0.28;
186
+ float fade = smoothstep(footprint * 2.0, footprint * 7.0, s);
187
+ if (fade <= 0.0) continue;
188
+ float d = s * 0.25;
189
+ vec2 q = (k == 0 ? vec2(p.x * 0.629 + p.y * 0.777, p.y * 0.629 - p.x * 0.777) : vec2(p.x * 0.857 - p.y * 0.515, p.x * 0.515 + p.y * 0.857)) / s;
190
+ float dx = vnoise(q + vec2(0.25, 0.0)) - vnoise(q - vec2(0.25, 0.0));
191
+ float dy = vnoise(q + vec2(0.0, 0.25)) - vnoise(q - vec2(0.0, 0.25));
192
+ g += fade * amp * vec2(dx, dy) / (0.5 * s) * 0.012;
193
+ }
194
+ return g;
195
+ }
196
+ `;
197
+ /** The ground's vertices: a ring index and an angle across the view, laid out as the viewer's own polar grid, then raised to the measured heights. */
198
+ export const GROUND_VERTEX = /* glsl */ `
199
+ precision highp float;
200
+ precision highp sampler2D;
201
+ // position (declared by three.js) holds x: 0 to 1 out along the rays (ring spacing grows with the distance), y: -1 to 1 across the view
202
+ uniform mat4 uVP;
203
+ uniform vec2 uEyeEN; // the eyes' place in the site's plane: east, north
204
+ uniform float uYaw; // radians, clockwise from north
205
+ uniform float uSpan; // half the angle drawn across the view, radians
206
+ uniform float uRMin;
207
+ uniform float uRMax;
208
+ ${HEIGHTS}
209
+ ${DETAIL}
210
+ varying vec3 vP;
211
+ void main() {
212
+ float r = uRMin * pow(uRMax / uRMin, position.x);
213
+ float a = uYaw + position.y * uSpan;
214
+ vec2 p = uEyeEN + r * vec2(sin(a), cos(a));
215
+ float y = terrainY(p, r, true);
216
+ // the made craters, those the mesh can hold here: bigger than a few of its steps
217
+ if (uDetail > 0.5 && r < 3000.0) y += madeGround(p, max(2.5 * r * uPixel, 0.06 * r)).x;
218
+ vP = vec3(p.x, y, p.y);
219
+ gl_Position = uVP * vec4(p.x, y, -p.y, 1.0);
220
+ }
221
+ `;
222
+ /** What the ground, the rocks and their shadows all need to be lit: the Sun, the eyes, the heights and the made detail, the ground's height with the craters on it, and a point's light (a hard shadow by a march toward the Sun). */
223
+ export const LIGHT = /* glsl */ `
224
+ uniform vec3 uSun;
225
+ uniform vec3 uEye;
226
+ uniform vec2 uEyeEN;
227
+ uniform float uExposure;
228
+ uniform float uAlbedo;
229
+ uniform float uMaxY; // the highest ground over the site's, for the end of a shadow's march
230
+ uniform float uEarthshine; // the light of the sunlit ground round the shade, a small share of a lit slope's
231
+ uniform float uShadows; // 1 for the ground to shadow itself (the default), 0 for none (a try-out)
232
+ uniform float uSunDegrees; // the Sun's elevation in degrees
233
+ uniform vec3 uNear; // the near march: steps, the distance it reaches in metres, the distance out to which the made craters are in it (the quality of the draw)
234
+ ${HEIGHTS}
235
+ ${DETAIL}
236
+
237
+ // the ground's height at a point: the measured heights and, near the eyes, the made craters (kept to the ones a pixel here can show)
238
+ float groundY(vec2 p, float dist, bool fine, float fromD) {
239
+ float y = terrainY(p, dist, fine);
240
+ if (uDetail > 0.5 && fromD < 24.0) y += madeGround(p, fromD).x;
241
+ return y;
242
+ }
243
+
244
+ // 1 where the Sun reaches a point, 0 where the ground is in its way. The measured ground's shadow is read from the baked horizon; the march toward the Sun over the heights (and the made craters) is the near
245
+ // field's only, a few tens of metres with a fixed number of steps, as sharp as the Sun's edge. Where there is no baked horizon the march goes all the way, as it did before the maps.
246
+ float lit(vec3 P, float dist0) {
247
+ vec2 dir = normalize(uSun.xz);
248
+ float slope = uSun.y / max(length(uSun.xz), 1e-4);
249
+ if (uHzOn > 0.5 && horizonAt(P.xz) - 0.3 > uSunDegrees) return 0.0;
250
+ // the ground at the point from the heights themselves, not the mesh's slope between its corners, which can be a long way off on a far mountain
251
+ P.y = groundY(P.xz, dist0, true, 2.5 * dist0 * uPixel);
252
+ float t = 0.5 * spacing(P.xz, dist0) + 0.25;
253
+ if (uDetail > 0.5) t = min(t, 0.2 + 0.02 * dist0);
254
+ // a pixel's own start along the ray: the edge of a rim's shadow is a line of steps, and a line of steps is jagged; with a start of its own each pixel it becomes a fine grain instead
255
+ float jitter = fract(sin(dot(P.xz, vec2(12.9898, 78.233))) * 43758.5453);
256
+ float range = uHzOn > 0.5 ? uNear.y : 80000.0;
257
+ for (int i = 0; i < 260; i += 1) {
258
+ if (float(i) >= uNear.x) break;
259
+ vec2 q = P.xz + dir * t;
260
+ float y = P.y + slope * t;
261
+ float dist = max(t, distance(q, uEyeEN));
262
+ float res = spacing(q, dist);
263
+ float h = groundY(q, dist, t < 600.0, t < uNear.z ? max(0.2 * t, 2.5 * dist * uPixel) : 99.0);
264
+ float bias = (t < 40.0 ? 0.01 : 0.3 * res) + 0.002 * t;
265
+ if (h > y + bias) return 0.0;
266
+ if (y > uMaxY || t > range) break;
267
+ float step = t < 30.0 ? t * 0.045 + 0.04 : t * 0.06 + 0.5;
268
+ t += step * (0.65 + 0.7 * jitter);
269
+ }
270
+ return 1.0;
271
+ }
272
+ `;
273
+ /** The ground's light. */
274
+ export const GROUND_FRAGMENT = /* glsl */ `
275
+ precision highp float;
276
+ precision highp sampler2D;
277
+ ${LIGHT}
278
+ varying vec3 vP;
279
+
280
+ void main() {
281
+ float dist = distance(vP.xz, uEyeEN);
282
+ float fromD = 2.5 * dist * uPixel;
283
+ float e = max(spacing(vP.xz, dist), dist * 0.001);
284
+ float hE = terrainY(vP.xz + vec2(e, 0.0), dist, true);
285
+ float hW = terrainY(vP.xz - vec2(e, 0.0), dist, true);
286
+ float hN = terrainY(vP.xz + vec2(0.0, e), dist, true);
287
+ float hS = terrainY(vP.xz - vec2(0.0, e), dist, true);
288
+ vec2 slope = vec2((hE - hW) / (2.0 * e), (hN - hS) / (2.0 * e));
289
+ if (uDetail > 0.5) {
290
+ if (fromD < 24.0) slope += madeGround(vP.xz, fromD).yz;
291
+ slope += roughness(vP.xz, dist * uPixel);
292
+ }
293
+ vec3 n = normalize(vec3(-slope.x, 1.0, -slope.y));
294
+ float mu0 = dot(n, uSun);
295
+ float mu = max(dot(n, normalize(uEye - vP)), 0.03);
296
+ float shine = 0.0;
297
+ if (mu0 > 0.0 && uSun.y > -0.01) shine = (uShadows > 0.5 ? lit(vP, dist) : 1.0) * 2.0 * mu0 / (mu0 + mu);
298
+ float light = uAlbedo * albedoAt(vP.xz, dist) * (shine + uEarthshine * max(n.y, 0.0)) * uExposure;
299
+ gl_FragColor = vec4(vec3(pow(clamp(light, 0.0, 1.0), 1.0 / 2.2)), 1.0);
300
+ }
301
+ `;
302
+ /** The rocks: pieces of ground standing on it. Their corners come from the page already in the site's plane (east, up, north). */
303
+ export const ROCK_VERTEX = /* glsl */ `
304
+ precision highp float;
305
+ // position, normal and uv are declared by three.js
306
+ attribute float tone;
307
+ uniform mat4 uVP;
308
+ varying vec3 vP;
309
+ varying vec3 vN;
310
+ varying float vTone;
311
+ void main() {
312
+ vP = position;
313
+ vN = normal;
314
+ vTone = tone;
315
+ gl_Position = uVP * vec4(position.x, position.y, -position.z, 1.0);
316
+ }
317
+ `;
318
+ export const ROCK_FRAGMENT = /* glsl */ `
319
+ precision highp float;
320
+ precision highp sampler2D;
321
+ ${LIGHT}
322
+ varying vec3 vP;
323
+ varying vec3 vN;
324
+ varying float vTone;
325
+ void main() {
326
+ vec3 n = normalize(vN);
327
+ float mu0 = dot(n, uSun);
328
+ float mu = max(dot(n, normalize(uEye - vP)), 0.03);
329
+ float dist = distance(vP.xz, uEyeEN);
330
+ float shine = 0.0;
331
+ if (mu0 > 0.0 && uSun.y > -0.01) shine = (uShadows > 0.5 ? lit(vP + n * 0.02, dist) : 1.0) * 2.0 * mu0 / (mu0 + mu);
332
+ float light = uAlbedo * 1.1 * vTone * (shine + 0.12 * (n.y * 0.5 + 0.5)) * uExposure; // the shade of a stone is lit by the ground round it
333
+ gl_FragColor = vec4(vec3(pow(clamp(light, 0.0, 1.0), 1.0 / 2.2)), 1.0);
334
+ }
335
+ `;
336
+ /** A rock's shadow on the ground: a quad laid along the way the Sun's light goes, a soft dark shape, drawn over the ground. */
337
+ export const SHADOW_VERTEX = /* glsl */ `
338
+ precision highp float;
339
+ uniform mat4 uVP;
340
+ varying vec2 vUv;
341
+ void main() {
342
+ vUv = uv;
343
+ gl_Position = uVP * vec4(position.x, position.y, -position.z, 1.0);
344
+ }
345
+ `;
346
+ export const SHADOW_FRAGMENT = /* glsl */ `
347
+ precision highp float;
348
+ varying vec2 vUv;
349
+ void main() {
350
+ float u = vUv.x;
351
+ float across = smoothstep(1.0, 0.5, abs(vUv.y));
352
+ float along = smoothstep(0.0, 0.06, u) * (1.0 - smoothstep(0.78, 1.0, u));
353
+ float a = across * along * 0.85;
354
+ if (a < 0.01) discard;
355
+ gl_FragColor = vec4(0.0, 0.0, 0.0, a);
356
+ }
357
+ `;
358
+ /** A flat disc that always faces the viewer, set at a direction and a size: the Sun, and the Earth with its lit side toward the Sun. */
359
+ export const DISC_VERTEX = /* glsl */ `
360
+ precision highp float;
361
+ // position (declared by three.js) holds the corners of a square, -1 to 1
362
+ uniform mat4 uVP;
363
+ uniform vec3 uEyeWorld;
364
+ uniform vec3 uDirWorld; // unit, scene frame
365
+ uniform vec3 uRightWorld;
366
+ uniform vec3 uUpWorld;
367
+ uniform float uHalf; // half the square's side as a share of the distance
368
+ varying vec2 vUv;
369
+ void main() {
370
+ vUv = position.xy;
371
+ float d = 400000.0;
372
+ vec3 w = uEyeWorld + uDirWorld * d + (uRightWorld * position.x + uUpWorld * position.y) * uHalf * d;
373
+ gl_Position = uVP * vec4(w, 1.0);
374
+ }
375
+ `;
376
+ /** The Sun as a disc, or the Earth as a lit sphere: the same shader, `uKind` 0 for the Sun and 1 for the Earth. */
377
+ export const DISC_FRAGMENT = /* glsl */ `
378
+ precision highp float;
379
+ uniform float uKind;
380
+ uniform vec3 uSunEnu; // the Sun's direction in the sky's own frame (east, north, up)
381
+ uniform vec3 uDirEnu; // the disc's direction in the same frame
382
+ uniform vec3 uRightEnu;
383
+ uniform vec3 uUpEnu;
384
+ uniform float uExposure;
385
+ varying vec2 vUv;
386
+ void main() {
387
+ float r2 = dot(vUv, vUv);
388
+ if (r2 > 1.0) discard;
389
+ if (uKind < 0.5) { gl_FragColor = vec4(vec3(1.0), 1.0); return; }
390
+ // the sphere's normal toward the viewer, in the sky's frame: the viewer looks along uDirEnu, so the face toward it points the other way
391
+ vec3 nrm = normalize(-uDirEnu * sqrt(1.0 - r2) + uRightEnu * vUv.x + uUpEnu * vUv.y);
392
+ float d = dot(nrm, uSunEnu);
393
+ float day = smoothstep(-0.02, 0.06, d);
394
+ vec3 ocean = vec3(0.10, 0.27, 0.62);
395
+ vec3 cloud = vec3(0.85, 0.88, 0.92);
396
+ float band = 0.5 + 0.5 * sin(nrm.z * 9.0 + nrm.x * 5.0);
397
+ vec3 c = mix(ocean, cloud, smoothstep(0.55, 0.95, band) * 0.6);
398
+ float limb = 1.0 - 0.35 * r2;
399
+ vec3 colour = c * (0.04 + 0.96 * max(d, 0.0)) * day * limb;
400
+ colour += c * 0.012; // the night side's own faint light
401
+ gl_FragColor = vec4(pow(clamp(colour, 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
402
+ }
403
+ `;