@johnmorrisdotca/chikyu-earth 0.7.0 → 0.9.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.
@@ -3,6 +3,8 @@ import { ATMOSPHERE_FRAG, ATMOSPHERE_VERT, BODY_FRAG, CLOUD_FRAG, GLOW_FRAG, GLO
3
3
  import { createTextureSet } from "../realistic/textures.js";
4
4
  import { createTrack, orbitPath, PLANETS } from "./ephemeris.js";
5
5
  import { placeAround, placeMoon, placeSun, RADIUS } from "./scale.js";
6
+ import { ridePose } from "./ride.js";
7
+ import { createSatelliteLayer, newSatelliteState } from "./satellite-layer.js";
6
8
  const RAD = Math.PI / 180;
7
9
  /** The camera's distance, in Earth radii, at which the Earth fills the view at zoom 1. */
8
10
  export const FIT_DISTANCE = 3.4;
@@ -34,6 +36,16 @@ export const newSpaceModel = () => ({
34
36
  focus: "Earth",
35
37
  drawn: null,
36
38
  lookups: 0,
39
+ speed: 1,
40
+ ride: null,
41
+ rideLost: false,
42
+ transition: null,
43
+ drawnView: null,
44
+ heightPx: 420,
45
+ pose: { p: [0, 0, 0], q: [0, 0, 0, 1], fov: FOV, valid: false },
46
+ rideMotion: 0,
47
+ satellites: newSatelliteState(),
48
+ pick: null,
37
49
  ratio: () => Math.min(globalThis.devicePixelRatio || 1, 2),
38
50
  });
39
51
  /** An inertial direction from the Earth's centre, in scene axes, for right ascension and declination in degrees. */
@@ -162,9 +174,21 @@ export function createSpaceItem(model, deps) {
162
174
  scene.add(drawing.object, orbit);
163
175
  planets.set(name, { drawing, orbit, positions, samples: [], epoch: Number.NaN });
164
176
  }
177
+ let layer;
178
+ const rideState = new Float64Array(6);
179
+ let selectedShown = false;
165
180
  const camera = new three.PerspectiveCamera(FOV, width / Math.max(1, height), 0.05, 1000);
166
181
  const target = new three.Vector3();
182
+ const desiredQ = camera.quaternion.clone();
183
+ const fromQ = camera.quaternion.clone();
167
184
  const projected = new three.Vector3();
185
+ const placeAll = (at) => {
186
+ const sunAt = placeSun(model.mode, at.sun);
187
+ const all = { Earth: [0, 0, 0], Moon: placeMoon(model.mode, at.moon), Sun: sunAt };
188
+ for (const name of PLANETS)
189
+ all[name] = placeAround(model.mode, sunAt, [at.planets[name][0] - at.sun[0], at.planets[name][1] - at.sun[1], at.planets[name][2] - at.sun[2]]);
190
+ return all;
191
+ };
168
192
  const labelFor = (v) => {
169
193
  projected.set(v[0], v[1], v[2]).project(camera);
170
194
  if (projected.z > 1 || projected.z < -1)
@@ -229,26 +253,123 @@ export function createSpaceItem(model, deps) {
229
253
  p.orbit.geometry.getAttribute("position").needsUpdate = true;
230
254
  }
231
255
  }
232
- // the camera: looks at the focus body from the direction the sky was in when it was last moved, turned by yaw and tilt, as far as the zoom says
233
- const focus = model.focus;
234
- const focusRadius = radiusOf(focus);
235
- const spin = model.cameraSpin ?? frame.spin;
236
- const lon = model.centre.lon - model.view.yaw;
237
- const lat = Math.max(-90, Math.min(90, model.centre.lat + model.view.tilt));
238
- const aim = direction(spin + lon, lat);
239
- const distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * focusRadius) / Math.max(1e-9, model.view.zoom));
240
- target.set(pos[focus][0], pos[focus][1], pos[focus][2]);
241
- camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
242
- camera.up.set(0, 1, 0);
243
- camera.lookAt(target);
244
- camera.near = Math.max(0.0005, distance * 0.02 - focusRadius * 0.5, focusRadius * 0.01);
245
- camera.far = Math.max(2000, distance * 4 + (mode === "true" ? 40 * 23_481 : 400));
256
+ if (!layer && model.satellites.groups.size > 0) {
257
+ try {
258
+ layer = createSatelliteLayer(three, scene, model.satellites, model.ratio);
259
+ }
260
+ catch (error) {
261
+ model.satellites.groups.clear();
262
+ deps.onSatellitesError?.(error);
263
+ }
264
+ }
265
+ layer?.update(model.time, model.speed);
266
+ model.pick = layer ? (x, y, radius) => layer.pick(x, y, camera, size, radius) : null;
267
+ // the camera: looks at the focus body from the direction the sky was in when it was last moved, turned by yaw and tilt, as far as the zoom says; or, riding along, sits at the
268
+ // body and looks at the Earth
269
+ let focus = model.focus;
270
+ let focusPos = pos[focus];
271
+ let focusRadius = radiusOf(focus);
272
+ let rideFrom = null;
273
+ model.rideLost = false;
274
+ model.rideMotion = 0;
275
+ const ride = model.ride;
276
+ if (ride) {
277
+ if (typeof ride.target === "string") {
278
+ if (ride.view === "follow") {
279
+ focus = ride.target;
280
+ focusPos = pos[focus];
281
+ focusRadius = radiusOf(focus);
282
+ }
283
+ else {
284
+ const ends = track.ends();
285
+ const was = ends ? placeAll(ends[0])[ride.target] : pos[ride.target];
286
+ const will = ends ? placeAll(ends[1])[ride.target] : pos[ride.target];
287
+ const seconds = ends ? Math.max(1e-3, (ends[1].ms - ends[0].ms) / 1000) : 1;
288
+ rideFrom = { position: pos[ride.target], velocity: [will[0] - was[0], will[1] - was[1], will[2] - was[2]], hidden: ride.target, gameSeconds: seconds };
289
+ }
290
+ }
291
+ else if (layer?.exact(ride.target, model.time, rideState)) {
292
+ const place = [rideState[0], rideState[1], rideState[2]];
293
+ if (ride.view === "follow") {
294
+ focusPos = place;
295
+ focusRadius = Math.max(1e-4, ride.distanceKm / 6371 / FIT_DISTANCE);
296
+ }
297
+ else
298
+ rideFrom = { position: place, velocity: [rideState[3], rideState[4], rideState[5]], hidden: null, gameSeconds: 0 };
299
+ }
300
+ else
301
+ model.rideLost = true;
302
+ }
303
+ const hidden = rideFrom?.hidden ?? null;
304
+ moon.visible = hidden !== "Moon";
305
+ sunDrawing.object.visible = hidden !== "Sun";
306
+ for (const [name, p] of planets)
307
+ p.drawing.object.visible = hidden !== name;
308
+ let fovNow = FOV;
309
+ let distance = 0;
310
+ if (rideFrom) {
311
+ const pose = ridePose({ position: rideFrom.position, velocity: rideFrom.velocity, look: ride.look, yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom });
312
+ camera.position.set(pose.position[0], pose.position[1], pose.position[2]);
313
+ camera.up.set(pose.up[0], pose.up[1], pose.up[2]);
314
+ camera.lookAt(pose.position[0] + pose.forward[0], pose.position[1] + pose.forward[1], pose.position[2] + pose.forward[2]);
315
+ fovNow = pose.fov;
316
+ const away = Math.hypot(...rideFrom.position);
317
+ camera.near = Math.max(0.0008, Math.min(0.05, (away - 1) * 0.1));
318
+ camera.far = Math.max(2000, 400 + (mode === "true" ? 40 * 23_481 : 0));
319
+ // how fast the ground goes by under the camera, in pixels a second at 1x: the speed over the height for a satellite, the turn of the direction to the Earth for a body
320
+ const focal = (size.height / 2) / Math.tan((fovNow / 2) * RAD);
321
+ const speed = Math.hypot(...rideFrom.velocity);
322
+ model.rideMotion = typeof ride.target === "string" ? ((speed / Math.max(1e-9, away)) / rideFrom.gameSeconds) * focal : (speed / Math.max(50, (away - 1) * 6371)) * focal;
323
+ }
324
+ else {
325
+ const spin = model.cameraSpin ?? frame.spin;
326
+ const lon = model.centre.lon - model.view.yaw;
327
+ const lat = Math.max(-90, Math.min(90, model.centre.lat + model.view.tilt));
328
+ const aim = direction(spin + lon, lat);
329
+ distance = Math.max(1.05 * focusRadius, (FIT_DISTANCE * focusRadius) / Math.max(1e-9, model.view.zoom));
330
+ target.set(focusPos[0], focusPos[1], focusPos[2]);
331
+ camera.position.set(target.x + aim[0] * distance, target.y + aim[1] * distance, target.z + aim[2] * distance);
332
+ camera.up.set(0, 1, 0);
333
+ camera.lookAt(target);
334
+ camera.near = Math.max(0.0005, distance * 0.02 - focusRadius * 0.5, focusRadius * 0.01);
335
+ camera.far = Math.max(2000, distance * 4 + (mode === "true" ? 40 * 23_481 : 400));
336
+ if (ride && ride.view === "follow" && typeof ride.target !== "string") {
337
+ const focal = (size.height / 2) / Math.tan((fovNow / 2) * RAD);
338
+ model.rideMotion = (Math.hypot(rideState[3], rideState[4], rideState[5]) / Math.max(50, ride.distanceKm / Math.max(1e-9, model.view.zoom))) * focal;
339
+ }
340
+ }
341
+ // a ride that changed: glide from where the last draw left the camera (a jump for a reader who asked for reduced motion; the element does not start one then)
342
+ const glide = model.transition;
343
+ if (glide) {
344
+ const k = Math.min(1, (performance.now() - glide.start) / glide.duration);
345
+ const ease = k * k * (3 - 2 * k);
346
+ desiredQ.copy(camera.quaternion);
347
+ camera.position.set(glide.from.p[0] + (camera.position.x - glide.from.p[0]) * ease, glide.from.p[1] + (camera.position.y - glide.from.p[1]) * ease, glide.from.p[2] + (camera.position.z - glide.from.p[2]) * ease);
348
+ fromQ.set(glide.from.q[0], glide.from.q[1], glide.from.q[2], glide.from.q[3]);
349
+ camera.quaternion.copy(fromQ.slerp(desiredQ, ease));
350
+ fovNow = glide.from.fov + (fovNow - glide.from.fov) * ease;
351
+ if (k >= 1)
352
+ model.transition = null;
353
+ }
354
+ camera.fov = fovNow;
246
355
  camera.aspect = size.width / Math.max(1, size.height);
247
356
  camera.updateProjectionMatrix();
248
357
  camera.updateMatrixWorld();
249
358
  sunView.value.copy(sunEarth.value).transformDirection(camera.matrixWorldInverse);
359
+ const pose = model.pose;
360
+ pose.p[0] = camera.position.x;
361
+ pose.p[1] = camera.position.y;
362
+ pose.p[2] = camera.position.z;
363
+ pose.q[0] = camera.quaternion.x;
364
+ pose.q[1] = camera.quaternion.y;
365
+ pose.q[2] = camera.quaternion.z;
366
+ pose.q[3] = camera.quaternion.w;
367
+ pose.fov = fovNow;
368
+ pose.valid = true;
369
+ model.drawnView = { yaw: model.view.yaw, tilt: model.view.tilt, zoom: model.view.zoom };
370
+ model.heightPx = size.height;
250
371
  // how many pixels across each body is: for the pictures the Earth and the Moon need, and for what is chosen
251
- const half = Math.tan((FOV / 2) * RAD);
372
+ const half = Math.tan((fovNow / 2) * RAD);
252
373
  const radiusPxOf = (name) => {
253
374
  const d = Math.hypot(pos[name][0] - camera.position.x, pos[name][1] - camera.position.y, pos[name][2] - camera.position.z);
254
375
  const r = radiusOf(name);
@@ -264,17 +385,29 @@ export function createSpaceItem(model, deps) {
264
385
  if (deps.onLabels && model.labels) {
265
386
  const places = {};
266
387
  for (const name of BODY_NAMES) {
267
- const at = labelFor(pos[name]);
388
+ const at = name === hidden || (rideFrom && name === "Earth") ? null : labelFor(pos[name]);
268
389
  places[name] = at ? { x: at.x, y: at.y, r: px[name] } : null;
269
390
  }
270
391
  deps.onLabels(places);
271
392
  }
393
+ if (layer && deps.onSelected) {
394
+ const at = model.satellites.selected ? layer.place(camera, size) : null;
395
+ if (at || selectedShown)
396
+ deps.onSelected(at);
397
+ selectedShown = at !== null;
398
+ }
399
+ if (model.rideLost)
400
+ deps.onRideLost?.();
401
+ if (model.transition)
402
+ deps.onMoving?.();
272
403
  return false;
273
404
  },
274
405
  resize(w, h) {
275
406
  size = { width: w, height: h };
276
407
  },
277
408
  dispose() {
409
+ layer?.dispose();
410
+ model.pick = null;
278
411
  textures.dispose();
279
412
  sunDrawing.dispose();
280
413
  for (const p of planets.values()) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/chikyu-earth",
3
- "version": "0.7.0",
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, and markers for any place. A web component and an SVG string.",
3
+ "version": "0.9.0",
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",
7
7
  "globe",
@@ -31,7 +31,13 @@
31
31
  "solar-system",
32
32
  "moon",
33
33
  "planets",
34
- "ephemeris"
34
+ "ephemeris",
35
+ "satellites",
36
+ "sgp4",
37
+ "orbits",
38
+ "iss",
39
+ "starlink",
40
+ "celestrak"
35
41
  ],
36
42
  "homepage": "https://johnmorrisdotca.github.io/chikyu-earth/",
37
43
  "repository": {
@@ -91,6 +97,11 @@
91
97
  "import": "./dist/space/element.js",
92
98
  "default": "./dist/space/element.js"
93
99
  },
100
+ "./satellites": {
101
+ "types": "./dist/satellites/index.d.ts",
102
+ "import": "./dist/satellites/index.js",
103
+ "default": "./dist/satellites/index.js"
104
+ },
94
105
  "./package.json": "./package.json"
95
106
  },
96
107
  "files": [
@@ -134,7 +145,8 @@
134
145
  "peerDependencies": {
135
146
  "@johnmorrisdotca/kazoku-ui": ">=0.9.0",
136
147
  "three": ">=0.160.0",
137
- "astronomy-engine": ">=2.1.0"
148
+ "astronomy-engine": ">=2.1.0",
149
+ "satellite.js": ">=6.0.0"
138
150
  },
139
151
  "peerDependenciesMeta": {
140
152
  "@johnmorrisdotca/kazoku-ui": {
@@ -145,6 +157,9 @@
145
157
  },
146
158
  "astronomy-engine": {
147
159
  "optional": true
160
+ },
161
+ "satellite.js": {
162
+ "optional": true
148
163
  }
149
164
  },
150
165
  "devDependencies": {
@@ -171,6 +186,7 @@
171
186
  "react": "^19.3.1",
172
187
  "react-dom": "^19.3.1",
173
188
  "rxjs": "^7.8.2",
189
+ "satellite.js": "6.0.2",
174
190
  "svelte": "^5.57.3",
175
191
  "three": "0.186.1",
176
192
  "typescript": "^5.9.0",