@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,104 +1,104 @@
1
- // Conversion of tessellated scenes (OBJ / STL / glTF imports) into ES3D documents.
2
- //
3
- // Every distinct mesh becomes one <mesh> definition in <defs>, every instance
4
- // a <use> with its transform, every material a <material>. This is the
5
- // "escape hatch" path: the result is valid ES3D but not procedural, so it is
6
- // about as large as the source mesh format (see the report).
7
-
8
- import { h, Document } from '../format/ast.js';
9
- import { serialize } from '../format/serializer.js';
10
- import { formatNumber, formatFloat32 } from '../format/value-format.js';
11
- import { matrixToTransformString } from '../transform/transform.js';
12
- import { linearToSrgb, colorToHex } from '../format/colors.js';
13
- import { weldMap } from '../geometry/mesh.js';
14
- import { materialNames } from './external-scene.js';
15
-
16
- /** Decimals that keep coordinates to `relative` of the scene size (e.g. 1e-5). */
17
- export function decimalsFor(bounds, relative) {
18
- const size = Math.max(bounds.max[0] - bounds.min[0], bounds.max[1] - bounds.min[1], bounds.max[2] - bounds.min[2]);
19
- if (!(size > 0) || !Number.isFinite(size)) return 5;
20
- return Math.max(0, Math.min(9, Math.ceil(-Math.log10(size * relative))));
21
- }
22
-
23
- /** Merge vertices with equal positions (when neither normals nor UVs are written, they are redundant). */
24
- function welded(mesh) {
25
- const map = weldMap(mesh.positions);
26
- const n = mesh.vertexCount;
27
- const remap = new Int32Array(n).fill(-1);
28
- let unique = 0;
29
- for (let i = 0; i < n; i++) if (remap[map[i]] < 0) remap[map[i]] = unique++;
30
- if (unique === n) return { positions: mesh.positions, indices: mesh.indices };
31
- const positions = new Float32Array(unique * 3);
32
- for (let i = 0; i < n; i++) {
33
- const r = remap[map[i]] * 3;
34
- positions[r] = mesh.positions[i * 3];
35
- positions[r + 1] = mesh.positions[i * 3 + 1];
36
- positions[r + 2] = mesh.positions[i * 3 + 2];
37
- }
38
- const indices = new Uint32Array(mesh.indices.length);
39
- for (let i = 0; i < indices.length; i++) indices[i] = remap[map[mesh.indices[i]]];
40
- return { positions, indices };
41
- }
42
-
43
- /**
44
- * @param {import('../scene/flatten.js').RenderScene} scene
45
- * @param {(entry: object, lod: number) => import('../geometry/mesh.js').Mesh} meshOf
46
- * @param {string} [title]
47
- * @param {{digits?: number|null|'auto', normals?: boolean, uvs?: boolean|'auto', smooth?: number, relative?: number}} [opts]
48
- * digits: decimals for positions ('auto' = relative precision of the scene size, null = exact float32);
49
- * normals: false omits them, and the reader recomputes them with the crease angle `smooth`;
50
- * uvs: 'auto' writes texture coordinates only when they are not all zero
51
- * @returns {string} ES3D markup
52
- */
53
- export function sceneToMeshDocument(scene, meshOf, title = 'Imported mesh', { digits = 'auto', normals = true, uvs = 'auto', smooth = 30, relative = 1e-5 } = {}) {
54
- const decimals = digits === 'auto' ? decimalsFor(scene.bounds, relative) : digits;
55
- const fmt = decimals === null ? (v) => formatFloat32(v) : (v) => formatNumber(v, decimals);
56
- const names = materialNames(scene.materials);
57
- const defs = h('defs');
58
- scene.materials.forEach((m, i) => {
59
- const attrs = { id: names[i], color: colorToHex([linearToSrgb(m.baseColor[0]), linearToSrgb(m.baseColor[1]), linearToSrgb(m.baseColor[2]), 1]) };
60
- if (m.metallic) attrs.metallic = formatNumber(m.metallic, 3);
61
- if (m.roughness !== 0.5) attrs.roughness = formatNumber(m.roughness, 3);
62
- if (m.opacity < 1) attrs.opacity = formatNumber(m.opacity, 3);
63
- if (m.alphaMode === 'mask') {
64
- attrs.alphaMode = 'mask';
65
- if (m.alphaCutoff !== 0.5) attrs.alphaCutoff = formatNumber(m.alphaCutoff, 3);
66
- } else if (m.alphaMode === 'blend') attrs.alphaMode = 'blend';
67
- const peak = Math.max(...m.emissive);
68
- if (peak > 0) {
69
- // emissive is stored linear and pre-multiplied by its intensity
70
- const k = peak > 1 ? 1 / peak : 1;
71
- attrs.emissive = colorToHex([linearToSrgb(m.emissive[0] * k), linearToSrgb(m.emissive[1] * k), linearToSrgb(m.emissive[2] * k), 1]);
72
- if (peak > 1) attrs.emissiveIntensity = formatNumber(peak, 3);
73
- }
74
- if (m.doubleSided) attrs.doubleSided = 'true';
75
- defs.appendChild(h('material', attrs));
76
- });
77
- const meshIds = new Map();
78
- scene.geometries.forEach((entry, gi) => {
79
- const mesh = meshOf(entry, 0);
80
- if (mesh.kind !== 'triangles' || !mesh.vertexCount) return;
81
- const id = `mesh${gi}`;
82
- meshIds.set(gi, id);
83
- const writeUvs = uvs === true || (uvs === 'auto' && mesh.uvs.some((v) => v !== 0));
84
- const data = !normals && !writeUvs ? welded(mesh) : mesh;
85
- const attrs = { id, positions: Array.from(data.positions, fmt).join(' ') };
86
- if (normals) attrs.normals = Array.from(mesh.normals, (v) => formatNumber(v, 2)).join(' ');
87
- if (writeUvs) attrs.uvs = Array.from(mesh.uvs, (v) => formatNumber(v, 4)).join(' ');
88
- attrs.indices = Array.from(data.indices).join(' ');
89
- if (!normals && smooth !== 30) attrs.smooth = formatNumber(smooth, 2);
90
- defs.appendChild(h('mesh', attrs));
91
- });
92
- const root = h('es3d', { version: '1' }, h('title', {}, String(title)), defs);
93
- const m = new Float64Array(16);
94
- for (let i = 0; i < scene.count; i++) {
95
- const id = meshIds.get(scene.geom[i]);
96
- if (!id) continue;
97
- for (let k = 0; k < 16; k++) m[k] = scene.matrices[i * 16 + k];
98
- const attrs = { ref: id, material: names[scene.mat[i]] };
99
- const t = matrixToTransformString(m, (v) => formatNumber(v, 6));
100
- if (t) attrs.transform = t;
101
- root.appendChild(h('use', attrs));
102
- }
103
- return serialize(new Document({ root, syntax: 'xml' }), { pretty: true });
104
- }
1
+ // Conversion of tessellated scenes (OBJ / STL / glTF imports) into ES3D documents.
2
+ //
3
+ // Every distinct mesh becomes one <mesh> definition in <defs>, every instance
4
+ // a <use> with its transform, every material a <material>. This is the
5
+ // "escape hatch" path: the result is valid ES3D but not procedural, so it is
6
+ // about as large as the source mesh format (see the report).
7
+
8
+ import { h, Document } from '../format/ast.js';
9
+ import { serialize } from '../format/serializer.js';
10
+ import { formatNumber, formatFloat32 } from '../format/value-format.js';
11
+ import { matrixToTransformString } from '../transform/transform.js';
12
+ import { linearToSrgb, colorToHex } from '../format/colors.js';
13
+ import { weldMap } from '../geometry/mesh.js';
14
+ import { materialNames } from './external-scene.js';
15
+
16
+ /** Decimals that keep coordinates to `relative` of the scene size (e.g. 1e-5). */
17
+ export function decimalsFor(bounds, relative) {
18
+ const size = Math.max(bounds.max[0] - bounds.min[0], bounds.max[1] - bounds.min[1], bounds.max[2] - bounds.min[2]);
19
+ if (!(size > 0) || !Number.isFinite(size)) return 5;
20
+ return Math.max(0, Math.min(9, Math.ceil(-Math.log10(size * relative))));
21
+ }
22
+
23
+ /** Merge vertices with equal positions (when neither normals nor UVs are written, they are redundant). */
24
+ function welded(mesh) {
25
+ const map = weldMap(mesh.positions);
26
+ const n = mesh.vertexCount;
27
+ const remap = new Int32Array(n).fill(-1);
28
+ let unique = 0;
29
+ for (let i = 0; i < n; i++) if (remap[map[i]] < 0) remap[map[i]] = unique++;
30
+ if (unique === n) return { positions: mesh.positions, indices: mesh.indices };
31
+ const positions = new Float32Array(unique * 3);
32
+ for (let i = 0; i < n; i++) {
33
+ const r = remap[map[i]] * 3;
34
+ positions[r] = mesh.positions[i * 3];
35
+ positions[r + 1] = mesh.positions[i * 3 + 1];
36
+ positions[r + 2] = mesh.positions[i * 3 + 2];
37
+ }
38
+ const indices = new Uint32Array(mesh.indices.length);
39
+ for (let i = 0; i < indices.length; i++) indices[i] = remap[map[mesh.indices[i]]];
40
+ return { positions, indices };
41
+ }
42
+
43
+ /**
44
+ * @param {import('../scene/flatten.js').RenderScene} scene
45
+ * @param {(entry: object, lod: number) => import('../geometry/mesh.js').Mesh} meshOf
46
+ * @param {string} [title]
47
+ * @param {{digits?: number|null|'auto', normals?: boolean, uvs?: boolean|'auto', smooth?: number, relative?: number}} [opts]
48
+ * digits: decimals for positions ('auto' = relative precision of the scene size, null = exact float32);
49
+ * normals: false omits them, and the reader recomputes them with the crease angle `smooth`;
50
+ * uvs: 'auto' writes texture coordinates only when they are not all zero
51
+ * @returns {string} ES3D markup
52
+ */
53
+ export function sceneToMeshDocument(scene, meshOf, title = 'Imported mesh', { digits = 'auto', normals = true, uvs = 'auto', smooth = 30, relative = 1e-5 } = {}) {
54
+ const decimals = digits === 'auto' ? decimalsFor(scene.bounds, relative) : digits;
55
+ const fmt = decimals === null ? (v) => formatFloat32(v) : (v) => formatNumber(v, decimals);
56
+ const names = materialNames(scene.materials);
57
+ const defs = h('defs');
58
+ scene.materials.forEach((m, i) => {
59
+ const attrs = { id: names[i], color: colorToHex([linearToSrgb(m.baseColor[0]), linearToSrgb(m.baseColor[1]), linearToSrgb(m.baseColor[2]), 1]) };
60
+ if (m.metallic) attrs.metallic = formatNumber(m.metallic, 3);
61
+ if (m.roughness !== 0.5) attrs.roughness = formatNumber(m.roughness, 3);
62
+ if (m.opacity < 1) attrs.opacity = formatNumber(m.opacity, 3);
63
+ if (m.alphaMode === 'mask') {
64
+ attrs.alphaMode = 'mask';
65
+ if (m.alphaCutoff !== 0.5) attrs.alphaCutoff = formatNumber(m.alphaCutoff, 3);
66
+ } else if (m.alphaMode === 'blend') attrs.alphaMode = 'blend';
67
+ const peak = Math.max(...m.emissive);
68
+ if (peak > 0) {
69
+ // emissive is stored linear and pre-multiplied by its intensity
70
+ const k = peak > 1 ? 1 / peak : 1;
71
+ attrs.emissive = colorToHex([linearToSrgb(m.emissive[0] * k), linearToSrgb(m.emissive[1] * k), linearToSrgb(m.emissive[2] * k), 1]);
72
+ if (peak > 1) attrs.emissiveIntensity = formatNumber(peak, 3);
73
+ }
74
+ if (m.doubleSided) attrs.doubleSided = 'true';
75
+ defs.appendChild(h('material', attrs));
76
+ });
77
+ const meshIds = new Map();
78
+ scene.geometries.forEach((entry, gi) => {
79
+ const mesh = meshOf(entry, 0);
80
+ if (mesh.kind !== 'triangles' || !mesh.vertexCount) return;
81
+ const id = `mesh${gi}`;
82
+ meshIds.set(gi, id);
83
+ const writeUvs = uvs === true || (uvs === 'auto' && mesh.uvs.some((v) => v !== 0));
84
+ const data = !normals && !writeUvs ? welded(mesh) : mesh;
85
+ const attrs = { id, positions: Array.from(data.positions, fmt).join(' ') };
86
+ if (normals) attrs.normals = Array.from(mesh.normals, (v) => formatNumber(v, 2)).join(' ');
87
+ if (writeUvs) attrs.uvs = Array.from(mesh.uvs, (v) => formatNumber(v, 4)).join(' ');
88
+ attrs.indices = Array.from(data.indices).join(' ');
89
+ if (!normals && smooth !== 30) attrs.smooth = formatNumber(smooth, 2);
90
+ defs.appendChild(h('mesh', attrs));
91
+ });
92
+ const root = h('es3d', { version: '1' }, h('title', {}, String(title)), defs);
93
+ const m = new Float64Array(16);
94
+ for (let i = 0; i < scene.count; i++) {
95
+ const id = meshIds.get(scene.geom[i]);
96
+ if (!id) continue;
97
+ for (let k = 0; k < 16; k++) m[k] = scene.matrices[i * 16 + k];
98
+ const attrs = { ref: id, material: names[scene.mat[i]] };
99
+ const t = matrixToTransformString(m, (v) => formatNumber(v, 6));
100
+ if (t) attrs.transform = t;
101
+ root.appendChild(h('use', attrs));
102
+ }
103
+ return serialize(new Document({ root, syntax: 'xml' }), { pretty: true });
104
+ }