@johnmorrisdotca/wakusei-planets 0.46.0 → 0.47.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 +5 -0
- package/NOTICE.md +1 -0
- package/README.md +3 -3
- package/dist/scene/body.d.ts +1 -1
- package/dist/scene/textures.d.ts +2 -2
- package/dist/surface/detail.d.ts +17 -0
- package/dist/surface/detail.js +25 -1
- package/dist/surface/element.d.ts +13 -0
- package/dist/surface/element.js +218 -15
- package/dist/surface/glsl.d.ts +11 -5
- package/dist/surface/glsl.js +55 -1
- package/dist/surface/index.d.ts +3 -1
- package/dist/surface/index.js +3 -1
- package/dist/surface/objects-apollo-17.d.ts +4 -0
- package/dist/surface/objects-apollo-17.js +19 -0
- package/dist/surface/objects.d.ts +127 -0
- package/dist/surface/objects.js +179 -0
- package/dist/surface/scene.d.ts +8 -0
- package/dist/surface/scene.js +169 -4
- package/dist/surface/shapes.d.ts +62 -0
- package/dist/surface/shapes.js +247 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// Things at a site, drawn by this package at their published places: the lunar module's descent stage, the rover, the flag, the ALSEP station and the electrical-properties transmitter at Apollo 17, in plain shapes
|
|
2
|
+
// (boxes, cylinders, struts), with the rover's traverse as a faint line. The positions come from published coordinates (`objects-apollo-17.ts`, made by `scripts/make-apollo17-objects.py`); the shapes, the
|
|
3
|
+
// rover's heading and what the line looks like are made here, and the page says so. Pure: no DOM.
|
|
4
|
+
import { APOLLO_17_OBJECTS, APOLLO_17_TRAVERSE } from "./objects-apollo-17.js";
|
|
5
|
+
import { box, cylinder, landerParts, placePart, roverParts, strut } from "./shapes.js";
|
|
6
|
+
/**
|
|
7
|
+
* The things at a site, at their published places; none for a site that has none.
|
|
8
|
+
*
|
|
9
|
+
* @param site - A site's name from `SURFACE_SITES`.
|
|
10
|
+
* @returns The objects.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { objectsOf } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
15
|
+
*
|
|
16
|
+
* console.log(objectsOf("apollo-17").map((object) => object.id).join(", "), objectsOf("nowhere").length);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function objectsOf(site) {
|
|
20
|
+
return site === "apollo-17" ? APOLLO_17_OBJECTS : [];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The rover's traverses at a site as lines, each east, north, east, north, ... in metres from the site point (the published path, digitised from the orbiter's images).
|
|
24
|
+
*
|
|
25
|
+
* @param site - A site's name from `SURFACE_SITES`.
|
|
26
|
+
* @returns The lines, or none.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { traverseOf } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
31
|
+
*
|
|
32
|
+
* const lines = traverseOf("apollo-17");
|
|
33
|
+
* console.log(lines.length, "traverses,", lines.reduce((n, line) => n + line.length / 2, 0), "points");
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function traverseOf(site) {
|
|
37
|
+
return site === "apollo-17" ? APOLLO_17_TRAVERSE : [];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* How far round an object the made craters and rocks keep away: nothing is made inside `inner` metres of its place, and the ground is all its own again from `outer` metres.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { KEEP_OUT } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
45
|
+
*
|
|
46
|
+
* console.log(KEEP_OUT.lander.inner, KEEP_OUT.lander.outer);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const KEEP_OUT = {
|
|
50
|
+
lander: { inner: 9, outer: 15 },
|
|
51
|
+
rover: { inner: 4, outer: 8 },
|
|
52
|
+
flag: { inner: 2, outer: 5 },
|
|
53
|
+
station: { inner: 4, outer: 8 },
|
|
54
|
+
instrument: { inner: 2, outer: 5 },
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The shape of one object as plain parts placed at its site: the lunar module's descent stage (an octagonal gold body on four splayed legs with braces and footpads, a ladder, a bell under it), the rover (a frame
|
|
58
|
+
* on four wheels with fenders, two seats, a console, the dish and the camera on their masts), the flag, the ALSEP station and the transmitter. `base` is the height of the ground at the object's place in metres over
|
|
59
|
+
* the site's ground; the shapes stand on it. All of it is this package's drawing from published sizes: it is no model of the real thing.
|
|
60
|
+
*
|
|
61
|
+
* @param object - The object.
|
|
62
|
+
* @param base - The ground's height at its place.
|
|
63
|
+
* @returns The parts, in the site's plane (x east, y up, z north).
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { objectParts, objectsOf } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
68
|
+
*
|
|
69
|
+
* const lander = objectsOf("apollo-17").find((object) => object.kind === "lander")!;
|
|
70
|
+
* const parts = objectParts(lander, 0);
|
|
71
|
+
* console.log(parts.length, "parts,", parts.reduce((n, part) => n + part.position.length / 9, 0), "triangles");
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function objectParts(object, base) {
|
|
75
|
+
const { east: e, north: n } = object;
|
|
76
|
+
const at = (x, y, z) => [e + x, base + y, n + z];
|
|
77
|
+
switch (object.kind) {
|
|
78
|
+
case "lander":
|
|
79
|
+
return landerParts().map((part) => placePart(part, e, base, n, 0));
|
|
80
|
+
case "rover":
|
|
81
|
+
return roverParts().map((part) => placePart(part, e, base, n, ((object.heading ?? 0) * Math.PI) / 180));
|
|
82
|
+
case "flag":
|
|
83
|
+
return [cylinder(at(0, 0, 0), 0.015, 2.4, 6, 1.3, false, [0.85, 0.85, 0.88]), box(at(0.55, 2.0, 0), [1.1, 0.7, 0.012], 0, 1.7, 0, [0.9, 0.9, 0.95])];
|
|
84
|
+
case "station":
|
|
85
|
+
return [box(at(0, 0.25, 0), [0.7, 0.5, 0.5], 0.4, 1.4, 0, [0.9, 0.78, 0.45]), cylinder(at(1.0, 0, 0.3), 0.18, 0.55, 10, 0.9, false, DARKER), strut(at(-0.1, 0.5, 0), at(-0.1, 1.5, 0), 0.03, 1.3, [0.85, 0.85, 0.88])];
|
|
86
|
+
case "instrument":
|
|
87
|
+
return [box(at(0, 0.15, 0), [0.45, 0.3, 0.35], 0.5, 1.4, 0, [0.85, 0.85, 0.88])];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const DARKER = [0.35, 0.35, 0.38];
|
|
91
|
+
/**
|
|
92
|
+
* The shadows of an object's parts on the ground for a Sun at an azimuth and an altitude: for each part the convex hull of where its corners fall on the plane of its base, as triangles (x east, y up, z north) lifted a
|
|
93
|
+
* hair off the ground. Flat and hard-edged: the Moon has no air.
|
|
94
|
+
*
|
|
95
|
+
* @param parts - The object's parts, as `objectParts` makes them.
|
|
96
|
+
* @param base - The ground's height under it.
|
|
97
|
+
* @param azimuth - The Sun's azimuth, degrees clockwise from north.
|
|
98
|
+
* @param altitude - The Sun's altitude in degrees.
|
|
99
|
+
* @returns The triangles' corners, three numbers each.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { objectParts, objectsOf, partShadows } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
104
|
+
*
|
|
105
|
+
* const flag = objectsOf("apollo-17").find((object) => object.kind === "flag")!;
|
|
106
|
+
* const shadow = partShadows(objectParts(flag, 0), 0, 95, 13);
|
|
107
|
+
* console.log(shadow.length / 9, "triangles of shadow");
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function partShadows(parts, base, azimuth, altitude) {
|
|
111
|
+
const out = [];
|
|
112
|
+
if (altitude < 0.5)
|
|
113
|
+
return out;
|
|
114
|
+
const reach = 1 / Math.tan((Math.min(altitude, 89) * Math.PI) / 180);
|
|
115
|
+
const away = [-Math.sin((azimuth * Math.PI) / 180), -Math.cos((azimuth * Math.PI) / 180)];
|
|
116
|
+
for (const part of parts) {
|
|
117
|
+
const pts = [];
|
|
118
|
+
for (let i = 0; i < part.position.length; i += 3) {
|
|
119
|
+
const h = Math.max(0, part.position[i + 1] - base);
|
|
120
|
+
const clamp = Math.min(h * reach, 80);
|
|
121
|
+
pts.push([part.position[i] + away[0] * clamp, part.position[i + 2] + away[1] * clamp]);
|
|
122
|
+
pts.push([part.position[i], part.position[i + 2]]);
|
|
123
|
+
}
|
|
124
|
+
const hull = convexHull(pts);
|
|
125
|
+
for (let i = 1; i + 1 < hull.length; i += 1) {
|
|
126
|
+
for (const p of [hull[0], hull[i], hull[i + 1]])
|
|
127
|
+
out.push(p[0], base + 0.02, p[1]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
/** The convex hull of points on the plane, counter-clockwise (Andrew's monotone chain). */
|
|
133
|
+
function convexHull(points) {
|
|
134
|
+
const p = [...points].sort((a, b) => a[0] - b[0] || a[1] - b[1]);
|
|
135
|
+
if (p.length < 3)
|
|
136
|
+
return p;
|
|
137
|
+
const cross2 = (o, a, b) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
|
|
138
|
+
const lower = [];
|
|
139
|
+
for (const q of p) {
|
|
140
|
+
while (lower.length >= 2 && cross2(lower[lower.length - 2], lower[lower.length - 1], q) <= 0)
|
|
141
|
+
lower.pop();
|
|
142
|
+
lower.push(q);
|
|
143
|
+
}
|
|
144
|
+
const upper = [];
|
|
145
|
+
for (const q of [...p].reverse()) {
|
|
146
|
+
while (upper.length >= 2 && cross2(upper[upper.length - 2], upper[upper.length - 1], q) <= 0)
|
|
147
|
+
upper.pop();
|
|
148
|
+
upper.push(q);
|
|
149
|
+
}
|
|
150
|
+
lower.pop();
|
|
151
|
+
upper.pop();
|
|
152
|
+
return [...lower, ...upper];
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* How much of the made ground (its craters and rocks) is kept at a point: 0 at an object's place, up to 1 where it is the ground's own again, so that no made crater cuts the lunar module's legs. The smallest over the objects.
|
|
156
|
+
*
|
|
157
|
+
* @param x - Metres east of the site point.
|
|
158
|
+
* @param y - Metres north of the site point.
|
|
159
|
+
* @param objects - The objects of the site.
|
|
160
|
+
* @returns A share from 0 to 1.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* import { keepAway, objectsOf } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
165
|
+
*
|
|
166
|
+
* const objects = objectsOf("apollo-17");
|
|
167
|
+
* console.log(keepAway(objects[0]!.east, objects[0]!.north, objects), keepAway(500, 500, objects));
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export function keepAway(x, y, objects) {
|
|
171
|
+
let keep = 1;
|
|
172
|
+
for (const o of objects) {
|
|
173
|
+
const { inner, outer } = KEEP_OUT[o.kind];
|
|
174
|
+
const d = Math.hypot(x - o.east, y - o.north);
|
|
175
|
+
const t = Math.min(1, Math.max(0, (d - inner) / (outer - inner)));
|
|
176
|
+
keep = Math.min(keep, t * t * (3 - 2 * t));
|
|
177
|
+
}
|
|
178
|
+
return keep;
|
|
179
|
+
}
|
package/dist/surface/scene.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Item3D, Scene3D } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
2
2
|
import type { ThreeParts } from "../scene/body.ts";
|
|
3
|
+
import { type SiteObject } from "./objects.ts";
|
|
3
4
|
import type { HeightLevel, TerrainManifest } from "./height.ts";
|
|
4
5
|
import type { MoonSky } from "./sky.ts";
|
|
5
6
|
import { type Look } from "./view.ts";
|
|
@@ -23,6 +24,10 @@ export interface SurfaceState {
|
|
|
23
24
|
detail?: boolean;
|
|
24
25
|
/** How much the draw spends on shadows: `full` (the default), `medium` or `fast` (the near march shortened, the made craters left out of it): a draw while the view moves is `fast`. */
|
|
25
26
|
quality?: "full" | "medium" | "fast";
|
|
27
|
+
/** The share of the canvas's width and height the ground is drawn at, 1 (the default) to 0.25: below 1 it is drawn to a smaller target and stretched, which is what a draw while the view moves does on a machine that is slow. It needs `WebGLRenderTarget` in the three.js module (a page's own cut-down module may not have it; the draw is then at full size). */
|
|
28
|
+
scale?: number;
|
|
29
|
+
/** Whether the things at the site (the lander, the rover, the flag, the station) and the rover's tracks are drawn (default true; the made ground keeps away from them while they are). */
|
|
30
|
+
objects?: boolean;
|
|
26
31
|
/** Time the next draw to the end of the graphics queue (it waits for the card, so it is for a few draws, not every one). */
|
|
27
32
|
measure?: boolean;
|
|
28
33
|
}
|
|
@@ -57,6 +62,9 @@ export interface SurfaceItemOptions {
|
|
|
57
62
|
/** The ground's height at a point, metres east and north of the site, for the rocks to stand on: the measured heights, the made craters, the Moon's curve taken off. */
|
|
58
63
|
groundY?(east: number, north: number): number;
|
|
59
64
|
manifest: TerrainManifest;
|
|
65
|
+
/** The things at the site, at their published places, and the rover's traverses as lines (east, north, east, north, ...). */
|
|
66
|
+
objects?: readonly SiteObject[];
|
|
67
|
+
traverse?: readonly (readonly number[])[];
|
|
60
68
|
/** A key that changes whenever the picture would: the renderer keeps one still for each key. */
|
|
61
69
|
key: string;
|
|
62
70
|
}
|
package/dist/surface/scene.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { rocksAround } from "./detail.js";
|
|
2
|
-
import { DISC_FRAGMENT, DISC_VERTEX, GROUND_FRAGMENT, GROUND_VERTEX, ROCK_FRAGMENT, ROCK_VERTEX, SHADOW_FRAGMENT, SHADOW_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 } from "./glsl.js";
|
|
3
|
+
import { KEEP_OUT, objectParts, partShadows } from "./objects.js";
|
|
3
4
|
import { rockMesh } from "./rocks.js";
|
|
4
5
|
import { cameraAxes, viewProjection } from "./view.js";
|
|
5
6
|
const RINGS = 480;
|
|
@@ -126,6 +127,8 @@ export function createSurfaceItem(options) {
|
|
|
126
127
|
uHzSun: { value: new T.Vector3(0, 1, 0) },
|
|
127
128
|
uHzRange: { value: new T.Vector2(hz?.from ?? -10, hz?.to ?? 60) },
|
|
128
129
|
uSunDegrees: { value: 13 },
|
|
130
|
+
uKeep: { value: Array.from({ length: 6 }, (_, i) => { const o = (options.objects ?? [])[i]; const k = o === undefined ? undefined : KEEP_OUT[o.kind]; return [o?.east ?? 0, o?.north ?? 0, k?.inner ?? 1, k?.outer ?? 2]; }).flat() },
|
|
131
|
+
uKeepCount: { value: Math.min(6, options.objects?.length ?? 0) },
|
|
129
132
|
uNear: { value: new T.Vector3(56, 80, 36) },
|
|
130
133
|
uPixel: { value: 0.0024 },
|
|
131
134
|
uDetail: { value: 1 },
|
|
@@ -181,7 +184,7 @@ export function createSurfaceItem(options) {
|
|
|
181
184
|
const ground = options.groundY;
|
|
182
185
|
if (ground === undefined || state.detail === false)
|
|
183
186
|
return;
|
|
184
|
-
const pixel =
|
|
187
|
+
const pixel = fullPixel;
|
|
185
188
|
const key = `${Math.round(state.eye.east / 8)}|${Math.round(state.eye.north / 8)}|${state.sky.sun.azimuth.toFixed(0)}|${state.sky.sun.altitude.toFixed(0)}|${pixel.toFixed(4)}`;
|
|
186
189
|
if (key === rockKey)
|
|
187
190
|
return;
|
|
@@ -213,15 +216,136 @@ export function createSurfaceItem(options) {
|
|
|
213
216
|
shadowMeshObject.geometry = shadowGeometry;
|
|
214
217
|
}
|
|
215
218
|
};
|
|
219
|
+
// the things at the site: their parts as one mesh (the rocks' shader and light), their shadows (the rocks' shadow material) and the rover's traverse as a strip along the line. The parts are built where the ground
|
|
220
|
+
// is (from the callback), so they are made again when the ground has changed or the Sun has moved.
|
|
221
|
+
const objectMaterial = new T.ShaderMaterial({ vertexShader: OBJECT_VERTEX, fragmentShader: OBJECT_FRAGMENT, uniforms });
|
|
222
|
+
let objectMesh;
|
|
223
|
+
let objectShadowMesh;
|
|
224
|
+
let objectKey = "";
|
|
225
|
+
const makeObjects = (state) => {
|
|
226
|
+
const list = options.objects ?? [];
|
|
227
|
+
const ground = options.groundY;
|
|
228
|
+
const on = state.objects !== false && list.length > 0 && ground !== undefined;
|
|
229
|
+
if (objectMesh !== undefined)
|
|
230
|
+
objectMesh.visible = on;
|
|
231
|
+
if (objectShadowMesh !== undefined)
|
|
232
|
+
objectShadowMesh.visible = on;
|
|
233
|
+
if (trackMesh !== undefined)
|
|
234
|
+
trackMesh.visible = on;
|
|
235
|
+
if (!on || ground === undefined)
|
|
236
|
+
return;
|
|
237
|
+
const key = `${state.sky.sun.azimuth.toFixed(1)}|${state.sky.sun.altitude.toFixed(1)}|${objectVersion}`;
|
|
238
|
+
if (key === objectKey)
|
|
239
|
+
return;
|
|
240
|
+
objectKey = key;
|
|
241
|
+
const position = [];
|
|
242
|
+
const normal = [];
|
|
243
|
+
const tone = [];
|
|
244
|
+
const tint = [];
|
|
245
|
+
const shadow = [];
|
|
246
|
+
for (const object of list) {
|
|
247
|
+
const base = ground(object.east, object.north);
|
|
248
|
+
const parts = objectParts(object, base);
|
|
249
|
+
for (const part of parts) {
|
|
250
|
+
position.push(...part.position);
|
|
251
|
+
normal.push(...part.normal);
|
|
252
|
+
for (let i = 0; i < part.position.length / 3; i += 1) {
|
|
253
|
+
tone.push(part.tone);
|
|
254
|
+
tint.push(...(part.colour ?? [1, 1, 1]));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
shadow.push(...partShadows(parts, base, state.sky.sun.azimuth, state.sky.sun.altitude));
|
|
258
|
+
}
|
|
259
|
+
const geometry = new T.BufferGeometry();
|
|
260
|
+
geometry.setAttribute("position", new T.BufferAttribute(new Float32Array(position), 3));
|
|
261
|
+
geometry.setAttribute("normal", new T.BufferAttribute(new Float32Array(normal), 3));
|
|
262
|
+
geometry.setAttribute("tone", new T.BufferAttribute(new Float32Array(tone), 1));
|
|
263
|
+
geometry.setAttribute("tint", new T.BufferAttribute(new Float32Array(tint), 3));
|
|
264
|
+
const shadowGeometry = new T.BufferGeometry();
|
|
265
|
+
shadowGeometry.setAttribute("position", new T.BufferAttribute(new Float32Array(shadow), 3));
|
|
266
|
+
shadowGeometry.setAttribute("uv", new T.BufferAttribute(new Float32Array((shadow.length / 3) * 2).fill(0.5).map((v, i) => (i % 2 === 1 ? 0 : v)), 2));
|
|
267
|
+
if (objectMesh === undefined) {
|
|
268
|
+
objectMesh = new T.Mesh(geometry, objectMaterial);
|
|
269
|
+
objectMesh.frustumCulled = false;
|
|
270
|
+
objectMesh.renderOrder = 1;
|
|
271
|
+
objectShadowMesh = new T.Mesh(shadowGeometry, shadowMaterial);
|
|
272
|
+
objectShadowMesh.frustumCulled = false;
|
|
273
|
+
objectShadowMesh.renderOrder = 2;
|
|
274
|
+
scene.add(objectMesh, objectShadowMesh);
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
objectMesh.geometry.dispose();
|
|
278
|
+
objectShadowMesh.geometry.dispose();
|
|
279
|
+
objectMesh.geometry = geometry;
|
|
280
|
+
objectShadowMesh.geometry = shadowGeometry;
|
|
281
|
+
}
|
|
282
|
+
objectMesh.visible = true;
|
|
283
|
+
objectShadowMesh.visible = true;
|
|
284
|
+
};
|
|
285
|
+
let objectVersion = 0;
|
|
286
|
+
// the traverse: a strip 2.8 m wide along each line, points about 2 m apart, with its distance along and its side as the texture coordinates
|
|
287
|
+
let trackMesh;
|
|
288
|
+
let trackMaterial;
|
|
289
|
+
let trackGeometry;
|
|
290
|
+
if (options.traverse !== undefined && options.traverse.length > 0) {
|
|
291
|
+
const positions = [];
|
|
292
|
+
const uvs = [];
|
|
293
|
+
const indices = [];
|
|
294
|
+
let along = 0;
|
|
295
|
+
for (const line of options.traverse) {
|
|
296
|
+
// densified: a point at least every 2 m
|
|
297
|
+
const pts = [];
|
|
298
|
+
for (let i = 0; i + 1 < line.length / 2; i += 1) {
|
|
299
|
+
const [x0, y0, x1, y1] = [line[2 * i], line[2 * i + 1], line[2 * i + 2], line[2 * i + 3]];
|
|
300
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(x1 - x0, y1 - y0) / 2));
|
|
301
|
+
for (let k = 0; k < steps; k += 1)
|
|
302
|
+
pts.push([x0 + ((x1 - x0) * k) / steps, y0 + ((y1 - y0) * k) / steps]);
|
|
303
|
+
}
|
|
304
|
+
pts.push([line[line.length - 2], line[line.length - 1]]);
|
|
305
|
+
const first = positions.length / 3;
|
|
306
|
+
for (let i = 0; i < pts.length; i += 1) {
|
|
307
|
+
const a = pts[Math.max(0, i - 1)];
|
|
308
|
+
const b = pts[Math.min(pts.length - 1, i + 1)];
|
|
309
|
+
const len = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1;
|
|
310
|
+
const [nx, ny] = [-(b[1] - a[1]) / len, (b[0] - a[0]) / len];
|
|
311
|
+
if (i > 0)
|
|
312
|
+
along += Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]);
|
|
313
|
+
for (const side of [-1, 1]) {
|
|
314
|
+
positions.push(pts[i][0] + nx * 1.4 * side, pts[i][1] + ny * 1.4 * side, 0);
|
|
315
|
+
uvs.push(along, side);
|
|
316
|
+
}
|
|
317
|
+
if (i > 0) {
|
|
318
|
+
const v = first + 2 * i;
|
|
319
|
+
indices.push(v - 2, v - 1, v, v - 1, v + 1, v);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
trackGeometry = new T.BufferGeometry();
|
|
324
|
+
trackGeometry.setAttribute("position", new T.BufferAttribute(new Float32Array(positions), 3));
|
|
325
|
+
trackGeometry.setAttribute("uv", new T.BufferAttribute(new Float32Array(uvs), 2));
|
|
326
|
+
trackGeometry.setIndex(new T.BufferAttribute(new Uint32Array(indices), 1));
|
|
327
|
+
trackMaterial = new T.ShaderMaterial({ vertexShader: TRACK_VERTEX, fragmentShader: TRACK_FRAGMENT, uniforms, transparent: true, depthWrite: false, side: 2 });
|
|
328
|
+
trackMaterial.polygonOffset = true;
|
|
329
|
+
trackMaterial.polygonOffsetFactor = -3;
|
|
330
|
+
trackMaterial.polygonOffsetUnits = -3;
|
|
331
|
+
trackMesh = new T.Mesh(trackGeometry, trackMaterial);
|
|
332
|
+
trackMesh.frustumCulled = false;
|
|
333
|
+
trackMesh.renderOrder = 0.5;
|
|
334
|
+
scene.add(trackMesh);
|
|
335
|
+
}
|
|
216
336
|
let measuring = false;
|
|
217
337
|
let drawMs;
|
|
218
338
|
let aspect = context.width / Math.max(1, context.height);
|
|
219
339
|
let pixelHeight = Math.max(1, context.height);
|
|
340
|
+
let fullPixel = 0.0024;
|
|
341
|
+
let scale = 1;
|
|
220
342
|
let last;
|
|
221
343
|
const place = (state) => {
|
|
222
344
|
last = state;
|
|
223
345
|
const { eye, look, sky } = state;
|
|
346
|
+
fullPixel = (2 * Math.tan((look.fov * Math.PI) / 360)) / pixelHeight;
|
|
224
347
|
makeRocks(state);
|
|
348
|
+
makeObjects(state);
|
|
225
349
|
measuring = state.measure === true;
|
|
226
350
|
const world = new T.Vector3(eye.east, eye.up, -eye.north);
|
|
227
351
|
const vp = viewProjection([world.x, world.y, world.z], look, aspect);
|
|
@@ -247,7 +371,9 @@ export function createSurfaceItem(options) {
|
|
|
247
371
|
const q = state.quality ?? "full";
|
|
248
372
|
const near = q === "full" ? [56, 80, 36] : q === "medium" ? [28, 40, 16] : [10, 14, 0];
|
|
249
373
|
uniforms.uNear.value.set(near[0], near[1], near[2]);
|
|
250
|
-
|
|
374
|
+
scale = Math.min(1, Math.max(0.25, state.scale ?? 1));
|
|
375
|
+
// a pixel of a smaller target is wider by the share it is drawn at
|
|
376
|
+
uniforms.uPixel.value = fullPixel / (T.WebGLRenderTarget === undefined ? 1 : scale);
|
|
251
377
|
const axes = cameraAxes(look);
|
|
252
378
|
const toEnu = (v) => [v[0], -v[2], v[1]];
|
|
253
379
|
const sunWorld = [sky.sun.enu[0], sky.sun.enu[2], -sky.sun.enu[1]];
|
|
@@ -271,6 +397,20 @@ export function createSurfaceItem(options) {
|
|
|
271
397
|
earth.mesh.visible = sky.earth.altitude > -1;
|
|
272
398
|
};
|
|
273
399
|
const camera = new T.OrthographicCamera(-1, 1, 1, -1, 0, 1);
|
|
400
|
+
// the stretch of a smaller draw to the canvas: one square with the target as its texture (made when first needed)
|
|
401
|
+
let target;
|
|
402
|
+
const blitUniforms = { uMap: { value: null } };
|
|
403
|
+
const blitMaterial = new T.ShaderMaterial({
|
|
404
|
+
vertexShader: "varying vec2 vUv; void main() { vUv = position.xy * 0.5 + 0.5; gl_Position = vec4(position.xy, 0.0, 1.0); }",
|
|
405
|
+
fragmentShader: "precision highp float; uniform sampler2D uMap; varying vec2 vUv; void main() { gl_FragColor = texture2D(uMap, vUv); }",
|
|
406
|
+
uniforms: blitUniforms,
|
|
407
|
+
depthTest: false,
|
|
408
|
+
depthWrite: false,
|
|
409
|
+
});
|
|
410
|
+
const blitMesh = new T.Mesh(square, blitMaterial);
|
|
411
|
+
blitMesh.frustumCulled = false;
|
|
412
|
+
const blitScene = new T.Scene();
|
|
413
|
+
blitScene.add(blitMesh);
|
|
274
414
|
return {
|
|
275
415
|
scene,
|
|
276
416
|
camera,
|
|
@@ -283,7 +423,24 @@ export function createSurfaceItem(options) {
|
|
|
283
423
|
},
|
|
284
424
|
render(gl) {
|
|
285
425
|
const t0 = performance.now();
|
|
286
|
-
|
|
426
|
+
if (scale < 1 && T.WebGLRenderTarget !== undefined) {
|
|
427
|
+
// the ground at a smaller size, stretched to the canvas by one textured square
|
|
428
|
+
const w = Math.max(1, Math.round(gl.domElement.width * scale));
|
|
429
|
+
const h = Math.max(1, Math.round(gl.domElement.height * scale));
|
|
430
|
+
const three = gl;
|
|
431
|
+
if (target === undefined) {
|
|
432
|
+
target = new T.WebGLRenderTarget(w, h, { minFilter: T.LinearFilter, magFilter: T.LinearFilter, depthBuffer: true });
|
|
433
|
+
blitUniforms.uMap.value = target.texture;
|
|
434
|
+
}
|
|
435
|
+
else if (target.width !== w || target.height !== h)
|
|
436
|
+
target.setSize(w, h);
|
|
437
|
+
three.setRenderTarget(target);
|
|
438
|
+
gl.render(scene, camera);
|
|
439
|
+
three.setRenderTarget(null);
|
|
440
|
+
gl.render(blitScene, camera);
|
|
441
|
+
}
|
|
442
|
+
else
|
|
443
|
+
gl.render(scene, camera);
|
|
287
444
|
if (measuring) {
|
|
288
445
|
const canvas = gl.domElement;
|
|
289
446
|
// reading one pixel back waits for the card to finish the draw (`finish` returns at once in a browser that draws in another process)
|
|
@@ -299,6 +456,7 @@ export function createSurfaceItem(options) {
|
|
|
299
456
|
(options.pictures ?? []).forEach((level, i) => wanted.has(level.id) && (pictures[i].needsUpdate = true));
|
|
300
457
|
(options.horizons ?? []).forEach((h, i) => wanted.has(`horizon:${h.id}`) && (horizons[i].needsUpdate = true));
|
|
301
458
|
rockKey = "";
|
|
459
|
+
objectVersion += 1;
|
|
302
460
|
},
|
|
303
461
|
resize(width, height) {
|
|
304
462
|
aspect = width / Math.max(1, height);
|
|
@@ -316,6 +474,13 @@ export function createSurfaceItem(options) {
|
|
|
316
474
|
shadowMaterial.dispose();
|
|
317
475
|
sun.material.dispose();
|
|
318
476
|
earth.material.dispose();
|
|
477
|
+
target?.dispose();
|
|
478
|
+
objectMesh?.geometry.dispose();
|
|
479
|
+
objectShadowMesh?.geometry.dispose();
|
|
480
|
+
trackGeometry?.dispose();
|
|
481
|
+
trackMaterial?.dispose();
|
|
482
|
+
objectMaterial.dispose();
|
|
483
|
+
blitMaterial.dispose();
|
|
319
484
|
},
|
|
320
485
|
};
|
|
321
486
|
},
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One piece of an object's shape: triangles with their corners and normals (x east, y up, z north), how bright it is (foil and paint are lighter than dust), a flat colour that tints it (white where it has none), and for
|
|
3
|
+
* the rover which wheel it is (0 to 3: front left, front right, rear left, rear right) so that a wheel can be turned.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { objectParts, objectsOf, type Part } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
8
|
+
*
|
|
9
|
+
* const flag = objectsOf("apollo-17").find((object) => object.kind === "flag")!;
|
|
10
|
+
* const pole: Part = objectParts(flag, 0)[0]!;
|
|
11
|
+
* console.log(pole.position.length / 9, "triangles, tone", pole.tone);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export interface Part {
|
|
15
|
+
position: number[];
|
|
16
|
+
normal: number[];
|
|
17
|
+
tone: number;
|
|
18
|
+
colour?: [number, number, number];
|
|
19
|
+
wheel?: number;
|
|
20
|
+
}
|
|
21
|
+
export type V3 = [number, number, number];
|
|
22
|
+
/** A box with its centre, its three sizes, a turn about the up axis (radians) and a tilt about its own x axis. */
|
|
23
|
+
export declare function box(centre: V3, size: V3, turn?: number, tone?: number, tilt?: number, colour?: V3): Part;
|
|
24
|
+
/** A cone or a cylinder standing on its base (axis up) or lying along x (a wheel): radius at the base and at the top, smooth sides, ends closed. */
|
|
25
|
+
export declare function frustum(base: V3, r0: number, r1: number, height: number, sides?: number, tone?: number, lying?: boolean, colour?: V3): Part;
|
|
26
|
+
/** A cylinder: a frustum with one radius. */
|
|
27
|
+
export declare const cylinder: (base: V3, radius: number, height: number, sides?: number, tone?: number, lying?: boolean, colour?: V3) => Part;
|
|
28
|
+
/** A strut: a thin box from one point to another. */
|
|
29
|
+
export declare function strut(a: V3, b: V3, thickness: number, tone?: number, colour?: V3): Part;
|
|
30
|
+
/** A shallow dish: a cone with its rim a radius out and its middle `depth` back, facing along `facing`. Both sides drawn. */
|
|
31
|
+
export declare function dish(centre: V3, facing: V3, radius: number, depth: number, sides?: number, tone?: number, colour?: V3): Part;
|
|
32
|
+
/** Turn and move a part about the up axis (radians, clockwise from north as heading is) and put it at a place: `x, z` of the part's own frame are right and forward. */
|
|
33
|
+
export declare function placePart(part: Part, east: number, base: number, north: number, turn: number): Part;
|
|
34
|
+
/**
|
|
35
|
+
* The lunar module's descent stage in its own frame (x right, z forward, the middle of its footprint on the ground at the origin): an octagonal body in a flat gold, a dark deck, the engine's bell under it, four splayed
|
|
36
|
+
* legs each with two braces and a footpad, and the ladder down the forward leg to a porch. Sizes from the published ones (about 4.2 m across the body, the deck 3.2 m up, 4.7 m from the middle to a footpad).
|
|
37
|
+
*
|
|
38
|
+
* @returns The parts.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { landerParts } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
43
|
+
*
|
|
44
|
+
* console.log(landerParts().length, "parts");
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function landerParts(): Part[];
|
|
48
|
+
/**
|
|
49
|
+
* The lunar rover in its own frame (x right, z forward, the middle of its wheelbase on the ground at the origin): a frame and floor, four wheels (parts 0 to 3: front left, front right, rear left, rear right) with a
|
|
50
|
+
* fender over each, two seats, the console with its handle, the dish on its mast, the television camera on its own mast, a whip antenna and the boxes at both ends. Sizes from the published ones (3.1 m long, 2.3 m
|
|
51
|
+
* wide, wheels 0.82 m across, the wheelbase 2.3 m).
|
|
52
|
+
*
|
|
53
|
+
* @returns The parts.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { roverParts } from "@johnmorrisdotca/wakusei-planets/surface";
|
|
58
|
+
*
|
|
59
|
+
* console.log(roverParts().filter((part) => part.wheel !== undefined).length, "wheels");
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare function roverParts(): Part[];
|