lk3d 0.2.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/src/index.js CHANGED
@@ -27,6 +27,7 @@ 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';
@@ -86,6 +87,7 @@ function mount(el, spec, opts = {}) {
86
87
  cut: false,
87
88
  section: { normal: [0, 0, 1], offset: 0, ...spec.section }, // (the cut's plane: the scene's, moved by the page)
88
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)
89
91
  xray: false,
90
92
  yaw: (spec.view?.yaw ?? -35) * D2R,
91
93
  pitch: (spec.view?.pitch ?? 18) * D2R,
@@ -143,6 +145,7 @@ function mount(el, spec, opts = {}) {
143
145
  const startWatching = makeLoop(c);
144
146
  makeLook(c);
145
147
  makeLabels(c);
148
+ makeDimensions(c);
146
149
  makePick(c);
147
150
  makeIdle(c);
148
151
  const makeHandleInputs = makeInput(c);
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)',
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)',
@@ -746,7 +746,8 @@ function glRenderer(gl, scene, options = {}) {
746
746
  changed.put(frame.cut && part.cut);
747
747
  changed.put(part.batches.reduce((sum, batch) => sum + (batch.version || 0), 0));
748
748
  }
749
- for (const value of [...frame.plane, frame.bounds.radius, frame.cut, loaded, ...extra]) changed.put(value);
749
+ for (const value of [...frame.plane, ...(frame.plane2 || []), frame.bounds.radius, frame.cut, loaded, ...extra])
750
+ changed.put(value);
750
751
  return changed.end();
751
752
  }
752
753
  // the glow: emission drawn at half resolution, blurred at a quarter, added onto the picture
@@ -838,6 +839,7 @@ function glRenderer(gl, scene, options = {}) {
838
839
  if (uniforms.uVP) gl.uniformMatrix4fv(uniforms.uVP, false, viewProjection || frame.viewProjection);
839
840
  if (uniforms.uEye) gl.uniform3fv(uniforms.uEye, frame.eye);
840
841
  if (uniforms.uPlane) gl.uniform4fv(uniforms.uPlane, frame.plane);
842
+ if (uniforms.uPlane2) gl.uniform4fv(uniforms.uPlane2, frame.plane2 || [0, 0, 0, 1]); // (none: always beyond it)
841
843
  if (uniforms.uCut) gl.uniform1f(uniforms.uCut, frame.cut ? 1 : 0);
842
844
  if (uniforms.uOutline) gl.uniform1f(uniforms.uOutline, 0);
843
845
  if (uniforms.uViewD) gl.uniform1f(uniforms.uViewD, frame.viewDistance || 0);
package/src/shaders.js CHANGED
@@ -64,7 +64,7 @@ void main() {
64
64
  // way back towards the wall: where two parts' sections overlap, the nearer wall's wins, steadily.
65
65
  const PART_FS = `
66
66
  uniform vec3 uColor; uniform float uAlpha; uniform float uFlat; uniform vec3 uMat; // (uColor: the outline's)
67
- uniform vec3 uLight; uniform vec3 uEye; uniform vec4 uPlane; uniform vec3 uCap;
67
+ uniform vec3 uLight; uniform vec3 uEye; uniform vec4 uPlane; uniform vec4 uPlane2; uniform vec3 uCap;
68
68
  // (a cap's hatching lies on the cut's plane, every uHatch units: uFwd and uOrtho give the ray from the eye)
69
69
  uniform vec3 uFwd; uniform float uOrtho; uniform float uHatch;
70
70
  uniform float uDpr; uniform float uAOk; uniform vec3 uPaper; uniform vec3 uSolid;
@@ -139,7 +139,7 @@ vec2 envBRDF(float NoV, float r) { // the split-sum environment term, as Karis
139
139
  #endif
140
140
  void main() {
141
141
  float cut = step(1.5, vIE.w), hi = step(0.5, vIE.w - 2.0 * cut);
142
- if (cut > 0.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0) discard;
142
+ if (cut > 0.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0 && dot(uPlane2.xyz, vW) + uPlane2.w > 0.0) discard; // (beyond both planes: one, when uPlane2 is (0, 0, 0, 1))
143
143
  #ifdef CUTS
144
144
  if (uTex.x > 0.5 && texture2D(uBase, vUV).a < uTex.w) discard; // a cut-out's clear texels
145
145
  #endif
@@ -151,10 +151,18 @@ void main() {
151
151
  // behind open space, a bore, is hidden by the nearer wall), hatched where the ray to it crosses the cut's plane,
152
152
  // in the plane's own coordinates: the hatching stays put on the section as the camera drifts
153
153
  vec3 rd = uOrtho > 0.5 ? uFwd : vW - uEye;
154
- float den = dot(uPlane.xyz, rd);
155
- vec3 P = abs(den) > 1e-6 ? vW - rd * ((dot(uPlane.xyz, vW) + uPlane.w) / den) : vW;
156
- vec3 ax = normalize(cross(uPlane.xyz, abs(uPlane.y) < 0.9 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0)));
157
- float hs = (dot(P, ax) + dot(P, cross(uPlane.xyz, ax))) / max(uHatch, 1e-4), h = step(0.55, fract(hs));
154
+ vec4 pl = uPlane;
155
+ float den = dot(pl.xyz, rd);
156
+ vec3 P = abs(den) > 1e-6 ? vW - rd * ((dot(pl.xyz, vW) + pl.w) / den) : vW;
157
+ // (a quarter cut: the face the ray left the cut-away wedge through; the second plane's where the first's crossing
158
+ // lies outside the wedge)
159
+ if (dot(uPlane2.xyz, P) + uPlane2.w < 0.0) {
160
+ pl = uPlane2;
161
+ den = dot(pl.xyz, rd);
162
+ P = abs(den) > 1e-6 ? vW - rd * ((dot(pl.xyz, vW) + pl.w) / den) : vW;
163
+ }
164
+ vec3 ax = normalize(cross(pl.xyz, abs(pl.y) < 0.9 ? vec3(0.0, 1.0, 0.0) : vec3(1.0, 0.0, 0.0)));
165
+ float hs = (dot(P, ax) + dot(P, cross(pl.xyz, ax))) / max(uHatch, 1e-4), h = step(0.55, fract(hs));
158
166
  #ifdef GL2
159
167
  h = mix(h, 0.45, smoothstep(0.15, 0.4, fwidth(hs))); // (lines too fine for the pixels: their even tone, no moiré)
160
168
  #endif
@@ -275,12 +283,12 @@ void main() {
275
283
  }`;
276
284
 
277
285
  // ---- depth only, for the shadow map (and a cut-out's clear texels casting none)
278
- const DEPTH_FS = `uniform vec4 uPlane; varying vec3 vW; varying vec4 vIE;
286
+ const DEPTH_FS = `uniform vec4 uPlane; uniform vec4 uPlane2; varying vec3 vW; varying vec4 vIE;
279
287
  #ifdef CUTS
280
288
  uniform sampler2D uBase; uniform vec4 uTex; varying vec2 vUV;
281
289
  #endif
282
290
  void main() {
283
- if (vIE.w > 1.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0) discard;
291
+ if (vIE.w > 1.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0 && dot(uPlane2.xyz, vW) + uPlane2.w > 0.0) discard;
284
292
  #ifdef CUTS
285
293
  if (uTex.x > 0.5 && texture2D(uBase, vUV).a < uTex.w) discard;
286
294
  #endif
@@ -288,8 +296,8 @@ void main() {
288
296
  }`;
289
297
 
290
298
  // ---- emission only, for the glow
291
- const GLOW_FS = `uniform vec4 uPlane; varying vec3 vW; varying vec4 vIE;
292
- void main() { if (vIE.w > 1.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0) discard; gl_FragColor = vec4(vIE.rgb, 1.0); }`;
299
+ const GLOW_FS = `uniform vec4 uPlane; uniform vec4 uPlane2; varying vec3 vW; varying vec4 vIE;
300
+ void main() { if (vIE.w > 1.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0 && dot(uPlane2.xyz, vW) + uPlane2.w > 0.0) discard; gl_FragColor = vec4(vIE.rgb, 1.0); }`;
293
301
 
294
302
  // ---- the ground: a disc under the model that only shows the shadow (and a soft contact shadow)
295
303
  const GROUND_VS = `attribute vec4 aPos; uniform mat4 uVP; uniform vec3 uCenter; uniform float uRadius; uniform float uY;
@@ -379,9 +387,9 @@ void main() {
379
387
  gl_Position = a; vA = aA; vSide = aPos.w; vT = aDir.w;
380
388
  }`;
381
389
  const FLOW_FS = `uniform vec3 uC0; uniform vec3 uC1; uniform vec3 uC2; uniform vec3 uC3; uniform float uAlpha;
382
- uniform vec4 uPlane; uniform float uCut; varying float vA; varying float vSide; varying float vT; varying vec3 vW;
390
+ uniform vec4 uPlane; uniform vec4 uPlane2; uniform float uCut; varying float vA; varying float vSide; varying float vT; varying vec3 vW;
383
391
  void main() {
384
- if (uCut > 0.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0) discard; // cut away with the parts it flows through
392
+ if (uCut > 0.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0 && dot(uPlane2.xyz, vW) + uPlane2.w > 0.0) discard; // cut away with the parts it flows through
385
393
  float x = clamp(vT, 0.0, 1.0) * 3.0;
386
394
  vec3 c = x < 1.0 ? mix(uC0, uC1, x) : x < 2.0 ? mix(uC1, uC2, x - 1.0) : mix(uC2, uC3, x - 2.0);
387
395
  float a = uAlpha * vA * (1.0 - smoothstep(0.3, 1.0, abs(vSide)));
@@ -405,10 +413,10 @@ void main() {
405
413
  gl_Position = a; vSide = aPos.w; vW = w.xyz; vLit = dot(normalize(mat3(m) * aFace), uLight);
406
414
  vTint = aIC.a; vCut = aIE.w;
407
415
  }`;
408
- const EDGE_FS = `uniform vec3 uColor; uniform vec3 uLit; uniform float uAlpha; uniform vec4 uPlane;
416
+ const EDGE_FS = `uniform vec3 uColor; uniform vec3 uLit; uniform float uAlpha; uniform vec4 uPlane; uniform vec4 uPlane2;
409
417
  varying float vSide; varying vec3 vW; varying float vLit; varying float vTint; varying float vCut;
410
418
  void main() {
411
- if (vCut > 1.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0) discard;
419
+ if (vCut > 1.5 && dot(uPlane.xyz, vW) + uPlane.w > 0.0 && dot(uPlane2.xyz, vW) + uPlane2.w > 0.0) discard;
412
420
  // a lit part's edges light (an accent, a glowing one, the one chosen: as if catching the light); and an edge whose
413
421
  // faces turn to the light lighter, one turned away darker (a bevel catching the light)
414
422
  vec3 c = vTint > 0.5 ? uLit : uColor;
@@ -427,7 +435,7 @@ const STREAM_FS = `uniform vec4 uPts[16]; uniform vec3 uTab[24]; uniform int uN;
427
435
  uniform vec3 uC0; uniform vec3 uC1; uniform vec3 uC2; uniform vec3 uC3;
428
436
  uniform vec3 uEye; uniform vec3 uFwd; uniform float uOrtho; uniform mat4 uVP;
429
437
  uniform sampler2D uDepth; uniform float uHasDepth; uniform vec2 uView;
430
- uniform vec4 uF; uniform vec3 uG; uniform float uDen; uniform vec4 uPlane; uniform float uCut; uniform vec3 uLo; uniform vec3 uHi;
438
+ uniform vec4 uF; uniform vec3 uG; uniform float uDen; uniform vec4 uPlane; uniform vec4 uPlane2; uniform float uCut; uniform vec3 uLo; uniform vec3 uHi;
431
439
  varying vec3 vW; // uF: how far the air has travelled, the field's scale along the path, reach (how far a ray crosses),
432
440
  // turbulence; uG: swirl, the path's length, where it fades; uTab: inner and outer radius and travel time along the path
433
441
  float h3(vec3 p) { p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }
@@ -475,7 +483,7 @@ void main() {
475
483
  vec4 acc = vec4(0.0);
476
484
  for (int i = 0; i < 48; i++) {
477
485
  vec3 p = vW - dir * st * (float(i) + j);
478
- if (uCut > 0.5 && dot(uPlane.xyz, p) + uPlane.w > 0.0) continue;
486
+ if (uCut > 0.5 && dot(uPlane.xyz, p) + uPlane.w > 0.0 && dot(uPlane2.xyz, p) + uPlane2.w > 0.0) continue;
479
487
  vec4 cp = uVP * vec4(p, 1.0);
480
488
  if (cp.z / cp.w * 0.5 + 0.5 > zs) continue;
481
489
  float a = min(1.0, dens(p, f) * st * 2.0 * uDen);
package/src/stylesheet.js CHANGED
@@ -22,13 +22,15 @@ lk3d-figure{display:block}
22
22
  .lk3d-lines{position:absolute; inset:0; width:100%; height:100%; pointer-events:none; overflow:visible}
23
23
  .lk3d-lines line{stroke:var(--lk3d-ink, #1d1a1e); stroke-width:1.1}
24
24
  .lk3d-lines circle{fill:var(--lk3d-ink, #1d1a1e)}
25
+ .lk3d-lines path.lk3d-dim{fill:none; stroke:var(--lk3d-ink, #1d1a1e); stroke-width:1}
26
+ .lk3d-dimtext{position:absolute; left:0; top:0; font:12.5px/1.25 system-ui, sans-serif; color:var(--lk3d-ink, #1d1a1e); background:var(--lk3d-paper, #f7f5ef); padding:0 3px; border-radius:3px; white-space:nowrap}
25
27
  .lk3d-tags{position:absolute; inset:0; pointer-events:none}
26
28
  .lk3d-tag{position:absolute; left:0; top:0; will-change:transform; white-space:nowrap; font-family:var(--lk3d-font, system-ui, sans-serif); font-size:14px; line-height:20px; padding:0 7px;
27
29
  background:var(--lk3d-paper, #f7f5ef); color:var(--lk3d-ink, #1d1a1e); border:1.2px solid var(--lk3d-ink, #1d1a1e); border-radius:4px 7px 5px 6px}
28
30
  .lk3d-tag.on{background:#f3d78f}
29
31
  .lk3d-stage[data-look="studio"] .lk3d-tag{background:rgba(14,18,24,.82); color:#eef2f7; border-color:rgba(255,255,255,.35); font-family:system-ui, sans-serif; font-size:12.5px; letter-spacing:.02em}
30
32
  .lk3d-stage[data-look="studio"] .lk3d-tag.on{background:rgba(232,150,60,.9); color:#111}
31
- .lk3d-stage[data-look="studio"] .lk3d-lines line{stroke:rgba(255,255,255,.55)} .lk3d-stage[data-look="studio"] .lk3d-lines circle{fill:#fff}
33
+ .lk3d-stage[data-look="studio"] .lk3d-lines line, .lk3d-stage[data-look="studio"] .lk3d-lines path.lk3d-dim{stroke:rgba(255,255,255,.7)} .lk3d-stage[data-look="studio"] .lk3d-dimtext{background:rgba(20,22,26,.75); color:#fff} .lk3d-stage[data-look="studio"] .lk3d-lines circle{fill:#fff}
32
34
  .lk3d-hint{position:absolute; left:10px; bottom:8px; font-size:13px; color:var(--lk3d-muted, #6b6670); background:rgba(247,245,239,.85); padding:1px 7px; border-radius:4px; pointer-events:none; transition:opacity .4s}
33
35
  .lk3d-stage[data-look="studio"] .lk3d-hint{color:#c9d1dc; background:rgba(10,12,16,.6)}
34
36
  .lk3d-hint.gone{opacity:0}
@@ -40,7 +42,7 @@ lk3d-figure{display:block}
40
42
  .lk3d-stage[data-look="studio"] .lk3d-scale{color:#eef2f7} .lk3d-stage[data-look="studio"] .lk3d-note{background:rgba(10,12,16,.7); color:#ffb38a}
41
43
  .lk3d-stage[data-theme] .lk3d-tag{background:var(--lk3d-paper); color:var(--lk3d-ink); border:1px solid var(--lk3d-line); font-family:system-ui, sans-serif; font-size:12.5px}
42
44
  .lk3d-stage[data-theme] .lk3d-tag.on{background:var(--lk3d-highlight); color:var(--lk3d-ink)}
43
- .lk3d-stage[data-theme] .lk3d-lines line{stroke:var(--lk3d-line); stroke-width:.8} .lk3d-stage[data-theme] .lk3d-lines circle{fill:var(--lk3d-line)}
45
+ .lk3d-stage[data-theme] .lk3d-lines line{stroke:var(--lk3d-line); stroke-width:.8} .lk3d-stage[data-theme] .lk3d-lines path.lk3d-dim{stroke:var(--lk3d-line)} .lk3d-stage[data-theme] .lk3d-dimtext{background:var(--lk3d-paper); color:var(--lk3d-ink)} .lk3d-stage[data-theme] .lk3d-lines circle{fill:var(--lk3d-line)}
44
46
  .lk3d-stage[data-theme] .lk3d-hint{color:var(--lk3d-muted); background:var(--lk3d-paper)} .lk3d-stage[data-theme] .lk3d-mark{color:var(--lk3d-muted)}
45
47
  .lk3d-stage[data-theme] .lk3d-scale{color:var(--lk3d-ink)} .lk3d-stage[data-theme] .lk3d-note{background:var(--lk3d-paper); color:var(--lk3d-warn)}
46
48
  .lk3d-sr{position:absolute; width:1px; height:1px; margin:-1px; padding:0; border:0; overflow:hidden; clip:rect(0 0 0 0); white-space:nowrap}
package/src/validate.js CHANGED
@@ -139,6 +139,8 @@ function links(scene, out) {
139
139
  const section = scene.section;
140
140
  if (section && typeof section === 'object' && zero(section.normal))
141
141
  out.push({ path: 'section.normal', message: NO_WAY });
142
+ if (section && typeof section === 'object' && section.second && zero(section.second.normal))
143
+ out.push({ path: 'section.second.normal', message: NO_WAY });
142
144
  const names = new Set(parts.flatMap((part) => [part.id, part.group].filter(Boolean))),
143
145
  seen = new Set(),
144
146
  models = scene.models || {};
@@ -284,6 +286,7 @@ function links(scene, out) {
284
286
  if (joint && typeof joint === 'object' && !visit.has(joint.id) && loop(joint))
285
287
  out.push({ path: `joints[${i}]`, message: `${say(joint.id)} reads itself through other joints (a loop)` });
286
288
  });
289
+ dimensions(scene, out, parts);
287
290
  steps(scene, out, names, parts);
288
291
  // motions: one id each; tracks that move parts that exist, with keys in order and within the range
289
292
  const seenMotions = new Set();
@@ -316,6 +319,39 @@ function links(scene, out) {
316
319
  });
317
320
  }
318
321
 
322
+ // dimensions: one id each; the parts their points ride on exist; a length between two places, an angle with arms
323
+ function dimensions(scene, out, parts) {
324
+ const seen = new Set(),
325
+ isPart = (id) => parts.some((part) => part.id === id);
326
+ (Array.isArray(scene.dimensions) ? scene.dimensions : []).forEach((dimension, i) => {
327
+ if (!dimension || typeof dimension !== 'object') return;
328
+ const bad = (field, message) => out.push({ path: `dimensions[${i}]${field}`, message });
329
+ if (dimension.id != null) {
330
+ if (seen.has(dimension.id)) bad('.id', `${say(dimension.id)} is used by two dimensions`);
331
+ seen.add(dimension.id);
332
+ }
333
+ const points = [
334
+ ['.from', dimension.from],
335
+ ['.to', dimension.to],
336
+ ...(Array.isArray(dimension.angle) ? dimension.angle.map((point, k) => [`.angle[${k}]`, point]) : []),
337
+ ];
338
+ for (const [field, point] of points)
339
+ if (point && typeof point === 'object' && !Array.isArray(point) && point.part != null && !isPart(point.part))
340
+ bad(`${field}.part`, `${say(point.part)} is no part`);
341
+ // (the same place twice, where neither rides on a part: nothing to measure)
342
+ const same = (a, b) => Array.isArray(a) && Array.isArray(b) && a.every((value, k) => value === b[k]);
343
+ if (same(dimension.from, dimension.to)) bad('.to', 'is where from is: a length needs two places');
344
+ if (
345
+ Array.isArray(dimension.angle) &&
346
+ (same(dimension.angle[0], dimension.angle[1]) || same(dimension.angle[2], dimension.angle[1]))
347
+ )
348
+ bad('.angle', 'has an arm of no length: its points are a point on each arm, and the vertex between them');
349
+ if (dimension.from && dimension.angle) bad('', 'is a length (from, to) or an angle, not both');
350
+ if (dimension.unit && !scene.unit)
351
+ bad('.unit', `converts to ${say(dimension.unit)}, but the scene has no unit to convert from`);
352
+ });
353
+ }
354
+
319
355
  // guided steps: one id each; what they name exists (parts and groups, motions within their range; a part labelled has
320
356
  // a label); a cut only where
321
357
  // the scene offers one; a part chosen or labelled that the same step does not hide; home, the figure's first view, not
@@ -362,6 +398,9 @@ function steps(scene, out, names, parts) {
362
398
  bad(`motion.${id}`, `${value} is outside its range [${start}, ${end}]`);
363
399
  }
364
400
  for (const id of list(step.play)) if (!motions.has(id)) bad('play', `${say(id)} is no motion`);
401
+ const dimensionIds = new Set(list(scene.dimensions).map((dimension) => dimension && dimension.id));
402
+ for (const id of list(step.dimensions))
403
+ if (!dimensionIds.has(id)) bad('dimensions', `${say(id)} is no dimension (give it an id)`);
365
404
  });
366
405
  }
367
406