@johnmorrisdotca/wakusei-planets 0.60.0 → 0.62.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 (47) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/NOTICE.md +9 -0
  3. package/README.md +18 -3
  4. package/dist/belts/filter.d.ts +128 -0
  5. package/dist/belts/filter.js +117 -0
  6. package/dist/belts/index.d.ts +2 -0
  7. package/dist/belts/index.js +1 -0
  8. package/dist/bodies/earth.js +22 -1
  9. package/dist/data/places-moon.d.ts +12 -9
  10. package/dist/data/places-moon.js +3 -3
  11. package/dist/element.d.ts +1 -1
  12. package/dist/flat/system-define.d.ts +3 -0
  13. package/dist/flat/system-define.js +6 -0
  14. package/dist/flat/system-element.d.ts +133 -0
  15. package/dist/flat/system-element.js +1435 -0
  16. package/dist/flat/system-words.d.ts +28 -0
  17. package/dist/flat/system-words.js +102 -0
  18. package/dist/flat/system.d.ts +440 -0
  19. package/dist/flat/system.js +521 -0
  20. package/dist/landing-sites.js +6 -6
  21. package/dist/space/travel.js +36 -5
  22. package/dist/surface/drive.d.ts +8 -4
  23. package/dist/surface/drive.js +18 -19
  24. package/dist/surface/dust.d.ts +2 -1
  25. package/dist/surface/dust.js +3 -2
  26. package/dist/surface/element.d.ts +41 -4
  27. package/dist/surface/element.js +173 -25
  28. package/dist/surface/flat.d.ts +34 -0
  29. package/dist/surface/flat.js +63 -0
  30. package/dist/surface/glsl.d.ts +6 -3
  31. package/dist/surface/glsl.js +35 -2
  32. package/dist/surface/height.d.ts +1 -1
  33. package/dist/surface/index.d.ts +4 -2
  34. package/dist/surface/index.js +4 -2
  35. package/dist/surface/land.d.ts +7 -5
  36. package/dist/surface/land.js +10 -8
  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 +9 -2
  42. package/dist/surface/scene.js +63 -5
  43. package/dist/surface/site.d.ts +32 -3
  44. package/dist/surface/site.js +99 -2
  45. package/dist/surface/sky.d.ts +92 -1
  46. package/dist/surface/sky.js +78 -4
  47. package/package.json +26 -2
@@ -0,0 +1,147 @@
1
+ import { toWorld } from "./drive.js";
2
+ import { MODEL_NODES, fromModelFrame, modelPose, rigOf } from "./rig.js";
3
+ /**
4
+ * Load a rover model from a GLB and read its rig. The nodes it must give are in `MODEL_NODES`: `wheel-xx` (rolled about X), `steer-xx` (turned about Y) and `eye` or `seat-commander`; a model with no corner wheel is refused.
5
+ * The loader and the meshopt decoder are three.js's own add-ons, imported when this is first called. Each call makes its own copy (the loaded file is read again from the browser's cache), which `dispose()` frees.
6
+ *
7
+ * @param url - Where the GLB is served from.
8
+ * @param options - `signal` to cancel the request.
9
+ * @returns The model, or a rejection when the file cannot be read or has no corner wheel.
10
+ *
11
+ * @example
12
+ * ```ts no-run
13
+ * import { loadVehicleModel } from "@johnmorrisdotca/wakusei-planets/surface/model";
14
+ *
15
+ * const model = await loadVehicleModel("https://johnmorrisdotca.github.io/wakusei-planets/models/perseverance.glb");
16
+ * console.log(model.rig.track.toFixed(2), "m between the wheels");
17
+ * ```
18
+ */
19
+ export async function loadVehicleModel(url, options = {}) {
20
+ const [three, { GLTFLoader }, { MeshoptDecoder }] = await Promise.all([import("three"), import("three/addons/loaders/GLTFLoader.js"), import("three/addons/libs/meshopt_decoder.module.js")]);
21
+ options.signal?.throwIfAborted();
22
+ const loader = new GLTFLoader();
23
+ loader.setMeshoptDecoder(MeshoptDecoder);
24
+ const gltf = await loader.loadAsync(url);
25
+ options.signal?.throwIfAborted();
26
+ return makeModel(three, gltf.scene);
27
+ }
28
+ function makeModel(T, root) {
29
+ root.matrixAutoUpdate = false;
30
+ root.updateMatrixWorld(true);
31
+ const found = new Map();
32
+ root.traverse((node) => {
33
+ if (node.name !== "" && !found.has(node.name))
34
+ found.set(node.name, node);
35
+ });
36
+ const names = [...MODEL_NODES.wheels, ...MODEL_NODES.steers, ...MODEL_NODES.eyes].filter((name) => found.has(name));
37
+ const at = {};
38
+ const here = new T.Vector3();
39
+ for (const name of names) {
40
+ found.get(name).getWorldPosition(here);
41
+ at[name] = fromModelFrame([here.x, here.y, here.z]);
42
+ }
43
+ const box = new T.Box3().setFromObject(root);
44
+ const size = box.getSize(new T.Vector3());
45
+ // a wheel's radius: half the height of the biggest wheel node
46
+ let wheelRadius = 0;
47
+ const wheelBox = new T.Box3();
48
+ for (const name of MODEL_NODES.wheels) {
49
+ const node = found.get(name);
50
+ if (node === undefined)
51
+ continue;
52
+ wheelBox.setFromObject(node);
53
+ wheelRadius = Math.max(wheelRadius, (wheelBox.max.y - wheelBox.min.y) / 2);
54
+ }
55
+ const nodes = { at, wheelRadius, length: size.z };
56
+ const rig = rigOf(nodes);
57
+ if (rig === undefined) {
58
+ disposeAll(root);
59
+ throw new Error("the model has no wheel-fl, wheel-fr, wheel-rl and wheel-rr nodes");
60
+ }
61
+ // the nodes that move, with the turn they were loaded with
62
+ const moving = new Map();
63
+ for (const name of [...MODEL_NODES.wheels, ...MODEL_NODES.steers]) {
64
+ const node = found.get(name);
65
+ if (node !== undefined)
66
+ moving.set(name, { node, base: node.quaternion.clone() });
67
+ }
68
+ root.traverse((node) => {
69
+ node.frustumCulled = false;
70
+ });
71
+ const scene = new T.Scene();
72
+ const sunLight = new T.DirectionalLight(0xffffff, 3);
73
+ const fill = new T.AmbientLight(0xffffff, 0.35);
74
+ scene.add(root, sunLight, fill, sunLight.target);
75
+ const camera = new T.Camera();
76
+ camera.matrixAutoUpdate = false;
77
+ const angles = {};
78
+ let renders = 0;
79
+ const [ax, ay] = [new T.Vector3(1, 0, 0), new T.Vector3(0, 1, 0)];
80
+ const turn = new T.Quaternion();
81
+ return {
82
+ rig,
83
+ nodes: names,
84
+ height: size.y,
85
+ angles,
86
+ get renders() {
87
+ return renders;
88
+ },
89
+ place(state, sun, strength) {
90
+ const pose = modelPose(state);
91
+ // a positive roll is forward (the top moving toward +Z, a turn about +X); the model's +X is the left, so a steer to the right is a turn about +Y the other way
92
+ for (const [name, entry] of moving) {
93
+ const roll = pose.roll[name];
94
+ const steer = pose.steer[name];
95
+ if (roll !== undefined) {
96
+ angles[name] = roll;
97
+ entry.node.quaternion.copy(entry.base).multiply(turn.setFromAxisAngle(ax, roll));
98
+ }
99
+ else if (steer !== undefined) {
100
+ angles[name] = -steer;
101
+ entry.node.quaternion.copy(entry.base).multiply(turn.setFromAxisAngle(ay, -steer));
102
+ }
103
+ entry.node.matrixWorldNeedsUpdate = true;
104
+ entry.node.updateMatrix();
105
+ }
106
+ // the model's frame in the scene's (x east, y up, z minus north): the drive frame's axes, with left and right swapped for the model's
107
+ const o = toWorld(state, [0, 0, 0]);
108
+ const axis = (v) => {
109
+ const p = toWorld(state, v);
110
+ return [p[0] - o[0], p[1] - o[1], -(p[2] - o[2])];
111
+ };
112
+ const [cx, cy, cz] = [axis([-1, 0, 0]), axis([0, 1, 0]), axis([0, 0, 1])];
113
+ root.matrix.set(cx[0], cy[0], cz[0], o[0], cx[1], cy[1], cz[1], o[1], cx[2], cy[2], cz[2], -o[2], 0, 0, 0, 1);
114
+ root.updateMatrixWorld(true);
115
+ sunLight.position.set(sun[0] * 100, sun[1] * 100, sun[2] * 100).add(here.set(o[0], o[1], -o[2]));
116
+ sunLight.target.position.set(o[0], o[1], -o[2]);
117
+ sunLight.target.updateMatrixWorld();
118
+ sunLight.intensity = 3 * strength;
119
+ },
120
+ render(gl, viewProjection) {
121
+ camera.projectionMatrix.fromArray(viewProjection);
122
+ camera.projectionMatrixInverse.copy(camera.projectionMatrix).invert();
123
+ renders += 1;
124
+ const was = gl.autoClear;
125
+ gl.autoClear = false;
126
+ gl.render(scene, camera);
127
+ gl.autoClear = was;
128
+ },
129
+ dispose() {
130
+ disposeAll(root);
131
+ },
132
+ };
133
+ }
134
+ function disposeAll(root) {
135
+ root.traverse((node) => {
136
+ const mesh = node;
137
+ if (mesh.geometry === undefined)
138
+ return;
139
+ mesh.geometry.dispose();
140
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) {
141
+ for (const value of Object.values(material))
142
+ if (value !== null && typeof value === "object" && "isTexture" in value)
143
+ value.dispose();
144
+ material.dispose();
145
+ }
146
+ });
147
+ }
@@ -0,0 +1,125 @@
1
+ import { type DriveState } from "./drive.ts";
2
+ import { type Part, type V3 } from "./shapes.ts";
3
+ /**
4
+ * 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`
5
+ * 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.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { MODEL_NODES } from "@johnmorrisdotca/wakusei-planets/surface";
10
+ *
11
+ * console.log(MODEL_NODES.wheels.join(" "), MODEL_NODES.eyes.join(" or "));
12
+ * ```
13
+ */
14
+ export declare const MODEL_NODES: {
15
+ readonly wheels: readonly ["wheel-fl", "wheel-fr", "wheel-ml", "wheel-mr", "wheel-rl", "wheel-rr"];
16
+ readonly steers: readonly ["steer-fl", "steer-fr", "steer-rl", "steer-rr"];
17
+ readonly eyes: readonly ["eye", "seat-commander"];
18
+ };
19
+ /**
20
+ * The measures a drive step takes from a vehicle: where its four corner wheels touch the ground, how big they are and where the eyes are. `ROVER` is the lunar rover's; a model gives its own (`rigOf`).
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * import { ROVER, type Rig } from "@johnmorrisdotca/wakusei-planets/surface";
25
+ *
26
+ * const rig: Rig = ROVER;
27
+ * console.log(rig.wheels.length, rig.wheelRadius);
28
+ * ```
29
+ */
30
+ export interface Rig {
31
+ length: number;
32
+ wheelbase: number;
33
+ track: number;
34
+ wheelRadius: number;
35
+ /** The four corner wheels' centres in the vehicle's frame (x right, y up, z forward): front left, front right, rear left, rear right. */
36
+ wheels: readonly V3[];
37
+ /** The eyes of the view from the vehicle. */
38
+ seat: V3;
39
+ }
40
+ /**
41
+ * What the loader finds in a model: where each named node's origin is (in the drive frame, metres), the radius of the wheels and the length of the model along Z.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * import type { ModelNodes } from "@johnmorrisdotca/wakusei-planets/surface";
46
+ *
47
+ * const nodes: ModelNodes = { at: { eye: [0, 1.9, 0] }, wheelRadius: 0.3, length: 2.4 };
48
+ * console.log(Object.keys(nodes.at).length, nodes.length);
49
+ * ```
50
+ */
51
+ export interface ModelNodes {
52
+ /** The world position of each named node that exists. */
53
+ at: Readonly<Record<string, V3>>;
54
+ /** The radius of the wheels, metres, from the biggest wheel's size (`0` when it could not be told). */
55
+ wheelRadius: number;
56
+ /** The model's length along Z, metres. */
57
+ length: number;
58
+ }
59
+ /**
60
+ * 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
61
+ * 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).
62
+ *
63
+ * @param nodes - What was found in the model.
64
+ * @returns The rig, or `undefined` when a corner wheel is missing.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * import { rigOf } from "@johnmorrisdotca/wakusei-planets/surface";
69
+ *
70
+ * 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 });
71
+ * console.log(rig?.wheelbase, rig?.track, rig?.seat);
72
+ * ```
73
+ */
74
+ export declare function rigOf(nodes: ModelNodes): Rig | undefined;
75
+ /**
76
+ * 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'
77
+ * turn about Y (the front ones with the steer, the rear ones the other way, as the lunar rover's four wheels steer).
78
+ *
79
+ * @param state - The vehicle.
80
+ * @returns Radians for each node name: `roll` for the `wheel-xx` nodes about X, `steer` for the `steer-xx` nodes about Y.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * import { modelPose, startDrive } from "@johnmorrisdotca/wakusei-planets/surface";
85
+ *
86
+ * const rover = { ...startDrive(0, 0, 0, 0), steer: 0.2, spin: [1, 1, 3, 3] };
87
+ * const pose = modelPose(rover);
88
+ * console.log(pose.roll["wheel-ml"], pose.steer["steer-fl"], pose.steer["steer-rr"]);
89
+ * ```
90
+ */
91
+ export declare function modelPose(state: Pick<DriveState, "spin" | "steer">): {
92
+ roll: Record<string, number>;
93
+ steer: Record<string, number>;
94
+ };
95
+ /**
96
+ * 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.
97
+ *
98
+ * @param p - A point in the model's metres.
99
+ * @returns The point in the drive frame.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * import { fromModelFrame } from "@johnmorrisdotca/wakusei-planets/surface";
104
+ *
105
+ * console.log(fromModelFrame([0.5, 1.2, 2]));
106
+ * ```
107
+ */
108
+ export declare const fromModelFrame: (p: readonly [number, number, number]) => V3;
109
+ /**
110
+ * 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).
111
+ *
112
+ * @param state - The vehicle.
113
+ * @param rig - The model's rig.
114
+ * @param height - The model's height over the ground, metres.
115
+ * @returns One part, to hand to `partShadows`.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * import { modelShadowParts, partShadows, startDrive, ROVER } from "@johnmorrisdotca/wakusei-planets/surface";
120
+ *
121
+ * const parts = modelShadowParts(startDrive(0, 0, 0, 0), ROVER, 1.5);
122
+ * console.log(partShadows(parts, 0, 90, 20).length / 9, "triangles of shadow");
123
+ * ```
124
+ */
125
+ export declare function modelShadowParts(state: Pick<DriveState, "east" | "north" | "heading" | "pitch" | "roll" | "y">, rig: Rig, height: number): Part[];
@@ -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,9 +1,10 @@
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
- import type { MoonSky } from "./sky.ts";
7
+ import type { MoonSky, SkyAir } from "./sky.ts";
7
8
  import { type Look } from "./view.ts";
8
9
  /** Everything that changes how the view looks. The element fills one in and hands it over; the scene holds no clock of its own. */
9
10
  export interface SurfaceState {
@@ -14,7 +15,11 @@ export interface SurfaceState {
14
15
  up: number;
15
16
  };
16
17
  look: Look;
17
- sky: MoonSky;
18
+ /** The sky: the Moon's (`moonSky`) or a body's with air (`standSky`: `air` is its colours, and `earthDisc` false hides the Earth's disc). */
19
+ sky: MoonSky & {
20
+ air?: SkyAir | null;
21
+ earthDisc?: boolean;
22
+ };
18
23
  /** Brightness: the sunlit ground of an albedo of 0.14 comes out as this share of white times the Sun's height factor (default 4.5). */
19
24
  exposure?: number;
20
25
  /** The ground's own brightness, 0 to 1 (default 0.14, a grey a little lighter than the Moon's mean). */
@@ -29,6 +34,8 @@ export interface SurfaceState {
29
34
  scale?: number;
30
35
  /** The rover being driven, as `driveStep` leaves it: drawn in place of the one standing at its published place. */
31
36
  vehicle?: DriveState;
37
+ /** A loaded rover model (`loadVehicleModel`): drawn in place of the shapes of the rover being driven, with its own materials, over the ground. */
38
+ model?: VehicleModel;
32
39
  /** The path the driven rover has left, east, north, east, north, ... in metres: drawn as tracks. */
33
40
  trail?: readonly number[];
34
41
  /** 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
- 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";
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, SKY_FRAGMENT, SKY_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";
@@ -108,6 +109,7 @@ export function createSurfaceItem(options) {
108
109
  uAlbedo: { value: 0.14 },
109
110
  uMaxY: { value: maxY },
110
111
  uEarthshine: { value: 0.05 },
112
+ uTint: { value: new T.Vector3(1, 1, 1) },
111
113
  uShadows: { value: 1 },
112
114
  uHmin: { value: manifest.height.min },
113
115
  uHrange: { value: manifest.height.range },
@@ -168,8 +170,25 @@ export function createSurfaceItem(options) {
168
170
  };
169
171
  const sun = disc(0, Math.tan((0.533 * Math.PI) / 360));
170
172
  const earth = disc(1, Math.tan((1.9 * Math.PI) / 360));
173
+ // the sky of a body with air: one square behind the discs, hidden where there is none
174
+ const skyUniforms = {
175
+ uForwardEnu: { value: new T.Vector3(0, 1, 0) },
176
+ uRightEnu: { value: new T.Vector3(1, 0, 0) },
177
+ uUpEnu: { value: new T.Vector3(0, 0, 1) },
178
+ uTan: { value: new T.Vector2(1, 1) },
179
+ uSunEnu: { value: new T.Vector3(0, 0, 1) },
180
+ uHorizon: { value: new T.Vector3() },
181
+ uZenith: { value: new T.Vector3() },
182
+ uGlow: { value: new T.Vector3() },
183
+ uGlowSize: { value: 0.2 },
184
+ };
185
+ const skyMaterial = new T.ShaderMaterial({ vertexShader: SKY_VERTEX, fragmentShader: SKY_FRAGMENT, uniforms: skyUniforms, depthTest: false, depthWrite: false });
186
+ const skyMesh = new T.Mesh(square, skyMaterial);
187
+ skyMesh.frustumCulled = false;
188
+ skyMesh.renderOrder = -20;
189
+ skyMesh.visible = false;
171
190
  const scene = new T.Scene();
172
- scene.add(sun.mesh, earth.mesh, ground);
191
+ scene.add(skyMesh, sun.mesh, earth.mesh, ground);
173
192
  // the rocks and their shadows, made again when the eyes have walked a while or the Sun has moved
174
193
  const rockMaterial = new T.ShaderMaterial({ vertexShader: ROCK_VERTEX, fragmentShader: ROCK_FRAGMENT, uniforms });
175
194
  const shadowMaterial = new T.ShaderMaterial({ vertexShader: SHADOW_VERTEX, fragmentShader: SHADOW_FRAGMENT, uniforms: { uVP: uniforms.uVP }, transparent: true, depthWrite: false });
@@ -345,15 +364,28 @@ export function createSurfaceItem(options) {
345
364
  let vehicleMesh;
346
365
  let vehicleShadow;
347
366
  let vehicleCount = 0;
367
+ let modelDrawn;
348
368
  const makeVehicle = (state) => {
349
369
  const v = state.vehicle;
350
370
  if (v === undefined || state.objects === false) {
371
+ modelDrawn = undefined;
351
372
  if (vehicleMesh !== undefined)
352
373
  vehicleMesh.visible = false;
353
374
  if (vehicleShadow !== undefined)
354
375
  vehicleShadow.visible = false;
355
376
  return;
356
377
  }
378
+ if (state.model !== undefined) {
379
+ // 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
380
+ if (vehicleMesh !== undefined)
381
+ vehicleMesh.visible = false;
382
+ const sun = state.sky.sun;
383
+ state.model.place(v, [sun.enu[0], sun.enu[2], -sun.enu[1]], Math.max(0, Math.min(1, sun.altitude / 8)));
384
+ modelDrawn = state.model;
385
+ makeVehicleShadow(state, modelShadowParts(v, state.model.rig, state.model.height));
386
+ return;
387
+ }
388
+ modelDrawn = undefined;
357
389
  const parts = driveParts(v);
358
390
  const count = parts.reduce((n, part) => n + part.position.length / 3, 0);
359
391
  if (vehicleMesh === undefined || count !== vehicleCount) {
@@ -394,6 +426,10 @@ export function createSurfaceItem(options) {
394
426
  position.needsUpdate = true;
395
427
  normal.needsUpdate = true;
396
428
  vehicleMesh.visible = true;
429
+ makeVehicleShadow(state, parts);
430
+ };
431
+ const makeVehicleShadow = (state, parts) => {
432
+ const v = state.vehicle;
397
433
  const shadow = state.shadows === false ? [] : partShadows(parts, v.y, state.sky.sun.azimuth, state.sky.sun.altitude);
398
434
  const geometry = new T.BufferGeometry();
399
435
  geometry.setAttribute("position", new T.BufferAttribute(new Float32Array(shadow), 3));
@@ -637,7 +673,10 @@ export function createSurfaceItem(options) {
637
673
  const tilt = Math.abs(look.pitch) + look.fov / 2;
638
674
  uniforms.uSpan.value = tilt > 78 ? Math.PI : Math.min(Math.PI, halfH / Math.cos((tilt * Math.PI) / 180) + 0.06);
639
675
  uniforms.uSun.value.set(sky.sun.enu[0], sky.sun.enu[2], sky.sun.enu[1]);
640
- uniforms.uExposure.value = state.exposure ?? 4.5;
676
+ const air = sky.air ?? null;
677
+ uniforms.uExposure.value = state.exposure ?? (air === null ? 4.5 : 3.2);
678
+ uniforms.uEarthshine.value = air === null ? 0.05 : air.ambient;
679
+ uniforms.uTint.value.set(air?.tint[0] ?? 1, air?.tint[1] ?? 1, air?.tint[2] ?? 1);
641
680
  uniforms.uAlbedo.value = state.albedo ?? 0.14;
642
681
  uniforms.uShadows.value = state.shadows === false ? 0 : 1;
643
682
  uniforms.uDetail.value = state.detail === false ? 0 : 1;
@@ -673,7 +712,22 @@ export function createSurfaceItem(options) {
673
712
  earth.uniforms.uHalf.value = Math.tan((sky.earthDiameter * Math.PI) / 360);
674
713
  sun.uniforms.uHalf.value = Math.tan((sky.sunDiameter * Math.PI) / 360);
675
714
  sun.mesh.visible = sky.sun.altitude > -1;
676
- earth.mesh.visible = sky.earth.altitude > -1;
715
+ earth.mesh.visible = sky.earthDisc !== false && sky.earth.altitude > -1;
716
+ skyMesh.visible = air !== null;
717
+ if (air !== null) {
718
+ const e = toEnu(axes.forward);
719
+ skyUniforms.uForwardEnu.value.set(e[0], e[1], e[2]);
720
+ const r2 = toEnu(axes.right);
721
+ skyUniforms.uRightEnu.value.set(r2[0], r2[1], r2[2]);
722
+ const u2 = toEnu(axes.up);
723
+ skyUniforms.uUpEnu.value.set(u2[0], u2[1], u2[2]);
724
+ skyUniforms.uTan.value.set(halfV * aspect, halfV);
725
+ skyUniforms.uSunEnu.value.set(sky.sun.enu[0], sky.sun.enu[1], sky.sun.enu[2]);
726
+ skyUniforms.uHorizon.value.set(...air.horizon);
727
+ skyUniforms.uZenith.value.set(...air.zenith);
728
+ skyUniforms.uGlow.value.set(...air.glow);
729
+ skyUniforms.uGlowSize.value = air.glowSize;
730
+ }
677
731
  };
678
732
  const camera = new T.OrthographicCamera(-1, 1, 1, -1, 0, 1);
679
733
  // the stretch of a smaller draw to the canvas: one square with the target as its texture (made when first needed)
@@ -715,11 +769,14 @@ export function createSurfaceItem(options) {
715
769
  target.setSize(w, h);
716
770
  three.setRenderTarget(target);
717
771
  gl.render(scene, camera);
772
+ modelDrawn?.render(gl, uniforms.uVP.value);
718
773
  three.setRenderTarget(null);
719
774
  gl.render(blitScene, camera);
720
775
  }
721
- else
776
+ else {
722
777
  gl.render(scene, camera);
778
+ modelDrawn?.render(gl, uniforms.uVP.value);
779
+ }
723
780
  if (measuring) {
724
781
  const canvas = gl.domElement;
725
782
  // reading one pixel back waits for the card to finish the draw (`finish` returns at once in a browser that draws in another process)
@@ -752,6 +809,7 @@ export function createSurfaceItem(options) {
752
809
  rockMaterial.dispose();
753
810
  shadowMaterial.dispose();
754
811
  sun.material.dispose();
812
+ skyMaterial.dispose();
755
813
  earth.material.dispose();
756
814
  target?.dispose();
757
815
  objectMesh?.geometry.dispose();
@@ -14,7 +14,13 @@ export interface SurfaceSite {
14
14
  /** The name in the `site` attribute. */
15
15
  id: string;
16
16
  /** The body it is on. */
17
- body: "moon";
17
+ body: "moon" | "mars";
18
+ /**
19
+ * What the ground is: `measured` (height tiles made from a published height model) or `flat`, a flat stand-in at the site's datum until a model is added. A flat site has no relief, no made craters and no made rocks, and says so in the line the page shows.
20
+ */
21
+ ground: "measured" | "flat";
22
+ /** The body's surface gravity at the site, m/s2: what the driven rover and the thrown dust fall at. */
23
+ gravity: number;
18
24
  name: {
19
25
  en: string;
20
26
  ja: string;
@@ -30,8 +36,31 @@ export interface SurfaceSite {
30
36
  look: Look;
31
37
  }
32
38
  /**
33
- * The sites with heights. The Apollo 17 landing site stands in Taurus-Littrow, a valley a few kilometres wide between mountains 2 kilometres high; it is first looked at toward the South Massif,
34
- * at the moment of the landing, with the Sun 13 degrees up and the Earth above the mountain.
39
+ * The Moon's surface gravity, m/s2.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import { MOON_GRAVITY } from "@johnmorrisdotca/wakusei-planets/surface";
44
+ *
45
+ * console.log(`${MOON_GRAVITY} m/s2, a sixth of the Earth's`);
46
+ * ```
47
+ */
48
+ export declare const MOON_GRAVITY = 1.62;
49
+ /**
50
+ * Mars's surface gravity, m/s2 (NASA's fact sheet gives 3.73 as the mean and 3.69 at the equator; 3.71 is the figure the standing view uses).
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * import { MARS_GRAVITY, MOON_GRAVITY } from "@johnmorrisdotca/wakusei-planets/surface";
55
+ *
56
+ * console.log(`Mars pulls ${(MARS_GRAVITY / MOON_GRAVITY).toFixed(1)} times as hard as the Moon`);
57
+ * ```
58
+ */
59
+ export declare const MARS_GRAVITY = 3.71;
60
+ /**
61
+ * The sites a person can stand at. Apollo 17 is the one with measured heights: it stands in Taurus-Littrow, a valley a few kilometres wide between mountains 2 kilometres high; it is first looked at toward the South Massif,
62
+ * at the moment of the landing, with the Sun 13 degrees up and the Earth above the mountain. Perseverance (Jezero), Curiosity (Gale), Apollo 15, Apollo 16, Chang'e 3 and Chang'e 4 have the real Sun, sky and gravity of
63
+ * their place and time and a flat stand-in for the ground (`ground: "flat"`) until a height model is added.
35
64
  *
36
65
  * @example
37
66
  * ```ts