lk3d 0.1.0 → 0.2.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 +35 -0
- package/README.md +19 -6
- package/bin/lk3d.mjs +24 -13
- package/dist/lk3d-export.min.js +1 -1
- package/dist/lk3d.min.js +55 -23
- package/dist/lk3d.mjs +55 -23
- package/docs/api.md +105 -24
- package/docs/design.md +10 -7
- package/docs/performance.md +41 -10
- package/docs/scene-format.md +30 -29
- package/index.d.ts +49 -2
- package/package.json +1 -3
- package/scene.d.ts +96 -1
- package/schema/scene.schema.json +165 -1
- package/skill/lk3d/SKILL.md +11 -1
- package/src/ao.js +58 -0
- package/src/controls.js +21 -2
- package/src/element.js +111 -0
- package/src/figure/api.js +12 -2
- package/src/figure/camera.js +49 -9
- package/src/figure/context.d.ts +23 -1
- package/src/figure/idle.js +57 -19
- package/src/figure/input.js +81 -7
- package/src/figure/labels.js +12 -9
- package/src/figure/loop.js +6 -2
- package/src/figure/pick.js +19 -5
- package/src/figure/steps.js +143 -0
- package/src/flow.js +33 -2
- package/src/index.js +13 -2
- package/src/locales/en.js +6 -0
- package/src/locales/vi.js +6 -0
- package/src/renderer-gl.js +85 -15
- package/src/shaders.js +39 -1
- package/src/stylesheet.js +10 -2
- package/src/validate.js +50 -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/camera.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
// The camera: where it stands and looks (fitted to the stage), the views and their glides, zooming towards a point,
|
|
3
3
|
// the cut's plane, boxes round parts, and the ray through a point of the stage. Owns viewProj_, eye_, frustum_,
|
|
4
|
-
// baseDist_, explodeShift_, camera_, project_, cutPlane_, boxOf_, shownBox_, rayAt_, zoom_, view_
|
|
5
|
-
//
|
|
4
|
+
// baseDist_, explodeShift_, camera_, project_, cutPlane_, cutRange_, boxOf_, frame_, shownBox_, rayAt_, zoom_, view_,
|
|
5
|
+
// viewAngles_ and cancelView_;
|
|
6
|
+
// reads state_, spec_, built_, width_, height_ and animations_, and calls offsetOf_, pickHit_, measure_, markDirty_ and kick_.
|
|
6
7
|
import { D2R, M, sub, add, mulv, len, norm, cross, dot } from '../math.js';
|
|
7
8
|
import { clock } from '../clock.js';
|
|
8
9
|
import LK3D from '../namespace.js';
|
|
@@ -48,11 +49,9 @@ export function makeCamera(c) {
|
|
|
48
49
|
);
|
|
49
50
|
return out;
|
|
50
51
|
}
|
|
51
|
-
function fitNow() {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
ortho = c.state_.projection === 'ortho',
|
|
55
|
-
key = width / height + (ortho ? 'o' : 'p');
|
|
52
|
+
function fitNow(aspect = c.width_ / c.height_) {
|
|
53
|
+
const ortho = c.state_.projection === 'ortho',
|
|
54
|
+
key = aspect + (ortho ? 'o' : 'p');
|
|
56
55
|
if (key === fitFor) return fit;
|
|
57
56
|
fitFor = key;
|
|
58
57
|
const cosPitch = Math.cos(home.pitch),
|
|
@@ -66,12 +65,29 @@ export function makeCamera(c) {
|
|
|
66
65
|
need = Math.max(
|
|
67
66
|
need,
|
|
68
67
|
(ortho ? 0 : dot(fromTarget, back)) +
|
|
69
|
-
Math.max(Math.abs(dot(fromTarget, up)), Math.abs(dot(fromTarget, right)) /
|
|
68
|
+
Math.max(Math.abs(dot(fromTarget, up)), Math.abs(dot(fromTarget, right)) / aspect) / tanHalfFov,
|
|
70
69
|
);
|
|
71
70
|
}
|
|
72
71
|
// (and, as before, on a stage squarer than the scene's own proportions (a phone), back far enough for its width)
|
|
73
|
-
return (fit = Math.max(1, (c.spec_.w || 16) / (c.spec_.h || 10) /
|
|
72
|
+
return (fit = Math.max(1, (c.spec_.w || 16) / (c.spec_.h || 10) / aspect, need / home.dist));
|
|
74
73
|
}
|
|
74
|
+
// where the camera looks and from how far (as the state holds them: before the fit to the stage, the step back and
|
|
75
|
+
// the shift when taken apart) to frame some parts as they are once taken apart by `explode`: the sphere round them
|
|
76
|
+
// inside the frame, however narrow it is
|
|
77
|
+
c.frame_ = (parts, explode = c.state_.explode) => {
|
|
78
|
+
if (c.width_ <= 1) c.measure_(); // (not laid out yet: a figure just mounted)
|
|
79
|
+
const now = c.state_.explode;
|
|
80
|
+
c.state_.explode = explode;
|
|
81
|
+
const box = c.boxOf_(parts);
|
|
82
|
+
c.state_.explode = now;
|
|
83
|
+
const aspect = c.width_ > 1 && c.height_ > 1 ? c.width_ / c.height_ : (c.spec_.w || 16) / (c.spec_.h || 10),
|
|
84
|
+
tanHalf = (Math.tan(((c.spec_.view?.fov ?? 35) * D2R) / 2) * Math.min(1, aspect)) / 1.08, // (with a margin)
|
|
85
|
+
distance = c.state_.projection === 'ortho' ? box.radius / tanHalf : box.radius / Math.sin(Math.atan(tanHalf));
|
|
86
|
+
return {
|
|
87
|
+
target: sub(box.center, mulv(c.spec_.view?.explodeShift || [0, 0, 0], explode)),
|
|
88
|
+
dist: distance / fitNow(aspect) / (1 + (c.spec_.view?.explodeZoom ?? 0.5) * explode),
|
|
89
|
+
};
|
|
90
|
+
};
|
|
75
91
|
c.camera_ = () => {
|
|
76
92
|
const width = c.width_,
|
|
77
93
|
height = c.height_,
|
|
@@ -145,6 +161,28 @@ export function makeCamera(c) {
|
|
|
145
161
|
}
|
|
146
162
|
return { center: mulv(add(min, max), 0.5), radius: len(sub(max, min)) / 2 || 1, min, max };
|
|
147
163
|
};
|
|
164
|
+
// how far along the cut's normal the parts it cuts reach, as they are now: where the plane can go [from, to]
|
|
165
|
+
c.cutRange_ = () => {
|
|
166
|
+
const normal = norm(c.state_.section.normal);
|
|
167
|
+
let from = Infinity,
|
|
168
|
+
to = -Infinity;
|
|
169
|
+
for (const part of c.built_.parts)
|
|
170
|
+
if (part.cut && !part.hidden) {
|
|
171
|
+
const offset = c.offsetOf_(part);
|
|
172
|
+
for (let corner = 0; corner < 8; corner++) {
|
|
173
|
+
const along = dot(
|
|
174
|
+
normal,
|
|
175
|
+
add(
|
|
176
|
+
offset,
|
|
177
|
+
[0, 1, 2].map((axis) => ((corner >> axis) & 1 ? part.max[axis] : part.min[axis])),
|
|
178
|
+
),
|
|
179
|
+
);
|
|
180
|
+
from = Math.min(from, along);
|
|
181
|
+
to = Math.max(to, along);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return from <= to ? [from, to] : [-1, 1];
|
|
185
|
+
};
|
|
148
186
|
// a point of the scene on the stage, in CSS pixels (null behind the eye), by the camera as last placed
|
|
149
187
|
c.project_ = (point) => M.project(c.viewProj_, point, c.width_, c.height_);
|
|
150
188
|
c.rayAt_ = (x, y) => {
|
|
@@ -235,6 +273,8 @@ export function makeCamera(c) {
|
|
|
235
273
|
c.animations_.add(tween);
|
|
236
274
|
c.kick_();
|
|
237
275
|
};
|
|
276
|
+
// a named view's angles (degrees), else where the camera looks now
|
|
277
|
+
c.viewAngles_ = (name) => VIEWS[name] || [c.state_.yaw / D2R, c.state_.pitch / D2R];
|
|
238
278
|
c.cancelView_ = () => {
|
|
239
279
|
if (viewTween) c.animations_.delete(viewTween);
|
|
240
280
|
viewTween = null;
|
package/src/figure/context.d.ts
CHANGED
|
@@ -21,6 +21,8 @@ export interface Renderer {
|
|
|
21
21
|
upload?(batch: any): void;
|
|
22
22
|
instances?(batch: any): void;
|
|
23
23
|
release?(thing: any): void;
|
|
24
|
+
/** whether it places this flow's streaks itself, on the GPU (WebGL 2), so the frame loop need not */
|
|
25
|
+
placesFlow?(flow: any): boolean;
|
|
24
26
|
edges?(batch: any): void;
|
|
25
27
|
dispose(): void;
|
|
26
28
|
}
|
|
@@ -95,6 +97,8 @@ export interface Ctx {
|
|
|
95
97
|
treesMs: number | null;
|
|
96
98
|
aoMs: number | null;
|
|
97
99
|
refreshMs: number | null;
|
|
100
|
+
/** whether the crease shading was worked out in the page's worker (ao.js), not here */
|
|
101
|
+
aoWorker: boolean;
|
|
98
102
|
};
|
|
99
103
|
/** performance.now() when mount began (stats_ count from it) */
|
|
100
104
|
mountedAt_: number;
|
|
@@ -188,12 +192,18 @@ export interface Ctx {
|
|
|
188
192
|
project_(q: number[]): number[] | null;
|
|
189
193
|
/** the cut's plane [nx, ny, nz, -offset] */
|
|
190
194
|
cutPlane_(): number[];
|
|
195
|
+
/** how far along the cut's normal the parts it cuts reach now: where the plane can go [from, to] */
|
|
196
|
+
cutRange_(): number[];
|
|
191
197
|
/** the box round the parts drawn now */
|
|
192
198
|
shownBox_(): { center: number[]; radius: number; min: number[]; max: number[] };
|
|
193
199
|
boxOf_(ps: Part[]): { center: number[]; radius: number; min: number[]; max: number[] };
|
|
200
|
+
/** the target and distance (as the state holds them) that frame these parts, taken apart by explode */
|
|
201
|
+
frame_(ps: Part[], explode?: number): { target: number[]; dist: number };
|
|
194
202
|
rayAt_(px: number, py: number): { origin: number[]; direction: number[] };
|
|
195
203
|
zoom_(k: number, x?: number, y?: number): void;
|
|
196
204
|
view_(name: any, o?: { ms?: number; [key: string]: any }): void;
|
|
205
|
+
/** a named view's [yaw, pitch] in degrees, else the camera's now */
|
|
206
|
+
viewAngles_(name: string): number[];
|
|
197
207
|
cancelView_(): void;
|
|
198
208
|
|
|
199
209
|
// ---- dom.js
|
|
@@ -212,6 +222,8 @@ export interface Ctx {
|
|
|
212
222
|
/** the stage's size, CSS pixels */
|
|
213
223
|
width_: number;
|
|
214
224
|
height_: number;
|
|
225
|
+
/** measure (and shape) the stage now */
|
|
226
|
+
measure_(): void;
|
|
215
227
|
dpr_: number;
|
|
216
228
|
/** the share of full resolution drawn (lowered while frames come late) */
|
|
217
229
|
resolution_: number;
|
|
@@ -241,7 +253,7 @@ export interface Ctx {
|
|
|
241
253
|
px: number,
|
|
242
254
|
py: number,
|
|
243
255
|
see?: number,
|
|
244
|
-
): { id: string | null; t: number; origin: number[]; direction: number[] };
|
|
256
|
+
): { id: string | null; t: number; origin: number[]; direction: number[]; onCut: boolean };
|
|
245
257
|
pickAt_(px: number, py: number, see?: number): string | null;
|
|
246
258
|
|
|
247
259
|
// ---- idle.js
|
|
@@ -257,6 +269,10 @@ export interface Ctx {
|
|
|
257
269
|
setHandle_(grip: Grip, v: number, byReader: boolean): void;
|
|
258
270
|
/** a hidden part's handle out of the Tab order and of what screen readers read */
|
|
259
271
|
syncHandleInputs_(): void;
|
|
272
|
+
/** whether the reader may move the cut now (it is on, and the scene does not fix it) */
|
|
273
|
+
cutMovable_(): boolean;
|
|
274
|
+
/** the cut moved by the reader (held within the parts it cuts; onSection called) */
|
|
275
|
+
moveCut_(offset: number): void;
|
|
260
276
|
|
|
261
277
|
// ---- additions.js
|
|
262
278
|
addParticles_(o: any): any;
|
|
@@ -266,6 +282,12 @@ export interface Ctx {
|
|
|
266
282
|
|
|
267
283
|
// ---- api.js
|
|
268
284
|
api_: any;
|
|
285
|
+
|
|
286
|
+
// ---- steps.js
|
|
287
|
+
/** the step shown (from 0); -1 for a scene without steps */
|
|
288
|
+
stepAt_: number;
|
|
289
|
+
/** go to a step by its place (from 0) or id; ms: 0 sets it at once */
|
|
290
|
+
goToStep_(which: number | string, o?: { ms?: number }): void;
|
|
269
291
|
}
|
|
270
292
|
|
|
271
293
|
declare global {
|
package/src/figure/idle.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
// The work done in idle slices after the first picture, a few milliseconds at a time: the trees for picking, the crease
|
|
3
3
|
// shading (ambient occlusion) and the pen look's edges; and whether the figure is complete (checkReady_: its `ready`).
|
|
4
|
-
// Owns aoComplete_, showEdges_, findEdges_ and checkReady_; reads built_, spec_, opts_, stats_, mountedAt_, renderer_,
|
|
4
|
+
// Owns aoComplete_, showEdges_, findEdges_ and checkReady_ (the crease shading in the page's worker: ao.js); reads built_, spec_, opts_, stats_, mountedAt_, renderer_,
|
|
5
5
|
// look_, destroyed_, resolveReady_ and api_, and calls bvhOf_ and kick_.
|
|
6
6
|
import { sub, len } from '../math.js';
|
|
7
7
|
import { edgesOf } from '../geometry.js';
|
|
8
8
|
import { aoJob } from '../bvh.js';
|
|
9
|
+
import { shadeAway } from '../ao.js';
|
|
9
10
|
|
|
10
11
|
/** @param {import('./context').Ctx} c */
|
|
11
12
|
export function makeIdle(c) {
|
|
@@ -47,30 +48,44 @@ export function makeIdle(c) {
|
|
|
47
48
|
setTimeout(build, 30);
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
// ---- ambient occlusion: creases and the insides of hollow parts darkened, by rays against
|
|
51
|
+
// ---- ambient occlusion: creases and the insides of hollow parts darkened, by rays against each shape's tree, then
|
|
52
|
+
// drawn. Each shape is sent to the page's worker (ao.js) as it comes up, and drawn when its shading comes back; where
|
|
53
|
+
// there is no worker (or it stops), shaded here in idle slices, a few milliseconds at a time
|
|
51
54
|
if (c.renderer_.kind !== '2d' && c.opts_.ao !== false) {
|
|
52
55
|
const queue = batches.slice(),
|
|
53
|
-
aoByMesh = new Map()
|
|
56
|
+
aoByMesh = new Map(),
|
|
57
|
+
away = new Map(); // a mesh in the worker → the batches waiting for its shading
|
|
54
58
|
/** @type {{ batch: any, job: ReturnType<typeof aoJob> } | null} */
|
|
55
|
-
let current = null
|
|
59
|
+
let current = null, // (the batch being shaded here, and its job)
|
|
60
|
+
working = false;
|
|
61
|
+
const shaded = (batch, out) => {
|
|
62
|
+
batch.ao = out;
|
|
63
|
+
aoByMesh.set(batch.mesh, out);
|
|
64
|
+
c.renderer_.setAO(batch);
|
|
65
|
+
};
|
|
66
|
+
const finish = () => {
|
|
67
|
+
if (aoDone || queue.length || current || away.size) return;
|
|
68
|
+
c.aoComplete_ = aoDone = true;
|
|
69
|
+
c.stats_.aoMs = performance.now() - c.mountedAt_;
|
|
70
|
+
c.kick_();
|
|
71
|
+
c.checkReady_();
|
|
72
|
+
};
|
|
56
73
|
const work = () => {
|
|
74
|
+
working = false;
|
|
57
75
|
if (c.destroyed_) return;
|
|
58
76
|
const end = performance.now() + 6;
|
|
59
77
|
while (performance.now() < end) {
|
|
60
78
|
if (!current) {
|
|
61
79
|
const batch = queue.shift();
|
|
62
|
-
if (batch
|
|
80
|
+
if (!batch) return finish();
|
|
81
|
+
if (aoByMesh.has(batch.mesh)) {
|
|
63
82
|
// (a mesh shared by parts alike: its crease shading is the same, made once)
|
|
64
|
-
batch
|
|
65
|
-
c.renderer_.setAO(batch);
|
|
83
|
+
shaded(batch, aoByMesh.get(batch.mesh));
|
|
66
84
|
continue;
|
|
67
85
|
}
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
c.kick_();
|
|
72
|
-
c.checkReady_();
|
|
73
|
-
return;
|
|
86
|
+
if (away.has(batch.mesh)) {
|
|
87
|
+
away.get(batch.mesh).push(batch);
|
|
88
|
+
continue;
|
|
74
89
|
}
|
|
75
90
|
if (batch.mesh.count > 40000) continue; // a very detailed shape: its own folds already read; spare the phone the rays
|
|
76
91
|
const mesh = batch.mesh;
|
|
@@ -82,19 +97,42 @@ export function makeIdle(c) {
|
|
|
82
97
|
hi[axis] = Math.max(hi[axis], mesh.positions[3 * vertex + axis]);
|
|
83
98
|
}
|
|
84
99
|
// (rays as long as a quarter of the shape, within limits: creases darken, not the whole of a big part)
|
|
85
|
-
const reach = Math.max(0.02, Math.min(len(sub(hi, lo)) * 0.25, c.built_.radius * 0.12))
|
|
100
|
+
const reach = Math.max(0.02, Math.min(len(sub(hi, lo)) * 0.25, c.built_.radius * 0.12)),
|
|
101
|
+
inWorker = shadeAway(mesh, reach);
|
|
102
|
+
if (inWorker) {
|
|
103
|
+
away.set(mesh, [batch]);
|
|
104
|
+
inWorker.then(
|
|
105
|
+
(out) => {
|
|
106
|
+
if (c.destroyed_) return;
|
|
107
|
+
c.stats_.aoWorker = true;
|
|
108
|
+
for (const each of away.get(mesh)) shaded(each, out);
|
|
109
|
+
away.delete(mesh);
|
|
110
|
+
c.kick_();
|
|
111
|
+
finish();
|
|
112
|
+
},
|
|
113
|
+
() => {
|
|
114
|
+
// (the worker stopped: its batches shaded here)
|
|
115
|
+
queue.push(...away.get(mesh));
|
|
116
|
+
away.delete(mesh);
|
|
117
|
+
start();
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
86
122
|
current = { batch, job: aoJob(mesh, c.bvhOf_(batch), reach) };
|
|
87
123
|
}
|
|
88
124
|
if (current.job.step(Math.max(1, end - performance.now()))) {
|
|
89
|
-
current.batch
|
|
90
|
-
aoByMesh.set(current.batch.mesh, current.job.out);
|
|
91
|
-
c.renderer_.setAO(current.batch);
|
|
125
|
+
shaded(current.batch, current.job.out);
|
|
92
126
|
current = null;
|
|
93
127
|
}
|
|
94
128
|
}
|
|
95
|
-
|
|
129
|
+
start(16);
|
|
130
|
+
};
|
|
131
|
+
const start = (ms = 0) => {
|
|
132
|
+
if (!working) setTimeout(work, ms);
|
|
133
|
+
working = true;
|
|
96
134
|
};
|
|
97
|
-
|
|
135
|
+
start(60);
|
|
98
136
|
}
|
|
99
137
|
|
|
100
138
|
// ---- edges: drawn in the pen look (and in any look with the scene's edges: true; never with edges: false), found in
|
package/src/figure/input.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
// The reader's hands on a figure: the pointer (orbit, pan, pinch, hover, pick, grabbing a handle and dragging it),
|
|
3
3
|
// the wheel (only once the figure is taken hold of), the keys (turn, zoom, go through the parts), and each handle as
|
|
4
|
-
// a range input (its value set by the reader or the page: setHandle_)
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
|
|
4
|
+
// a range input (its value set by the reader or the page: setHandle_), and the cut moved by the reader (its face
|
|
5
|
+
// dragged, or the built-in slider: moveCut_). Owns setHandle_, syncHandleInputs_, cutMovable_ and moveCut_; reads
|
|
6
|
+
// host_, canvas_, spec_, state_, opts_, words_, stage_, hint_, namedParts_, handles_, animations_, controls_ and
|
|
7
|
+
// height_, and calls pickAt_, pickHit_, rayAt_, project_, cutPlane_, cutRange_, baseMatrix_, zoom_, cancelView_,
|
|
8
|
+
// showInfo_, part_, markDirty_ and kick_.
|
|
9
|
+
import { M, sub, add, mulv, len, dot, cross, norm } from '../math.js';
|
|
8
10
|
import * as HD from '../handles.js';
|
|
9
11
|
import { plainText } from './dom.js';
|
|
10
12
|
import LK3D from '../namespace.js';
|
|
@@ -116,6 +118,45 @@ export function makeInput(c) {
|
|
|
116
118
|
}
|
|
117
119
|
c.setHandle_(grip, value, true);
|
|
118
120
|
}
|
|
121
|
+
// ---- the cut moved by the reader: its face taken hold of and dragged along the plane's normal (unless the scene
|
|
122
|
+
// fixes it: section.fixed). A drag reads depth by the normal's direction on the stage, or by up and down where the
|
|
123
|
+
// cut face is seen nearly face on (the normal pointing at the eye has no direction on the stage)
|
|
124
|
+
c.cutMovable_ = () =>
|
|
125
|
+
!!c.state_.cut && c.spec_.section !== false && !c.spec_.section?.fixed && c.renderer_.kind !== '2d';
|
|
126
|
+
c.moveCut_ = (offset) => {
|
|
127
|
+
const [from, to] = c.cutRange_(),
|
|
128
|
+
held = Math.max(from, Math.min(to, offset));
|
|
129
|
+
if (held === c.state_.section.offset) return;
|
|
130
|
+
c.state_.section = { ...c.state_.section, offset: held };
|
|
131
|
+
if (c.controls_.style) c.controls_.style(); // (the slider follows)
|
|
132
|
+
c.kick_();
|
|
133
|
+
if (c.opts_.onSection) {
|
|
134
|
+
const [x, y, z] = c.state_.section.normal;
|
|
135
|
+
c.opts_.onSection({ normal: [x, y, z], offset: held });
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
/** @type {{ id: number, x: number, y: number, from: number, along: number[], moved: number, part: string } | null} */
|
|
139
|
+
let cutting = null;
|
|
140
|
+
/** @param {number} x @param {number} y */
|
|
141
|
+
function cutAt(x, y) {
|
|
142
|
+
if (!c.cutMovable_()) return null;
|
|
143
|
+
const hit = c.pickHit_(x, y);
|
|
144
|
+
if (!hit.onCut || !hit.id) return null;
|
|
145
|
+
const plane = c.cutPlane_(),
|
|
146
|
+
normal = plane.slice(0, 3),
|
|
147
|
+
facing = dot(normal, hit.direction);
|
|
148
|
+
if (Math.abs(facing) < 1e-9) return null;
|
|
149
|
+
const point = add(hit.origin, mulv(hit.direction, -(dot(normal, hit.origin) + plane[3]) / facing)),
|
|
150
|
+
across = norm(cross(normal, Math.abs(normal[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0])),
|
|
151
|
+
here = c.project_(point),
|
|
152
|
+
deeper = c.project_(add(point, normal)),
|
|
153
|
+
aside = c.project_(add(point, across));
|
|
154
|
+
if (!here || !deeper || !aside) return null;
|
|
155
|
+
const perUnit = Math.hypot(aside[0] - here[0], aside[1] - here[1]); // (pixels a scene unit, across the cut)
|
|
156
|
+
let along = [deeper[0] - here[0], deeper[1] - here[1]];
|
|
157
|
+
if (Math.hypot(along[0], along[1]) < 0.3 * perUnit) along = [0, -perUnit];
|
|
158
|
+
return { x, y, from: c.state_.section.offset, along, moved: 0, part: hit.id };
|
|
159
|
+
}
|
|
119
160
|
const capture = (pointerId) => {
|
|
120
161
|
try {
|
|
121
162
|
c.canvas_.setPointerCapture(pointerId);
|
|
@@ -131,7 +172,18 @@ export function makeInput(c) {
|
|
|
131
172
|
}
|
|
132
173
|
if (!pointers.size) {
|
|
133
174
|
const [x, y] = local(event),
|
|
134
|
-
grip = handleAt(x, y)
|
|
175
|
+
grip = handleAt(x, y),
|
|
176
|
+
cut = grip ? null : cutAt(x, y);
|
|
177
|
+
if (cut) {
|
|
178
|
+
// the reader takes hold of the cut face: the plane moves, the view does not
|
|
179
|
+
cutting = { ...cut, id: event.pointerId };
|
|
180
|
+
capture(event.pointerId);
|
|
181
|
+
c.canvas_.style.cursor = 'grabbing';
|
|
182
|
+
c.hint_.classList.add('gone');
|
|
183
|
+
setActive(true);
|
|
184
|
+
event.preventDefault();
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
135
187
|
if (grip) {
|
|
136
188
|
// the reader takes hold of a handle: it moves, the view does not
|
|
137
189
|
grab = {
|
|
@@ -160,6 +212,15 @@ export function makeInput(c) {
|
|
|
160
212
|
c.hint_.classList.add('gone');
|
|
161
213
|
});
|
|
162
214
|
c.canvas_.addEventListener('pointermove', (event) => {
|
|
215
|
+
if (cutting && event.pointerId === cutting.id) {
|
|
216
|
+
const [x, y] = local(event),
|
|
217
|
+
dx = x - cutting.x,
|
|
218
|
+
dy = y - cutting.y,
|
|
219
|
+
along = cutting.along;
|
|
220
|
+
cutting.moved = Math.max(cutting.moved, Math.abs(dx) + Math.abs(dy));
|
|
221
|
+
c.moveCut_(cutting.from + (dx * along[0] + dy * along[1]) / (along[0] * along[0] + along[1] * along[1]));
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
163
224
|
if (grab && event.pointerId === grab.id) {
|
|
164
225
|
const [x, y] = local(event);
|
|
165
226
|
grab.moved += Math.abs(event.movementX || 0) + Math.abs(event.movementY || 0);
|
|
@@ -201,8 +262,10 @@ export function makeInput(c) {
|
|
|
201
262
|
// (hovering: what is under the pointer, at most every 40 ms)
|
|
202
263
|
lastHover = performance.now();
|
|
203
264
|
const [x, y] = local(event),
|
|
204
|
-
|
|
205
|
-
|
|
265
|
+
hit = c.pickHit_(x, y),
|
|
266
|
+
id = hit.id;
|
|
267
|
+
// (a handle, or a cut face the reader may move: grabbed, not chosen)
|
|
268
|
+
const onHandle = (id ? !!c.part_(id)?.handleOf : !!handleAt(x, y)) || (hit.onCut && c.cutMovable_());
|
|
206
269
|
if (id !== c.state_.hover || onHandle !== wasOnHandle) {
|
|
207
270
|
c.state_.hover = id;
|
|
208
271
|
wasOnHandle = onHandle;
|
|
@@ -213,6 +276,17 @@ export function makeInput(c) {
|
|
|
213
276
|
}
|
|
214
277
|
});
|
|
215
278
|
const end = (event) => {
|
|
279
|
+
if (cutting && event.pointerId === cutting.id) {
|
|
280
|
+
// let go of the cut; a tap without moving chooses the part, as elsewhere
|
|
281
|
+
if (cutting.moved < 4 && event.type === 'pointerup') {
|
|
282
|
+
c.state_.selected = cutting.part === c.state_.selected ? null : cutting.part;
|
|
283
|
+
c.showInfo_('pick');
|
|
284
|
+
}
|
|
285
|
+
cutting = null;
|
|
286
|
+
c.canvas_.style.cursor = 'grab';
|
|
287
|
+
c.kick_();
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
216
290
|
if (grab && event.pointerId === grab.id) {
|
|
217
291
|
// let go of a handle; a tap without moving selects the part, as elsewhere
|
|
218
292
|
if (grab.moved < 4 && event.type === 'pointerup') {
|
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_, 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,18 @@ 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;
|
|
128
129
|
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
|
-
|
|
135
|
-
|
|
130
|
+
// the part's anchor on the stage, if its label is wanted (a spinning part's label stays put), and not cut away
|
|
131
|
+
// (a label pointing at nothing once the cut has passed its place)
|
|
132
|
+
const anchor = M.pt(
|
|
133
|
+
tag.part.spin || tag.part.handleOf?.round ? c.baseMatrix_(tag.part) : tag.part.matrix,
|
|
134
|
+
tag.part.anchor,
|
|
135
|
+
),
|
|
136
|
+
cutAway =
|
|
137
|
+
plane && tag.part.cut && plane[0] * anchor[0] + plane[1] * anchor[1] + plane[2] * anchor[2] + plane[3] > 0;
|
|
138
|
+
const at = which !== 'none' && wanted(tag.part) && !cutAway && c.project_(anchor);
|
|
136
139
|
if (!at) {
|
|
137
140
|
show(tag, false);
|
|
138
141
|
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;
|
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_ and cutPlane_.
|
|
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)
|
|
@@ -51,7 +61,8 @@ export function makePick(c) {
|
|
|
51
61
|
plane = c.cutPlane_();
|
|
52
62
|
let best = Infinity,
|
|
53
63
|
/** @type {string | null} */
|
|
54
|
-
id = null
|
|
64
|
+
id = null,
|
|
65
|
+
onCut = false;
|
|
55
66
|
for (const part of c.built_.parts) {
|
|
56
67
|
if (part.drawAlpha < minAlpha || rayBox(origin, direction, part) >= best) continue; // (missed by the ray, or behind a nearer hit)
|
|
57
68
|
for (const batch of part.batches)
|
|
@@ -72,10 +83,13 @@ export function makePick(c) {
|
|
|
72
83
|
if (hit && hit.t < best) {
|
|
73
84
|
best = hit.t;
|
|
74
85
|
id = part.id;
|
|
86
|
+
// (a cut part's surface seen from inside, its outward face turned away from the eye: the ray came in
|
|
87
|
+
// through the cut, so what is seen there is the cut face, filled at the plane)
|
|
88
|
+
onCut = !!accept && facesAway(batch.mesh, hit.triangle, shapeDirection);
|
|
75
89
|
}
|
|
76
90
|
}
|
|
77
91
|
}
|
|
78
|
-
return { id, t: best, origin, direction };
|
|
92
|
+
return { id, t: best, origin, direction, onCut };
|
|
79
93
|
};
|
|
80
94
|
c.pickAt_ = (x, y, minAlpha) => c.pickHit_(x, y, minAlpha).id;
|
|
81
95
|
}
|