@es3d/es3d 1.0.0 → 1.0.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/LICENSE +201 -201
- package/README.md +86 -86
- package/docs/SPEC.md +417 -417
- package/package.json +1 -1
- package/scripts/es3d-cli.mjs +377 -377
- package/src/cache/geometry-store.js +392 -392
- package/src/cache/mesh-bundle.js +123 -123
- package/src/cache/sha256.js +73 -73
- package/src/format/ast.js +238 -238
- package/src/format/colors.js +93 -93
- package/src/format/compact-parser.js +171 -171
- package/src/format/compact-tokenizer.js +165 -165
- package/src/format/errors.js +247 -247
- package/src/format/json.js +55 -55
- package/src/format/limits.js +14 -14
- package/src/format/parse.js +92 -92
- package/src/format/schema.js +617 -617
- package/src/format/serializer.js +333 -333
- package/src/format/validator.js +330 -330
- package/src/format/value-error.js +12 -12
- package/src/format/value-format.js +314 -314
- package/src/format/values.js +459 -459
- package/src/format/xml-parser.js +136 -136
- package/src/format/xml-tokenizer.js +237 -237
- package/src/geometry/contour.js +86 -86
- package/src/geometry/extrude.js +175 -175
- package/src/geometry/gear.js +85 -85
- package/src/geometry/geometry-cache.js +159 -159
- package/src/geometry/lod-error.js +178 -178
- package/src/geometry/mesh.js +458 -458
- package/src/geometry/primitives.js +299 -299
- package/src/geometry/registry.js +645 -645
- package/src/geometry/revolve.js +136 -136
- package/src/geometry/sweep.js +441 -441
- package/src/index.js +47 -47
- package/src/io/compression.js +67 -67
- package/src/io/convert.js +236 -236
- package/src/io/download.js +13 -13
- package/src/io/external-scene.js +127 -127
- package/src/io/file-loader.js +28 -28
- package/src/io/gltf.js +630 -630
- package/src/io/mesh-import.js +104 -104
- package/src/io/obj.js +352 -352
- package/src/io/step-brep.js +439 -439
- package/src/io/step-exact.js +1149 -1149
- package/src/io/step.js +1418 -1418
- package/src/io/stl.js +193 -193
- package/src/io/svg-import.js +787 -787
- package/src/material/materials.js +154 -154
- package/src/math/mat4.js +475 -475
- package/src/math/random.js +30 -30
- package/src/math/trig.js +65 -65
- package/src/math/vec3.js +81 -81
- package/src/optimize/optimizer.js +579 -579
- package/src/path/path-flatten.js +322 -322
- package/src/path/path-parser.js +332 -332
- package/src/path/shapes.js +129 -129
- package/src/path/triangulate.js +517 -517
- package/src/pipeline.js +74 -74
- package/src/render/camera.js +168 -168
- package/src/render/frame-builder.js +362 -362
- package/src/render/geometry-pool.js +149 -149
- package/src/render/lighting.js +149 -149
- package/src/render/orbit-controls.js +237 -237
- package/src/render/renderer.js +258 -249
- package/src/render/scroll-timeline.js +104 -104
- package/src/render/shaders/glsl.js +285 -285
- package/src/render/shaders/wgsl.js +252 -252
- package/src/render/stage.js +364 -352
- package/src/render/texture-cache.js +40 -40
- package/src/render/textures.js +223 -223
- package/src/render/webgl2/webgl2-backend.js +586 -532
- package/src/render/webgpu/webgpu-backend.js +482 -480
- package/src/scene/flatten.js +754 -754
- package/src/scene/picking.js +161 -161
- package/src/scene/scene-graph.js +320 -320
- package/src/transform/transform.js +211 -211
- package/src/wasm/kernels-binary.js +3 -3
- package/src/wasm/kernels.js +385 -385
package/src/path/shapes.js
CHANGED
|
@@ -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
|
+
}
|