@es3d/es3d 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +95 -86
  3. package/docs/SPEC.md +426 -417
  4. package/package.json +1 -1
  5. package/scripts/es3d-cli.mjs +390 -377
  6. package/src/cache/geometry-store.js +392 -392
  7. package/src/cache/mesh-bundle.js +123 -123
  8. package/src/cache/sha256.js +73 -73
  9. package/src/format/ast.js +238 -238
  10. package/src/format/colors.js +93 -93
  11. package/src/format/compact-parser.js +171 -171
  12. package/src/format/compact-tokenizer.js +165 -165
  13. package/src/format/errors.js +247 -247
  14. package/src/format/json.js +55 -55
  15. package/src/format/limits.js +14 -14
  16. package/src/format/parse.js +132 -92
  17. package/src/format/schema.js +617 -617
  18. package/src/format/serializer.js +333 -333
  19. package/src/format/validator.js +330 -330
  20. package/src/format/value-error.js +12 -12
  21. package/src/format/value-format.js +314 -314
  22. package/src/format/values.js +459 -459
  23. package/src/format/xml-parser.js +136 -136
  24. package/src/format/xml-tokenizer.js +237 -237
  25. package/src/geometry/contour.js +86 -86
  26. package/src/geometry/extrude.js +175 -175
  27. package/src/geometry/gear.js +85 -85
  28. package/src/geometry/geometry-cache.js +159 -159
  29. package/src/geometry/lod-error.js +178 -178
  30. package/src/geometry/mesh.js +458 -458
  31. package/src/geometry/primitives.js +299 -299
  32. package/src/geometry/registry.js +645 -645
  33. package/src/geometry/revolve.js +136 -136
  34. package/src/geometry/sweep.js +441 -441
  35. package/src/index.js +47 -47
  36. package/src/io/compression.js +67 -67
  37. package/src/io/convert.js +236 -236
  38. package/src/io/download.js +13 -13
  39. package/src/io/external-scene.js +127 -127
  40. package/src/io/file-loader.js +28 -28
  41. package/src/io/gltf-pack.js +1461 -0
  42. package/src/io/gltf.js +630 -630
  43. package/src/io/mesh-import.js +104 -104
  44. package/src/io/obj.js +352 -352
  45. package/src/io/step-brep.js +439 -439
  46. package/src/io/step-exact.js +1149 -1149
  47. package/src/io/step.js +1418 -1418
  48. package/src/io/stl.js +193 -193
  49. package/src/io/svg-import.js +787 -787
  50. package/src/material/materials.js +154 -154
  51. package/src/math/mat4.js +475 -475
  52. package/src/math/random.js +30 -30
  53. package/src/math/trig.js +65 -65
  54. package/src/math/vec3.js +81 -81
  55. package/src/optimize/optimizer.js +604 -579
  56. package/src/path/path-flatten.js +322 -322
  57. package/src/path/path-parser.js +332 -332
  58. package/src/path/shapes.js +129 -129
  59. package/src/path/triangulate.js +517 -517
  60. package/src/pipeline.js +74 -74
  61. package/src/render/camera.js +168 -168
  62. package/src/render/frame-builder.js +362 -362
  63. package/src/render/geometry-pool.js +149 -149
  64. package/src/render/lighting.js +149 -149
  65. package/src/render/orbit-controls.js +237 -237
  66. package/src/render/renderer.js +258 -249
  67. package/src/render/scroll-timeline.js +104 -104
  68. package/src/render/shaders/glsl.js +285 -285
  69. package/src/render/shaders/wgsl.js +252 -252
  70. package/src/render/stage.js +365 -352
  71. package/src/render/texture-cache.js +40 -40
  72. package/src/render/textures.js +223 -223
  73. package/src/render/webgl2/webgl2-backend.js +586 -532
  74. package/src/render/webgpu/webgpu-backend.js +482 -480
  75. package/src/scene/flatten.js +754 -754
  76. package/src/scene/picking.js +161 -161
  77. package/src/scene/scene-graph.js +320 -320
  78. package/src/transform/transform.js +211 -211
  79. package/src/wasm/kernels-binary.js +3 -3
  80. package/src/wasm/kernels.js +385 -385
@@ -1,136 +1,136 @@
1
- // Revolution (lathe) of a 2D profile around the Y axis.
2
- //
3
- // The profile lives in the XY plane: x is the distance from the axis, y the
4
- // height. Open profiles are oriented automatically so their normals face away
5
- // from the axis region they enclose; closed profiles become solids of
6
- // revolution and get end caps when the angle is below 360 degrees.
7
-
8
- import { MeshBuilder } from './mesh.js';
9
- import { signedArea, reverseContour } from '../path/path-flatten.js';
10
- import { contourFrames } from './contour.js';
11
- import { triangulate } from '../path/triangulate.js';
12
-
13
- /** Orient an open profile so that (profile + axis) forms a counter-clockwise loop. */
14
- function orientOpen(c) {
15
- const p = c.points;
16
- const n = p.length / 2;
17
- const loop = new Float64Array(p.length + 4);
18
- loop.set(p);
19
- loop[p.length] = 0;
20
- loop[p.length + 1] = p[(n - 1) * 2 + 1];
21
- loop[p.length + 2] = 0;
22
- loop[p.length + 3] = p[1];
23
- return signedArea(loop) >= 0 ? c : reverseContour(c);
24
- }
25
-
26
- /**
27
- * @param {object[]} contours flattened 2D contours
28
- * @param {{angle:number, segments:number}} o angle in degrees
29
- * @returns {{mesh: import('./mesh.js').Mesh, doubleSided: boolean}}
30
- */
31
- export function revolveMesh(contours, o, report) {
32
- const full = o.angle >= 360 - 1e-9;
33
- const A = (Math.min(o.angle, 360) * Math.PI) / 180;
34
- const S = Math.max(2, o.segments);
35
- let open = false;
36
-
37
- // profiles first, so the mesh can be allocated once at its final size
38
- const profiles = [];
39
- for (const raw of contours) {
40
- const pts0 = raw.points;
41
- for (let i = 0; i < pts0.length; i += 2) {
42
- if (pts0[i] < -1e-9) {
43
- if (report) report('error', 'E_REVOLVE_AXIS', `The revolve profile crosses the axis: point (${pts0[i]}, ${pts0[i + 1]}) has x < 0. Profiles must stay at x ≥ 0.`);
44
- break;
45
- }
46
- }
47
- const closed = raw.closed;
48
- if (!closed) open = true;
49
- const c = closed ? (signedArea(raw.points) > 0 ? raw : reverseContour(raw)) : orientOpen(raw);
50
- const pts = c.points;
51
- const n = pts.length / 2;
52
- if (n < 2) continue;
53
- const f = contourFrames(pts, c.smooth, closed);
54
- const total = f.length || 1;
55
- // profile entries (x, y, nx, ny, v): per vertex, entryIn (end of the
56
- // previous edge) and entryOut (start of the next edge)
57
- const entries = [];
58
- const entryIn = new Int32Array(n).fill(-1);
59
- const entryOut = new Int32Array(n).fill(-1);
60
- const add = (x, y, nx, ny, v) => entries.push(x, y, nx, ny, v) / 5 - 1;
61
- for (let i = 0; i < n; i++) {
62
- const pe = closed ? (i - 1 + n) % n : i - 1;
63
- const ne = closed || i < n - 1 ? i : -1;
64
- const x = Math.max(0, pts[i * 2]), y = pts[i * 2 + 1];
65
- const v = f.arc[i] / total;
66
- if (pe >= 0 && ne >= 0 && f.corner[i]) {
67
- entryIn[i] = add(x, y, f.edgeN[pe * 2], f.edgeN[pe * 2 + 1], v);
68
- entryOut[i] = add(x, y, f.edgeN[ne * 2], f.edgeN[ne * 2 + 1], v);
69
- } else {
70
- const e = ne >= 0 ? ne : pe;
71
- const k = f.corner[i] ? add(x, y, f.edgeN[e * 2], f.edgeN[e * 2 + 1], v) : add(x, y, f.vertN[i * 2], f.vertN[i * 2 + 1], v);
72
- entryIn[i] = k;
73
- entryOut[i] = k;
74
- }
75
- }
76
- const tri = !full && closed ? triangulate(pts) : null;
77
- profiles.push({ closed, pts, n, entries, entryIn, entryOut, tri });
78
- }
79
- let vertices = 0, indices = 0;
80
- for (const p of profiles) {
81
- vertices += (p.entries.length / 5) * (S + 1);
82
- indices += (p.closed ? p.n : p.n - 1) * S * 6;
83
- if (p.tri) {
84
- vertices += 2 * p.n;
85
- indices += 2 * p.tri.length;
86
- }
87
- }
88
- const b = new MeshBuilder('triangles', vertices, indices);
89
- // the angle of each column is the same for every profile entry
90
- const cosCol = new Float64Array(S + 1), sinCol = new Float64Array(S + 1);
91
- for (let j = 0; j <= S; j++) {
92
- const th = (A * j) / S;
93
- cosCol[j] = Math.cos(th);
94
- sinCol[j] = Math.sin(th);
95
- }
96
-
97
- for (const { closed, pts, n, entries, entryIn, entryOut, tri } of profiles) {
98
- // closing seam of closed contours: v wraps to 1 at the last edge end
99
- const base = b.vertexCount;
100
- const cols = S + 1;
101
- for (let k = 0; k < entries.length; k += 5) {
102
- const x = entries[k], y = entries[k + 1], nx = entries[k + 2], ny = entries[k + 3], v = entries[k + 4];
103
- for (let j = 0; j <= S; j++) {
104
- const ct = cosCol[j], st = sinCol[j];
105
- b.vertex(x * ct, y, -x * st, nx * ct, ny, -nx * st, j / S, v);
106
- }
107
- }
108
- const edges = closed ? n : n - 1;
109
- for (let e = 0; e < edges; e++) {
110
- const s0 = entryOut[e];
111
- const t0 = entryIn[(e + 1) % n];
112
- for (let j = 0; j < S; j++) {
113
- const a = base + s0 * cols + j;
114
- const d = base + t0 * cols + j;
115
- b.quad(a, a + 1, d + 1, d);
116
- }
117
- }
118
- // caps for partial revolutions of closed profiles
119
- if (tri) {
120
- for (const [th, sign] of [[0, 1], [A, -1]]) {
121
- const ct = Math.cos(th), st = Math.sin(th);
122
- // start cap faces +Z (against the sweep); the end cap faces the sweep direction (-sin A, 0, -cos A)
123
- const cb = b.vertexCount;
124
- for (let i = 0; i < n; i++) {
125
- const x = Math.max(0, pts[i * 2]), y = pts[i * 2 + 1];
126
- b.vertex(x * ct, y, -x * st, sign > 0 ? 0 : -st, 0, sign > 0 ? 1 : -ct, x, y);
127
- }
128
- for (let i = 0; i < tri.length; i += 3) {
129
- if (sign > 0) b.tri(cb + tri[i], cb + tri[i + 1], cb + tri[i + 2]);
130
- else b.tri(cb + tri[i], cb + tri[i + 2], cb + tri[i + 1]);
131
- }
132
- }
133
- }
134
- }
135
- return { mesh: b.build(), doubleSided: open };
136
- }
1
+ // Revolution (lathe) of a 2D profile around the Y axis.
2
+ //
3
+ // The profile lives in the XY plane: x is the distance from the axis, y the
4
+ // height. Open profiles are oriented automatically so their normals face away
5
+ // from the axis region they enclose; closed profiles become solids of
6
+ // revolution and get end caps when the angle is below 360 degrees.
7
+
8
+ import { MeshBuilder } from './mesh.js';
9
+ import { signedArea, reverseContour } from '../path/path-flatten.js';
10
+ import { contourFrames } from './contour.js';
11
+ import { triangulate } from '../path/triangulate.js';
12
+
13
+ /** Orient an open profile so that (profile + axis) forms a counter-clockwise loop. */
14
+ function orientOpen(c) {
15
+ const p = c.points;
16
+ const n = p.length / 2;
17
+ const loop = new Float64Array(p.length + 4);
18
+ loop.set(p);
19
+ loop[p.length] = 0;
20
+ loop[p.length + 1] = p[(n - 1) * 2 + 1];
21
+ loop[p.length + 2] = 0;
22
+ loop[p.length + 3] = p[1];
23
+ return signedArea(loop) >= 0 ? c : reverseContour(c);
24
+ }
25
+
26
+ /**
27
+ * @param {object[]} contours flattened 2D contours
28
+ * @param {{angle:number, segments:number}} o angle in degrees
29
+ * @returns {{mesh: import('./mesh.js').Mesh, doubleSided: boolean}}
30
+ */
31
+ export function revolveMesh(contours, o, report) {
32
+ const full = o.angle >= 360 - 1e-9;
33
+ const A = (Math.min(o.angle, 360) * Math.PI) / 180;
34
+ const S = Math.max(2, o.segments);
35
+ let open = false;
36
+
37
+ // profiles first, so the mesh can be allocated once at its final size
38
+ const profiles = [];
39
+ for (const raw of contours) {
40
+ const pts0 = raw.points;
41
+ for (let i = 0; i < pts0.length; i += 2) {
42
+ if (pts0[i] < -1e-9) {
43
+ if (report) report('error', 'E_REVOLVE_AXIS', `The revolve profile crosses the axis: point (${pts0[i]}, ${pts0[i + 1]}) has x < 0. Profiles must stay at x ≥ 0.`);
44
+ break;
45
+ }
46
+ }
47
+ const closed = raw.closed;
48
+ if (!closed) open = true;
49
+ const c = closed ? (signedArea(raw.points) > 0 ? raw : reverseContour(raw)) : orientOpen(raw);
50
+ const pts = c.points;
51
+ const n = pts.length / 2;
52
+ if (n < 2) continue;
53
+ const f = contourFrames(pts, c.smooth, closed);
54
+ const total = f.length || 1;
55
+ // profile entries (x, y, nx, ny, v): per vertex, entryIn (end of the
56
+ // previous edge) and entryOut (start of the next edge)
57
+ const entries = [];
58
+ const entryIn = new Int32Array(n).fill(-1);
59
+ const entryOut = new Int32Array(n).fill(-1);
60
+ const add = (x, y, nx, ny, v) => entries.push(x, y, nx, ny, v) / 5 - 1;
61
+ for (let i = 0; i < n; i++) {
62
+ const pe = closed ? (i - 1 + n) % n : i - 1;
63
+ const ne = closed || i < n - 1 ? i : -1;
64
+ const x = Math.max(0, pts[i * 2]), y = pts[i * 2 + 1];
65
+ const v = f.arc[i] / total;
66
+ if (pe >= 0 && ne >= 0 && f.corner[i]) {
67
+ entryIn[i] = add(x, y, f.edgeN[pe * 2], f.edgeN[pe * 2 + 1], v);
68
+ entryOut[i] = add(x, y, f.edgeN[ne * 2], f.edgeN[ne * 2 + 1], v);
69
+ } else {
70
+ const e = ne >= 0 ? ne : pe;
71
+ const k = f.corner[i] ? add(x, y, f.edgeN[e * 2], f.edgeN[e * 2 + 1], v) : add(x, y, f.vertN[i * 2], f.vertN[i * 2 + 1], v);
72
+ entryIn[i] = k;
73
+ entryOut[i] = k;
74
+ }
75
+ }
76
+ const tri = !full && closed ? triangulate(pts) : null;
77
+ profiles.push({ closed, pts, n, entries, entryIn, entryOut, tri });
78
+ }
79
+ let vertices = 0, indices = 0;
80
+ for (const p of profiles) {
81
+ vertices += (p.entries.length / 5) * (S + 1);
82
+ indices += (p.closed ? p.n : p.n - 1) * S * 6;
83
+ if (p.tri) {
84
+ vertices += 2 * p.n;
85
+ indices += 2 * p.tri.length;
86
+ }
87
+ }
88
+ const b = new MeshBuilder('triangles', vertices, indices);
89
+ // the angle of each column is the same for every profile entry
90
+ const cosCol = new Float64Array(S + 1), sinCol = new Float64Array(S + 1);
91
+ for (let j = 0; j <= S; j++) {
92
+ const th = (A * j) / S;
93
+ cosCol[j] = Math.cos(th);
94
+ sinCol[j] = Math.sin(th);
95
+ }
96
+
97
+ for (const { closed, pts, n, entries, entryIn, entryOut, tri } of profiles) {
98
+ // closing seam of closed contours: v wraps to 1 at the last edge end
99
+ const base = b.vertexCount;
100
+ const cols = S + 1;
101
+ for (let k = 0; k < entries.length; k += 5) {
102
+ const x = entries[k], y = entries[k + 1], nx = entries[k + 2], ny = entries[k + 3], v = entries[k + 4];
103
+ for (let j = 0; j <= S; j++) {
104
+ const ct = cosCol[j], st = sinCol[j];
105
+ b.vertex(x * ct, y, -x * st, nx * ct, ny, -nx * st, j / S, v);
106
+ }
107
+ }
108
+ const edges = closed ? n : n - 1;
109
+ for (let e = 0; e < edges; e++) {
110
+ const s0 = entryOut[e];
111
+ const t0 = entryIn[(e + 1) % n];
112
+ for (let j = 0; j < S; j++) {
113
+ const a = base + s0 * cols + j;
114
+ const d = base + t0 * cols + j;
115
+ b.quad(a, a + 1, d + 1, d);
116
+ }
117
+ }
118
+ // caps for partial revolutions of closed profiles
119
+ if (tri) {
120
+ for (const [th, sign] of [[0, 1], [A, -1]]) {
121
+ const ct = Math.cos(th), st = Math.sin(th);
122
+ // start cap faces +Z (against the sweep); the end cap faces the sweep direction (-sin A, 0, -cos A)
123
+ const cb = b.vertexCount;
124
+ for (let i = 0; i < n; i++) {
125
+ const x = Math.max(0, pts[i * 2]), y = pts[i * 2 + 1];
126
+ b.vertex(x * ct, y, -x * st, sign > 0 ? 0 : -st, 0, sign > 0 ? 1 : -ct, x, y);
127
+ }
128
+ for (let i = 0; i < tri.length; i += 3) {
129
+ if (sign > 0) b.tri(cb + tri[i], cb + tri[i + 1], cb + tri[i + 2]);
130
+ else b.tri(cb + tri[i], cb + tri[i + 2], cb + tri[i + 1]);
131
+ }
132
+ }
133
+ }
134
+ }
135
+ return { mesh: b.build(), doubleSided: open };
136
+ }