@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,129 +1,129 @@
1
- // Contours -> fill regions (polygons with holes) following SVG fill rules.
2
- //
3
- // Closed contours are organised into a containment tree. A contour's interior
4
- // (minus its children) is filled when its nesting depth is even ("evenodd") or
5
- // when the accumulated winding number is non-zero ("nonzero", SVG default).
6
- // Side walls for extrusions/sweeps exist exactly where the fill state changes
7
- // across a contour; walls are oriented with the filled side on the left so the
8
- // right-hand perpendicular of each edge is the outward normal.
9
-
10
- import { signedArea, reverseContour } from './path-flatten.js';
11
- import { triangulate, triangulationError } from './triangulate.js';
12
-
13
- function pointInPolygon(points, x, y) {
14
- let inside = false;
15
- const n = points.length / 2;
16
- for (let i = 0, j = n - 1; i < n; j = i++) {
17
- const xi = points[i * 2], yi = points[i * 2 + 1];
18
- const xj = points[j * 2], yj = points[j * 2 + 1];
19
- if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) inside = !inside;
20
- }
21
- return inside;
22
- }
23
-
24
- function bbox(points) {
25
- let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
26
- for (let i = 0; i < points.length; i += 2) {
27
- const x = points[i], y = points[i + 1];
28
- if (x < minX) minX = x;
29
- if (y < minY) minY = y;
30
- if (x > maxX) maxX = x;
31
- if (y > maxY) maxY = y;
32
- }
33
- return { minX, minY, maxX, maxY };
34
- }
35
-
36
- /** A sample point strictly inside the contour near its first vertex (robust against shared vertices). */
37
- function samplePoint(points) {
38
- const n = points.length / 2;
39
- // centroid of a small triangle at a convex-ish vertex works for most inputs;
40
- // fall back to the first vertex.
41
- for (let k = 0; k < Math.min(n, 8); k++) {
42
- const i0 = ((k - 1 + n) % n) * 2, i1 = k * 2, i2 = ((k + 1) % n) * 2;
43
- const cx = (points[i0] + points[i1] + points[i2]) / 3;
44
- const cy = (points[i0 + 1] + points[i1 + 1] + points[i2 + 1]) / 3;
45
- if (pointInPolygon(points, cx, cy)) return [cx, cy];
46
- }
47
- return [points[0], points[1]];
48
- }
49
-
50
- /**
51
- * @param {{points: Float64Array, smooth: Uint8Array}[]} contours 2D contours (treated as closed)
52
- * @param {'nonzero'|'evenodd'} fillRule
53
- */
54
- export function buildRegions(contours, fillRule = 'nonzero') {
55
- const items = [];
56
- for (const c of contours) {
57
- if (c.points.length < 6) continue;
58
- const a = signedArea(c.points);
59
- if (Math.abs(a) < 1e-14) continue;
60
- items.push({ c, area: a, abs: Math.abs(a), box: bbox(c.points), parent: -1, depth: 0, winding: 0, children: [] });
61
- }
62
- items.sort((p, q) => q.abs - p.abs);
63
- for (let i = 0; i < items.length; i++) {
64
- const it = items[i];
65
- const [sx, sy] = samplePoint(it.c.points);
66
- for (let j = i - 1; j >= 0; j--) {
67
- const cand = items[j];
68
- const b = cand.box;
69
- if (sx < b.minX || sx > b.maxX || sy < b.minY || sy > b.maxY) continue;
70
- if (pointInPolygon(cand.c.points, sx, sy)) {
71
- it.parent = j;
72
- break;
73
- }
74
- }
75
- const sgn = it.area > 0 ? 1 : -1;
76
- if (it.parent >= 0) {
77
- const p = items[it.parent];
78
- it.depth = p.depth + 1;
79
- it.winding = p.winding + sgn;
80
- p.children.push(i);
81
- } else {
82
- it.depth = 0;
83
- it.winding = sgn;
84
- }
85
- it.filled = fillRule === 'evenodd' ? it.depth % 2 === 0 : it.winding !== 0;
86
- }
87
-
88
- // A contour is a wall where the fill state differs on its two sides.
89
- for (const it of items) it.isWall = it.filled !== (it.parent >= 0 ? items[it.parent].filled : false);
90
- const oriented = (it, wantCcw) => {
91
- const c = (signedArea(it.c.points) > 0) === wantCcw ? it.c : reverseContour(it.c);
92
- return { ...c, isWall: it.isWall };
93
- };
94
-
95
- const regions = [];
96
- const walls = [];
97
- for (let i = 0; i < items.length; i++) {
98
- const it = items[i];
99
- if (it.filled) {
100
- regions.push({ outer: oriented(it, true), holes: it.children.map((k) => oriented(items[k], false)) });
101
- }
102
- if (it.isWall) walls.push(oriented(it, it.filled));
103
- }
104
- return { regions, walls };
105
- }
106
-
107
- /**
108
- * Triangulate a region; returns {positions: Float64Array (2D), smooth, indices, error}.
109
- * `error` is the relative area mismatch (large = self-intersecting input).
110
- */
111
- export function triangulateRegion(region) {
112
- let total = region.outer.points.length;
113
- for (const h of region.holes) total += h.points.length;
114
- const data = new Float64Array(total);
115
- const smooth = new Uint8Array(total / 2);
116
- data.set(region.outer.points, 0);
117
- smooth.set(region.outer.smooth, 0);
118
- const holeIndices = [];
119
- let off = region.outer.points.length;
120
- for (const h of region.holes) {
121
- holeIndices.push(off / 2);
122
- data.set(h.points, off);
123
- smooth.set(h.smooth, off / 2);
124
- off += h.points.length;
125
- }
126
- const indices = triangulate(data, holeIndices.length ? holeIndices : null);
127
- const error = triangulationError(data, holeIndices.length ? holeIndices : null, indices);
128
- return { positions: data, smooth, indices, holeIndices, error };
129
- }
1
+ // Contours -> fill regions (polygons with holes) following SVG fill rules.
2
+ //
3
+ // Closed contours are organised into a containment tree. A contour's interior
4
+ // (minus its children) is filled when its nesting depth is even ("evenodd") or
5
+ // when the accumulated winding number is non-zero ("nonzero", SVG default).
6
+ // Side walls for extrusions/sweeps exist exactly where the fill state changes
7
+ // across a contour; walls are oriented with the filled side on the left so the
8
+ // right-hand perpendicular of each edge is the outward normal.
9
+
10
+ import { signedArea, reverseContour } from './path-flatten.js';
11
+ import { triangulate, triangulationError } from './triangulate.js';
12
+
13
+ function pointInPolygon(points, x, y) {
14
+ let inside = false;
15
+ const n = points.length / 2;
16
+ for (let i = 0, j = n - 1; i < n; j = i++) {
17
+ const xi = points[i * 2], yi = points[i * 2 + 1];
18
+ const xj = points[j * 2], yj = points[j * 2 + 1];
19
+ if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) inside = !inside;
20
+ }
21
+ return inside;
22
+ }
23
+
24
+ function bbox(points) {
25
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
26
+ for (let i = 0; i < points.length; i += 2) {
27
+ const x = points[i], y = points[i + 1];
28
+ if (x < minX) minX = x;
29
+ if (y < minY) minY = y;
30
+ if (x > maxX) maxX = x;
31
+ if (y > maxY) maxY = y;
32
+ }
33
+ return { minX, minY, maxX, maxY };
34
+ }
35
+
36
+ /** A sample point strictly inside the contour near its first vertex (robust against shared vertices). */
37
+ function samplePoint(points) {
38
+ const n = points.length / 2;
39
+ // centroid of a small triangle at a convex-ish vertex works for most inputs;
40
+ // fall back to the first vertex.
41
+ for (let k = 0; k < Math.min(n, 8); k++) {
42
+ const i0 = ((k - 1 + n) % n) * 2, i1 = k * 2, i2 = ((k + 1) % n) * 2;
43
+ const cx = (points[i0] + points[i1] + points[i2]) / 3;
44
+ const cy = (points[i0 + 1] + points[i1 + 1] + points[i2 + 1]) / 3;
45
+ if (pointInPolygon(points, cx, cy)) return [cx, cy];
46
+ }
47
+ return [points[0], points[1]];
48
+ }
49
+
50
+ /**
51
+ * @param {{points: Float64Array, smooth: Uint8Array}[]} contours 2D contours (treated as closed)
52
+ * @param {'nonzero'|'evenodd'} fillRule
53
+ */
54
+ export function buildRegions(contours, fillRule = 'nonzero') {
55
+ const items = [];
56
+ for (const c of contours) {
57
+ if (c.points.length < 6) continue;
58
+ const a = signedArea(c.points);
59
+ if (Math.abs(a) < 1e-14) continue;
60
+ items.push({ c, area: a, abs: Math.abs(a), box: bbox(c.points), parent: -1, depth: 0, winding: 0, children: [] });
61
+ }
62
+ items.sort((p, q) => q.abs - p.abs);
63
+ for (let i = 0; i < items.length; i++) {
64
+ const it = items[i];
65
+ const [sx, sy] = samplePoint(it.c.points);
66
+ for (let j = i - 1; j >= 0; j--) {
67
+ const cand = items[j];
68
+ const b = cand.box;
69
+ if (sx < b.minX || sx > b.maxX || sy < b.minY || sy > b.maxY) continue;
70
+ if (pointInPolygon(cand.c.points, sx, sy)) {
71
+ it.parent = j;
72
+ break;
73
+ }
74
+ }
75
+ const sgn = it.area > 0 ? 1 : -1;
76
+ if (it.parent >= 0) {
77
+ const p = items[it.parent];
78
+ it.depth = p.depth + 1;
79
+ it.winding = p.winding + sgn;
80
+ p.children.push(i);
81
+ } else {
82
+ it.depth = 0;
83
+ it.winding = sgn;
84
+ }
85
+ it.filled = fillRule === 'evenodd' ? it.depth % 2 === 0 : it.winding !== 0;
86
+ }
87
+
88
+ // A contour is a wall where the fill state differs on its two sides.
89
+ for (const it of items) it.isWall = it.filled !== (it.parent >= 0 ? items[it.parent].filled : false);
90
+ const oriented = (it, wantCcw) => {
91
+ const c = (signedArea(it.c.points) > 0) === wantCcw ? it.c : reverseContour(it.c);
92
+ return { ...c, isWall: it.isWall };
93
+ };
94
+
95
+ const regions = [];
96
+ const walls = [];
97
+ for (let i = 0; i < items.length; i++) {
98
+ const it = items[i];
99
+ if (it.filled) {
100
+ regions.push({ outer: oriented(it, true), holes: it.children.map((k) => oriented(items[k], false)) });
101
+ }
102
+ if (it.isWall) walls.push(oriented(it, it.filled));
103
+ }
104
+ return { regions, walls };
105
+ }
106
+
107
+ /**
108
+ * Triangulate a region; returns {positions: Float64Array (2D), smooth, indices, error}.
109
+ * `error` is the relative area mismatch (large = self-intersecting input).
110
+ */
111
+ export function triangulateRegion(region) {
112
+ let total = region.outer.points.length;
113
+ for (const h of region.holes) total += h.points.length;
114
+ const data = new Float64Array(total);
115
+ const smooth = new Uint8Array(total / 2);
116
+ data.set(region.outer.points, 0);
117
+ smooth.set(region.outer.smooth, 0);
118
+ const holeIndices = [];
119
+ let off = region.outer.points.length;
120
+ for (const h of region.holes) {
121
+ holeIndices.push(off / 2);
122
+ data.set(h.points, off);
123
+ smooth.set(h.smooth, off / 2);
124
+ off += h.points.length;
125
+ }
126
+ const indices = triangulate(data, holeIndices.length ? holeIndices : null);
127
+ const error = triangulationError(data, holeIndices.length ? holeIndices : null, indices);
128
+ return { positions: data, smooth, indices, holeIndices, error };
129
+ }