@johnmorrisdotca/wakusei-planets 0.59.0 → 0.61.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/NOTICE.md +9 -0
  3. package/README.md +31 -3
  4. package/dist/bodies/earth.js +22 -1
  5. package/dist/data/places-moon.d.ts +12 -9
  6. package/dist/data/places-moon.js +3 -3
  7. package/dist/element.d.ts +1 -1
  8. package/dist/flat/bodies.d.ts +122 -0
  9. package/dist/flat/bodies.js +115 -0
  10. package/dist/flat/define.d.ts +3 -0
  11. package/dist/flat/define.js +6 -0
  12. package/dist/flat/element.d.ts +145 -0
  13. package/dist/flat/element.js +1614 -0
  14. package/dist/flat/geometry.d.ts +127 -0
  15. package/dist/flat/geometry.js +121 -0
  16. package/dist/flat/index.d.ts +5 -0
  17. package/dist/flat/index.js +6 -0
  18. package/dist/flat/scene.d.ts +152 -0
  19. package/dist/flat/scene.js +195 -0
  20. package/dist/flat/system-define.d.ts +3 -0
  21. package/dist/flat/system-define.js +6 -0
  22. package/dist/flat/system-element.d.ts +133 -0
  23. package/dist/flat/system-element.js +1435 -0
  24. package/dist/flat/system-words.d.ts +28 -0
  25. package/dist/flat/system-words.js +102 -0
  26. package/dist/flat/system.d.ts +440 -0
  27. package/dist/flat/system.js +521 -0
  28. package/dist/flat/words.d.ts +55 -0
  29. package/dist/flat/words.js +133 -0
  30. package/dist/space/travel.js +36 -5
  31. package/dist/surface/drive.d.ts +5 -2
  32. package/dist/surface/drive.js +13 -15
  33. package/dist/surface/element.d.ts +38 -1
  34. package/dist/surface/element.js +144 -14
  35. package/dist/surface/index.d.ts +1 -0
  36. package/dist/surface/index.js +1 -0
  37. package/dist/surface/model.d.ts +54 -0
  38. package/dist/surface/model.js +147 -0
  39. package/dist/surface/rig.d.ts +125 -0
  40. package/dist/surface/rig.js +107 -0
  41. package/dist/surface/scene.d.ts +3 -0
  42. package/dist/surface/scene.js +22 -1
  43. package/package.json +38 -2
@@ -0,0 +1,107 @@
1
+ // A rover model's rig, without a DOM and without three.js: the names of the nodes a model (a GLB) must give, the measures a drive step needs read from where those nodes are, and the angles the wheels and
2
+ // steering pivots are turned to for a drive state. The loader (`model.ts`) finds the nodes; this decides what the numbers mean, so it is tested in node.
3
+ import { ROVER, toWorld } from "./drive.js";
4
+ import { box } from "./shapes.js";
5
+ /**
6
+ * The nodes a rover model may name: `wheel-fl`, `wheel-fr`, `wheel-ml`, `wheel-mr`, `wheel-rl` and `wheel-rr` roll about X (their origin is the wheel's centre), `steer-fl`, `steer-fr`, `steer-rl` and `steer-rr`
7
+ * turn about Y (a pivot holding the wheel and its leg), and the eye is `eye`, or `seat-commander` (the left seat) when the model has no `eye`. The model's metres have +Y up and its front on +Z.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { MODEL_NODES } from "@johnmorrisdotca/wakusei-planets/surface";
12
+ *
13
+ * console.log(MODEL_NODES.wheels.join(" "), MODEL_NODES.eyes.join(" or "));
14
+ * ```
15
+ */
16
+ export const MODEL_NODES = {
17
+ wheels: ["wheel-fl", "wheel-fr", "wheel-ml", "wheel-mr", "wheel-rl", "wheel-rr"],
18
+ steers: ["steer-fl", "steer-fr", "steer-rl", "steer-rr"],
19
+ eyes: ["eye", "seat-commander"],
20
+ };
21
+ /**
22
+ * The rig of a model: its four corner wheels where its `wheel-fl`, `wheel-fr`, `wheel-rl` and `wheel-rr` nodes are, its wheelbase and track from them, its eyes where `eye` (or `seat-commander`) is. What a model does
23
+ * not say is the lunar rover's value; a model with no corner wheel at all is `undefined` (it cannot be driven by its wheels).
24
+ *
25
+ * @param nodes - What was found in the model.
26
+ * @returns The rig, or `undefined` when a corner wheel is missing.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * import { rigOf } from "@johnmorrisdotca/wakusei-planets/surface";
31
+ *
32
+ * const rig = rigOf({ at: { "wheel-fl": [-1, 0.25, 1], "wheel-fr": [1, 0.25, 1], "wheel-rl": [-1, 0.25, -1], "wheel-rr": [1, 0.25, -1], eye: [0, 2, 0] }, wheelRadius: 0.25, length: 3 });
33
+ * console.log(rig?.wheelbase, rig?.track, rig?.seat);
34
+ * ```
35
+ */
36
+ export function rigOf(nodes) {
37
+ const [fl, fr, rl, rr] = ["wheel-fl", "wheel-fr", "wheel-rl", "wheel-rr"].map((name) => nodes.at[name]);
38
+ if (fl === undefined || fr === undefined || rl === undefined || rr === undefined)
39
+ return undefined;
40
+ const eye = nodes.at["eye"] ?? nodes.at["seat-commander"] ?? ROVER.seat;
41
+ const wheelbase = Math.abs((fl[2] + fr[2]) / 2 - (rl[2] + rr[2]) / 2);
42
+ const track = Math.abs((fr[0] + rr[0]) / 2 - (fl[0] + rl[0]) / 2);
43
+ const radius = nodes.wheelRadius > 0 ? nodes.wheelRadius : Math.max(0.05, (fl[1] + fr[1] + rl[1] + rr[1]) / 4);
44
+ return { length: nodes.length > 0 ? nodes.length : wheelbase + 2 * radius, wheelbase, track, wheelRadius: radius, wheels: [fl, fr, rl, rr], seat: eye };
45
+ }
46
+ /**
47
+ * How far each named node of a rover model is turned for a drive state: the wheels' roll about X (the four corner wheels as `spin` has them, each middle wheel the mean of the two beside it) and the steering pivots'
48
+ * turn about Y (the front ones with the steer, the rear ones the other way, as the lunar rover's four wheels steer).
49
+ *
50
+ * @param state - The vehicle.
51
+ * @returns Radians for each node name: `roll` for the `wheel-xx` nodes about X, `steer` for the `steer-xx` nodes about Y.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * import { modelPose, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
56
+ *
57
+ * const rover = { ...startDrive(0, 0, 0, 0), steer: 0.2, spin: [1, 1, 3, 3] };
58
+ * const pose = modelPose(rover);
59
+ * console.log(pose.roll["wheel-ml"], pose.steer["steer-fl"], pose.steer["steer-rr"]);
60
+ * ```
61
+ */
62
+ export function modelPose(state) {
63
+ const [fl, fr, rl, rr] = state.spin;
64
+ return {
65
+ roll: { "wheel-fl": fl, "wheel-fr": fr, "wheel-ml": (fl + rl) / 2, "wheel-mr": (fr + rr) / 2, "wheel-rl": rl, "wheel-rr": rr },
66
+ steer: { "steer-fl": state.steer, "steer-fr": state.steer, "steer-rl": -state.steer, "steer-rr": -state.steer },
67
+ };
68
+ }
69
+ /**
70
+ * Where a model's glTF frame (+Y up, front +Z, the left on +X) puts a point of the model in the drive frame (x right, y up, z forward): the left and right swapped.
71
+ *
72
+ * @param p - A point in the model's metres.
73
+ * @returns The point in the drive frame.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * import { fromModelFrame } from "@johnmorrisdotca/wakusei-planets/surface";
78
+ *
79
+ * console.log(fromModelFrame([0.5, 1.2, 2]));
80
+ * ```
81
+ */
82
+ export const fromModelFrame = (p) => [-p[0], p[1], p[2]];
83
+ /**
84
+ * The shadow of a model: one box the size of its body, placed as the drive state puts the vehicle (a shadow is the hull of a part's corners, so a box is its footprint).
85
+ *
86
+ * @param state - The vehicle.
87
+ * @param rig - The model's rig.
88
+ * @param height - The model's height over the ground, metres.
89
+ * @returns One part, to hand to `partShadows`.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * import { modelShadowParts, partShadows, startDrive, ROVER } from "@johnmorrisdotca/wakusei-planets/surface";
94
+ *
95
+ * const parts = modelShadowParts(startDrive(0, 0, 0, 0), ROVER, 1.5);
96
+ * console.log(partShadows(parts, 0, 90, 20).length / 9, "triangles of shadow");
97
+ * ```
98
+ */
99
+ export function modelShadowParts(state, rig, height) {
100
+ const part = box([0, height / 2 + rig.wheelRadius, 0], [rig.track * 0.8, height, rig.length * 0.85], 0, 1);
101
+ const position = [];
102
+ for (let i = 0; i < part.position.length; i += 3) {
103
+ const w = toWorld(state, [part.position[i], part.position[i + 1], part.position[i + 2]]);
104
+ position.push(w[0], w[1], w[2]);
105
+ }
106
+ return [{ ...part, position, normal: part.normal }];
107
+ }
@@ -1,6 +1,7 @@
1
1
  import type { Item3D, Scene3D } from "@johnmorrisdotca/kazoku-ui/renderer";
2
2
  import type { ThreeParts } from "../scene/body.ts";
3
3
  import { type DriveState } from "./drive.ts";
4
+ import type { VehicleModel } from "./model.ts";
4
5
  import { type SiteObject } from "./objects.ts";
5
6
  import type { HeightLevel, TerrainManifest } from "./height.ts";
6
7
  import type { MoonSky } from "./sky.ts";
@@ -29,6 +30,8 @@ export interface SurfaceState {
29
30
  scale?: number;
30
31
  /** The rover being driven, as `driveStep` leaves it: drawn in place of the one standing at its published place. */
31
32
  vehicle?: DriveState;
33
+ /** A loaded rover model (`loadVehicleModel`): drawn in place of the shapes of the rover being driven, with its own materials, over the ground. */
34
+ model?: VehicleModel;
32
35
  /** The path the driven rover has left, east, north, east, north, ... in metres: drawn as tracks. */
33
36
  trail?: readonly number[];
34
37
  /** The lunar module coming down (Apollo 17's descent): where it is, how high over the ground under it, which way it flies and the share of full thrust; drawn whole, with its plume, in place of the one standing at its place. */
@@ -1,6 +1,7 @@
1
1
  import { rocksAround } from "./detail.js";
2
2
  import { DISC_FRAGMENT, DISC_VERTEX, GROUND_FRAGMENT, GROUND_VERTEX, OBJECT_FRAGMENT, OBJECT_VERTEX, ROCK_FRAGMENT, ROCK_VERTEX, SHADOW_FRAGMENT, SHADOW_VERTEX, TRACK_FRAGMENT, TRACK_VERTEX, DUST_FRAGMENT, DUST_VERTEX, PLUME_FRAGMENT, PLUME_VERTEX } from "./glsl.js";
3
3
  import { driveParts } from "./drive.js";
4
+ import { modelShadowParts } from "./rig.js";
4
5
  import { placePart, ascentParts, landerParts } from "./shapes.js";
5
6
  import { KEEP_OUT, objectParts, partShadows } from "./objects.js";
6
7
  import { rockMesh } from "./rocks.js";
@@ -345,15 +346,28 @@ export function createSurfaceItem(options) {
345
346
  let vehicleMesh;
346
347
  let vehicleShadow;
347
348
  let vehicleCount = 0;
349
+ let modelDrawn;
348
350
  const makeVehicle = (state) => {
349
351
  const v = state.vehicle;
350
352
  if (v === undefined || state.objects === false) {
353
+ modelDrawn = undefined;
351
354
  if (vehicleMesh !== undefined)
352
355
  vehicleMesh.visible = false;
353
356
  if (vehicleShadow !== undefined)
354
357
  vehicleShadow.visible = false;
355
358
  return;
356
359
  }
360
+ if (state.model !== undefined) {
361
+ // a model draws itself (in its own pass, after the ground); the shapes are hidden and its shadow is a box the size of its body
362
+ if (vehicleMesh !== undefined)
363
+ vehicleMesh.visible = false;
364
+ const sun = state.sky.sun;
365
+ state.model.place(v, [sun.enu[0], sun.enu[2], -sun.enu[1]], Math.max(0, Math.min(1, sun.altitude / 8)));
366
+ modelDrawn = state.model;
367
+ makeVehicleShadow(state, modelShadowParts(v, state.model.rig, state.model.height));
368
+ return;
369
+ }
370
+ modelDrawn = undefined;
357
371
  const parts = driveParts(v);
358
372
  const count = parts.reduce((n, part) => n + part.position.length / 3, 0);
359
373
  if (vehicleMesh === undefined || count !== vehicleCount) {
@@ -394,6 +408,10 @@ export function createSurfaceItem(options) {
394
408
  position.needsUpdate = true;
395
409
  normal.needsUpdate = true;
396
410
  vehicleMesh.visible = true;
411
+ makeVehicleShadow(state, parts);
412
+ };
413
+ const makeVehicleShadow = (state, parts) => {
414
+ const v = state.vehicle;
397
415
  const shadow = state.shadows === false ? [] : partShadows(parts, v.y, state.sky.sun.azimuth, state.sky.sun.altitude);
398
416
  const geometry = new T.BufferGeometry();
399
417
  geometry.setAttribute("position", new T.BufferAttribute(new Float32Array(shadow), 3));
@@ -715,11 +733,14 @@ export function createSurfaceItem(options) {
715
733
  target.setSize(w, h);
716
734
  three.setRenderTarget(target);
717
735
  gl.render(scene, camera);
736
+ modelDrawn?.render(gl, uniforms.uVP.value);
718
737
  three.setRenderTarget(null);
719
738
  gl.render(blitScene, camera);
720
739
  }
721
- else
740
+ else {
722
741
  gl.render(scene, camera);
742
+ modelDrawn?.render(gl, uniforms.uVP.value);
743
+ }
723
744
  if (measuring) {
724
745
  const canvas = gl.domElement;
725
746
  // reading one pixel back waits for the card to finish the draw (`finish` returns at once in a browser that draws in another process)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnmorrisdotca/wakusei-planets",
3
- "version": "0.59.0",
3
+ "version": "0.61.0",
4
4
  "description": "Wakusei Planets 惑星 — The Sun, planets and moons for the web: each as a clean line drawing, a shaded realistic drawing or a painted surface map, with real sizes, axial tilt and rotation as data, and positions of the moons for any date and time. SVG for the lines, three.js (optional) for the rest.",
5
5
  "keywords": [
6
6
  "planets",
@@ -19,6 +19,9 @@
19
19
  "neptune",
20
20
  "pluto",
21
21
  "dwarf-planets",
22
+ "asteroids",
23
+ "comets",
24
+ "stars",
22
25
  "astronomy",
23
26
  "ephemeris",
24
27
  "heliocentric",
@@ -92,6 +95,8 @@
92
95
  "./dist/lineup/element.js",
93
96
  "./dist/space/element.js",
94
97
  "./dist/plan/element.js",
98
+ "./dist/flat/define.js",
99
+ "./dist/flat/system-define.js",
95
100
  "./dist/surface/element.js",
96
101
  "./dist/surface/land.js",
97
102
  "./dist/earth-places.js",
@@ -103,6 +108,8 @@
103
108
  "./src/lineup/element.ts",
104
109
  "./src/space/element.ts",
105
110
  "./src/plan/element.ts",
111
+ "./src/flat/define.ts",
112
+ "./src/flat/system-define.ts",
106
113
  "./src/surface/element.ts",
107
114
  "./src/surface/land.ts",
108
115
  "./src/earth-places.ts",
@@ -154,6 +161,26 @@
154
161
  "import": "./dist/plan/element.js",
155
162
  "default": "./dist/plan/element.js"
156
163
  },
164
+ "./flat": {
165
+ "types": "./dist/flat/index.d.ts",
166
+ "import": "./dist/flat/index.js",
167
+ "default": "./dist/flat/index.js"
168
+ },
169
+ "./flat/define": {
170
+ "types": "./dist/flat/define.d.ts",
171
+ "import": "./dist/flat/define.js",
172
+ "default": "./dist/flat/define.js"
173
+ },
174
+ "./flat/system": {
175
+ "types": "./dist/flat/system.d.ts",
176
+ "import": "./dist/flat/system.js",
177
+ "default": "./dist/flat/system.js"
178
+ },
179
+ "./flat/system/define": {
180
+ "types": "./dist/flat/system-define.d.ts",
181
+ "import": "./dist/flat/system-define.js",
182
+ "default": "./dist/flat/system-define.js"
183
+ },
157
184
  "./surface": {
158
185
  "types": "./dist/surface/index.d.ts",
159
186
  "import": "./dist/surface/index.js",
@@ -164,6 +191,11 @@
164
191
  "import": "./dist/surface/element.js",
165
192
  "default": "./dist/surface/element.js"
166
193
  },
194
+ "./surface/model": {
195
+ "types": "./dist/surface/model.d.ts",
196
+ "import": "./dist/surface/model.js",
197
+ "default": "./dist/surface/model.js"
198
+ },
167
199
  "./surface/land": {
168
200
  "types": "./dist/surface/land.d.ts",
169
201
  "import": "./dist/surface/land.js",
@@ -608,8 +640,12 @@
608
640
  "lint": "eslint .",
609
641
  "typecheck": "tsc -p tsconfig.check.json && tsc -p tsconfig.tools.json && tsc -p tsconfig.demo.json",
610
642
  "test": "node --test test/*.test.ts",
643
+ "test:coverage": "node --test --experimental-test-coverage --test-coverage-include='src/**' --test-coverage-lines=96 --test-coverage-branches=93 --test-coverage-functions=95 test/*.test.ts",
644
+ "size:budget": "node scripts/check-size.mjs",
645
+ "size:budget:update": "node scripts/check-size.mjs --write",
646
+ "api:update": "node scripts/api-surface.mjs --write",
611
647
  "test:package": "pnpm build && node scripts/check-package.ts",
612
- "check": "pnpm lint && pnpm typecheck && pnpm test && pnpm build && node scripts/check-package.ts",
648
+ "check": "pnpm lint && pnpm typecheck && pnpm test:coverage && pnpm build && node scripts/check-package.ts && pnpm size:budget",
613
649
  "prepublishOnly": "pnpm check",
614
650
  "tiles": ".cache/venv/bin/python -I scripts/make-body-tiles.py build",
615
651
  "tiles:moon": ".cache/venv/bin/python -I scripts/make-body-tiles.py sharp --bodies moon",