@johnmorrisdotca/chikyu-earth 0.27.0 → 0.28.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.28.0 - 2026-11-11
6
+
7
+ - **The air is light scattered once, worked out in the shader** (MODULES-843). The realistic Earth and the space view draw the atmosphere as a shell 100 km thick (0.0157 of the Earth's radius), sampled along each ray toward the camera and toward the Sun in the fragment shader, with the samples packed where the air is thick: Rayleigh scattering (a blue haze that thickens toward the limb on the day side), Mie scattering (a glow toward the Sun), the reddening along the day and night line, and the thin lit ring seen from the night side looking toward the Sun are what the sums give, not things drawn. The surface under it is dimmed by what the air takes out of the light. The old rim in the surface program is gone. Our own code, from the published single-scattering model (Nishita and others, 1993; GPU Gems 2, chapter 16), no code taken from any project. `air="light"` (6 samples along the ray and 2 toward the Sun; the default on a small or slow device) or `air="full"` (12 and 4), on both elements. It costs nothing at rest, at most 30 frames a second as before. New in `/realistic`: `AIR_TOP`, `AIR_QUALITY`, `AIR_VERT`, `airFrag`, `airFor` and `AirSamples`. `ATMOSPHERE_OUTER` stays the room the page leaves for it. Guide in English and Japanese: `docs/guides/full-screen-and-camera.md`.
8
+
5
9
  ## 0.27.0 - 2026-11-10
6
10
 
7
11
  - **The demo's line and thick globes open turning where it can be seen** (MODULES-898). Auto-turn opens on a new 1440× button (a day a minute, a turn in about a minute) beside 0.5×, 1× and 2×; 1× is still the Earth's own rate, `0.004178` degrees a second, and the package's elements and the embed page are unchanged. The 3D looks keep their own 6 degrees a second, and going back to a line look puts the 1440× back. A reader who prefers reduced motion still gets a still globe with Auto-turn ready. Tests: `e2e/globe-turn.demo.ts`, `e2e/speed.demo.ts`.
package/README.md CHANGED
@@ -489,7 +489,7 @@ console.log(countryList().filter((country) => country.alpha2 === null).map((coun
489
489
 
490
490
  ## How to cite
491
491
 
492
- > Morris, J. (2026). *Chikyu Earth* (version 0.27.0) [Computer software]. https://github.com/johnmorrisdotca/chikyu-earth
492
+ > Morris, J. (2026). *Chikyu Earth* (version 0.28.0) [Computer software]. https://github.com/johnmorrisdotca/chikyu-earth
493
493
 
494
494
  GitHub's "Cite this repository" button reads [CITATION.cff](CITATION.cff) and gives BibTeX and APA. Cite the version you used (`npm ls @johnmorrisdotca/chikyu-earth`), and credit the sources behind the numbers and pictures: [NOTICE.md](NOTICE.md) says who made each one and under what terms.
495
495
 
@@ -0,0 +1,77 @@
1
+ /**
2
+ * The air's own top, in Earth radii: 100 km of the Earth's 6,371. The element leaves `ATMOSPHERE_OUTER` of room for it, so it is never cut at the box's edge.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { AIR_TOP } from "@johnmorrisdotca/chikyu-earth/realistic";
7
+ * console.log(((AIR_TOP - 1) * 6371).toFixed(0), "km thick");
8
+ * ```
9
+ */
10
+ export declare const AIR_TOP: number;
11
+ /**
12
+ * How many samples the air takes: along the ray toward the camera and, at each of those, along the ray toward the Sun.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { AIR_QUALITY, type AirSamples } from "@johnmorrisdotca/chikyu-earth/realistic";
17
+ * const light: AirSamples = AIR_QUALITY.light;
18
+ * console.log(light.view, light.light);
19
+ * ```
20
+ */
21
+ export interface AirSamples {
22
+ view: number;
23
+ light: number;
24
+ }
25
+ /**
26
+ * The two costs of the air. `full` takes 12 samples along the ray toward the camera and 4 toward the Sun at each; `light` (a small or slow device) takes 6 and 2.
27
+ * Both put their samples where the air is thick (close to the ground, or at the nearest point of a ray that skims it), not evenly, so even the light one has no bands.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import { AIR_QUALITY } from "@johnmorrisdotca/chikyu-earth/realistic";
32
+ * console.log(AIR_QUALITY.full);
33
+ * ```
34
+ */
35
+ export declare const AIR_QUALITY: Record<"full" | "light", AirSamples>;
36
+ /**
37
+ * How much the air costs for a page: what its `air` attribute says (`full` or `light`), else `light` on a small or slow device (4 cores or fewer, 4 GB of memory or less, or a touch screen with no mouse) and `full` on the rest.
38
+ *
39
+ * @param given - The page's `air` attribute, or `null` when it has none.
40
+ * @returns `full` or `light`.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * import { airFor } from "@johnmorrisdotca/chikyu-earth/realistic";
45
+ * console.log(airFor("light"));
46
+ * ```
47
+ */
48
+ export declare function airFor(given: string | null): "full" | "light";
49
+ /**
50
+ * The air's program: where the shell is, in the world's own axes (the Earth sits at the origin and does not turn here, the view turns round it).
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import { AIR_VERT } from "@johnmorrisdotca/chikyu-earth/realistic";
55
+ * console.log(AIR_VERT.includes("vWorld"));
56
+ * ```
57
+ */
58
+ export declare const AIR_VERT = "\nvarying vec3 vWorld;\nvoid main() {\n vWorld = (modelMatrix * vec4(position, 1.0)).xyz;\n gl_Position = projectionMatrix * viewMatrix * vec4(vWorld, 1.0);\n}";
59
+ /**
60
+ * The air as light scattered once, worked out for each pixel: a ray from the camera through the shell, sampled `view` times, each sample looking `light` times toward the Sun.
61
+ * Rayleigh scattering (the air's own molecules: thicker the longer the path, and bluer) and Mie scattering (haze and dust: a bright halo round the Sun's direction), with the
62
+ * scale heights and coefficients of the standard sea-level air (Rayleigh 8 km, Mie 1.2 km; the Mie coefficient is that of very clear air, 0.4 of the usual, so the haze stays blue). What the sums give, and nothing drawn: the blue haze thickening toward the limb on the day side,
63
+ * the glow toward the Sun, the reddening along the day and night line (the Sun's light has crossed a long, low path) and the thin lit ring seen from the night side looking toward the Sun.
64
+ * The surface under it is dimmed by what the air takes out of the light (the shader's alpha is what gets through) and what it adds is the colour.
65
+ * Our own code, from the published single-scattering model (Nishita and others, 1993; O'Neil, GPU Gems 2, chapter 16), no code taken from any project.
66
+ * The Sun is in the world's axes (`sunDir`, toward the Sun); `ortho` is 1 when the camera looks down the z axis from far away (the realistic Earth) and 0 for a camera at a place (the space view).
67
+ *
68
+ * @param samples - How many samples to take (`AIR_QUALITY.full` or `AIR_QUALITY.light`).
69
+ * @returns The fragment program's source, for a `ShaderMaterial`.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * import { airFrag, AIR_QUALITY } from "@johnmorrisdotca/chikyu-earth/realistic";
74
+ * console.log(airFrag(AIR_QUALITY.light).includes("N_VIEW 6"));
75
+ * ```
76
+ */
77
+ export declare const airFrag: (samples: AirSamples) => string;
@@ -0,0 +1,177 @@
1
+ // The air round the Earth: light scattered once on its way through a shell 100 km thick, worked out for each pixel in the fragment shader and added to what is behind it.
2
+ // Both elements that draw the Earth (the realistic one and the space view) use this one program; it knows nothing of either, only the Earth at the origin with a radius of 1.
3
+ /**
4
+ * The air's own top, in Earth radii: 100 km of the Earth's 6,371. The element leaves `ATMOSPHERE_OUTER` of room for it, so it is never cut at the box's edge.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { AIR_TOP } from "@johnmorrisdotca/chikyu-earth/realistic";
9
+ * console.log(((AIR_TOP - 1) * 6371).toFixed(0), "km thick");
10
+ * ```
11
+ */
12
+ export const AIR_TOP = 1 + 100 / 6371;
13
+ /**
14
+ * The two costs of the air. `full` takes 12 samples along the ray toward the camera and 4 toward the Sun at each; `light` (a small or slow device) takes 6 and 2.
15
+ * Both put their samples where the air is thick (close to the ground, or at the nearest point of a ray that skims it), not evenly, so even the light one has no bands.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { AIR_QUALITY } from "@johnmorrisdotca/chikyu-earth/realistic";
20
+ * console.log(AIR_QUALITY.full);
21
+ * ```
22
+ */
23
+ export const AIR_QUALITY = {
24
+ full: { view: 12, light: 4 },
25
+ light: { view: 6, light: 2 },
26
+ };
27
+ /**
28
+ * How much the air costs for a page: what its `air` attribute says (`full` or `light`), else `light` on a small or slow device (4 cores or fewer, 4 GB of memory or less, or a touch screen with no mouse) and `full` on the rest.
29
+ *
30
+ * @param given - The page's `air` attribute, or `null` when it has none.
31
+ * @returns `full` or `light`.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { airFor } from "@johnmorrisdotca/chikyu-earth/realistic";
36
+ * console.log(airFor("light"));
37
+ * ```
38
+ */
39
+ export function airFor(given) {
40
+ if (given === "full" || given === "light")
41
+ return given;
42
+ const nav = typeof navigator === "undefined" ? undefined : navigator;
43
+ const coarse = typeof matchMedia === "function" && matchMedia("(pointer: coarse) and (hover: none)").matches;
44
+ return coarse || (nav?.hardwareConcurrency ?? 8) <= 4 || (nav?.deviceMemory ?? 8) <= 4 ? "light" : "full";
45
+ }
46
+ /**
47
+ * The air's program: where the shell is, in the world's own axes (the Earth sits at the origin and does not turn here, the view turns round it).
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import { AIR_VERT } from "@johnmorrisdotca/chikyu-earth/realistic";
52
+ * console.log(AIR_VERT.includes("vWorld"));
53
+ * ```
54
+ */
55
+ export const AIR_VERT = /* glsl */ `
56
+ varying vec3 vWorld;
57
+ void main() {
58
+ vWorld = (modelMatrix * vec4(position, 1.0)).xyz;
59
+ gl_Position = projectionMatrix * viewMatrix * vec4(vWorld, 1.0);
60
+ }`;
61
+ /**
62
+ * The air as light scattered once, worked out for each pixel: a ray from the camera through the shell, sampled `view` times, each sample looking `light` times toward the Sun.
63
+ * Rayleigh scattering (the air's own molecules: thicker the longer the path, and bluer) and Mie scattering (haze and dust: a bright halo round the Sun's direction), with the
64
+ * scale heights and coefficients of the standard sea-level air (Rayleigh 8 km, Mie 1.2 km; the Mie coefficient is that of very clear air, 0.4 of the usual, so the haze stays blue). What the sums give, and nothing drawn: the blue haze thickening toward the limb on the day side,
65
+ * the glow toward the Sun, the reddening along the day and night line (the Sun's light has crossed a long, low path) and the thin lit ring seen from the night side looking toward the Sun.
66
+ * The surface under it is dimmed by what the air takes out of the light (the shader's alpha is what gets through) and what it adds is the colour.
67
+ * Our own code, from the published single-scattering model (Nishita and others, 1993; O'Neil, GPU Gems 2, chapter 16), no code taken from any project.
68
+ * The Sun is in the world's axes (`sunDir`, toward the Sun); `ortho` is 1 when the camera looks down the z axis from far away (the realistic Earth) and 0 for a camera at a place (the space view).
69
+ *
70
+ * @param samples - How many samples to take (`AIR_QUALITY.full` or `AIR_QUALITY.light`).
71
+ * @returns The fragment program's source, for a `ShaderMaterial`.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * import { airFrag, AIR_QUALITY } from "@johnmorrisdotca/chikyu-earth/realistic";
76
+ * console.log(airFrag(AIR_QUALITY.light).includes("N_VIEW 6"));
77
+ * ```
78
+ */
79
+ export const airFrag = (samples) => /* glsl */ `
80
+ #define N_VIEW ${samples.view}
81
+ #define N_LIGHT ${samples.light}
82
+ uniform vec3 sunDir;
83
+ uniform float ortho;
84
+ varying vec3 vWorld;
85
+ const float TOP = ${AIR_TOP.toFixed(6)};
86
+ const float HR = 0.001256;
87
+ const float HM = 0.000188;
88
+ const vec3 BETA_R = vec3(36.95, 86.0, 210.9);
89
+ const float BETA_M = 53.5;
90
+ const float G = 0.76;
91
+ const float PI = 3.14159265358979;
92
+ const float SUN = 110.0;
93
+ // the nearest and the farthest point where a ray from o along d meets a sphere of radius r about the Earth's centre (-1 for both when it misses)
94
+ vec2 hit(vec3 o, vec3 d, float r) {
95
+ float b = dot(o, d);
96
+ float h = b * b - (dot(o, o) - r * r);
97
+ if (h < 0.0) return vec2(-1.0, -1.0);
98
+ h = sqrt(h);
99
+ return vec2(-b - h, -b + h);
100
+ }
101
+ // Sample i of n along a ray of length tEnd whose air is thickest at tDense: half of them each side of it, close together near it and wide apart away from it. Gives where it is (t) and the length of ray it stands for (dt).
102
+ void place(int i, int n, float tDense, float tEnd, out float t, out float dt) {
103
+ float near = tDense;
104
+ float far = max(tEnd - tDense, 0.0);
105
+ float nNear = near < 1e-6 ? 0.0 : (far < 1e-6 ? float(n) : floor(float(n) * 0.5));
106
+ float fi = float(i);
107
+ if (fi < nNear) {
108
+ float w = 1.0 - (fi + 0.5) / nNear;
109
+ t = near * (1.0 - w * w);
110
+ dt = 2.0 * near * w / nNear;
111
+ } else {
112
+ float cnt = float(n) - nNear;
113
+ float v = (fi - nNear + 0.5) / cnt;
114
+ t = tDense + far * v * v;
115
+ dt = 2.0 * far * v / cnt;
116
+ }
117
+ }
118
+ // the air's thickness (Rayleigh, Mie) from p along dir to the top of the shell
119
+ vec2 column(vec3 p, vec3 dir) {
120
+ float tEnd = hit(p, dir, TOP).y;
121
+ float tDense = clamp(-dot(p, dir), 0.0, tEnd);
122
+ vec2 sum = vec2(0.0);
123
+ for (int j = 0; j < N_LIGHT; j++) {
124
+ float t;
125
+ float dt;
126
+ place(j, N_LIGHT, tDense, tEnd, t, dt);
127
+ float height = max(length(p + dir * t) - 1.0, 0.0);
128
+ sum += vec2(exp(-height / HR), exp(-height / HM)) * dt;
129
+ }
130
+ return sum;
131
+ }
132
+ void main() {
133
+ vec3 o = vWorld;
134
+ vec3 d = ortho > 0.5 ? vec3(0.0, 0.0, -1.0) : normalize(vWorld - cameraPosition);
135
+ float b = dot(o, d);
136
+ // the ray ends where it meets the ground, or leaves the shell; the air is thickest at the ground, or at the nearest point of a ray that skims it
137
+ vec2 ground = hit(o, d, 1.0);
138
+ bool onGround = ground.x > 0.0;
139
+ float tEnd = onGround ? ground.x : hit(o, d, TOP).y;
140
+ float tDense = onGround ? ground.x : clamp(-b, 0.0, tEnd);
141
+ float mu = dot(d, sunDir);
142
+ float phaseR = 3.0 / (16.0 * PI) * (1.0 + mu * mu);
143
+ float phaseM = 3.0 / (8.0 * PI) * ((1.0 - G * G) * (1.0 + mu * mu)) / ((2.0 + G * G) * pow(1.0 + G * G - 2.0 * G * mu, 1.5));
144
+ // most of the disc is a short, steep path through the air: half the samples do for it, and only the limb takes them all
145
+ int used = onGround && tEnd < 0.08 ? N_VIEW / 2 : N_VIEW;
146
+ vec2 depth = vec2(0.0);
147
+ vec3 sumR = vec3(0.0);
148
+ vec3 sumM = vec3(0.0);
149
+ for (int i = 0; i < N_VIEW; i++) {
150
+ if (i >= used) break;
151
+ float t;
152
+ float dt;
153
+ place(i, used, tDense, tEnd, t, dt);
154
+ vec3 p = o + d * t;
155
+ float height = max(length(p) - 1.0, 0.0);
156
+ vec2 here = vec2(exp(-height / HR), exp(-height / HM)) * dt;
157
+ float shade = 1.0;
158
+ // behind the Earth's edge the Sun's light does not reach; the edge is soft by a few kilometres, as the lowest air makes it
159
+ float ahead = dot(p, sunDir);
160
+ if (ahead < 0.0) shade = smoothstep(1.0 - 0.003, 1.0 + 0.003, sqrt(max(dot(p, p) - ahead * ahead, 0.0)));
161
+ vec2 sun = shade > 0.0 ? column(p, sunDir) : vec2(0.0);
162
+ vec2 path = depth + 0.5 * here;
163
+ vec3 gone = exp(-(BETA_R * (path.x + sun.x) + BETA_M * 1.1 * (path.y + sun.y)));
164
+ sumR += gone * here.x * shade;
165
+ sumM += gone * here.y * shade;
166
+ depth += here;
167
+ }
168
+ vec3 colour = SUN * (sumR * BETA_R * phaseR + sumM * BETA_M * phaseM);
169
+ vec3 through = exp(-(BETA_R * depth.x + BETA_M * 1.1 * depth.y));
170
+ // the colour is premultiplied: what is added, and how much of what is behind it gets through
171
+ // a steep look at the ground through the air has only a thin veil over it; the haze gathers toward the limb, where the path through the air is long
172
+ colour *= onGround ? 0.1 + 0.9 * smoothstep(0.02, 0.16, tEnd) : 1.0;
173
+ // brightness is squeezed to what a screen shows by its strongest channel, so a thick, bright path stays blue instead of going grey; the curve keeps thin air quiet
174
+ float peak = max(max(colour.r, colour.g), colour.b);
175
+ vec3 shown = colour * pow(1.0 - exp(-peak), 2.0) / max(peak, 1e-6);
176
+ gl_FragColor = vec4(shown, 1.0 - through.g);
177
+ }`;
@@ -97,6 +97,8 @@ export interface EarthDeps {
97
97
  invalidate: () => void;
98
98
  onError?: (error: unknown) => void;
99
99
  onTier?: (layer: Layer, width: number) => void;
100
+ /** How much the air costs: `full` (12 samples along the ray and 4 toward the Sun) or `light` (6 and 2, for a small or slow device). Read once, when the scene is built. */
101
+ air?: () => "full" | "light";
100
102
  /** The satellites could not be drawn (the three.js parts handed over lack `Points`). */
101
103
  onSatellitesError?: (error: unknown) => void;
102
104
  /** Told at the start of every draw, before anything is read for it. */
@@ -1,5 +1,6 @@
1
1
  import { HOME_VIEW, lookAt } from "../view.js";
2
- import { ATMOSPHERE_FRAG, ATMOSPHERE_VERT, CLOUD_FRAG, SURFACE_FRAG, SURFACE_VERT } from "./shaders.js";
2
+ import { AIR_QUALITY, AIR_TOP, AIR_VERT, airFrag } from "./air.js";
3
+ import { CLOUD_FRAG, SURFACE_FRAG, SURFACE_VERT } from "./shaders.js";
3
4
  import { greenwichSidereal, subsolarPoint, unitVector } from "./sun.js";
4
5
  import { createSatelliteLayer, newSatelliteState } from "../space/satellite-layer.js";
5
6
  import { createTextureSet } from "./textures.js";
@@ -146,13 +147,14 @@ export function createEarthItem(model, deps) {
146
147
  transparent: true,
147
148
  depthWrite: false,
148
149
  });
150
+ // the air: light scattered once on its way through a shell 100 km thick, worked out for each pixel and laid over the surface. A device that is small or slow takes fewer samples (the `light` quality).
149
151
  const atmosphere = new three.ShaderMaterial({
150
- vertexShader: ATMOSPHERE_VERT,
151
- fragmentShader: ATMOSPHERE_FRAG,
152
- uniforms: { sunView: sunDir, outer: { value: ATMOSPHERE_OUTER }, ortho: { value: 1 } },
152
+ vertexShader: AIR_VERT,
153
+ fragmentShader: airFrag(AIR_QUALITY[deps.air?.() ?? "full"]),
154
+ uniforms: { sunDir, ortho: { value: 1 } },
153
155
  transparent: true,
156
+ premultipliedAlpha: true,
154
157
  depthWrite: false,
155
- side: three.BackSide,
156
158
  });
157
159
  const turned = new three.Group();
158
160
  const earth = new three.Mesh(grid, surface);
@@ -163,8 +165,8 @@ export function createEarthItem(model, deps) {
163
165
  clouds.renderOrder = 2;
164
166
  turned.add(earth, clouds);
165
167
  const glow = new three.Mesh(grid, atmosphere);
166
- glow.scale.setScalar(ATMOSPHERE_OUTER);
167
- glow.renderOrder = 1;
168
+ glow.scale.setScalar(AIR_TOP);
169
+ glow.renderOrder = 3;
168
170
  glow.frustumCulled = false;
169
171
  const scene = new three.Scene();
170
172
  scene.add(turned, glow);
@@ -3,6 +3,7 @@
3
3
  // changes, at most 30 times a second, and nothing at all while it is out of sight or the tab is hidden.
4
4
  import { carryControls } from "@johnmorrisdotca/kazoku-ui/view-controls";
5
5
  import { adoptStyles } from "../adopt.js";
6
+ import { airFor } from "./air.js";
6
7
  import { cameraCentre, createEarthItem, discRadius, lookAt, newModel } from "./earth.js";
7
8
  import { MAX_SATELLITE_SPEED } from "../satellites/limits.js";
8
9
  import { MAX_ORBIT_GROUP } from "../space/satellite-layer.js";
@@ -206,6 +207,7 @@ export class ChikyuEarthRealistic extends HTMLElement {
206
207
  }
207
208
  this.dispatchEvent(new CustomEvent("chikyu-tiles", { detail: { layers } }));
208
209
  },
210
+ air: () => airFor(this.getAttribute("air")),
209
211
  invalidate: () => this.#gate?.soon(),
210
212
  onDraw: () => this.#gate?.drawn(),
211
213
  onPose: (pose) => {
@@ -5,3 +5,4 @@ export { lookAt, discRadius, ATMOSPHERE_OUTER, cameraCentre, type CameraMode, ty
5
5
  export type { ThreeParts } from "./earth.ts";
6
6
  export { TEXEL_SLACK, coveringTile, parseManifest, pointOnEarth, tileColumns, tileLevel, tileRect, tileRows, tilesInView, type TileCamera, type TileId, type TileLayer, type VisibleTile } from "./tiles.ts";
7
7
  export { TILE_KEEP_MS, TILE_LIMIT } from "./tile-set.ts";
8
+ export { AIR_QUALITY, AIR_TOP, AIR_VERT, airFor, airFrag, type AirSamples } from "./air.ts";
@@ -6,3 +6,4 @@ export { createGate, frameCap } from "./gate.js";
6
6
  export { lookAt, discRadius, ATMOSPHERE_OUTER, cameraCentre } from "./earth.js";
7
7
  export { TEXEL_SLACK, coveringTile, parseManifest, pointOnEarth, tileColumns, tileLevel, tileRect, tileRows, tilesInView } from "./tiles.js";
8
8
  export { TILE_KEEP_MS, TILE_LIMIT } from "./tile-set.js";
9
+ export { AIR_QUALITY, AIR_TOP, AIR_VERT, airFor, airFrag } from "./air.js";
@@ -8,8 +8,6 @@ export declare const TILE_FRAG: string;
8
8
  */
9
9
  export declare const TILE_VERT = "\nuniform vec4 rect;\nuniform float radius;\nvarying vec2 vUv;\nvarying vec2 vUvG;\nvarying vec3 vNormal;\nvarying vec3 vWorld;\nvoid main() {\n vUv = uv;\n float lon = mix(rect.x, rect.z, uv.x);\n float lat = mix(rect.y, rect.w, uv.y);\n vec3 unit = vec3(cos(lat) * sin(lon), sin(lat), cos(lat) * cos(lon));\n vUvG = vec2((lon + 3.14159265358979) / 6.28318530717959, 0.5 - lat / 3.14159265358979);\n vNormal = normalize(mat3(modelMatrix) * unit);\n vec4 world = modelMatrix * vec4(unit * radius, 1.0);\n vWorld = world.xyz;\n gl_Position = projectionMatrix * viewMatrix * world;\n}";
10
10
  export declare const CLOUD_FRAG = "\nuniform sampler2D cloudMap;\nuniform vec3 sunDir;\nuniform float shift;\nuniform float opacity;\nvarying vec2 vUv;\nvarying vec3 vNormal;\nvoid main() {\n vec3 n = normalize(vNormal);\n float ndl = dot(n, sunDir);\n float c = texture2D(cloudMap, vec2(vUv.x + shift, vUv.y)).r;\n float a = smoothstep(0.28, 0.88, c) * opacity;\n float day = smoothstep(-0.10, 0.25, ndl);\n vec3 col = mix(vec3(0.015, 0.02, 0.04), vec3(1.0), day * (0.18 + 0.82 * pow(max(ndl, 0.0), 0.45)));\n col += vec3(0.55, 0.28, 0.14) * smoothstep(-0.14, 0.0, ndl) * (1.0 - smoothstep(0.0, 0.25, ndl)) * 0.35;\n gl_FragColor = vec4(col, a);\n}";
11
- export declare const ATMOSPHERE_VERT = "\nvarying vec3 vPosView;\nvarying vec3 vNormalView;\nvoid main() {\n vec4 view = modelViewMatrix * vec4(position, 1.0);\n vPosView = view.xyz;\n vNormalView = normalize(normalMatrix * position);\n gl_Position = projectionMatrix * view;\n}";
12
- export declare const ATMOSPHERE_FRAG = "\nuniform vec3 sunView;\nuniform float outer;\nuniform float ortho;\nvarying vec3 vPosView;\nvarying vec3 vNormalView;\nvoid main() {\n vec3 n = normalize(vNormalView);\n vec3 v = ortho > 0.5 ? vec3(0.0, 0.0, 1.0) : normalize(-vPosView);\n float nz = clamp(dot(n, v), -1.0, 1.0);\n float rho = outer * sqrt(max(0.0, 1.0 - nz * nz));\n float t = clamp((rho - 1.0) / (outer - 1.0), 0.0, 1.0);\n vec2 dir = normalize(n.xy + vec2(1e-5, 0.0));\n // the Sun's light at this point of the limb: 1 where the Sun stands behind it, 0 at the terminator, -1 on the night side\n float toward = dot(vec3(dir, 0.0), sunView);\n float side = smoothstep(-0.45, 0.45, toward);\n // a thin bright layer on the limb and a wide faint halo, both fading to nothing at the outer edge\n float limb = pow(1.0 - t, 5.0);\n float halo = pow(1.0 - t, 2.2);\n float glow = (limb * 0.85 + halo * 0.35) * (0.06 + 0.94 * side);\n vec3 day = mix(vec3(0.30, 0.52, 0.95), vec3(0.50, 0.74, 1.0), limb);\n vec3 dusk = mix(vec3(1.0, 0.42, 0.16), vec3(1.0, 0.62, 0.30), limb);\n float band = exp(-pow(toward / 0.32, 2.0));\n vec3 col = mix(mix(vec3(0.06, 0.12, 0.35), day, side), dusk, band * 0.85);\n gl_FragColor = vec4(col, clamp(glow, 0.0, 1.0) * (1.0 - t));\n}";
13
11
  /** The Moon and other plain bodies: a picture (or a flat colour) lit by the Sun, with a trace of light on the dark side. */
14
12
  export declare const BODY_FRAG = "\nuniform sampler2D map;\nuniform vec3 tint;\nuniform float textured;\nuniform float ambient;\nuniform vec3 sunDir;\nvarying vec2 vUv;\nvarying vec3 vNormal;\nvoid main() {\n vec3 n = normalize(vNormal);\n float ndl = dot(n, sunDir);\n vec3 albedo = textured > 0.5 ? texture2D(map, vUv).rgb : tint;\n float lit = smoothstep(-0.04, 0.12, ndl) * (0.10 + 0.95 * pow(max(ndl, 0.0), 0.6));\n gl_FragColor = vec4(albedo * (ambient + lit), 1.0);\n}";
15
13
  /** A flat quad facing the camera: the Sun's disc and its glow. */
@@ -1,5 +1,5 @@
1
- // The three programs of the realistic Earth, as GLSL strings: the surface (day, night, sun glint, rim), the cloud layer and the
2
- // atmosphere's glow. Colours are shaded in the textures' own (sRGB) space: a look, not a radiometric model.
1
+ // The programs of the realistic Earth, as GLSL strings: the surface (day, night, sun glint), the cloud layer and the Moon and the Sun's glow. The air is `air.ts`.
2
+ // Colours are shaded in the textures' own (sRGB) space: a look, not a radiometric model.
3
3
  export const SURFACE_VERT = /* glsl */ `
4
4
  varying vec2 vUv;
5
5
  varying vec3 vNormal;
@@ -17,8 +17,8 @@ void main() {
17
17
  * - the Sun's light reddens towards the terminator and the twilight has a warm band on the lit side and a blue hour beyond it;
18
18
  * - the ocean is a clearer blue, with a hard glint under the Sun, a broad sheen round it, and the sky reflected at grazing angles; clouds hide the glint;
19
19
  * - the clouds throw a shadow on the ground, displaced away from the Sun and longer as it gets low;
20
- * - the night side has the city lights warm and softly spread, dimmed where cloud covers them;
21
- * - the air shows as a rim on the limb, blue by day and orange along the terminator.
20
+ * - the night side has the city lights warm and softly spread, dimmed where cloud covers them.
21
+ * The air over it is the air program's (`air.ts`), drawn over the surface, so the surface has no rim of its own.
22
22
  */
23
23
  const surfaceFrag = (tile) => /* glsl */ `
24
24
  uniform sampler2D dayMap;
@@ -83,10 +83,6 @@ void main() {
83
83
  vec3 col = mix(night, lit, day);
84
84
  col += pow(albedo, vec3(0.75)) * boost * 0.65 * (1.0 - day);
85
85
 
86
- float rim = pow(1.0 - ndv, 3.0);
87
- float sunSide = 0.12 + 0.88 * smoothstep(-0.25, 0.5, ndl);
88
- vec3 rimColour = mix(vec3(0.20, 0.45, 1.0), vec3(1.0, 0.45, 0.18), 0.8 * (1.0 - smoothstep(0.0, 0.30, abs(ndl))));
89
- col += rimColour * rim * sunSide * 0.6;
90
86
  gl_FragColor = vec4(col, 1.0);
91
87
  }`;
92
88
  export const SURFACE_FRAG = surfaceFrag(false);
@@ -131,41 +127,6 @@ void main() {
131
127
  col += vec3(0.55, 0.28, 0.14) * smoothstep(-0.14, 0.0, ndl) * (1.0 - smoothstep(0.0, 0.25, ndl)) * 0.35;
132
128
  gl_FragColor = vec4(col, a);
133
129
  }`;
134
- export const ATMOSPHERE_VERT = /* glsl */ `
135
- varying vec3 vPosView;
136
- varying vec3 vNormalView;
137
- void main() {
138
- vec4 view = modelViewMatrix * vec4(position, 1.0);
139
- vPosView = view.xyz;
140
- vNormalView = normalize(normalMatrix * position);
141
- gl_Position = projectionMatrix * view;
142
- }`;
143
- export const ATMOSPHERE_FRAG = /* glsl */ `
144
- uniform vec3 sunView;
145
- uniform float outer;
146
- uniform float ortho;
147
- varying vec3 vPosView;
148
- varying vec3 vNormalView;
149
- void main() {
150
- vec3 n = normalize(vNormalView);
151
- vec3 v = ortho > 0.5 ? vec3(0.0, 0.0, 1.0) : normalize(-vPosView);
152
- float nz = clamp(dot(n, v), -1.0, 1.0);
153
- float rho = outer * sqrt(max(0.0, 1.0 - nz * nz));
154
- float t = clamp((rho - 1.0) / (outer - 1.0), 0.0, 1.0);
155
- vec2 dir = normalize(n.xy + vec2(1e-5, 0.0));
156
- // the Sun's light at this point of the limb: 1 where the Sun stands behind it, 0 at the terminator, -1 on the night side
157
- float toward = dot(vec3(dir, 0.0), sunView);
158
- float side = smoothstep(-0.45, 0.45, toward);
159
- // a thin bright layer on the limb and a wide faint halo, both fading to nothing at the outer edge
160
- float limb = pow(1.0 - t, 5.0);
161
- float halo = pow(1.0 - t, 2.2);
162
- float glow = (limb * 0.85 + halo * 0.35) * (0.06 + 0.94 * side);
163
- vec3 day = mix(vec3(0.30, 0.52, 0.95), vec3(0.50, 0.74, 1.0), limb);
164
- vec3 dusk = mix(vec3(1.0, 0.42, 0.16), vec3(1.0, 0.62, 0.30), limb);
165
- float band = exp(-pow(toward / 0.32, 2.0));
166
- vec3 col = mix(mix(vec3(0.06, 0.12, 0.35), day, side), dusk, band * 0.85);
167
- gl_FragColor = vec4(col, clamp(glow, 0.0, 1.0) * (1.0 - t));
168
- }`;
169
130
  /** The Moon and other plain bodies: a picture (or a flat colour) lit by the Sun, with a trace of light on the dark side. */
170
131
  export const BODY_FRAG = /* glsl */ `
171
132
  uniform sampler2D map;
@@ -1,5 +1,6 @@
1
1
  import { carryControls } from "@johnmorrisdotca/kazoku-ui/view-controls";
2
2
  import { adoptStyles } from "../adopt.js";
3
+ import { airFor } from "../realistic/air.js";
3
4
  import { createGate, frameCap } from "../realistic/gate.js";
4
5
  import { setThreeLoader, takeRenderer } from "../realistic/shared.js";
5
6
  import { METEOR_NOTE_STYLE, prefersLessMotion, setMeteorNote } from "../meteors/note.js";
@@ -171,6 +172,7 @@ export class ChikyuSpace extends HTMLElement {
171
172
  assets: taken.renderer.assets,
172
173
  textureBase: () => this.#textureBase(),
173
174
  cap: () => this.#textureCap(),
175
+ air: () => airFor(this.getAttribute("air")),
174
176
  invalidate: () => this.#gate?.request(),
175
177
  onError: (error) => this.#fail(error instanceof Error ? error.message : String(error)),
176
178
  onTier: (layer, width) => {
@@ -274,6 +274,8 @@ export interface SpaceDeps {
274
274
  invalidate: () => void;
275
275
  onError?: (error: unknown) => void;
276
276
  onTier?: (layer: Layer, width: number) => void;
277
+ /** How much the air costs: `full` (12 samples along the ray and 4 toward the Sun) or `light` (6 and 2, for a small or slow device). Read once, when the scene is built. */
278
+ air?: () => "full" | "light";
277
279
  /** Where each body is on the screen, in CSS pixels in the box, or null when it is behind the camera. Called at each draw. */
278
280
  onLabels?: (places: Record<BodyName, BodyPlace | null>) => void;
279
281
  /** Where the chosen satellite is on the screen, or null. Called at each draw while a satellite is chosen (and once when it is let go). */
@@ -1,5 +1,6 @@
1
- import { ATMOSPHERE_OUTER, sphere } from "../realistic/earth.js";
2
- import { ATMOSPHERE_FRAG, ATMOSPHERE_VERT, BODY_FRAG, CLOUD_FRAG, GLOW_FRAG, GLOW_VERT, SURFACE_FRAG, SURFACE_VERT } from "../realistic/shaders.js";
1
+ import { AIR_QUALITY, AIR_TOP, AIR_VERT, airFrag } from "../realistic/air.js";
2
+ import { sphere } from "../realistic/earth.js";
3
+ import { BODY_FRAG, CLOUD_FRAG, GLOW_FRAG, GLOW_VERT, SURFACE_FRAG, SURFACE_VERT } from "../realistic/shaders.js";
3
4
  import { createTextureSet } from "../realistic/textures.js";
4
5
  import { createTrack, orbitPath, PLANETS } from "./ephemeris.js";
5
6
  import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
@@ -161,7 +162,6 @@ export function createSpaceItem(model, deps) {
161
162
  /** The Sun in the Earth's own axes (the surface program finds where a cloud's shadow falls from it). */
162
163
  const sunLocal = { value: new three.Vector3(1, 0, 0) };
163
164
  const cloudShift = { value: 0 };
164
- const sunView = { value: new three.Vector3(1, 0, 0) };
165
165
  const grid = sphere(three, 96, 48);
166
166
  const solid = sphere(three, 48, 24);
167
167
  const earthMaterial = new three.ShaderMaterial({
@@ -176,21 +176,22 @@ export function createSpaceItem(model, deps) {
176
176
  transparent: true,
177
177
  depthWrite: false,
178
178
  });
179
+ // the air: light scattered once on its way through a shell 100 km thick, worked out for each pixel (the same program as the realistic Earth's), in the world's own axes
179
180
  const atmosphereMaterial = new three.ShaderMaterial({
180
- vertexShader: ATMOSPHERE_VERT,
181
- fragmentShader: ATMOSPHERE_FRAG,
182
- uniforms: { sunView, outer: { value: ATMOSPHERE_OUTER }, ortho: { value: 0 } },
181
+ vertexShader: AIR_VERT,
182
+ fragmentShader: airFrag(AIR_QUALITY[deps.air?.() ?? "full"]),
183
+ uniforms: { sunDir: sunEarth, ortho: { value: 0 } },
183
184
  transparent: true,
185
+ premultipliedAlpha: true,
184
186
  depthWrite: false,
185
- side: three.BackSide,
186
187
  });
187
188
  const earth = new three.Mesh(grid, earthMaterial);
188
189
  const clouds = new three.Mesh(grid, cloudMaterial);
189
190
  clouds.scale.setScalar(1.006);
190
191
  clouds.renderOrder = 3;
191
192
  const glow = new three.Mesh(grid, atmosphereMaterial);
192
- glow.scale.setScalar(ATMOSPHERE_OUTER);
193
- glow.renderOrder = 2;
193
+ glow.scale.setScalar(AIR_TOP);
194
+ glow.renderOrder = 4;
194
195
  glow.frustumCulled = false;
195
196
  const moonSun = { value: new three.Vector3(1, 0, 0) };
196
197
  const moon = new three.Mesh(solid, new three.ShaderMaterial({ vertexShader: SURFACE_VERT, fragmentShader: BODY_FRAG, uniforms: { map: uniform.moon, tint: { value: new three.Color("#bbbbbb") }, textured: { value: 1 }, sunDir: moonSun, ambient: { value: 0.03 } } }));
@@ -483,7 +484,6 @@ export function createSpaceItem(model, deps) {
483
484
  // always lit: the light comes from the camera's side of the Earth (the Sun, Moon and planets stay where they are)
484
485
  if (model.light === "lit")
485
486
  sunEarth.value.set(camera.position.x, camera.position.y, camera.position.z).normalize();
486
- sunView.value.copy(sunEarth.value).transformDirection(camera.matrixWorldInverse);
487
487
  const pose = model.pose;
488
488
  pose.p[0] = camera.position.x;
489
489
  pose.p[1] = camera.position.y;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "Chikyu Earth 地球 — A line-drawn globe for the web: country outlines that turn at the Earth's own rate, centred on where you are, with an optional realistic Earth from space (three.js), the Sun, Moon and planets, satellites on their orbits, and markers for any place. A web component and an SVG string.",
5
5
  "keywords": [
6
6
  "earth",