@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.
Files changed (79) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +86 -86
  3. package/docs/SPEC.md +417 -417
  4. package/package.json +1 -1
  5. package/scripts/es3d-cli.mjs +377 -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 +92 -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.js +630 -630
  42. package/src/io/mesh-import.js +104 -104
  43. package/src/io/obj.js +352 -352
  44. package/src/io/step-brep.js +439 -439
  45. package/src/io/step-exact.js +1149 -1149
  46. package/src/io/step.js +1418 -1418
  47. package/src/io/stl.js +193 -193
  48. package/src/io/svg-import.js +787 -787
  49. package/src/material/materials.js +154 -154
  50. package/src/math/mat4.js +475 -475
  51. package/src/math/random.js +30 -30
  52. package/src/math/trig.js +65 -65
  53. package/src/math/vec3.js +81 -81
  54. package/src/optimize/optimizer.js +579 -579
  55. package/src/path/path-flatten.js +322 -322
  56. package/src/path/path-parser.js +332 -332
  57. package/src/path/shapes.js +129 -129
  58. package/src/path/triangulate.js +517 -517
  59. package/src/pipeline.js +74 -74
  60. package/src/render/camera.js +168 -168
  61. package/src/render/frame-builder.js +362 -362
  62. package/src/render/geometry-pool.js +149 -149
  63. package/src/render/lighting.js +149 -149
  64. package/src/render/orbit-controls.js +237 -237
  65. package/src/render/renderer.js +258 -249
  66. package/src/render/scroll-timeline.js +104 -104
  67. package/src/render/shaders/glsl.js +285 -285
  68. package/src/render/shaders/wgsl.js +252 -252
  69. package/src/render/stage.js +364 -352
  70. package/src/render/texture-cache.js +40 -40
  71. package/src/render/textures.js +223 -223
  72. package/src/render/webgl2/webgl2-backend.js +586 -532
  73. package/src/render/webgpu/webgpu-backend.js +482 -480
  74. package/src/scene/flatten.js +754 -754
  75. package/src/scene/picking.js +161 -161
  76. package/src/scene/scene-graph.js +320 -320
  77. package/src/transform/transform.js +211 -211
  78. package/src/wasm/kernels-binary.js +3 -3
  79. package/src/wasm/kernels.js +385 -385
@@ -1,30 +1,30 @@
1
- // Deterministic pseudo random numbers.
2
- //
3
- // ES3D procedural elements (<scatter>) must produce the same result in every
4
- // implementation, so the generator is part of the format specification:
5
- // mulberry32 seeded with the 32-bit unsigned integer `seed`, returning floats in
6
- // [0, 1) as (state >>> 0) / 2^32 exactly as implemented below.
7
-
8
- export function mulberry32(seed) {
9
- let a = seed >>> 0;
10
- return function next() {
11
- a = (a + 0x6d2b79f5) >>> 0;
12
- let t = a;
13
- t = Math.imul(t ^ (t >>> 15), t | 1);
14
- t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
15
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
16
- };
17
- }
18
-
19
- /** Uniform value in [lo, hi] for a range [lo, hi]. */
20
- export function inRange(rand, range) {
21
- return range[0] + (range[1] - range[0]) * rand();
22
- }
23
-
24
- /** Integer hash -> [0,1) (used for value noise in procedural textures). */
25
- export function hash2(x, y, seed) {
26
- let h = Math.imul(x | 0, 0x27d4eb2d) ^ Math.imul(y | 0, 0x165667b1) ^ Math.imul(seed | 0, 0x9e3779b9);
27
- h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
28
- h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
29
- return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
30
- }
1
+ // Deterministic pseudo random numbers.
2
+ //
3
+ // ES3D procedural elements (<scatter>) must produce the same result in every
4
+ // implementation, so the generator is part of the format specification:
5
+ // mulberry32 seeded with the 32-bit unsigned integer `seed`, returning floats in
6
+ // [0, 1) as (state >>> 0) / 2^32 exactly as implemented below.
7
+
8
+ export function mulberry32(seed) {
9
+ let a = seed >>> 0;
10
+ return function next() {
11
+ a = (a + 0x6d2b79f5) >>> 0;
12
+ let t = a;
13
+ t = Math.imul(t ^ (t >>> 15), t | 1);
14
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
15
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
16
+ };
17
+ }
18
+
19
+ /** Uniform value in [lo, hi] for a range [lo, hi]. */
20
+ export function inRange(rand, range) {
21
+ return range[0] + (range[1] - range[0]) * rand();
22
+ }
23
+
24
+ /** Integer hash -> [0,1) (used for value noise in procedural textures). */
25
+ export function hash2(x, y, seed) {
26
+ let h = Math.imul(x | 0, 0x27d4eb2d) ^ Math.imul(y | 0, 0x165667b1) ^ Math.imul(seed | 0, 0x9e3779b9);
27
+ h = Math.imul(h ^ (h >>> 15), 0x85ebca6b);
28
+ h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
29
+ return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
30
+ }
package/src/math/trig.js CHANGED
@@ -1,65 +1,65 @@
1
- // Normative sine and cosine for placement.
2
- //
3
- // IEEE 754 does not require Math.sin and Math.cos to be correctly rounded, so
4
- // two readers can disagree in the last bit. Scatter positions and rotation
5
- // matrices use the functions below instead. Every operation is a binary64
6
- // round-to-nearest add, subtract or multiply, in the order written, so every
7
- // IEEE 754 implementation produces the same bits.
8
- //
9
- // Tessellation of spheres, gears and paths may keep the platform sin and cos.
10
- // Those meshes are bounded by a chordal error, not hashed.
11
-
12
- const PI = Math.PI;
13
- const TWO_PI = PI * 2;
14
- const HALF_PI = PI * 0.5;
15
- const QUARTER_PI = PI * 0.25;
16
-
17
- // fdlibm __kernel_sin coefficients, as binary64 literals. Valid for |x| <= π/4.
18
- const S1 = -1.66666666666666324348e-1;
19
- const S2 = 8.33333333332248946124e-3;
20
- const S3 = -1.98412698298579493134e-4;
21
- const S4 = 2.75573137070700676789e-6;
22
- const S5 = -2.50507602534068634195e-8;
23
- const S6 = 1.58969099521155010221e-10;
24
-
25
- // fdlibm __kernel_cos coefficients.
26
- const C1 = 4.16666666666666019037e-2;
27
- const C2 = -1.38888888888741095749e-3;
28
- const C3 = 2.48015872894767294178e-5;
29
- const C4 = -2.75573143513906633035e-7;
30
- const C5 = 2.0875723212981748279e-9;
31
- const C6 = -1.13596475577881948265e-11;
32
-
33
- function kernelSin(x) {
34
- const z = x * x;
35
- const v = z * x;
36
- const r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
37
- return x + v * (S1 + z * r);
38
- }
39
-
40
- function kernelCos(x) {
41
- const z = x * x;
42
- const r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
43
- // 1 - z/2 + z^2 * (C1 + ...). The extra z is the Taylor power; r already holds one.
44
- return 1 - 0.5 * z + z * r;
45
- }
46
-
47
- /** Sine of a radian angle. Finite inputs only; non-finite values pass through. */
48
- export function es3dSin(x) {
49
- if (!Number.isFinite(x)) return x;
50
- let y = x - TWO_PI * Math.round(x / TWO_PI);
51
- let sign = 1;
52
- if (y < 0) {
53
- sign = -1;
54
- y = -y;
55
- }
56
- if (y > HALF_PI) y = PI - y;
57
- if (y < 0) y = 0;
58
- const s = y > QUARTER_PI ? kernelCos(HALF_PI - y) : kernelSin(y);
59
- return sign < 0 ? -s : s;
60
- }
61
-
62
- /** Cosine. Defined as sin(x + π/2) with the same reduction. */
63
- export function es3dCos(x) {
64
- return es3dSin(x + HALF_PI);
65
- }
1
+ // Normative sine and cosine for placement.
2
+ //
3
+ // IEEE 754 does not require Math.sin and Math.cos to be correctly rounded, so
4
+ // two readers can disagree in the last bit. Scatter positions and rotation
5
+ // matrices use the functions below instead. Every operation is a binary64
6
+ // round-to-nearest add, subtract or multiply, in the order written, so every
7
+ // IEEE 754 implementation produces the same bits.
8
+ //
9
+ // Tessellation of spheres, gears and paths may keep the platform sin and cos.
10
+ // Those meshes are bounded by a chordal error, not hashed.
11
+
12
+ const PI = Math.PI;
13
+ const TWO_PI = PI * 2;
14
+ const HALF_PI = PI * 0.5;
15
+ const QUARTER_PI = PI * 0.25;
16
+
17
+ // fdlibm __kernel_sin coefficients, as binary64 literals. Valid for |x| <= π/4.
18
+ const S1 = -1.66666666666666324348e-1;
19
+ const S2 = 8.33333333332248946124e-3;
20
+ const S3 = -1.98412698298579493134e-4;
21
+ const S4 = 2.75573137070700676789e-6;
22
+ const S5 = -2.50507602534068634195e-8;
23
+ const S6 = 1.58969099521155010221e-10;
24
+
25
+ // fdlibm __kernel_cos coefficients.
26
+ const C1 = 4.16666666666666019037e-2;
27
+ const C2 = -1.38888888888741095749e-3;
28
+ const C3 = 2.48015872894767294178e-5;
29
+ const C4 = -2.75573143513906633035e-7;
30
+ const C5 = 2.0875723212981748279e-9;
31
+ const C6 = -1.13596475577881948265e-11;
32
+
33
+ function kernelSin(x) {
34
+ const z = x * x;
35
+ const v = z * x;
36
+ const r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
37
+ return x + v * (S1 + z * r);
38
+ }
39
+
40
+ function kernelCos(x) {
41
+ const z = x * x;
42
+ const r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
43
+ // 1 - z/2 + z^2 * (C1 + ...). The extra z is the Taylor power; r already holds one.
44
+ return 1 - 0.5 * z + z * r;
45
+ }
46
+
47
+ /** Sine of a radian angle. Finite inputs only; non-finite values pass through. */
48
+ export function es3dSin(x) {
49
+ if (!Number.isFinite(x)) return x;
50
+ let y = x - TWO_PI * Math.round(x / TWO_PI);
51
+ let sign = 1;
52
+ if (y < 0) {
53
+ sign = -1;
54
+ y = -y;
55
+ }
56
+ if (y > HALF_PI) y = PI - y;
57
+ if (y < 0) y = 0;
58
+ const s = y > QUARTER_PI ? kernelCos(HALF_PI - y) : kernelSin(y);
59
+ return sign < 0 ? -s : s;
60
+ }
61
+
62
+ /** Cosine. Defined as sin(x + π/2) with the same reduction. */
63
+ export function es3dCos(x) {
64
+ return es3dSin(x + HALF_PI);
65
+ }
package/src/math/vec3.js CHANGED
@@ -1,81 +1,81 @@
1
- // Small vector helpers operating on plain arrays [x, y, z].
2
-
3
- export const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
4
- export const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
5
- export const scale = (a, s) => [a[0] * s, a[1] * s, a[2] * s];
6
- export const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
7
- export const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
8
- export const length = (a) => Math.hypot(a[0], a[1], a[2]);
9
- export const distance = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
10
- export const lerp = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];
11
-
12
- export function normalize(a) {
13
- const l = Math.hypot(a[0], a[1], a[2]);
14
- return l > 1e-20 ? [a[0] / l, a[1] / l, a[2] / l] : [0, 0, 0];
15
- }
16
-
17
- /** Any unit vector perpendicular to a (unit) vector. */
18
- export function perpendicular(a) {
19
- const ax = Math.abs(a[0]), ay = Math.abs(a[1]), az = Math.abs(a[2]);
20
- const other = ax <= ay && ax <= az ? [1, 0, 0] : ay <= az ? [0, 1, 0] : [0, 0, 1];
21
- return normalize(cross(a, other));
22
- }
23
-
24
- /** Rotate v around unit axis k by angle (Rodrigues). */
25
- export function rotateAround(v, k, angle) {
26
- const c = Math.cos(angle), s = Math.sin(angle);
27
- const kv = cross(k, v);
28
- const kd = dot(k, v) * (1 - c);
29
- return [v[0] * c + kv[0] * s + k[0] * kd, v[1] * c + kv[1] * s + k[1] * kd, v[2] * c + kv[2] * s + k[2] * kd];
30
- }
31
-
32
- /** Axis-aligned bounding box helpers: {min:[3], max:[3]} */
33
- export function emptyBounds() {
34
- return { min: [Infinity, Infinity, Infinity], max: [-Infinity, -Infinity, -Infinity] };
35
- }
36
-
37
- export function boundsIsEmpty(b) {
38
- return !(b.min[0] <= b.max[0]);
39
- }
40
-
41
- export function expandBounds(b, x, y, z) {
42
- if (x < b.min[0]) b.min[0] = x;
43
- if (y < b.min[1]) b.min[1] = y;
44
- if (z < b.min[2]) b.min[2] = z;
45
- if (x > b.max[0]) b.max[0] = x;
46
- if (y > b.max[1]) b.max[1] = y;
47
- if (z > b.max[2]) b.max[2] = z;
48
- return b;
49
- }
50
-
51
- export function unionBounds(a, b) {
52
- expandBounds(a, b.min[0], b.min[1], b.min[2]);
53
- expandBounds(a, b.max[0], b.max[1], b.max[2]);
54
- return a;
55
- }
56
-
57
- /** Transform an AABB by a matrix (returns the AABB of the transformed box). */
58
- export function transformBounds(b, m) {
59
- const out = emptyBounds();
60
- if (boundsIsEmpty(b)) return out;
61
- for (let i = 0; i < 8; i++) {
62
- const x = i & 1 ? b.max[0] : b.min[0];
63
- const y = i & 2 ? b.max[1] : b.min[1];
64
- const z = i & 4 ? b.max[2] : b.min[2];
65
- expandBounds(
66
- out,
67
- m[0] * x + m[4] * y + m[8] * z + m[12],
68
- m[1] * x + m[5] * y + m[9] * z + m[13],
69
- m[2] * x + m[6] * y + m[10] * z + m[14],
70
- );
71
- }
72
- return out;
73
- }
74
-
75
- export function boundsCenter(b) {
76
- return [(b.min[0] + b.max[0]) / 2, (b.min[1] + b.max[1]) / 2, (b.min[2] + b.max[2]) / 2];
77
- }
78
-
79
- export function boundsRadius(b) {
80
- return Math.hypot(b.max[0] - b.min[0], b.max[1] - b.min[1], b.max[2] - b.min[2]) / 2;
81
- }
1
+ // Small vector helpers operating on plain arrays [x, y, z].
2
+
3
+ export const add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
4
+ export const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
5
+ export const scale = (a, s) => [a[0] * s, a[1] * s, a[2] * s];
6
+ export const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
7
+ export const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
8
+ export const length = (a) => Math.hypot(a[0], a[1], a[2]);
9
+ export const distance = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
10
+ export const lerp = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];
11
+
12
+ export function normalize(a) {
13
+ const l = Math.hypot(a[0], a[1], a[2]);
14
+ return l > 1e-20 ? [a[0] / l, a[1] / l, a[2] / l] : [0, 0, 0];
15
+ }
16
+
17
+ /** Any unit vector perpendicular to a (unit) vector. */
18
+ export function perpendicular(a) {
19
+ const ax = Math.abs(a[0]), ay = Math.abs(a[1]), az = Math.abs(a[2]);
20
+ const other = ax <= ay && ax <= az ? [1, 0, 0] : ay <= az ? [0, 1, 0] : [0, 0, 1];
21
+ return normalize(cross(a, other));
22
+ }
23
+
24
+ /** Rotate v around unit axis k by angle (Rodrigues). */
25
+ export function rotateAround(v, k, angle) {
26
+ const c = Math.cos(angle), s = Math.sin(angle);
27
+ const kv = cross(k, v);
28
+ const kd = dot(k, v) * (1 - c);
29
+ return [v[0] * c + kv[0] * s + k[0] * kd, v[1] * c + kv[1] * s + k[1] * kd, v[2] * c + kv[2] * s + k[2] * kd];
30
+ }
31
+
32
+ /** Axis-aligned bounding box helpers: {min:[3], max:[3]} */
33
+ export function emptyBounds() {
34
+ return { min: [Infinity, Infinity, Infinity], max: [-Infinity, -Infinity, -Infinity] };
35
+ }
36
+
37
+ export function boundsIsEmpty(b) {
38
+ return !(b.min[0] <= b.max[0]);
39
+ }
40
+
41
+ export function expandBounds(b, x, y, z) {
42
+ if (x < b.min[0]) b.min[0] = x;
43
+ if (y < b.min[1]) b.min[1] = y;
44
+ if (z < b.min[2]) b.min[2] = z;
45
+ if (x > b.max[0]) b.max[0] = x;
46
+ if (y > b.max[1]) b.max[1] = y;
47
+ if (z > b.max[2]) b.max[2] = z;
48
+ return b;
49
+ }
50
+
51
+ export function unionBounds(a, b) {
52
+ expandBounds(a, b.min[0], b.min[1], b.min[2]);
53
+ expandBounds(a, b.max[0], b.max[1], b.max[2]);
54
+ return a;
55
+ }
56
+
57
+ /** Transform an AABB by a matrix (returns the AABB of the transformed box). */
58
+ export function transformBounds(b, m) {
59
+ const out = emptyBounds();
60
+ if (boundsIsEmpty(b)) return out;
61
+ for (let i = 0; i < 8; i++) {
62
+ const x = i & 1 ? b.max[0] : b.min[0];
63
+ const y = i & 2 ? b.max[1] : b.min[1];
64
+ const z = i & 4 ? b.max[2] : b.min[2];
65
+ expandBounds(
66
+ out,
67
+ m[0] * x + m[4] * y + m[8] * z + m[12],
68
+ m[1] * x + m[5] * y + m[9] * z + m[13],
69
+ m[2] * x + m[6] * y + m[10] * z + m[14],
70
+ );
71
+ }
72
+ return out;
73
+ }
74
+
75
+ export function boundsCenter(b) {
76
+ return [(b.min[0] + b.max[0]) / 2, (b.min[1] + b.max[1]) / 2, (b.min[2] + b.max[2]) / 2];
77
+ }
78
+
79
+ export function boundsRadius(b) {
80
+ return Math.hypot(b.max[0] - b.min[0], b.max[1] - b.min[1], b.max[2] - b.min[2]) / 2;
81
+ }