lk3d 0.1.0 → 0.2.1
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 +59 -0
- package/README.md +28 -7
- package/bin/lk3d.mjs +24 -13
- package/dist/lk3d-export.min.js +1 -1
- package/dist/lk3d.min.js +73 -35
- package/dist/lk3d.mjs +72 -34
- package/docs/api.md +132 -51
- package/docs/design.md +12 -7
- package/docs/handles-and-lines.md +27 -0
- package/docs/performance.md +42 -10
- package/docs/scene-format.md +31 -29
- package/index.d.ts +53 -2
- package/package.json +2 -3
- package/scene.d.ts +200 -1
- package/schema/scene.schema.json +329 -1
- package/skill/lk3d/SKILL.md +14 -1
- package/src/ao.js +58 -0
- package/src/controls.js +21 -2
- package/src/element.js +111 -0
- package/src/figure/api.js +18 -3
- package/src/figure/camera.js +57 -9
- package/src/figure/context.d.ts +37 -2
- package/src/figure/dimensions.js +140 -0
- package/src/figure/idle.js +57 -19
- package/src/figure/input.js +105 -7
- package/src/figure/labels.js +26 -8
- package/src/figure/loop.js +8 -2
- package/src/figure/pick.js +24 -8
- package/src/figure/steps.js +144 -0
- package/src/flow.js +33 -2
- package/src/index.js +16 -2
- package/src/locales/en.js +7 -0
- package/src/locales/vi.js +7 -0
- package/src/renderer-gl.js +88 -16
- package/src/shaders.js +63 -17
- package/src/stylesheet.js +14 -4
- package/src/validate.js +89 -0
- package/docs/flow.png +0 -0
- package/docs/generator.png +0 -0
- package/docs/pen-blueprint.png +0 -0
- package/docs/pen-dark.png +0 -0
- package/docs/pen-ortho.png +0 -0
- package/docs/shapes/blade.png +0 -0
- package/docs/shapes/blob.png +0 -0
- package/docs/shapes/box.png +0 -0
- package/docs/shapes/capsule.png +0 -0
- package/docs/shapes/cylinder.png +0 -0
- package/docs/shapes/dna.png +0 -0
- package/docs/shapes/ellipsoid.png +0 -0
- package/docs/shapes/extrude.png +0 -0
- package/docs/shapes/helix.png +0 -0
- package/docs/shapes/lathe.png +0 -0
- package/docs/shapes/loop.png +0 -0
- package/docs/shapes/molecule.png +0 -0
- package/docs/shapes/orbit.png +0 -0
- package/docs/shapes/ring.png +0 -0
- package/docs/shapes/scatter.png +0 -0
- package/docs/shapes/shell.png +0 -0
- package/docs/shapes/sphere.png +0 -0
- package/docs/shapes/terrain.png +0 -0
- package/docs/shapes/torus.png +0 -0
- package/docs/shapes/tree.png +0 -0
- package/docs/shapes/tube.png +0 -0
package/src/figure/labels.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
// Labels and their leader lines (HTML and SVG over the canvas, measured once, moved with transforms), and the scale
|
|
3
3
|
// bar. Owns labelWidths_ and placeLabels_ (the frame loop calls it after each picture); reads built_, state_, spec_,
|
|
4
|
-
// tagLayer_, leaderSvg_, scaleEl_, lookKey_, width_ and height_, and calls project_, explodeShift_, baseMatrix_ and
|
|
4
|
+
// tagLayer_, leaderSvg_, scaleEl_, lookKey_, width_ and height_, and calls project_, cutPlane_, cutPlane2_, explodeShift_, baseMatrix_ and
|
|
5
5
|
// kick_.
|
|
6
6
|
import { M, sub, add, mulv, len } from '../math.js';
|
|
7
7
|
import textWidth from '../text-width.js';
|
|
@@ -124,15 +124,33 @@ export function makeLabels(c) {
|
|
|
124
124
|
part.id === c.state_.selected ||
|
|
125
125
|
(which === 'auto' && c.state_.explode > 0.45));
|
|
126
126
|
const middle = c.project_(add(c.built_.center, c.explodeShift_())) || [width / 2, height / 2];
|
|
127
|
-
const items = []
|
|
127
|
+
const items = [],
|
|
128
|
+
plane = c.state_.cut ? c.cutPlane_() : null,
|
|
129
|
+
second = c.cutPlane2_();
|
|
128
130
|
for (const tag of tags) {
|
|
129
|
-
// the part's anchor on the stage, if its label is wanted (a spinning part's label stays put)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
131
|
+
// the part's anchor on the stage, if its label is wanted (a spinning part's label stays put). Where the cut has
|
|
132
|
+
// taken the anchor's place (beyond its plane, and beyond the second too in a quarter cut), the label points at the
|
|
133
|
+
// part's cut face instead (the anchor moved onto the nearer plane), and shows not at all once the cut has taken
|
|
134
|
+
// the whole part (it would point at nothing)
|
|
135
|
+
let anchor = M.pt(
|
|
136
|
+
tag.part.spin || tag.part.handleOf?.round ? c.baseMatrix_(tag.part) : tag.part.matrix,
|
|
137
|
+
tag.part.anchor,
|
|
138
|
+
);
|
|
139
|
+
const cut = /** @type {number[]} */ (plane),
|
|
140
|
+
along = (p, point) => p[0] * point[0] + p[1] * point[1] + p[2] * point[2] + p[3],
|
|
141
|
+
beyond = (point) => Math.min(along(cut, point), along(second, point)), // (> 0: cut away)
|
|
142
|
+
past = plane && tag.part.cut ? beyond(anchor) : 0;
|
|
143
|
+
let cutAway = false;
|
|
144
|
+
if (past > 0) {
|
|
145
|
+
const offset = c.offsetOf_(tag.part),
|
|
146
|
+
{ min, max } = tag.part,
|
|
147
|
+
nearer = along(cut, anchor) <= along(second, anchor) ? cut : second;
|
|
148
|
+
cutAway = [0, 1, 2, 3, 4, 5, 6, 7].every(
|
|
149
|
+
(corner) => beyond([0, 1, 2].map((axis) => offset[axis] + ((corner >> axis) & 1 ? max : min)[axis])) > 0,
|
|
135
150
|
);
|
|
151
|
+
anchor = anchor.map((value, axis) => value - nearer[axis] * past);
|
|
152
|
+
}
|
|
153
|
+
const at = which !== 'none' && wanted(tag.part) && !cutAway && c.project_(anchor);
|
|
136
154
|
if (!at) {
|
|
137
155
|
show(tag, false);
|
|
138
156
|
continue;
|
package/src/figure/loop.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// The frame loop: a frame drawn only when something changed (kick_) or moves, the canvas's size and resolution
|
|
3
3
|
// (adaptive while frames come late), the screen's refresh, things moved on along paths by the clock (travel_), and
|
|
4
4
|
// watching size, visibility and the screen's density. Owns width_ and height_ (the stage's size), kick_, markDirty_,
|
|
5
|
-
// sizeCanvas_, drawNow_ and travel_ (and dpr_, resolution_ and refreshMs_ for stats); reads nearly everything else, to
|
|
5
|
+
// measure_, sizeCanvas_, drawNow_ and travel_ (and dpr_, resolution_ and refreshMs_ for stats); reads nearly everything else, to
|
|
6
6
|
// draw it, and empties labelWidths_ when the layout changes.
|
|
7
7
|
import { sub, add, norm } from '../math.js';
|
|
8
8
|
import { ramp } from '../flow.js';
|
|
@@ -60,6 +60,8 @@ export function makeLoop(c) {
|
|
|
60
60
|
dirty = true;
|
|
61
61
|
if (!frameRequest && visible && !c.destroyed_) frameRequest = requestAnimationFrame(frame);
|
|
62
62
|
};
|
|
63
|
+
// the stage measured (and shaped) now: also for a step framing parts before the first measure comes
|
|
64
|
+
c.measure_ = resize;
|
|
63
65
|
function resize() {
|
|
64
66
|
const narrow = c.host_.clientWidth < 560,
|
|
65
67
|
aspectRatio = narrow ? c.spec_.narrow || '1 / 0.9' : `${c.spec_.w || 16} / ${c.spec_.h || 10}`;
|
|
@@ -224,7 +226,9 @@ export function makeLoop(c) {
|
|
|
224
226
|
}
|
|
225
227
|
const flows = c.flows_.filter((flow) => flow.visible && flow.amount > 0);
|
|
226
228
|
for (const flow of flows.concat(c.streams_)) {
|
|
227
|
-
|
|
229
|
+
// (placed by the renderer on the GPU where it can, WebGL 2: only how many show, here)
|
|
230
|
+
if (flow.drawn && c.renderer_.placesFlow && c.renderer_.placesFlow(flow)) flow.drawn();
|
|
231
|
+
else if (flow.update) flow.update(flow.owner ? flow.owner.matrix : null);
|
|
228
232
|
if (!flow.colors || flow.dark !== look.dark) {
|
|
229
233
|
flow.colors = ramp(flow.ramp, look.dark);
|
|
230
234
|
flow.dark = look.dark;
|
|
@@ -259,6 +263,7 @@ export function makeLoop(c) {
|
|
|
259
263
|
edges: c.showEdges_(),
|
|
260
264
|
cut: !!c.state_.cut,
|
|
261
265
|
plane: c.cutPlane_(),
|
|
266
|
+
plane2: c.cutPlane2_(),
|
|
262
267
|
hatch: c.built_.radius / 80, // (a cap's hatching: lines this far apart on the cut, a few px when the model fills the view)
|
|
263
268
|
hover: c.state_.hover,
|
|
264
269
|
selected: c.state_.selected,
|
|
@@ -291,6 +296,7 @@ export function makeLoop(c) {
|
|
|
291
296
|
else {
|
|
292
297
|
if (c.keptPicture_) c.keptPicture_.remove(); // (drawn again: the picture kept while the context was given up goes)
|
|
293
298
|
c.placeLabels_();
|
|
299
|
+
c.placeDimensions_();
|
|
294
300
|
dirty = false;
|
|
295
301
|
if (c.stats_.firstFrameMs == null) {
|
|
296
302
|
c.stats_.firstFrameMs = performance.now() - c.mountedAt_;
|
package/src/figure/pick.js
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
// Picking: a ray from the eye through a point of the stage, against each part's bounding-volume trees (bvh.js, each
|
|
3
|
-
// built when first needed, or in idle time: idle.js); the nearest hit on a part that is drawn and not cut away
|
|
4
|
-
// bvhOf_, pickHit_ and pickAt_; reads built_ and state_, and
|
|
5
|
-
|
|
3
|
+
// built when first needed, or in idle time: idle.js); the nearest hit on a part that is drawn and not cut away, and
|
|
4
|
+
// whether what is seen there is the part's cut face. Owns bvhOf_, pickHit_ and pickAt_; reads built_ and state_, and
|
|
5
|
+
// calls rayAt_, cutPlane_ and cutPlane2_.
|
|
6
|
+
import { M, add, sub, mulv, cross, dot } from '../math.js';
|
|
6
7
|
import { buildBVH, intersect } from '../bvh.js';
|
|
7
8
|
|
|
9
|
+
// whether a triangle's outward face (its corners anticlockwise) is turned away from a ray going this way
|
|
10
|
+
/** @param {{ positions: ArrayLike<number>, indices: ArrayLike<number> }} mesh @param {number} triangle @param {number[]} direction */
|
|
11
|
+
function facesAway(mesh, triangle, direction) {
|
|
12
|
+
const [a, b, d] = [0, 1, 2].map((corner) => 3 * mesh.indices[3 * triangle + corner]),
|
|
13
|
+
corner = (index) => [mesh.positions[index], mesh.positions[index + 1], mesh.positions[index + 2]],
|
|
14
|
+
normal = cross(sub(corner(b), corner(a)), sub(corner(d), corner(a)));
|
|
15
|
+
return dot(normal, direction) > 0;
|
|
16
|
+
}
|
|
17
|
+
|
|
8
18
|
/** @param {import('./context').Ctx} c */
|
|
9
19
|
export function makePick(c) {
|
|
10
20
|
const trees = new WeakMap(); // (per geometry: parts alike share one tree)
|
|
@@ -48,10 +58,12 @@ export function makePick(c) {
|
|
|
48
58
|
c.pickHit_ = (x, y, minAlpha = 0.3) => {
|
|
49
59
|
// parts fainter than minAlpha let the ray through; the part hit, and where along the ray (A + D t)
|
|
50
60
|
const { origin, direction } = c.rayAt_(x, y),
|
|
51
|
-
plane = c.cutPlane_()
|
|
61
|
+
plane = c.cutPlane_(),
|
|
62
|
+
second = c.cutPlane2_();
|
|
52
63
|
let best = Infinity,
|
|
53
64
|
/** @type {string | null} */
|
|
54
|
-
id = null
|
|
65
|
+
id = null,
|
|
66
|
+
onCut = false;
|
|
55
67
|
for (const part of c.built_.parts) {
|
|
56
68
|
if (part.drawAlpha < minAlpha || rayBox(origin, direction, part) >= best) continue; // (missed by the ray, or behind a nearer hit)
|
|
57
69
|
for (const batch of part.batches)
|
|
@@ -64,18 +76,22 @@ export function makePick(c) {
|
|
|
64
76
|
const accept =
|
|
65
77
|
c.state_.cut && part.cut
|
|
66
78
|
? (t) => {
|
|
67
|
-
const point = add(origin, mulv(direction, t))
|
|
68
|
-
|
|
79
|
+
const point = add(origin, mulv(direction, t)),
|
|
80
|
+
beyond = (p) => p[0] * point[0] + p[1] * point[1] + p[2] * point[2] + p[3] > 0;
|
|
81
|
+
return !(beyond(plane) && beyond(second)); // (cut away only beyond both, in a quarter cut)
|
|
69
82
|
}
|
|
70
83
|
: null;
|
|
71
84
|
const hit = intersect(batch.mesh, c.bvhOf_(batch), shapeOrigin, shapeDirection, best, false, accept);
|
|
72
85
|
if (hit && hit.t < best) {
|
|
73
86
|
best = hit.t;
|
|
74
87
|
id = part.id;
|
|
88
|
+
// (a cut part's surface seen from inside, its outward face turned away from the eye: the ray came in
|
|
89
|
+
// through the cut, so what is seen there is the cut face, filled at the plane)
|
|
90
|
+
onCut = !!accept && facesAway(batch.mesh, hit.triangle, shapeDirection);
|
|
75
91
|
}
|
|
76
92
|
}
|
|
77
93
|
}
|
|
78
|
-
return { id, t: best, origin, direction };
|
|
94
|
+
return { id, t: best, origin, direction, onCut };
|
|
79
95
|
};
|
|
80
96
|
c.pickAt_ = (x, y, minAlpha) => c.pickHit_(x, y, minAlpha).id;
|
|
81
97
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Guided steps (the scene's `steps`): the figure explained in order, each step a title and words and a state of the
|
|
3
|
+
// figure: where the camera looks, how far apart, the cut, see-through, which labels, parts hidden, a part chosen, the
|
|
4
|
+
// motions' values and which play. Going to a step sets all of that, and what a step does not say goes back to the
|
|
5
|
+
// figure's start, so a step is the same picture however the reader came to it (the camera moves only when the step
|
|
6
|
+
// says where). The reader still turns, takes apart and cuts as they like; the next step sets its own state again.
|
|
7
|
+
// Owns goToStep_ and stepAt_; reads spec_, opts_, words_, state_, built_, motions_, controls_ and stage_, and calls
|
|
8
|
+
// view_, viewAngles_, frame_, explodeNow_, tweenExplode_, setMotion_, stopMotion_, playMotion_, syncHandleInputs_,
|
|
9
|
+
// showInfo_, markDirty_ and kick_.
|
|
10
|
+
import { clock } from '../clock.js';
|
|
11
|
+
import { startOf } from '../motion.js';
|
|
12
|
+
import { safeHTML } from './dom.js';
|
|
13
|
+
import LK3D from '../namespace.js';
|
|
14
|
+
|
|
15
|
+
/** @param {import('./context').Ctx} c @param {string} buttonClass */
|
|
16
|
+
export function makeSteps(c, buttonClass) {
|
|
17
|
+
const steps = (c.spec_.steps || []).filter((step) => step && typeof step === 'object');
|
|
18
|
+
c.stepAt_ = -1;
|
|
19
|
+
c.goToStep_ = () => {};
|
|
20
|
+
if (!steps.length) return;
|
|
21
|
+
|
|
22
|
+
// ---- the cut, swept in (or out) as the explode and the camera glide, rather than appearing at once
|
|
23
|
+
/** @type {((now: number) => boolean) | null} */
|
|
24
|
+
let cutTween = null;
|
|
25
|
+
/** @param {boolean | number} to @param {number} ms */
|
|
26
|
+
const sweepCut = (to, ms) => {
|
|
27
|
+
if (cutTween) c.animations_.delete(cutTween);
|
|
28
|
+
cutTween = null;
|
|
29
|
+
const amount = (/** @type {boolean | number} */ cut) => (cut === true ? 1 : Math.max(0, Math.min(1, +cut || 0))),
|
|
30
|
+
from = amount(c.state_.cut),
|
|
31
|
+
target = amount(to);
|
|
32
|
+
if (!(ms > 0) || LK3D.reduce || from === target) return void (c.state_.cut = to);
|
|
33
|
+
const start = clock.now();
|
|
34
|
+
const tween = (cutTween = (now) => {
|
|
35
|
+
const progress = Math.max(0, Math.min(1, (now - start) / ms)),
|
|
36
|
+
eased = progress < 0.5 ? 4 * progress * progress * progress : 1 - Math.pow(-2 * progress + 2, 3) / 2;
|
|
37
|
+
c.state_.cut = progress >= 1 ? to : from + (target - from) * eased;
|
|
38
|
+
c.markDirty_();
|
|
39
|
+
if (progress < 1) return true;
|
|
40
|
+
c.animations_.delete(tween);
|
|
41
|
+
cutTween = null;
|
|
42
|
+
if (c.controls_.style) c.controls_.style(); // (the Cut button pressed once the cut is in, not while it sweeps)
|
|
43
|
+
return false;
|
|
44
|
+
});
|
|
45
|
+
c.animations_.add(tween);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// ---- the panel under the stage: the step's words, and Back and Next (unless the page drives the steps itself)
|
|
49
|
+
const panel = (c.opts_.steps ?? c.opts_.controls) !== false ? document.createElement('div') : null,
|
|
50
|
+
words = document.createElement('div'),
|
|
51
|
+
/** @type {HTMLButtonElement[]} */
|
|
52
|
+
buttons = [];
|
|
53
|
+
if (panel) {
|
|
54
|
+
panel.className = 'lk3d-steps';
|
|
55
|
+
panel.setAttribute('role', 'group');
|
|
56
|
+
panel.setAttribute('aria-label', c.words_.steps);
|
|
57
|
+
words.className = 'lk3d-step';
|
|
58
|
+
words.setAttribute('aria-live', 'polite'); // (each step's words are said aloud as the reader comes to it)
|
|
59
|
+
const nav = document.createElement('span');
|
|
60
|
+
nav.className = 'lk3d-step-nav';
|
|
61
|
+
for (const [label, by] of /** @type {[string, number][]} */ ([
|
|
62
|
+
[c.words_.back, -1],
|
|
63
|
+
[c.words_.next, 1],
|
|
64
|
+
])) {
|
|
65
|
+
const button = document.createElement('button');
|
|
66
|
+
button.type = 'button';
|
|
67
|
+
button.className = buttonClass;
|
|
68
|
+
button.textContent = label;
|
|
69
|
+
// (at the first or last step it says so and does nothing, rather than being disabled: a disabled button would
|
|
70
|
+
// drop the keyboard's focus to the page)
|
|
71
|
+
button.addEventListener('click', () => {
|
|
72
|
+
const to = c.stepAt_ + by;
|
|
73
|
+
if (to < 0 || to >= steps.length) return;
|
|
74
|
+
c.goToStep_(to);
|
|
75
|
+
if (c.opts_.onStep) c.opts_.onStep(to, steps[to]);
|
|
76
|
+
});
|
|
77
|
+
nav.append(button);
|
|
78
|
+
buttons.push(button);
|
|
79
|
+
}
|
|
80
|
+
panel.append(words, nav);
|
|
81
|
+
c.stage_.after(panel);
|
|
82
|
+
// (before the first step, opts.step -1: no words yet, Next goes to the first)
|
|
83
|
+
buttons[0].setAttribute('aria-disabled', 'true');
|
|
84
|
+
buttons[1].setAttribute('aria-disabled', 'false');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---- going to a step: by its place (from 0) or its id; options.ms 0 sets it at once (no glides)
|
|
88
|
+
c.goToStep_ = (which, options = {}) => {
|
|
89
|
+
const index = typeof which === 'string' ? steps.findIndex((step) => step.id === which) : Math.round(+which);
|
|
90
|
+
const step = steps[index];
|
|
91
|
+
if (!step) return;
|
|
92
|
+
c.stepAt_ = index;
|
|
93
|
+
const atOnce = options.ms === 0,
|
|
94
|
+
listed = (/** @type {unknown} */ list) => (Array.isArray(list) ? list : []),
|
|
95
|
+
hidden = listed(step.hide);
|
|
96
|
+
// what shows, and how
|
|
97
|
+
for (const part of c.built_.parts)
|
|
98
|
+
part.hidden = part.hiddenByScene || hidden.includes(part.id) || hidden.includes(part.group);
|
|
99
|
+
c.syncHandleInputs_();
|
|
100
|
+
c.state_.xray = !!step.xray;
|
|
101
|
+
c.state_.labels = step.labels ?? c.opts_.labels ?? 'auto';
|
|
102
|
+
c.state_.dimensions = step.dimensions ?? null;
|
|
103
|
+
c.state_.section = { normal: [0, 0, 1], offset: 0, ...c.spec_.section, ...step.section };
|
|
104
|
+
const part = step.select != null && c.part_(step.select);
|
|
105
|
+
c.state_.selected = part && !part.hidden ? part.id : null;
|
|
106
|
+
// the motions: each at the step's value, else its start; those the step plays, played from there
|
|
107
|
+
for (const motion of c.motions_) {
|
|
108
|
+
c.stopMotion_(motion);
|
|
109
|
+
c.setMotion_(motion, step.motion?.[motion.id] ?? startOf(motion));
|
|
110
|
+
}
|
|
111
|
+
for (const id of listed(step.play)) {
|
|
112
|
+
const motion = c.motions_.find((each) => each.id === id);
|
|
113
|
+
if (motion) c.playMotion_(motion);
|
|
114
|
+
}
|
|
115
|
+
// taken apart, cut and looked at: glided there together
|
|
116
|
+
const explode = Math.max(0, Math.min(1, +(step.explode ?? 0) || 0));
|
|
117
|
+
if (atOnce) c.explodeNow_(explode);
|
|
118
|
+
else c.tweenExplode_(explode);
|
|
119
|
+
sweepCut(c.spec_.section === false ? false : (step.cut ?? false), atOnce ? 0 : 900);
|
|
120
|
+
const focus = listed(step.focus);
|
|
121
|
+
if (step.view === 'home') c.view_('home', options);
|
|
122
|
+
else if (step.view != null || focus.length) {
|
|
123
|
+
const view = typeof step.view === 'object' && step.view && !Array.isArray(step.view) ? step.view : {},
|
|
124
|
+
[yaw, pitch] = typeof step.view === 'string' ? c.viewAngles_(step.view) : [view.yaw, view.pitch],
|
|
125
|
+
framed = c.built_.parts.filter((each) => focus.includes(each.id) || focus.includes(each.group));
|
|
126
|
+
const framing = framed.length && !view.target ? c.frame_(framed, explode) : null;
|
|
127
|
+
const target = view.target || framing?.target,
|
|
128
|
+
dist = view.dist ?? (view.zoom ? undefined : framing?.dist);
|
|
129
|
+
c.view_({ yaw, pitch, target, dist, zoom: view.zoom }, options);
|
|
130
|
+
}
|
|
131
|
+
if (c.controls_.style) c.controls_.style(); // (the buttons show which toggles are on)
|
|
132
|
+
c.showInfo_();
|
|
133
|
+
if (panel) {
|
|
134
|
+
const count = c.words_.stepOf.replace('{n}', String(index + 1)).replace('{count}', String(steps.length));
|
|
135
|
+
words.innerHTML =
|
|
136
|
+
`<span class="lk3d-step-count">${safeHTML(count)}</span> ` +
|
|
137
|
+
(step.title ? `<b>${safeHTML(step.title)}</b> ` : '') +
|
|
138
|
+
safeHTML(step.text);
|
|
139
|
+
buttons[0].setAttribute('aria-disabled', String(index === 0));
|
|
140
|
+
buttons[1].setAttribute('aria-disabled', String(index === steps.length - 1));
|
|
141
|
+
}
|
|
142
|
+
c.kick_();
|
|
143
|
+
};
|
|
144
|
+
}
|
package/src/flow.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// Flow: streaks that run along paths (air through an engine, blood along a vessel, water round a bend), each a thin
|
|
2
2
|
// ribbon that fades from its head to its tail and takes its colour from a ramp along the path (cool at the intake,
|
|
3
|
-
// hot at the exhaust).
|
|
3
|
+
// hot at the exhaust). On WebGL 2 the renderer places them on the GPU from what is here (each path as a table of its
|
|
4
|
+
// points, each streak's fixed numbers: gpuData), sending only the time travelled each frame; elsewhere (WebGL 1, the 2D
|
|
5
|
+
// fallback) their geometry is made here on the CPU, every frame they move (update), and a renderer only widens them on
|
|
4
6
|
// screen and blends them. pathOf() is also what particles along a path use.
|
|
5
7
|
import { sub, mulv, len, rgb } from './math.js';
|
|
6
8
|
|
|
@@ -32,6 +34,11 @@ export function pathOf(points, closed = false) {
|
|
|
32
34
|
};
|
|
33
35
|
return {
|
|
34
36
|
total,
|
|
37
|
+
// each point and its distance along the path, [x, y, z, distance], the closing point too for a closed path
|
|
38
|
+
table: [
|
|
39
|
+
...segments.map(([start, , , startDistance]) => [...start, startDistance]),
|
|
40
|
+
[...path[segments.length % path.length], total],
|
|
41
|
+
],
|
|
35
42
|
into,
|
|
36
43
|
at(distance) {
|
|
37
44
|
const point = [0, 0, 0];
|
|
@@ -115,7 +122,9 @@ export function makeFlow(options = {}) {
|
|
|
115
122
|
drawnCount: 0,
|
|
116
123
|
vertices: new Float32Array(count * STREAK_POINTS * 2 * STRIDE),
|
|
117
124
|
// each streak: its path, where it starts, how fast it goes, and how it wanders round the path
|
|
125
|
+
paths,
|
|
118
126
|
seeds: Array.from({ length: count }, (_, i) => ({
|
|
127
|
+
row: i % paths.length,
|
|
119
128
|
path: paths[i % paths.length],
|
|
120
129
|
start: random(i, 1),
|
|
121
130
|
pace: 0.8 + 0.4 * random(i, 2),
|
|
@@ -127,8 +136,12 @@ export function makeFlow(options = {}) {
|
|
|
127
136
|
step(dt) {
|
|
128
137
|
flow.travelled += flow.speed * dt;
|
|
129
138
|
},
|
|
139
|
+
// how many streaks show: count × amount
|
|
140
|
+
drawn() {
|
|
141
|
+
return (flow.drawnCount = Math.round(flow.count * Math.max(0, Math.min(1, flow.amount))));
|
|
142
|
+
},
|
|
130
143
|
update(m) {
|
|
131
|
-
const drawnCount =
|
|
144
|
+
const drawnCount = flow.drawn(),
|
|
132
145
|
vertices = flow.vertices,
|
|
133
146
|
points = new Float64Array(STREAK_POINTS * 3), // (one small array a frame: nothing a streak)
|
|
134
147
|
point = new Float64Array(3);
|
|
@@ -188,3 +201,21 @@ export function flowIndices(streakCount) {
|
|
|
188
201
|
}
|
|
189
202
|
return indices;
|
|
190
203
|
}
|
|
204
|
+
|
|
205
|
+
// What the GPU places a flow's streaks from (WebGL 2): its paths as a texture of 4 floats a texel, a row a path (the
|
|
206
|
+
// first texel its point count and length, then each point and its distance along), and each streak's fixed numbers,
|
|
207
|
+
// 12 floats a streak (the path's row, where it starts, its pace, its phase; its offset from the path and its turns; how
|
|
208
|
+
// it wanders). The shader in shaders.js (FLOW_VS, GPU) works out what update() does from them.
|
|
209
|
+
export function gpuData(flow) {
|
|
210
|
+
const width = 1 + Math.max(...flow.paths.map((path) => path.table.length)),
|
|
211
|
+
table = new Float32Array(width * flow.paths.length * 4),
|
|
212
|
+
seeds = new Float32Array(flow.count * 12);
|
|
213
|
+
flow.paths.forEach((path, row) => {
|
|
214
|
+
table.set([path.table.length, path.total, 0, 0], row * width * 4);
|
|
215
|
+
path.table.forEach((point, k) => table.set(point, (row * width + 1 + k) * 4));
|
|
216
|
+
});
|
|
217
|
+
flow.seeds.forEach((seed, i) =>
|
|
218
|
+
seeds.set([seed.row, seed.start, seed.pace, seed.phase, ...seed.offset, seed.turns, ...seed.wobble, 0], i * 12),
|
|
219
|
+
);
|
|
220
|
+
return { width, height: flow.paths.length, table, seeds };
|
|
221
|
+
}
|
package/src/index.js
CHANGED
|
@@ -27,11 +27,13 @@ import { makeDom, safeHTML } from './figure/dom.js';
|
|
|
27
27
|
import { makeLoop } from './figure/loop.js';
|
|
28
28
|
import { makeLook } from './figure/look.js';
|
|
29
29
|
import { makeLabels } from './figure/labels.js';
|
|
30
|
+
import { makeDimensions } from './figure/dimensions.js';
|
|
30
31
|
import { makePick } from './figure/pick.js';
|
|
31
32
|
import { makeIdle } from './figure/idle.js';
|
|
32
33
|
import { makeInput } from './figure/input.js';
|
|
33
34
|
import { makeAdditions } from './figure/additions.js';
|
|
34
35
|
import { makeApi } from './figure/api.js';
|
|
36
|
+
import { makeSteps } from './figure/steps.js';
|
|
35
37
|
import { M, D2R } from './math.js';
|
|
36
38
|
import { buildFitting } from './geometry.js';
|
|
37
39
|
import { guard } from './guard.js';
|
|
@@ -40,6 +42,8 @@ import { controls } from './controls.js';
|
|
|
40
42
|
import { clock, refreshFrom } from './clock.js';
|
|
41
43
|
import LK3D, { FORMAT } from './namespace.js';
|
|
42
44
|
import en from './locales/en.js';
|
|
45
|
+
import { defineElement } from './element.js';
|
|
46
|
+
import { serveAO } from './ao.js';
|
|
43
47
|
|
|
44
48
|
let warned = false;
|
|
45
49
|
|
|
@@ -47,7 +51,7 @@ let warned = false;
|
|
|
47
51
|
// and timers are made in): the WebGL context · where parts are (pose) · liquids and gases · the camera · the elements ·
|
|
48
52
|
// the renderer (WebGL 2 or 1, else Canvas 2D) · the frame loop · the look · labels · picking and the idle work (trees,
|
|
49
53
|
// crease shading, edges) · the pointer, keys and handles · what a page adds · the API · the built-in controls
|
|
50
|
-
// (controls.js) · handles as range inputs · watching size, visibility and the screen's density.
|
|
54
|
+
// (controls.js) · handles as range inputs · guided steps · watching size, visibility and the screen's density.
|
|
51
55
|
/**
|
|
52
56
|
* @param {HTMLElement & { lk3d?: any }} el
|
|
53
57
|
* @param {import('../scene').Scene} spec
|
|
@@ -71,7 +75,7 @@ function mount(el, spec, opts = {}) {
|
|
|
71
75
|
const pageButtonClass = opts.buttonClass ?? defaults.buttonClass,
|
|
72
76
|
buttonClass = 'lk3d-btn ' + (pageButtonClass || 'lk3d-plain');
|
|
73
77
|
const mountedAt = performance.now(),
|
|
74
|
-
stats = { setupMs: 0, firstFrameMs: null, treesMs: null, aoMs: null, refreshMs: null };
|
|
78
|
+
stats = { setupMs: 0, firstFrameMs: null, treesMs: null, aoMs: null, refreshMs: null, aoWorker: false };
|
|
75
79
|
const state = {
|
|
76
80
|
style: opts.style || spec.style || 'pen',
|
|
77
81
|
theme: opts.theme || spec.theme || null,
|
|
@@ -83,6 +87,7 @@ function mount(el, spec, opts = {}) {
|
|
|
83
87
|
cut: false,
|
|
84
88
|
section: { normal: [0, 0, 1], offset: 0, ...spec.section }, // (the cut's plane: the scene's, moved by the page)
|
|
85
89
|
labels: opts.labels || 'auto',
|
|
90
|
+
dimensions: null, // (the dimensions shown: null, the scene's own; or a step's or the page's, by id)
|
|
86
91
|
xray: false,
|
|
87
92
|
yaw: (spec.view?.yaw ?? -35) * D2R,
|
|
88
93
|
pitch: (spec.view?.pitch ?? 18) * D2R,
|
|
@@ -140,6 +145,7 @@ function mount(el, spec, opts = {}) {
|
|
|
140
145
|
const startWatching = makeLoop(c);
|
|
141
146
|
makeLook(c);
|
|
142
147
|
makeLabels(c);
|
|
148
|
+
makeDimensions(c);
|
|
143
149
|
makePick(c);
|
|
144
150
|
makeIdle(c);
|
|
145
151
|
const makeHandleInputs = makeInput(c);
|
|
@@ -160,11 +166,16 @@ function mount(el, spec, opts = {}) {
|
|
|
160
166
|
explodable: built.parts.some((part) => part.explode.some((offset) => offset !== 0)),
|
|
161
167
|
motions: c.motions_.filter((motion) => motion.name),
|
|
162
168
|
cuttable: spec.section !== false && c.renderer_ && c.renderer_.kind !== '2d',
|
|
169
|
+
cutRange: c.cutRange_,
|
|
170
|
+
moveCut: spec.section && spec.section.fixed ? null : c.moveCut_,
|
|
163
171
|
seeThrough: built.parts.some((part) => part.xray),
|
|
164
172
|
}),
|
|
165
173
|
);
|
|
166
174
|
}
|
|
167
175
|
makeHandleInputs();
|
|
176
|
+
// ---- guided steps (the scene's steps): their panel under the stage, and the first one (opts.step) set at once
|
|
177
|
+
makeSteps(c, buttonClass);
|
|
178
|
+
c.goToStep_(opts.step ?? 0, { ms: 0 });
|
|
168
179
|
startWatching();
|
|
169
180
|
c.showInfo_();
|
|
170
181
|
stats.setupMs = performance.now() - mountedAt;
|
|
@@ -173,6 +184,9 @@ function mount(el, spec, opts = {}) {
|
|
|
173
184
|
|
|
174
185
|
LK3D.mount = mount;
|
|
175
186
|
if (typeof window !== 'undefined') window.LK3D = LK3D;
|
|
187
|
+
defineElement(mount); // <lk3d-figure>, for pages that write no script
|
|
188
|
+
// (this script run as the page's crease-shading worker, ao.js: it answers its jobs and draws nothing)
|
|
189
|
+
if (typeof self !== 'undefined' && /** @type {any} */ (self).lk3dAO) serveAO();
|
|
176
190
|
export default LK3D;
|
|
177
191
|
export const { build, reduce, version, locales, defaults, themes, format } = LK3D; // (the same objects: setting defaults here sets them for LK3D; reduce is read once, at load)
|
|
178
192
|
export { mount, M, refreshFrom, clock, safeHTML };
|
package/src/locales/en.js
CHANGED
|
@@ -5,6 +5,7 @@ export default {
|
|
|
5
5
|
hintTouch: 'Drag sideways to turn, tap it to turn any way · pinch to zoom',
|
|
6
6
|
keys: 'Arrow keys turn it, + and − zoom, Page Down and Page Up go through its parts, Escape lets go.',
|
|
7
7
|
parts: 'Parts',
|
|
8
|
+
dimensions: 'Dimensions',
|
|
8
9
|
hintMouse: 'Drag to turn · Shift + drag to move · Ctrl + scroll to zoom',
|
|
9
10
|
credit: '3D model: ',
|
|
10
11
|
ai: ' (an illustration made by AI, not a real specimen)',
|
|
@@ -18,6 +19,8 @@ export default {
|
|
|
18
19
|
cut: 'Cut open',
|
|
19
20
|
cutTitle: 'Cut the model in half to see inside',
|
|
20
21
|
xray: 'See through',
|
|
22
|
+
cutAt: 'Cut at',
|
|
23
|
+
cutAtLabel: 'Move the cut through the model',
|
|
21
24
|
xrayTitle: 'Make the casing transparent',
|
|
22
25
|
labels: 'Labels',
|
|
23
26
|
labelsTitle: 'Show all labels',
|
|
@@ -29,5 +32,9 @@ export default {
|
|
|
29
32
|
side: 'Side',
|
|
30
33
|
top: 'Top',
|
|
31
34
|
home: 'Reset',
|
|
35
|
+
steps: 'Steps',
|
|
36
|
+
back: 'Back',
|
|
37
|
+
next: 'Next',
|
|
38
|
+
stepOf: 'Step {n} of {count}',
|
|
32
39
|
decimal: '.',
|
|
33
40
|
};
|
package/src/locales/vi.js
CHANGED
|
@@ -4,6 +4,7 @@ export default {
|
|
|
4
4
|
hintTouch: 'Kéo ngang để xoay, chạm vào hình để xoay mọi hướng · chụm hai ngón để phóng to',
|
|
5
5
|
keys: 'Phím mũi tên để xoay, + và − để phóng to hay thu nhỏ, Page Down và Page Up để đi qua từng bộ phận, Esc để bỏ chọn.',
|
|
6
6
|
parts: 'Các bộ phận',
|
|
7
|
+
dimensions: 'Kích thước',
|
|
7
8
|
hintMouse: 'Kéo để xoay · Shift + kéo để dời · Ctrl + cuộn để phóng to',
|
|
8
9
|
credit: 'Mô hình 3D: ',
|
|
9
10
|
ai: ' (hình minh họa do AI tạo, không phải mẫu thật)',
|
|
@@ -17,6 +18,8 @@ export default {
|
|
|
17
18
|
cut: 'Cắt dọc',
|
|
18
19
|
cutTitle: 'Cắt đôi mô hình để xem bên trong',
|
|
19
20
|
xray: 'Nhìn xuyên',
|
|
21
|
+
cutAt: 'Mặt cắt',
|
|
22
|
+
cutAtLabel: 'Dời mặt cắt qua mô hình',
|
|
20
23
|
xrayTitle: 'Làm vỏ trong suốt',
|
|
21
24
|
labels: 'Nhãn',
|
|
22
25
|
labelsTitle: 'Hiện tất cả nhãn',
|
|
@@ -28,5 +31,9 @@ export default {
|
|
|
28
31
|
side: 'Bên',
|
|
29
32
|
top: 'Trên',
|
|
30
33
|
home: 'Ban đầu',
|
|
34
|
+
steps: 'Các bước',
|
|
35
|
+
back: 'Bước trước',
|
|
36
|
+
next: 'Bước tiếp',
|
|
37
|
+
stepOf: 'Bước {n}/{count}',
|
|
31
38
|
decimal: ',',
|
|
32
39
|
};
|