@johnmorrisdotca/wakusei-planets 0.45.1 → 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.
Files changed (40) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/NOTICE.md +12 -0
  3. package/README.md +4 -3
  4. package/dist/scene/body.d.ts +1 -1
  5. package/dist/scene/textures.d.ts +2 -2
  6. package/dist/space/element.d.ts +7 -0
  7. package/dist/space/element.js +12 -0
  8. package/dist/surface/detail.d.ts +186 -0
  9. package/dist/surface/detail.js +316 -0
  10. package/dist/surface/element.d.ts +103 -0
  11. package/dist/surface/element.js +685 -0
  12. package/dist/surface/glsl.d.ts +26 -0
  13. package/dist/surface/glsl.js +457 -0
  14. package/dist/surface/height.d.ts +257 -0
  15. package/dist/surface/height.js +232 -0
  16. package/dist/surface/index.d.ts +10 -0
  17. package/dist/surface/index.js +10 -0
  18. package/dist/surface/land.d.ts +111 -0
  19. package/dist/surface/land.js +261 -0
  20. package/dist/surface/load.d.ts +133 -0
  21. package/dist/surface/load.js +183 -0
  22. package/dist/surface/objects-apollo-17.d.ts +4 -0
  23. package/dist/surface/objects-apollo-17.js +19 -0
  24. package/dist/surface/objects.d.ts +127 -0
  25. package/dist/surface/objects.js +179 -0
  26. package/dist/surface/rocks.d.ts +45 -0
  27. package/dist/surface/rocks.js +166 -0
  28. package/dist/surface/scene.d.ts +72 -0
  29. package/dist/surface/scene.js +488 -0
  30. package/dist/surface/shapes.d.ts +62 -0
  31. package/dist/surface/shapes.js +247 -0
  32. package/dist/surface/site.d.ts +58 -0
  33. package/dist/surface/site.js +38 -0
  34. package/dist/surface/sky.d.ts +81 -0
  35. package/dist/surface/sky.js +60 -0
  36. package/dist/surface/view.d.ts +129 -0
  37. package/dist/surface/view.js +139 -0
  38. package/dist/version.d.ts +1 -1
  39. package/dist/version.js +1 -1
  40. package/package.json +23 -1
@@ -0,0 +1,488 @@
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 } from "./glsl.js";
3
+ import { KEEP_OUT, objectParts, partShadows } from "./objects.js";
4
+ import { rockMesh } from "./rocks.js";
5
+ import { cameraAxes, viewProjection } from "./view.js";
6
+ const RINGS = 480;
7
+ const RAYS = 600;
8
+ const R_MIN = 0.1;
9
+ const R_MAX = 300_000;
10
+ /** The distances out to which each level is read, finest first (the coarsest reads to the end): a level's own samples are about this far from being finer than a pixel. */
11
+ const BANDS = [4_000, 16_000, 60_000, Infinity];
12
+ /** The ground's mesh: rings spaced out geometrically (a share of the distance apart) and rays across the view; its positions are not positions but where to look in the polar grid. */
13
+ function groundGeometry(T) {
14
+ const positions = new Float32Array((RINGS + 1) * (RAYS + 1) * 3);
15
+ let at = 0;
16
+ for (let i = 0; i <= RINGS; i += 1) {
17
+ for (let j = 0; j <= RAYS; j += 1) {
18
+ positions[at++] = i / RINGS;
19
+ positions[at++] = (j / RAYS) * 2 - 1;
20
+ positions[at++] = 0;
21
+ }
22
+ }
23
+ const indices = new Uint32Array(RINGS * RAYS * 6);
24
+ let k = 0;
25
+ for (let i = 0; i < RINGS; i += 1) {
26
+ for (let j = 0; j < RAYS; j += 1) {
27
+ const a = i * (RAYS + 1) + j;
28
+ const b = a + 1;
29
+ const c = a + RAYS + 1;
30
+ const d = c + 1;
31
+ indices[k++] = a;
32
+ indices[k++] = b;
33
+ indices[k++] = c;
34
+ indices[k++] = b;
35
+ indices[k++] = d;
36
+ indices[k++] = c;
37
+ }
38
+ }
39
+ const geometry = new T.BufferGeometry();
40
+ geometry.setAttribute("position", new T.BufferAttribute(positions, 3));
41
+ geometry.setIndex(new T.BufferAttribute(indices, 1));
42
+ return { geometry, triangles: indices.length / 3 };
43
+ }
44
+ function squareGeometry(T) {
45
+ const geometry = new T.BufferGeometry();
46
+ geometry.setAttribute("position", new T.BufferAttribute(new Float32Array([-1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0]), 3));
47
+ geometry.setIndex(new T.BufferAttribute(new Uint16Array([0, 1, 2, 0, 2, 3]), 1));
48
+ return geometry;
49
+ }
50
+ /** The Moon's standing view for one site as an item of the shared renderer. */
51
+ export function createSurfaceItem(options) {
52
+ return {
53
+ key: options.key,
54
+ build(context) {
55
+ const T = context.three;
56
+ const { manifest } = options;
57
+ const textures = options.levels.map((level) => context.assets.acquire(`surface:${manifest.site}:${level.id}`, () => {
58
+ const texture = new T.DataTexture(level.data, level.size, level.size, T.RGBAFormat);
59
+ texture.minFilter = 1003; // NearestFilter: the two bytes of a height must not be blended
60
+ texture.magFilter = 1003;
61
+ texture.wrapS = T.ClampToEdgeWrapping;
62
+ texture.wrapT = T.ClampToEdgeWrapping;
63
+ texture.generateMipmaps = false;
64
+ texture.flipY = false;
65
+ texture.needsUpdate = true;
66
+ return texture;
67
+ }, (texture) => texture.dispose()));
68
+ const pictures = (options.pictures ?? []).map((level) => context.assets.acquire(`surface:${manifest.site}:picture:${level.id}`, () => {
69
+ const texture = new T.DataTexture(level.data, level.size, level.size, T.RGBAFormat);
70
+ texture.minFilter = 1008; // LinearMipmapLinearFilter: a picture seen from far is averaged, not sparkling
71
+ texture.magFilter = T.LinearFilter;
72
+ texture.wrapS = T.ClampToEdgeWrapping;
73
+ texture.wrapT = T.ClampToEdgeWrapping;
74
+ texture.generateMipmaps = true;
75
+ texture.flipY = false;
76
+ texture.needsUpdate = true;
77
+ return texture;
78
+ }, (texture) => texture.dispose()));
79
+ const horizons = (options.horizons ?? []).map((h) => context.assets.acquire(`surface:${manifest.site}:horizon:${h.id}`, () => {
80
+ const texture = new T.DataTexture(h.data, h.width, h.height, T.RGBAFormat);
81
+ texture.minFilter = T.LinearFilter; // a smooth horizon between the cells; each colour is its own direction, so blending them is blending the same direction's neighbours
82
+ texture.magFilter = T.LinearFilter;
83
+ texture.wrapS = T.ClampToEdgeWrapping;
84
+ texture.wrapT = T.ClampToEdgeWrapping;
85
+ texture.generateMipmaps = false;
86
+ texture.flipY = false;
87
+ texture.needsUpdate = true;
88
+ return texture;
89
+ }, (texture) => texture.dispose()));
90
+ const hz = manifest.horizon;
91
+ const level = (i) => {
92
+ const l = options.levels[i];
93
+ return { value: [l.half, l.res, l.size, BANDS[i] ?? Infinity] };
94
+ };
95
+ const maxY = Math.max(...options.levels.map((l) => l.max ?? manifest.height.min + manifest.height.range)) - manifest.ground;
96
+ const uniforms = {
97
+ uVP: { value: new Float32Array(16) },
98
+ uEyeEN: { value: new T.Vector2(0, 0) },
99
+ uEye: { value: new T.Vector3(0, 1.7, 0) },
100
+ uYaw: { value: 0 },
101
+ uSpan: { value: 0.6 },
102
+ uRMin: { value: R_MIN },
103
+ uRMax: { value: R_MAX },
104
+ uSun: { value: new T.Vector3(0, 0.5, 0.8) },
105
+ uExposure: { value: 4.5 },
106
+ uAlbedo: { value: 0.14 },
107
+ uMaxY: { value: maxY },
108
+ uEarthshine: { value: 0.05 },
109
+ uShadows: { value: 1 },
110
+ uHmin: { value: manifest.height.min },
111
+ uHrange: { value: manifest.height.range },
112
+ uFarHeight: { value: new T.Vector2(options.levels[3]?.height?.min ?? manifest.height.min, options.levels[3]?.height?.range ?? manifest.height.range) },
113
+ uGround: { value: manifest.ground },
114
+ uRadius: { value: manifest.radius },
115
+ uH0: { value: textures[0] },
116
+ uH1: { value: textures[1] ?? textures[0] },
117
+ uH2: { value: textures[2] ?? textures[0] },
118
+ uH3: { value: textures[3] ?? textures[0] },
119
+ uA0: { value: pictures[0] ?? textures[0] },
120
+ uA1: { value: pictures[1] ?? textures[0] },
121
+ uA2: { value: pictures[2] ?? textures[0] },
122
+ uPicture: { value: pictures.length >= 3 ? 1 : 0 },
123
+ uZ0: { value: horizons[0] ?? textures[0] },
124
+ uZ1: { value: horizons[1] ?? textures[0] },
125
+ uZ2: { value: horizons[2] ?? textures[0] },
126
+ uHzOn: { value: horizons.length >= 3 && hz !== null && hz !== undefined ? 1 : 0 },
127
+ uHzSun: { value: new T.Vector3(0, 1, 0) },
128
+ uHzRange: { value: new T.Vector2(hz?.from ?? -10, hz?.to ?? 60) },
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) },
132
+ uNear: { value: new T.Vector3(56, 80, 36) },
133
+ uPixel: { value: 0.0024 },
134
+ uDetail: { value: 1 },
135
+ uL0: level(0),
136
+ uL1: level(1),
137
+ uL2: level(2),
138
+ uL3: level(3),
139
+ };
140
+ const { geometry, triangles } = groundGeometry(T);
141
+ const groundMaterial = new T.ShaderMaterial({ vertexShader: GROUND_VERTEX, fragmentShader: GROUND_FRAGMENT, uniforms });
142
+ const ground = new T.Mesh(geometry, groundMaterial);
143
+ ground.frustumCulled = false;
144
+ ground.renderOrder = 0;
145
+ const square = squareGeometry(T);
146
+ const disc = (kind, half) => {
147
+ const u = {
148
+ uVP: uniforms.uVP,
149
+ uEyeWorld: { value: new T.Vector3() },
150
+ uDirWorld: { value: new T.Vector3(0, 1, 0) },
151
+ uRightWorld: { value: new T.Vector3(1, 0, 0) },
152
+ uUpWorld: { value: new T.Vector3(0, 0, -1) },
153
+ uHalf: { value: half },
154
+ uKind: { value: kind },
155
+ uSunEnu: { value: new T.Vector3() },
156
+ uDirEnu: { value: new T.Vector3() },
157
+ uRightEnu: { value: new T.Vector3(1, 0, 0) },
158
+ uUpEnu: { value: new T.Vector3(0, 1, 0) },
159
+ uExposure: uniforms.uExposure,
160
+ };
161
+ const material = new T.ShaderMaterial({ vertexShader: DISC_VERTEX, fragmentShader: DISC_FRAGMENT, uniforms: u, depthTest: false, depthWrite: false });
162
+ const mesh = new T.Mesh(square, material);
163
+ mesh.frustumCulled = false;
164
+ mesh.renderOrder = -10 + kind;
165
+ return { mesh, material, uniforms: u };
166
+ };
167
+ const sun = disc(0, Math.tan((0.533 * Math.PI) / 360));
168
+ const earth = disc(1, Math.tan((1.9 * Math.PI) / 360));
169
+ const scene = new T.Scene();
170
+ scene.add(sun.mesh, earth.mesh, ground);
171
+ // the rocks and their shadows, made again when the eyes have walked a while or the Sun has moved
172
+ const rockMaterial = new T.ShaderMaterial({ vertexShader: ROCK_VERTEX, fragmentShader: ROCK_FRAGMENT, uniforms });
173
+ const shadowMaterial = new T.ShaderMaterial({ vertexShader: SHADOW_VERTEX, fragmentShader: SHADOW_FRAGMENT, uniforms: { uVP: uniforms.uVP }, transparent: true, depthWrite: false });
174
+ shadowMaterial.polygonOffset = true;
175
+ shadowMaterial.polygonOffsetFactor = -2;
176
+ shadowMaterial.polygonOffsetUnits = -2;
177
+ let rockGeometry;
178
+ let shadowGeometry;
179
+ let rockMeshObject;
180
+ let shadowMeshObject;
181
+ let rockKey = "";
182
+ let rockCount = 0;
183
+ const makeRocks = (state) => {
184
+ const ground = options.groundY;
185
+ if (ground === undefined || state.detail === false)
186
+ return;
187
+ const pixel = fullPixel;
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)}`;
189
+ if (key === rockKey)
190
+ return;
191
+ rockKey = key;
192
+ const rocks = rocksAround(state.eye.east, state.eye.north, 55, pixel);
193
+ const mesh = rockMesh(rocks, ground, { azimuth: state.sky.sun.azimuth, altitude: state.sky.sun.altitude });
194
+ rockCount = mesh.count;
195
+ rockGeometry?.dispose();
196
+ shadowGeometry?.dispose();
197
+ rockGeometry = new T.BufferGeometry();
198
+ rockGeometry.setAttribute("position", new T.BufferAttribute(mesh.position, 3));
199
+ rockGeometry.setAttribute("normal", new T.BufferAttribute(mesh.normal, 3));
200
+ rockGeometry.setAttribute("tone", new T.BufferAttribute(mesh.tone, 1));
201
+ shadowGeometry = new T.BufferGeometry();
202
+ shadowGeometry.setAttribute("position", new T.BufferAttribute(mesh.shadowPosition, 3));
203
+ shadowGeometry.setAttribute("uv", new T.BufferAttribute(mesh.shadowUv, 2));
204
+ shadowGeometry.setIndex(new T.BufferAttribute(mesh.shadowIndex, 1));
205
+ if (rockMeshObject === undefined) {
206
+ rockMeshObject = new T.Mesh(rockGeometry, rockMaterial);
207
+ rockMeshObject.frustumCulled = false;
208
+ rockMeshObject.renderOrder = 1;
209
+ shadowMeshObject = new T.Mesh(shadowGeometry, shadowMaterial);
210
+ shadowMeshObject.frustumCulled = false;
211
+ shadowMeshObject.renderOrder = 2;
212
+ scene.add(rockMeshObject, shadowMeshObject);
213
+ }
214
+ else {
215
+ rockMeshObject.geometry = rockGeometry;
216
+ shadowMeshObject.geometry = shadowGeometry;
217
+ }
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
+ }
336
+ let measuring = false;
337
+ let drawMs;
338
+ let aspect = context.width / Math.max(1, context.height);
339
+ let pixelHeight = Math.max(1, context.height);
340
+ let fullPixel = 0.0024;
341
+ let scale = 1;
342
+ let last;
343
+ const place = (state) => {
344
+ last = state;
345
+ const { eye, look, sky } = state;
346
+ fullPixel = (2 * Math.tan((look.fov * Math.PI) / 360)) / pixelHeight;
347
+ makeRocks(state);
348
+ makeObjects(state);
349
+ measuring = state.measure === true;
350
+ const world = new T.Vector3(eye.east, eye.up, -eye.north);
351
+ const vp = viewProjection([world.x, world.y, world.z], look, aspect);
352
+ uniforms.uVP.value.set(vp);
353
+ uniforms.uEyeEN.value.set(eye.east, eye.north);
354
+ uniforms.uEye.value.set(eye.east, eye.up, eye.north);
355
+ uniforms.uYaw.value = (look.azimuth * Math.PI) / 180;
356
+ const halfV = Math.tan((look.fov * Math.PI) / 360);
357
+ const halfH = Math.atan(halfV * aspect);
358
+ const tilt = Math.abs(look.pitch) + look.fov / 2;
359
+ uniforms.uSpan.value = tilt > 78 ? Math.PI : Math.min(Math.PI, halfH / Math.cos((tilt * Math.PI) / 180) + 0.06);
360
+ uniforms.uSun.value.set(sky.sun.enu[0], sky.sun.enu[2], sky.sun.enu[1]);
361
+ uniforms.uExposure.value = state.exposure ?? 4.5;
362
+ uniforms.uAlbedo.value = state.albedo ?? 0.14;
363
+ uniforms.uShadows.value = state.shadows === false ? 0 : 1;
364
+ uniforms.uDetail.value = state.detail === false ? 0 : 1;
365
+ uniforms.uSunDegrees.value = sky.sun.altitude;
366
+ // the sector pair the Sun's azimuth lies between, in the baked horizon's eighteen directions
367
+ const sectors = hz?.sectors ?? 18;
368
+ const f = (((sky.sun.azimuth % 360) + 360) % 360) / (360 / sectors);
369
+ const a = Math.floor(f);
370
+ uniforms.uHzSun.value.set(a % sectors, (a + 1) % sectors, f - a);
371
+ const q = state.quality ?? "full";
372
+ const near = q === "full" ? [56, 80, 36] : q === "medium" ? [28, 40, 16] : [10, 14, 0];
373
+ uniforms.uNear.value.set(near[0], near[1], near[2]);
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);
377
+ const axes = cameraAxes(look);
378
+ const toEnu = (v) => [v[0], -v[2], v[1]];
379
+ const sunWorld = [sky.sun.enu[0], sky.sun.enu[2], -sky.sun.enu[1]];
380
+ const earthWorld = [sky.earth.enu[0], sky.earth.enu[2], -sky.earth.enu[1]];
381
+ for (const [d, dir] of [[sun, sunWorld], [earth, earthWorld]]) {
382
+ d.uniforms.uEyeWorld.value = world;
383
+ d.uniforms.uDirWorld.value.set(dir[0], dir[1], dir[2]);
384
+ d.uniforms.uRightWorld.value.set(axes.right[0], axes.right[1], axes.right[2]);
385
+ d.uniforms.uUpWorld.value.set(axes.up[0], axes.up[1], axes.up[2]);
386
+ const de = toEnu(dir);
387
+ d.uniforms.uDirEnu.value.set(de[0], de[1], de[2]);
388
+ const re = toEnu(axes.right);
389
+ d.uniforms.uRightEnu.value.set(re[0], re[1], re[2]);
390
+ const ue = toEnu(axes.up);
391
+ d.uniforms.uUpEnu.value.set(ue[0], ue[1], ue[2]);
392
+ d.uniforms.uSunEnu.value.set(sky.sun.enu[0], sky.sun.enu[1], sky.sun.enu[2]);
393
+ }
394
+ earth.uniforms.uHalf.value = Math.tan((sky.earthDiameter * Math.PI) / 360);
395
+ sun.uniforms.uHalf.value = Math.tan((sky.sunDiameter * Math.PI) / 360);
396
+ sun.mesh.visible = sky.sun.altitude > -1;
397
+ earth.mesh.visible = sky.earth.altitude > -1;
398
+ };
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);
414
+ return {
415
+ scene,
416
+ camera,
417
+ mesh: { rings: RINGS, rays: RAYS, triangles },
418
+ get rocks() {
419
+ return rockCount;
420
+ },
421
+ get drawMs() {
422
+ return drawMs;
423
+ },
424
+ render(gl) {
425
+ const t0 = performance.now();
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);
444
+ if (measuring) {
445
+ const canvas = gl.domElement;
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)
447
+ const g = (canvas.getContext("webgl2") ?? canvas.getContext("webgl"));
448
+ g?.readPixels(0, 0, 1, 1, g.RGBA, g.UNSIGNED_BYTE, new Uint8Array(4));
449
+ drawMs = performance.now() - t0;
450
+ }
451
+ },
452
+ apply: place,
453
+ refresh(ids) {
454
+ const wanted = new Set(ids);
455
+ options.levels.forEach((level, i) => wanted.has(level.id) && (textures[i].needsUpdate = true));
456
+ (options.pictures ?? []).forEach((level, i) => wanted.has(level.id) && (pictures[i].needsUpdate = true));
457
+ (options.horizons ?? []).forEach((h, i) => wanted.has(`horizon:${h.id}`) && (horizons[i].needsUpdate = true));
458
+ rockKey = "";
459
+ objectVersion += 1;
460
+ },
461
+ resize(width, height) {
462
+ aspect = width / Math.max(1, height);
463
+ pixelHeight = Math.max(1, height);
464
+ if (last !== undefined)
465
+ place(last);
466
+ },
467
+ dispose() {
468
+ geometry.dispose();
469
+ square.dispose();
470
+ groundMaterial.dispose();
471
+ rockGeometry?.dispose();
472
+ shadowGeometry?.dispose();
473
+ rockMaterial.dispose();
474
+ shadowMaterial.dispose();
475
+ sun.material.dispose();
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();
484
+ },
485
+ };
486
+ },
487
+ };
488
+ }
@@ -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[];