@johnmorrisdotca/wakusei-planets 0.24.0 → 0.26.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 +14 -0
- package/README.md +36 -8
- package/dist/blackbody.d.ts +2 -0
- package/dist/blackbody.js +10 -0
- package/dist/element.d.ts +8 -0
- package/dist/element.js +102 -16
- package/dist/embed.d.ts +3 -0
- package/dist/embed.js +30 -8
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/kids/data.d.ts +109 -0
- package/dist/kids/data.js +164 -0
- package/dist/kids/index.d.ts +4 -0
- package/dist/kids/index.js +5 -0
- package/dist/kids/land.d.ts +3 -0
- package/dist/kids/land.js +29 -0
- package/dist/kids/picture.d.ts +39 -0
- package/dist/kids/picture.js +255 -0
- package/dist/kids/sheet.d.ts +60 -0
- package/dist/kids/sheet.js +79 -0
- package/dist/kids/svg.d.ts +46 -0
- package/dist/kids/svg.js +193 -0
- package/dist/line.d.ts +9 -1
- package/dist/line.js +3 -4
- package/dist/scene/body.d.ts +8 -1
- package/dist/scene/body.js +33 -9
- package/dist/scene/glsl.d.ts +7 -2
- package/dist/scene/glsl.js +49 -3
- package/dist/scene/look.d.ts +43 -0
- package/dist/scene/look.js +39 -1
- package/dist/scene/painter.js +2 -0
- package/dist/scene/store.d.ts +3 -0
- package/dist/scene/store.js +3 -1
- package/dist/scene/textures.d.ts +2 -2
- package/dist/space/element.d.ts +4 -1
- package/dist/space/element.js +17 -14
- package/dist/space/frame.d.ts +3 -3
- package/dist/space/frame.js +3 -3
- package/dist/space/index.d.ts +1 -1
- package/dist/space/index.js +1 -1
- package/dist/space/scale.d.ts +60 -7
- package/dist/space/scale.js +105 -9
- package/dist/space/scene.d.ts +7 -1
- package/dist/space/scene.js +36 -27
- package/dist/star-line.d.ts +20 -0
- package/dist/star-line.js +59 -0
- package/dist/star.d.ts +193 -0
- package/dist/star.js +261 -0
- package/dist/stars.js +3 -5
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +8 -2
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type Layout, type PageName } from "@johnmorrisdotca/kazoku-ui/print";
|
|
2
|
+
import { type KidsSheet, type Lang } from "./data.ts";
|
|
3
|
+
/** Where the sheets are on the web: the demo's address, and the QR code on each sheet leads to its own. */
|
|
4
|
+
export declare const KIDS_ADDRESS = "https://johnmorrisdotca.github.io/wakusei-planets/kids.html";
|
|
5
|
+
/**
|
|
6
|
+
* What to build a sheet with: the paper, the language, the date and the address for the QR code.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { type KidsOptions } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
11
|
+
*
|
|
12
|
+
* const options: KidsOptions = { page: "letter", language: "ja" };
|
|
13
|
+
* console.log(options.page);
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export interface KidsOptions {
|
|
17
|
+
/** `a4`, `letter`, `a3` or `tabloid`. Default `a4`. */
|
|
18
|
+
page?: PageName;
|
|
19
|
+
language?: Lang;
|
|
20
|
+
/** Written in the footer; a `Date` as `YYYY-MM-DD`. Default: today. */
|
|
21
|
+
date?: Date | string;
|
|
22
|
+
/** The address printed in the footer as a QR code. Default: this sheet's place in the demo. */
|
|
23
|
+
address?: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The layout of a sheet, to give kazoku-ui's `printCss`: its paper, its turn (the lineup lies on its side), large type for children and the language.
|
|
27
|
+
* @param id - A body with a sheet, or `lineup`.
|
|
28
|
+
* @param options - The paper and the language.
|
|
29
|
+
* @returns The layout.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { kidsLayout } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
34
|
+
*
|
|
35
|
+
* console.log(kidsLayout("lineup", { page: "letter" }));
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare const kidsLayout: (id: KidsSheet, options?: KidsOptions) => Partial<Layout>;
|
|
39
|
+
/** A sheet's title: the body's name, or for the lineup its own. */
|
|
40
|
+
export declare const kidsTitle: (id: KidsSheet, lang: Lang) => string;
|
|
41
|
+
/**
|
|
42
|
+
* A printable colouring sheet: one `.kazoku-sheet` with one page holding the sheet's SVG, in kazoku-ui's print layout (add `printCss(kidsLayout(id, options))` to the page, and print with `printSheet`).
|
|
43
|
+
* @param id - A body with a sheet, or `lineup`.
|
|
44
|
+
* @param options - The paper, the language, the date and the address for the QR code.
|
|
45
|
+
* @returns The sheet's element.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts no-run
|
|
49
|
+
* import { printCss, printSheet } from "@johnmorrisdotca/kazoku-ui/print";
|
|
50
|
+
* import { kidsLayout, kidsSheet } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
51
|
+
*
|
|
52
|
+
* const style = document.createElement("style");
|
|
53
|
+
* style.textContent = printCss(kidsLayout("saturn", { page: "letter" }));
|
|
54
|
+
* document.head.append(style);
|
|
55
|
+
* const sheet = kidsSheet("saturn", { page: "letter" });
|
|
56
|
+
* document.body.append(sheet);
|
|
57
|
+
* printSheet(sheet);
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function kidsSheet(id: KidsSheet, options?: KidsOptions): HTMLElement;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The printable page of a colouring sheet: kazoku-ui's print sheet (header with the brand, the package, the title and a Name box; footer with credits, version and a QR code back to the demo)
|
|
2
|
+
// around the sheet's one SVG. Needs a DOM; the picture itself (`kidsSvg`) does not.
|
|
3
|
+
import { buildSheet, printCss } from "@johnmorrisdotca/kazoku-ui/print";
|
|
4
|
+
import { VERSION } from "../version.js";
|
|
5
|
+
import { SAY, kidsName } from "./data.js";
|
|
6
|
+
import { kidsOrientation, kidsRoom, kidsSvg } from "./svg.js";
|
|
7
|
+
/** Where the sheets are on the web: the demo's address, and the QR code on each sheet leads to its own. */
|
|
8
|
+
export const KIDS_ADDRESS = "https://johnmorrisdotca.github.io/wakusei-planets/kids.html";
|
|
9
|
+
/**
|
|
10
|
+
* The layout of a sheet, to give kazoku-ui's `printCss`: its paper, its turn (the lineup lies on its side), large type for children and the language.
|
|
11
|
+
* @param id - A body with a sheet, or `lineup`.
|
|
12
|
+
* @param options - The paper and the language.
|
|
13
|
+
* @returns The layout.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { kidsLayout } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
18
|
+
*
|
|
19
|
+
* console.log(kidsLayout("lineup", { page: "letter" }));
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export const kidsLayout = (id, options = {}) => ({ page: options.page ?? "a4", orientation: kidsOrientation(id), type: "kid", language: options.language ?? "en" });
|
|
23
|
+
/** A sheet's title: the body's name, or for the lineup its own. */
|
|
24
|
+
export const kidsTitle = (id, lang) => (id === "lineup" ? SAY[lang].lineup : kidsName(id, lang));
|
|
25
|
+
/**
|
|
26
|
+
* A printable colouring sheet: one `.kazoku-sheet` with one page holding the sheet's SVG, in kazoku-ui's print layout (add `printCss(kidsLayout(id, options))` to the page, and print with `printSheet`).
|
|
27
|
+
* @param id - A body with a sheet, or `lineup`.
|
|
28
|
+
* @param options - The paper, the language, the date and the address for the QR code.
|
|
29
|
+
* @returns The sheet's element.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts no-run
|
|
33
|
+
* import { printCss, printSheet } from "@johnmorrisdotca/kazoku-ui/print";
|
|
34
|
+
* import { kidsLayout, kidsSheet } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
35
|
+
*
|
|
36
|
+
* const style = document.createElement("style");
|
|
37
|
+
* style.textContent = printCss(kidsLayout("saturn", { page: "letter" }));
|
|
38
|
+
* document.head.append(style);
|
|
39
|
+
* const sheet = kidsSheet("saturn", { page: "letter" });
|
|
40
|
+
* document.body.append(sheet);
|
|
41
|
+
* printSheet(sheet);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function kidsSheet(id, options = {}) {
|
|
45
|
+
const layout = kidsLayout(id, options);
|
|
46
|
+
const lang = options.language ?? "en";
|
|
47
|
+
const holder = document.createElement("div");
|
|
48
|
+
holder.style.cssText = "position:absolute;inset:0";
|
|
49
|
+
const sheet = buildSheet({
|
|
50
|
+
...layout,
|
|
51
|
+
brand: SAY[lang].brand,
|
|
52
|
+
packageName: SAY[lang].pack,
|
|
53
|
+
title: kidsTitle(id, lang),
|
|
54
|
+
credits: SAY[lang].credits,
|
|
55
|
+
version: VERSION,
|
|
56
|
+
date: options.date ?? new Date(),
|
|
57
|
+
address: options.address ?? `${KIDS_ADDRESS}?sheet=${id}`,
|
|
58
|
+
pages: [holder],
|
|
59
|
+
});
|
|
60
|
+
holder.innerHTML = kidsSvg(id, { ...roomOf(sheet, layout, kidsRoom(layout.page, layout.orientation)), lang });
|
|
61
|
+
return sheet;
|
|
62
|
+
}
|
|
63
|
+
/** The room the page's body really has: the sheet is laid out out of sight for a moment, with the print rules, and measured, so the picture is drawn at the size it will print. */
|
|
64
|
+
function roomOf(sheet, layout, estimate) {
|
|
65
|
+
const style = document.createElement("style");
|
|
66
|
+
style.textContent = printCss(layout);
|
|
67
|
+
const host = document.createElement("div");
|
|
68
|
+
host.style.cssText = "position:fixed;left:-10000px;top:0;visibility:hidden";
|
|
69
|
+
host.append(sheet);
|
|
70
|
+
document.head.append(style);
|
|
71
|
+
document.body.append(host);
|
|
72
|
+
const body = sheet.querySelector(".kazoku-body")?.getBoundingClientRect();
|
|
73
|
+
host.remove();
|
|
74
|
+
style.remove();
|
|
75
|
+
if (body === undefined || body.height === 0)
|
|
76
|
+
return estimate;
|
|
77
|
+
const mm = 25.4 / 96;
|
|
78
|
+
return { widthMm: Math.floor(body.width * mm * 10) / 10, heightMm: Math.floor(body.height * mm * 10) / 10 };
|
|
79
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type Orientation, type PageName } from "@johnmorrisdotca/kazoku-ui/print";
|
|
2
|
+
import { KIDS_BODIES, type KidsSheet, type Lang } from "./data.ts";
|
|
3
|
+
/** The lineup is wide, so it is printed on its side. */
|
|
4
|
+
export declare const kidsOrientation: (id: KidsSheet) => Orientation;
|
|
5
|
+
/**
|
|
6
|
+
* The room a sheet's picture has on a page, in millimetres: the page inside its margins, less the header and the footer.
|
|
7
|
+
* @param page - `a4`, `letter`, `a3` or `tabloid`.
|
|
8
|
+
* @param orientation - `portrait` or `landscape`.
|
|
9
|
+
* @returns The width and height.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { kidsRoom } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
14
|
+
*
|
|
15
|
+
* console.log(kidsRoom("a4", "portrait"));
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare function kidsRoom(page: PageName, orientation?: Orientation): {
|
|
19
|
+
widthMm: number;
|
|
20
|
+
heightMm: number;
|
|
21
|
+
};
|
|
22
|
+
/** The width of a text in millimetres, estimated on the wide side so a font that is wider than the rounded ones (a fallback on a server) still fits: a full-width character is as wide as the type is tall, a Latin capital or a digit 0.82 of it, a small letter 0.7 (DejaVu Sans Bold, the usual fallback on a server, is that wide), a space or a stop 0.38. */
|
|
23
|
+
export declare function textWidth(text: string, sizeMm: number): number;
|
|
24
|
+
/** Break a sentence into lines no wider than `maxMm`: at spaces for English, after a full stop (or anywhere) for Japanese. */
|
|
25
|
+
export declare function wrap(text: string, maxMm: number, sizeMm: number): string[];
|
|
26
|
+
/**
|
|
27
|
+
* One sheet as an SVG string measured in millimetres: the picture of the body, the colours to colour it with and three facts, in English or Japanese; or the lineup of all the planets at one scale. `widthMm` and
|
|
28
|
+
* `heightMm` are the room it is drawn for (`kidsRoom`), and the viewBox is that room, so a millimetre is a millimetre on paper.
|
|
29
|
+
* @param id - A body with a sheet, or `lineup`.
|
|
30
|
+
* @param options - The room, and the language.
|
|
31
|
+
* @returns The `<svg>` markup.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { kidsRoom, kidsSvg } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
36
|
+
*
|
|
37
|
+
* const room = kidsRoom("a4");
|
|
38
|
+
* console.log(kidsSvg("saturn", { ...room, lang: "en" }).includes("Colour me!"));
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function kidsSvg(id: KidsSheet, options: {
|
|
42
|
+
widthMm: number;
|
|
43
|
+
heightMm: number;
|
|
44
|
+
lang?: Lang;
|
|
45
|
+
}): string;
|
|
46
|
+
export { KIDS_BODIES };
|
package/dist/kids/svg.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// One colouring sheet as one SVG measured in millimetres: the picture, the colours to colour with and the three facts (or, for the lineup, all the planets in a row at one scale). Pure: a string.
|
|
2
|
+
import { TYPE_PRESETS, DEFAULT_MARGIN_MM, printableSize } from "@johnmorrisdotca/kazoku-ui/print";
|
|
3
|
+
import { BODIES } from "../facts.js";
|
|
4
|
+
import { KIDS_BODIES, LINEUP_IDS, SAY, SWATCHES, kidsFacts, kidsName, kidsSay, lineupFacts } from "./data.js";
|
|
5
|
+
import { colouringArt, colouringPicture, LINE_MM, OUTLINE_MM, reach, sheetPose } from "./picture.js";
|
|
6
|
+
const PT = 25.4 / 72;
|
|
7
|
+
const KID = TYPE_PRESETS.kid;
|
|
8
|
+
const BODY_MM = KID.bodyPt * PT;
|
|
9
|
+
const NAME_MM = KID.namePt * PT;
|
|
10
|
+
/** The planets' names on the lineup: the least size the kid type allows for a name, 24 pt. */
|
|
11
|
+
const LABEL_MM = 24 * PT;
|
|
12
|
+
/** What the sheet's header, footer and gaps take from the page's height, in millimetres, as measured (a narrow page's footer is taller, for its address wraps); a browser measures the real room (`kidsSheet`). */
|
|
13
|
+
const FRAME_MM = 89;
|
|
14
|
+
const WIDE_FRAME_MM = 77;
|
|
15
|
+
/** The lineup is wide, so it is printed on its side. */
|
|
16
|
+
export const kidsOrientation = (id) => (id === "lineup" ? "landscape" : "portrait");
|
|
17
|
+
/**
|
|
18
|
+
* The room a sheet's picture has on a page, in millimetres: the page inside its margins, less the header and the footer.
|
|
19
|
+
* @param page - `a4`, `letter`, `a3` or `tabloid`.
|
|
20
|
+
* @param orientation - `portrait` or `landscape`.
|
|
21
|
+
* @returns The width and height.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { kidsRoom } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
26
|
+
*
|
|
27
|
+
* console.log(kidsRoom("a4", "portrait"));
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function kidsRoom(page, orientation = "portrait") {
|
|
31
|
+
const size = printableSize(page, orientation, DEFAULT_MARGIN_MM);
|
|
32
|
+
return { widthMm: size.widthMm, heightMm: size.heightMm - (size.widthMm >= 240 ? WIDE_FRAME_MM : FRAME_MM) };
|
|
33
|
+
}
|
|
34
|
+
const esc = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
35
|
+
const n = (v) => (Math.round(v * 100) / 100).toString();
|
|
36
|
+
/** The width of a text in millimetres, estimated on the wide side so a font that is wider than the rounded ones (a fallback on a server) still fits: a full-width character is as wide as the type is tall, a Latin capital or a digit 0.82 of it, a small letter 0.7 (DejaVu Sans Bold, the usual fallback on a server, is that wide), a space or a stop 0.38. */
|
|
37
|
+
export function textWidth(text, sizeMm) {
|
|
38
|
+
let total = 0;
|
|
39
|
+
for (const ch of text)
|
|
40
|
+
total += /[\u3000-\u30ff\u4e00-\u9fff\uff00-\uffef]/.test(ch) ? sizeMm : /[ .,:;!'|]/.test(ch) ? 0.38 * sizeMm : /[A-Z0-9]/.test(ch) ? 0.82 * sizeMm : 0.7 * sizeMm;
|
|
41
|
+
return total;
|
|
42
|
+
}
|
|
43
|
+
/** Break a sentence into lines no wider than `maxMm`: at spaces for English, after a full stop (or anywhere) for Japanese. */
|
|
44
|
+
export function wrap(text, maxMm, sizeMm) {
|
|
45
|
+
const japanese = /[-ヿ一-鿿]/.test(text);
|
|
46
|
+
const pieces = japanese ? (text.match(/[^。]+。?/g) ?? [text]) : text.split(" ");
|
|
47
|
+
const joiner = japanese ? "" : " ";
|
|
48
|
+
const lines = [];
|
|
49
|
+
let line = "";
|
|
50
|
+
for (const piece of pieces) {
|
|
51
|
+
const next = line === "" ? piece : line + joiner + piece;
|
|
52
|
+
if (line !== "" && textWidth(next, sizeMm) > maxMm) {
|
|
53
|
+
lines.push(line);
|
|
54
|
+
line = piece;
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
line = next;
|
|
58
|
+
}
|
|
59
|
+
if (line !== "")
|
|
60
|
+
lines.push(line);
|
|
61
|
+
return japanese ? lines.flatMap((one) => cut(one, maxMm, sizeMm)) : lines;
|
|
62
|
+
}
|
|
63
|
+
;
|
|
64
|
+
function cut(line, maxMm, sizeMm) {
|
|
65
|
+
const out = [];
|
|
66
|
+
let current = "";
|
|
67
|
+
for (const ch of line) {
|
|
68
|
+
if (current !== "" && textWidth(current + ch, sizeMm) > maxMm) {
|
|
69
|
+
out.push(current);
|
|
70
|
+
current = ch;
|
|
71
|
+
}
|
|
72
|
+
else
|
|
73
|
+
current += ch;
|
|
74
|
+
}
|
|
75
|
+
return current === "" ? out : [...out, current];
|
|
76
|
+
}
|
|
77
|
+
const text = (x, y, size, body, extra = "") => `<text x="${n(x)}" y="${n(y)}" font-size="${n(size)}" font-weight="700" fill="#000"${extra}>${esc(body)}</text>`;
|
|
78
|
+
/** The facts as numbered dots and lines of words. */
|
|
79
|
+
function facts(list, width, y, numbered = true) {
|
|
80
|
+
const dot = 4.6;
|
|
81
|
+
const left = 2 * dot + 4;
|
|
82
|
+
const lineHeight = BODY_MM * 1.3;
|
|
83
|
+
const out = [];
|
|
84
|
+
let at = y;
|
|
85
|
+
list.forEach((fact, i) => {
|
|
86
|
+
const lines = wrap(fact, width - left, BODY_MM);
|
|
87
|
+
const rowHeight = Math.max(2 * dot, lines.length * lineHeight);
|
|
88
|
+
const mid = at + rowHeight / 2;
|
|
89
|
+
out.push(`<circle cx="${n(dot)}" cy="${n(mid)}" r="${dot}" fill="#000"/>`, numbered ? `<text x="${n(dot)}" y="${n(mid + BODY_MM * 0.36)}" font-size="${n(BODY_MM)}" font-weight="700" fill="#fff" text-anchor="middle">${i + 1}</text>` : "");
|
|
90
|
+
lines.forEach((line, k) => out.push(text(left, mid - (lines.length * lineHeight) / 2 + lineHeight * (k + 0.72), BODY_MM, line, ' data-fact=""')));
|
|
91
|
+
at += rowHeight + 3.5;
|
|
92
|
+
});
|
|
93
|
+
return { markup: `<g data-facts="">${out.join("")}</g>`, height: at - y - 3.5 };
|
|
94
|
+
}
|
|
95
|
+
/** The colours to colour with: the heading at the left, then a square of each real colour with its name under it. */
|
|
96
|
+
function swatches(id, lang, width, y) {
|
|
97
|
+
const list = SWATCHES[id];
|
|
98
|
+
const head = NAME_MM * 0.9;
|
|
99
|
+
const headWidth = 52;
|
|
100
|
+
const cell = (width - headWidth) / list.length;
|
|
101
|
+
const square = 14;
|
|
102
|
+
const out = [text(1.2, y + square / 2 + head * 0.35, head, SAY[lang].colour, ' data-colour-head=""')];
|
|
103
|
+
list.forEach((one, i) => {
|
|
104
|
+
const cx = headWidth + cell * i + cell / 2;
|
|
105
|
+
out.push(`<rect x="${n(cx - square / 2 - 1)}" y="${n(y)}" width="${n(square + 2)}" height="${n(square)}" rx="3" fill="${one.hex}" stroke="#000" stroke-width="${LINE_MM}" data-swatch="${one.hex}"/>`);
|
|
106
|
+
out.push(text(cx, y + square + 2 + BODY_MM * 0.85, BODY_MM, one[lang], ' text-anchor="middle" data-swatch-name=""'));
|
|
107
|
+
});
|
|
108
|
+
return { markup: `<g data-swatches="">${out.join("")}</g>`, height: square + 2 + BODY_MM * 1.2 };
|
|
109
|
+
}
|
|
110
|
+
/** The sheet of one body: the picture on top, then the colours, then the facts. */
|
|
111
|
+
function bodySheet(id, lang, width, height) {
|
|
112
|
+
const list = kidsFacts(id, lang);
|
|
113
|
+
const probe = facts(list, width, 0);
|
|
114
|
+
const colours = swatches(id, lang, width, 0);
|
|
115
|
+
const gap = 7;
|
|
116
|
+
const art = colouringPicture(id, width, height - probe.height - colours.height - 2 * gap);
|
|
117
|
+
const top = height - probe.height - colours.height - 2 * gap;
|
|
118
|
+
const parts = [art.markup, `<g transform="translate(0 ${n(top + gap)})">${colours.markup}</g>`, `<g transform="translate(0 ${n(top + colours.height + 2 * gap)})">${facts(list, width, 0).markup}</g>`];
|
|
119
|
+
return parts.join("");
|
|
120
|
+
}
|
|
121
|
+
/** What a planet takes of the row: its width, or its rings'. */
|
|
122
|
+
const extentKm = (id) => 2 * reach(id, sheetPose(id)).x * BODIES[id].radiusKm;
|
|
123
|
+
/** The lineup: every planet on one line at one scale, each with a number under it and a line to the number; below, the numbers with the names in the order from the Sun; and three facts. */
|
|
124
|
+
function lineupSheet(lang, width, height) {
|
|
125
|
+
const ext = LINEUP_IDS.map((id) => extentKm(id));
|
|
126
|
+
const badge = 4.4;
|
|
127
|
+
const reachKm = ext.reduce((total, e) => total + e, 0);
|
|
128
|
+
const rows = 2;
|
|
129
|
+
const pitch = LABEL_MM * 1.5;
|
|
130
|
+
const factsFor = (k) => facts(lineupFacts(lang, k), width, 0, false);
|
|
131
|
+
const tall = (k) => 2 * BODIES.jupiter.radiusKm * k;
|
|
132
|
+
// the gaps keep the numbers apart, then the rest of the width is shared
|
|
133
|
+
const gapsAt = (k) => ext.slice(1).map((e, i) => Math.max(3, 2 * badge + 2 - ((ext[i] + e) * k) / 2));
|
|
134
|
+
let k = (width - 2 * badge - 3 * (ext.length - 1)) / reachKm;
|
|
135
|
+
for (let pass = 0; pass < 6; pass += 1) {
|
|
136
|
+
const spare = width - 2 * badge - reachKm * k - gapsAt(k).reduce((t, g) => t + g, 0);
|
|
137
|
+
if (spare < 0)
|
|
138
|
+
k += spare / reachKm;
|
|
139
|
+
}
|
|
140
|
+
k = Math.min(k, (height - factsFor(k).height - rows * pitch - 2 * badge - 12) / tall(1));
|
|
141
|
+
const gaps = gapsAt(k);
|
|
142
|
+
const extra = (width - 2 * badge - reachKm * k - gaps.reduce((t, g) => t + g, 0)) / gaps.length;
|
|
143
|
+
const axis = tall(k) / 2 + OUTLINE_MM / 2;
|
|
144
|
+
const badgeY = axis + tall(k) / 2 + 3 + badge;
|
|
145
|
+
const out = [];
|
|
146
|
+
let x = badge;
|
|
147
|
+
LINEUP_IDS.forEach((id, i) => {
|
|
148
|
+
const extent = ext[i] * k;
|
|
149
|
+
const cx = x + extent / 2;
|
|
150
|
+
const r = BODIES[id].radiusKm * k;
|
|
151
|
+
const thick = r * 2 >= 24 ? OUTLINE_MM : Math.max(1, Math.min(OUTLINE_MM, r / 4));
|
|
152
|
+
out.push(colouringArt(id, cx, axis, r, thick));
|
|
153
|
+
out.push(`<path d="M${n(cx)} ${n(axis + (reach(id, sheetPose(id)).y * BODIES[id].radiusKm * k) + 1.5)}L${n(cx)} ${n(badgeY - badge)}" fill="none" stroke="#000" stroke-width="0.8" stroke-linecap="round" data-leader=""/>`);
|
|
154
|
+
out.push(`<circle cx="${n(cx)}" cy="${n(badgeY)}" r="${badge}" fill="#000" data-badge="${i + 1}"/>`, `<text x="${n(cx)}" y="${n(badgeY + BODY_MM * 0.36)}" font-size="${n(BODY_MM)}" font-weight="700" fill="#fff" text-anchor="middle">${i + 1}</text>`);
|
|
155
|
+
x += extent + (gaps[i] ?? 0) + extra;
|
|
156
|
+
});
|
|
157
|
+
const legendTop = badgeY + badge + 8;
|
|
158
|
+
const columns = LINEUP_IDS.length / rows;
|
|
159
|
+
const cell = width / columns;
|
|
160
|
+
LINEUP_IDS.forEach((id, i) => {
|
|
161
|
+
const column = i % columns;
|
|
162
|
+
const row = Math.floor(i / columns);
|
|
163
|
+
const y = legendTop + row * pitch + LABEL_MM;
|
|
164
|
+
out.push(`<circle cx="${n(column * cell + badge)}" cy="${n(y - LABEL_MM * 0.33)}" r="${badge}" fill="#000"/>`, `<text x="${n(column * cell + badge)}" y="${n(y - LABEL_MM * 0.33 + BODY_MM * 0.36)}" font-size="${n(BODY_MM)}" font-weight="700" fill="#fff" text-anchor="middle">${i + 1}</text>`);
|
|
165
|
+
out.push(text(column * cell + 2 * badge + 2, y, LABEL_MM, kidsSay(id, lang), ' data-planet-name=""'));
|
|
166
|
+
});
|
|
167
|
+
const block = factsFor(k);
|
|
168
|
+
out.push(`<g transform="translate(0 ${n(height - block.height)})">${block.markup}</g>`);
|
|
169
|
+
return out.join("");
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* One sheet as an SVG string measured in millimetres: the picture of the body, the colours to colour it with and three facts, in English or Japanese; or the lineup of all the planets at one scale. `widthMm` and
|
|
173
|
+
* `heightMm` are the room it is drawn for (`kidsRoom`), and the viewBox is that room, so a millimetre is a millimetre on paper.
|
|
174
|
+
* @param id - A body with a sheet, or `lineup`.
|
|
175
|
+
* @param options - The room, and the language.
|
|
176
|
+
* @returns The `<svg>` markup.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* import { kidsRoom, kidsSvg } from "@johnmorrisdotca/wakusei-planets/sheets";
|
|
181
|
+
*
|
|
182
|
+
* const room = kidsRoom("a4");
|
|
183
|
+
* console.log(kidsSvg("saturn", { ...room, lang: "en" }).includes("Colour me!"));
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export function kidsSvg(id, options) {
|
|
187
|
+
const lang = options.lang ?? "en";
|
|
188
|
+
const { widthMm: w, heightMm: h } = options;
|
|
189
|
+
const label = id === "lineup" ? SAY[lang].lineup : kidsName(id, lang);
|
|
190
|
+
const inner = id === "lineup" ? lineupSheet(lang, w, h) : bodySheet(id, lang, w, h);
|
|
191
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${n(w)} ${n(h)}" width="100%" height="100%" preserveAspectRatio="xMidYMid meet" role="img" aria-label="${esc(label)}" data-kids-sheet="${id}" font-family="inherit"><title>${esc(label)}</title>${inner}</svg>`;
|
|
192
|
+
}
|
|
193
|
+
export { KIDS_BODIES };
|
package/dist/line.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Pose } from "./ephemeris.ts";
|
|
2
|
-
import { type BodyId, type MoonId } from "./facts.ts";
|
|
2
|
+
import { type BodyFacts, type BodyId, type MoonId } from "./facts.ts";
|
|
3
3
|
import { type PortraitOptions } from "./pose.ts";
|
|
4
4
|
/**
|
|
5
5
|
* Options for `lineSvg`: the size, a pose (`tilt`, `pitch`, `phase`, `spin`, a `time` or a `pose`), `moons`, `range`, `fill`, the language and the colours.
|
|
@@ -48,6 +48,7 @@ export interface LineOptions extends PortraitOptions {
|
|
|
48
48
|
/** The colour of the disc. Default: a dark tint of the body's colour. */
|
|
49
49
|
paper?: string;
|
|
50
50
|
}
|
|
51
|
+
export declare const NAVY = "#0b1424";
|
|
51
52
|
/**
|
|
52
53
|
* Mix two `#rrggbb` colours: `t` is how much of `b` (0 to 1).
|
|
53
54
|
* @param a - The first colour.
|
|
@@ -63,6 +64,13 @@ export interface LineOptions extends PortraitOptions {
|
|
|
63
64
|
* ```
|
|
64
65
|
*/
|
|
65
66
|
export declare function mix(a: string, b: string, t: number): string;
|
|
67
|
+
export interface Frame {
|
|
68
|
+
cx: number;
|
|
69
|
+
cy: number;
|
|
70
|
+
r: number;
|
|
71
|
+
}
|
|
72
|
+
/** The body's outline, features, rings and dark side as SVG, centred in `frame`. `facts` and `clip` stand in for the table's entry and the clip path's name when a star that is not in the table is drawn. */
|
|
73
|
+
export declare function bodyGroup(id: BodyId, pose: Pose, frame: Frame, o: Required<Pick<LineOptions, "stroke" | "paper">>, hair: number, ringScale: number, facts?: BodyFacts, clipId?: string): string;
|
|
66
74
|
/**
|
|
67
75
|
* A body as a line drawing, as an SVG string: the outline (squeezed by its flattening and leaning at its axial tilt), bands and features, rings with their gaps, and the dark side
|
|
68
76
|
* dashed off. Give it a `time` to draw the body as the Earth sees it then; leave it out for a portrait. `moons: true` draws a planet with its moons on their orbits.
|
package/dist/line.js
CHANGED
|
@@ -3,7 +3,7 @@ import { apparent, nearMoons, orbitPath, system } from "./ephemeris.js";
|
|
|
3
3
|
import { BODIES, ringReach } from "./facts.js";
|
|
4
4
|
import { facing, portrait, silhouette, surfacePoint } from "./pose.js";
|
|
5
5
|
import { cross, RAD, scale, unit } from "./vec.js";
|
|
6
|
-
const NAVY = "#0b1424";
|
|
6
|
+
export const NAVY = "#0b1424";
|
|
7
7
|
const hex = (c) => [parseInt(c.slice(1, 3), 16), parseInt(c.slice(3, 5), 16), parseInt(c.slice(5, 7), 16)];
|
|
8
8
|
/**
|
|
9
9
|
* Mix two `#rrggbb` colours: `t` is how much of `b` (0 to 1).
|
|
@@ -177,13 +177,12 @@ function cachedOrbit(id, time) {
|
|
|
177
177
|
}
|
|
178
178
|
return path;
|
|
179
179
|
}
|
|
180
|
-
|
|
181
|
-
|
|
180
|
+
/** The body's outline, features, rings and dark side as SVG, centred in `frame`. `facts` and `clip` stand in for the table's entry and the clip path's name when a star that is not in the table is drawn. */
|
|
181
|
+
export function bodyGroup(id, pose, frame, o, hair, ringScale, facts = BODIES[id], clipId = `wk-${id}-clip`) {
|
|
182
182
|
const sil = silhouette(pose, facts.flattening, axesOf(facts));
|
|
183
183
|
const disc = `<ellipse cx="${n(frame.cx)}" cy="${n(frame.cy)}" rx="${n(frame.r * sil.a)}" ry="${n(frame.r * sil.b)}" transform="rotate(${n(sil.angle)} ${n(frame.cx)} ${n(frame.cy)})"`;
|
|
184
184
|
const rings = ringPaths(facts, pose, frame, o.stroke, hair, ringScale);
|
|
185
185
|
const dark = id === "sun" || facts.kind === "star" ? "" : nightPath(pose, frame);
|
|
186
|
-
const clipId = `wk-${id}-clip`;
|
|
187
186
|
return [
|
|
188
187
|
rings.back,
|
|
189
188
|
`${disc} fill="${o.paper}" stroke="${o.stroke}" stroke-width="${n(hair * 1.6)}"/>`,
|
package/dist/scene/body.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Item3D, Scene3D } from "@johnmorrisdotca/kazoku-ui/renderer";
|
|
2
2
|
import type * as THREE from "three";
|
|
3
3
|
import type { Pose } from "../ephemeris.ts";
|
|
4
|
-
import { type BodyId } from "../facts.ts";
|
|
4
|
+
import { type BodyFacts, type BodyId } from "../facts.ts";
|
|
5
|
+
import { type BodyLook } from "./look.ts";
|
|
5
6
|
export { fallbackLook } from "./fallback.ts";
|
|
6
7
|
/** The parts of three.js the bodies use: a page may hand over a module with only these (see `setThreeLoader`). */
|
|
7
8
|
export type ThreeParts = Pick<typeof THREE, "AdditiveBlending" | "BufferAttribute" | "BufferGeometry" | "ClampToEdgeWrapping" | "DataTexture" | "DoubleSide" | "Group" | "Line" | "LineBasicMaterial" | "LinearFilter" | "Mesh" | "NoColorSpace" | "OrthographicCamera" | "PlaneGeometry" | "RepeatWrapping" | "RGBAFormat" | "RingGeometry" | "Scene" | "ShaderMaterial" | "SphereGeometry" | "TextureLoader" | "Vector2" | "Vector3" | "WebGLRenderer">;
|
|
@@ -39,6 +40,12 @@ export interface BodyScene extends Scene3D {
|
|
|
39
40
|
}
|
|
40
41
|
export interface BodyItemOptions {
|
|
41
42
|
id: BodyId;
|
|
43
|
+
/** The look to draw instead of the one registered for `id`: a star made from numbers (`starLook`). */
|
|
44
|
+
shaded?: BodyLook;
|
|
45
|
+
/** The facts to draw instead of the table's for `id` (a star's size and flattening). */
|
|
46
|
+
facts?: BodyFacts;
|
|
47
|
+
/** What the renderer keeps this body's materials under, instead of `id`: a star's key, so two stars do not share a program. */
|
|
48
|
+
assetKey?: string;
|
|
42
49
|
look: "realistic" | "painted";
|
|
43
50
|
/** A key that changes whenever the picture would: the renderer keeps one still for each key. */
|
|
44
51
|
key: string;
|
package/dist/scene/body.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BODIES } from "../facts.js";
|
|
2
|
+
import { silhouette } from "../pose.js";
|
|
2
3
|
import { cross } from "../vec.js";
|
|
3
|
-
import { BALL_FRAGMENT, bodyFragment, GLOW_FRAGMENT, GLOW_VERTEX, PAINTED_SURFACE, RING_FRAGMENT, RING_VERTEX, VERTEX } from "./glsl.js";
|
|
4
|
+
import { BALL_FRAGMENT, bodyFragment, GLOW_FRAGMENT, GLOW_VERTEX, PAINTED_SURFACE, RING_FRAGMENT, RING_VERTEX, STAR_GLOW_FRAGMENT, VERTEX } from "./glsl.js";
|
|
4
5
|
import { lookOf } from "./look.js";
|
|
5
6
|
import { fallbackLook } from "./fallback.js";
|
|
6
7
|
import { writeBasis } from "./basis.js";
|
|
@@ -11,7 +12,7 @@ function makeBodyMaterial(T, look, painted) {
|
|
|
11
12
|
const a = look.atmosphere;
|
|
12
13
|
return new T.ShaderMaterial({
|
|
13
14
|
vertexShader: VERTEX,
|
|
14
|
-
fragmentShader: bodyFragment(painted ? PAINTED_SURFACE : look.surface, painted),
|
|
15
|
+
fragmentShader: bodyFragment(painted ? PAINTED_SURFACE : look.surface, painted, false, look.limbTint),
|
|
15
16
|
uniforms: {
|
|
16
17
|
uLight: { value: new T.Vector3(-0.6, 0.2, 0.77) },
|
|
17
18
|
uRelief: { value: (painted ? (look.painted?.relief ?? 0) : (look.relief ?? 0.6)) },
|
|
@@ -22,6 +23,7 @@ function makeBodyMaterial(T, look, painted) {
|
|
|
22
23
|
uAtmosPow: { value: a?.power ?? 3 },
|
|
23
24
|
uRimBase: { value: 0.12 },
|
|
24
25
|
uEmissive: { value: look.emissive === true ? 1 : 0 },
|
|
26
|
+
uTime: { value: 0 },
|
|
25
27
|
...(painted ? { uMark: { value: look.painted?.turn === "hexagon" ? 2 : look.painted?.turn === "bands" ? 1 : 0 } } : {}),
|
|
26
28
|
uRingN: { value: new T.Vector3(0, 1, 0) },
|
|
27
29
|
uRingR: { value: new T.Vector2(1, 2) },
|
|
@@ -45,15 +47,16 @@ function setBasis(object, x, y, z, sx = 1, sy = 1, sz = 1) {
|
|
|
45
47
|
* @returns The item for kazoku-ui's shared renderer.
|
|
46
48
|
*/
|
|
47
49
|
export function createBodyItem(options) {
|
|
48
|
-
const facts = BODIES[options.id];
|
|
50
|
+
const facts = options.facts ?? BODIES[options.id];
|
|
51
|
+
const held = options.assetKey ?? options.id;
|
|
49
52
|
return {
|
|
50
53
|
key: options.key,
|
|
51
54
|
async build({ three: T, assets, width, height }) {
|
|
52
|
-
const look = lookOf(options.id) ?? fallbackLook(facts);
|
|
55
|
+
const look = options.shaded ?? lookOf(options.id) ?? fallbackLook(facts);
|
|
53
56
|
const wantPainted = (options.look === "painted" || look.mapped === true) && look.painted !== undefined;
|
|
54
57
|
const geometry = assets.acquire("wk:sphere", () => new T.SphereGeometry(1, 128, 64), (g) => g.dispose());
|
|
55
|
-
const shader = assets.acquire(`wk:body:${
|
|
56
|
-
const paintedMaterial = wantPainted ? assets.acquire(`wk:body:${
|
|
58
|
+
const shader = assets.acquire(`wk:body:${held}:r`, () => makeBodyMaterial(T, look, false), (m) => m.dispose());
|
|
59
|
+
const paintedMaterial = wantPainted ? assets.acquire(`wk:body:${held}:p`, () => makeBodyMaterial(T, look, true), (m) => m.dispose()) : undefined;
|
|
57
60
|
const materials = paintedMaterial === undefined ? [shader] : [shader, paintedMaterial];
|
|
58
61
|
const baseWrap = look.wrap ?? (look.atmosphere === undefined ? 0.04 : 0.3);
|
|
59
62
|
const baseAmbient = look.ambient ?? 0.04;
|
|
@@ -124,13 +127,18 @@ export function createBodyItem(options) {
|
|
|
124
127
|
}
|
|
125
128
|
// the glow of a star
|
|
126
129
|
let glow;
|
|
130
|
+
let glowMaterialOf;
|
|
127
131
|
if (look.emissive === true) {
|
|
128
132
|
const glowGeometry = assets.acquire("wk:glow", () => new T.PlaneGeometry(6, 6), (g) => g.dispose());
|
|
129
|
-
const
|
|
133
|
+
const own = look.glow;
|
|
134
|
+
const glowMaterial = own === undefined
|
|
135
|
+
? assets.acquire("wk:glowmat", () => new T.ShaderMaterial({ vertexShader: GLOW_VERTEX, fragmentShader: GLOW_FRAGMENT, transparent: true, depthWrite: false, blending: T.AdditiveBlending, uniforms: { uColour: { value: new T.Vector3(1, 0.7, 0.3) } } }), (m) => m.dispose())
|
|
136
|
+
: assets.acquire(`wk:glowmat:${held}`, () => new T.ShaderMaterial({ vertexShader: GLOW_VERTEX, fragmentShader: STAR_GLOW_FRAGMENT, transparent: true, depthWrite: false, blending: T.AdditiveBlending, uniforms: { uColour: { value: new T.Vector3(...own.colour) }, uStrength: { value: own.strength }, uSpread: { value: own.spread }, uRim: { value: own.rim ?? 0 }, uProm: { value: own.prominence ?? 0 }, uSeed: { value: own.seed ?? 0 }, uTime: { value: 0 } } }), (m) => m.dispose());
|
|
130
137
|
glow = new T.Mesh(glowGeometry, glowMaterial);
|
|
131
138
|
glow.position.z = -5;
|
|
132
139
|
glow.scale.setScalar(1);
|
|
133
140
|
glow.frustumCulled = false;
|
|
141
|
+
glowMaterialOf = glowMaterial;
|
|
134
142
|
scene.add(glow);
|
|
135
143
|
}
|
|
136
144
|
// moons, made on first use
|
|
@@ -332,8 +340,24 @@ export function createBodyItem(options) {
|
|
|
332
340
|
mat.uniforms.uAmbient.value = sky ? 0.012 : baseAmbient;
|
|
333
341
|
mat.uniforms.uRimBase.value = sky ? 0.02 : 0.12;
|
|
334
342
|
}
|
|
335
|
-
if (glow !== undefined)
|
|
336
|
-
|
|
343
|
+
if (look.glow !== undefined) {
|
|
344
|
+
// the star's surface boils and its prominences move with the clock, but only when a frame is asked for: a star at rest draws nothing
|
|
345
|
+
const seconds = (performance.now() / 1000) % 100_000;
|
|
346
|
+
for (const mat of materials)
|
|
347
|
+
mat.uniforms.uTime.value = seconds;
|
|
348
|
+
if (glowMaterialOf !== undefined)
|
|
349
|
+
glowMaterialOf.uniforms.uTime.value = seconds;
|
|
350
|
+
}
|
|
351
|
+
if (glow !== undefined) {
|
|
352
|
+
// a flattened star is squeezed along its pole: the glow starts at its edge, not at a round one
|
|
353
|
+
if (look.glow !== undefined && facts.flattening > 0) {
|
|
354
|
+
const { b, angle } = silhouette(state.pose, facts.flattening);
|
|
355
|
+
glow.rotation.z = (-angle * Math.PI) / 180;
|
|
356
|
+
glow.scale.set(1, b, 1);
|
|
357
|
+
}
|
|
358
|
+
else
|
|
359
|
+
glow.scale.setScalar(1);
|
|
360
|
+
}
|
|
337
361
|
requestSharperTexture(state);
|
|
338
362
|
};
|
|
339
363
|
apply(latest);
|
package/dist/scene/glsl.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export declare const VERTEX = "\nvarying vec3 vObj;\nvarying vec3 vN;\nvarying vec3 vW;\nvoid main() {\n vObj = vec3(position.z, position.x, position.y);\n vN = normalize(normalMatrix * normal);\n vec4 w = modelMatrix * vec4(position, 1.0);\n vW = w.xyz;\n gl_Position = projectionMatrix * viewMatrix * w;\n}";
|
|
2
2
|
/** Noise, craters and the band palette, for the surface code of a body to call. */
|
|
3
3
|
export declare const PRELUDE = "\nfloat hash13(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }\nvec3 hash33(vec3 p) { p = fract(p * vec3(0.1031, 0.1030, 0.0973)); p += dot(p, p.yxz + 33.33); return fract((p.xxy + p.yxx) * p.zyx); }\nfloat vnoise(vec3 p) {\n vec3 i = floor(p); vec3 f = fract(p); f = f * f * (3.0 - 2.0 * f);\n return mix(mix(mix(hash13(i), hash13(i + vec3(1,0,0)), f.x), mix(hash13(i + vec3(0,1,0)), hash13(i + vec3(1,1,0)), f.x), f.y),\n mix(mix(hash13(i + vec3(0,0,1)), hash13(i + vec3(1,0,1)), f.x), mix(hash13(i + vec3(0,1,1)), hash13(i + vec3(1,1,1)), f.x), f.y), f.z);\n}\n// Noise that knows how big a pixel is: an octave finer than the pixel can show is replaced by its average (0.5) instead of being sampled, so a small body is smooth and not a spray of pixel-sized specks.\nfloat fbm(vec3 p) { float fw = length(fwidth(p)); float a = 0.5; float s = 0.0; for (int i = 0; i < 5; i++) { s += a * mix(0.5, vnoise(p), 1.0 - smoothstep(0.3, 0.7, fw)); p = p * 2.03 + vec3(1.7, 9.2, 3.1); fw *= 2.03; a *= 0.5; } return s; }\n// Craters: a bowl and a raised rim in each cell that has one. Returns a height, negative in a bowl. A crater smaller than about two pixels fades out (it would only be noise).\nfloat craters(vec3 p, float freq, float amp, float seed) {\n vec3 q = p * freq + seed; vec3 ip = floor(q); float h = 0.0;\n float fw = length(fwidth(q));\n for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) for (int z = -1; z <= 1; z++) {\n vec3 c = ip + vec3(float(x), float(y), float(z));\n float on = step(0.52, hash13(c + 3.7));\n vec3 ctr = c + hash33(c);\n float rad = mix(0.12, 0.36, hash13(c + 17.3));\n float d = length(q - ctr) / rad;\n float bowl = d < 1.0 ? (d * d - 1.0) : 0.0;\n float rim = d >= 1.0 ? exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d) : 0.0;\n h += on * amp * rad * (bowl + rim) * (1.0 - smoothstep(0.35, 0.9, fw / rad));\n }\n return h;\n}\nfloat lon(vec3 p) { return atan(p.y, p.x); }\nfloat lat(vec3 p) { return asin(clamp(p.z, -1.0, 1.0)); }\nfloat wrapPi(float a) { return a - 6.2831853 * floor((a + 3.1415927) / 6.2831853); }\nvec3 dirOf(float lonDeg, float latDeg) { float lo = radians(lonDeg); float la = radians(latDeg); return vec3(cos(la) * cos(lo), cos(la) * sin(lo), sin(la)); }\n// One big round bowl with a rim, by longitude, latitude and angular radius in degrees. Negative inside.\nfloat bowl(vec3 p, float lonDeg, float latDeg, float radDeg, float depth) {\n float d = acos(clamp(dot(p, dirOf(lonDeg, latDeg)), -1.0, 1.0)) / radians(radDeg);\n return d < 1.0 ? depth * (d * d - 1.0) : depth * exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d);\n}\n// A soft oval on the body, 1 inside and 0 outside, by longitude and latitude in degrees.\nfloat oval(vec3 p, float lon0, float lat0, float rx, float ry, float soft) {\n float dx = wrapPi(lon(p) - radians(lon0)) * cos(radians(lat0)) / radians(rx);\n float dy = (lat(p) - radians(lat0)) / radians(ry);\n return 1.0 - smoothstep(1.0 - soft, 1.0, sqrt(dx * dx + dy * dy));\n}";
|
|
4
|
-
/** The fragment shader of a body: the surface code goes between the prelude and `main`. */
|
|
5
|
-
export declare function bodyFragment(surface: string, painted: boolean, world?: boolean): string;
|
|
4
|
+
/** The fragment shader of a body: the surface code goes between the prelude and `main`. `limbTint` is the colour the edge of a glowing body shifts to (default the Sun's orange). */
|
|
5
|
+
export declare function bodyFragment(surface: string, painted: boolean, world?: boolean, limbTint?: readonly [number, number, number]): string;
|
|
6
6
|
/** A painted body's surface code: the map by longitude and latitude, with the seam at 180 degrees left alone. */
|
|
7
7
|
export declare const PAINTED_SURFACE = "\nvec4 surface(vec3 p) {\n float u1 = atan(p.y, p.x) / 6.2831853 + 0.5;\n float u2 = fract(u1 + 0.5) - 0.5;\n float v = asin(clamp(p.z, -1.0, 1.0)) / 3.1415927 + 0.5;\n vec2 a = vec2(u1, v); vec2 b = vec2(u2, v);\n vec2 ax = dFdx(a); vec2 ay = dFdy(a); vec2 bx = dFdx(b); vec2 by = dFdy(b);\n bool useA = dot(ax, ax) + dot(ay, ay) <= dot(bx, bx) + dot(by, by);\n vec3 c = textureGrad(uMap, useA ? a : b, useA ? ax : bx, useA ? ay : by).rgb;\n // A map of plain bands is the same at every longitude, so the body turns and the picture does not. The wave, the spot and the polar hexagon move with the surface.\n if (uMark > 0.5) {\n float lon = atan(p.y, p.x);\n float lat = asin(clamp(p.z, -1.0, 1.0));\n float wave = sin(lon * 4.0 + lat * 6.0);\n float belt = 1.0 - smoothstep(1.15, 1.45, abs(lat));\n c *= 0.86 + 0.22 * (0.5 + 0.5 * wave) * belt;\n if (uMark > 1.5) {\n float ang = atan(p.y, p.x);\n float radius = length(p.xy);\n float hexR = 0.26 + 0.045 * cos(ang * 6.0);\n float hex = (1.0 - smoothstep(0.0, 0.028, abs(radius - hexR))) * smoothstep(0.42, 0.68, p.z);\n c = mix(c, vec3(0.45, 0.55, 0.68), hex);\n } else {\n float spot = exp(-pow((lat - 0.32) / 0.2, 2.0) - pow(lon / 0.5, 2.0));\n c = mix(c, c * vec3(0.28, 0.34, 0.5), clamp(spot, 0.0, 1.0));\n }\n }\n return vec4(c, dot(c, vec3(0.3, 0.59, 0.11)));\n}";
|
|
8
8
|
export declare const RING_FRAGMENT = "\nprecision highp float;\nuniform sampler2D uRingTex;\nuniform vec2 uRingR;\nuniform vec3 uLight;\nuniform vec3 uRingN;\nvarying vec3 vL;\nvarying vec3 vW;\nvoid main() {\n float u = (length(vL.xy) - uRingR.x) / (uRingR.y - uRingR.x);\n if (u < 0.0 || u > 1.0) discard;\n vec4 t = texture2D(uRingTex, vec2(u, 0.5));\n float nl = dot(uRingN, uLight);\n float side = nl * uRingN.z;\n float bright = 0.16 + 0.84 * clamp(abs(nl) * 2.0, 0.0, 1.0);\n if (side < 0.0) bright *= 0.38;\n float b = dot(vW, uLight);\n float closest = sqrt(max(dot(vW, vW) - b * b, 0.0));\n float shadow = b < 0.0 ? smoothstep(1.02, 0.97, closest) : 0.0;\n vec3 c = pow(t.rgb, vec3(2.2)) * bright * (1.0 - 0.93 * shadow);\n gl_FragColor = vec4(pow(c, vec3(1.0 / 2.2)), t.a);\n}";
|
|
@@ -18,5 +18,10 @@ export declare const RING_VERTEX_WORLD = "\nuniform float uScale;\nvarying vec3
|
|
|
18
18
|
export declare const RING_FRAGMENT_WORLD: string;
|
|
19
19
|
export declare const GLOW_VERTEX = "\nvarying vec2 vP;\nvoid main() { vP = position.xy; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }";
|
|
20
20
|
export declare const GLOW_FRAGMENT = "\nprecision highp float;\nvarying vec2 vP;\nuniform vec3 uColour;\nvoid main() {\n float r = length(vP);\n if (r < 0.98) discard;\n float a = exp(-(r - 1.0) * 2.4) * 0.62;\n float streak = 0.82 + 0.18 * sin(atan(vP.y, vP.x) * 9.0 + r * 3.0);\n a *= streak * smoothstep(3.0, 1.6, r);\n gl_FragColor = vec4(uColour * a, a);\n}";
|
|
21
|
+
/**
|
|
22
|
+
* The glow round a star that is not the Sun, drawn on a plane behind it: a wide soft bloom, a tight bright inner bloom, a thin glowing edge (the chromosphere), and, on an active star, prominences standing off the edge
|
|
23
|
+
* with an occasional flare. `uStrength` is how bright, `uSpread` how fast the bloom fades, `uRim` the edge, `uProm` how big the prominences are, `uSeed` where they are and `uTime` moves them.
|
|
24
|
+
*/
|
|
25
|
+
export declare const STAR_GLOW_FRAGMENT = "\nprecision highp float;\nvarying vec2 vP;\nuniform vec3 uColour;\nuniform float uStrength;\nuniform float uSpread;\nuniform float uRim;\nuniform float uProm;\nuniform float uSeed;\nuniform float uTime;\nfloat h1(float n) { return fract(sin(n * 127.1 + uSeed * 91.7) * 43758.5453); }\nfloat n1(float x) { float i = floor(x); float f = fract(x); f = f * f * (3.0 - 2.0 * f); return mix(h1(i), h1(i + 1.0), f); }\nvoid main() {\n float r = length(vP);\n if (r < 0.98) discard;\n float x = r - 1.0;\n float fade = smoothstep(3.0, 1.6, r);\n float halo = exp(-x * uSpread) * uStrength;\n float tight = exp(-x * 14.0) * uStrength * 0.9;\n float rim = exp(-x * 60.0) * uRim;\n vec3 col = uColour * (halo + tight) + mix(uColour, vec3(1.0), 0.4) * rim;\n float a = halo + tight + rim;\n if (uProm > 0.0) {\n float ang = atan(vP.y, vP.x);\n float site = smoothstep(0.55, 0.85, n1(ang * 2.6 + uSeed * 20.0));\n float drift = 0.75 + 0.25 * sin(uTime * 0.5 + ang * 4.0);\n float height = site * uProm * 0.3 * drift;\n float strands = 0.65 + 0.35 * n1(ang * 70.0 + uTime * 0.3);\n float pr = step(0.0, x) * pow(smoothstep(height, 0.0, x), 1.6) * site * strands * 0.9;\n vec3 pc = mix(vec3(1.0, 0.42, 0.26), uColour, 0.2);\n float a0 = uSeed * 6.2831853 + uTime * 0.02;\n float da = atan(sin(ang - a0), cos(ang - a0));\n float burst = pow(max(0.0, sin(uTime * 0.9 + uSeed * 40.0)), 24.0) * uProm;\n float fl = exp(-da * da * 14.0) * exp(-x * 6.0) * burst * 1.6;\n col += pc * pr + vec3(1.0, 0.92, 0.75) * fl;\n a += pr + fl;\n }\n // additive blending multiplies by alpha, so the root of the light is the alpha and the colour carries the rest: the sum arrives as written\n float s = sqrt(min(a * fade, 1.0));\n gl_FragColor = vec4(col / max(a, 1e-4) * s, s);\n}";
|
|
21
26
|
/** A cheap lit ball for a moon seen small beside its planet. */
|
|
22
27
|
export declare const BALL_FRAGMENT = "\nprecision highp float;\nuniform vec3 uLight;\nuniform vec3 uColour;\nvarying vec3 vObj;\nvarying vec3 vN;\nvarying vec3 vW;\n\nfloat hash13(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }\nvec3 hash33(vec3 p) { p = fract(p * vec3(0.1031, 0.1030, 0.0973)); p += dot(p, p.yxz + 33.33); return fract((p.xxy + p.yxx) * p.zyx); }\nfloat vnoise(vec3 p) {\n vec3 i = floor(p); vec3 f = fract(p); f = f * f * (3.0 - 2.0 * f);\n return mix(mix(mix(hash13(i), hash13(i + vec3(1,0,0)), f.x), mix(hash13(i + vec3(0,1,0)), hash13(i + vec3(1,1,0)), f.x), f.y),\n mix(mix(hash13(i + vec3(0,0,1)), hash13(i + vec3(1,0,1)), f.x), mix(hash13(i + vec3(0,1,1)), hash13(i + vec3(1,1,1)), f.x), f.y), f.z);\n}\n// Noise that knows how big a pixel is: an octave finer than the pixel can show is replaced by its average (0.5) instead of being sampled, so a small body is smooth and not a spray of pixel-sized specks.\nfloat fbm(vec3 p) { float fw = length(fwidth(p)); float a = 0.5; float s = 0.0; for (int i = 0; i < 5; i++) { s += a * mix(0.5, vnoise(p), 1.0 - smoothstep(0.3, 0.7, fw)); p = p * 2.03 + vec3(1.7, 9.2, 3.1); fw *= 2.03; a *= 0.5; } return s; }\n// Craters: a bowl and a raised rim in each cell that has one. Returns a height, negative in a bowl. A crater smaller than about two pixels fades out (it would only be noise).\nfloat craters(vec3 p, float freq, float amp, float seed) {\n vec3 q = p * freq + seed; vec3 ip = floor(q); float h = 0.0;\n float fw = length(fwidth(q));\n for (int x = -1; x <= 1; x++) for (int y = -1; y <= 1; y++) for (int z = -1; z <= 1; z++) {\n vec3 c = ip + vec3(float(x), float(y), float(z));\n float on = step(0.52, hash13(c + 3.7));\n vec3 ctr = c + hash33(c);\n float rad = mix(0.12, 0.36, hash13(c + 17.3));\n float d = length(q - ctr) / rad;\n float bowl = d < 1.0 ? (d * d - 1.0) : 0.0;\n float rim = d >= 1.0 ? exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d) : 0.0;\n h += on * amp * rad * (bowl + rim) * (1.0 - smoothstep(0.35, 0.9, fw / rad));\n }\n return h;\n}\nfloat lon(vec3 p) { return atan(p.y, p.x); }\nfloat lat(vec3 p) { return asin(clamp(p.z, -1.0, 1.0)); }\nfloat wrapPi(float a) { return a - 6.2831853 * floor((a + 3.1415927) / 6.2831853); }\nvec3 dirOf(float lonDeg, float latDeg) { float lo = radians(lonDeg); float la = radians(latDeg); return vec3(cos(la) * cos(lo), cos(la) * sin(lo), sin(la)); }\n// One big round bowl with a rim, by longitude, latitude and angular radius in degrees. Negative inside.\nfloat bowl(vec3 p, float lonDeg, float latDeg, float radDeg, float depth) {\n float d = acos(clamp(dot(p, dirOf(lonDeg, latDeg)), -1.0, 1.0)) / radians(radDeg);\n return d < 1.0 ? depth * (d * d - 1.0) : depth * exp(-pow((d - 1.0) * 5.0, 2.0)) * 0.3 * smoothstep(1.0, 1.08, d);\n}\n// A soft oval on the body, 1 inside and 0 outside, by longitude and latitude in degrees.\nfloat oval(vec3 p, float lon0, float lat0, float rx, float ry, float soft) {\n float dx = wrapPi(lon(p) - radians(lon0)) * cos(radians(lat0)) / radians(rx);\n float dy = (lat(p) - radians(lat0)) / radians(ry);\n return 1.0 - smoothstep(1.0 - soft, 1.0, sqrt(dx * dx + dy * dy));\n}\nvoid main() {\n vec3 n = normalize(vN);\n float shade = 0.82 + 0.3 * (fbm(normalize(vObj) * 4.0) - 0.5);\n float diff = max(dot(n, uLight), 0.0);\n vec3 c = pow(uColour * shade, vec3(2.2)) * (0.04 + 1.1 * diff);\n gl_FragColor = vec4(pow(c, vec3(1.0 / 2.2)), 1.0);\n}";
|