@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,127 +1,127 @@
1
- // Shared helpers for exporters and importers of conventional mesh formats.
2
-
3
- import { RenderScene } from '../scene/flatten.js';
4
- import { Material, DEFAULT_MATERIAL_PARAMS } from '../material/materials.js';
5
-
6
- /**
7
- * Instances that a static exporter should write: every instance outside <lod>
8
- * switches, and the most detailed alternative (child 0) inside them.
9
- */
10
- export function exportableInstances(scene) {
11
- const sw = scene.lodSwitches;
12
- const visible = new Uint8Array(sw.length);
13
- for (let s = 0; s < sw.length; s++) {
14
- const w = sw[s];
15
- visible[s] = w.parent < 0 ? 1 : visible[w.parent] && w.parentChild === 0 ? 1 : 0;
16
- }
17
- const out = [];
18
- for (let i = 0; i < scene.count; i++) {
19
- const ln = scene.lodNode[i];
20
- if (ln >= 0 && !(visible[ln] && scene.lodChild[i] === 0)) continue;
21
- out.push(i);
22
- }
23
- return out;
24
- }
25
-
26
- /**
27
- * Geometry indices that are drawn with a textured material. Exporters write
28
- * texture coordinates only for these (with `uvs: 'auto'`): UVs of untextured
29
- * procedural geometry are dead weight in a mesh file.
30
- */
31
- export function texturedGeometries(scene) {
32
- const out = new Set();
33
- for (let i = 0; i < scene.count; i++) {
34
- const m = scene.materials[scene.mat[i]];
35
- if (m && (m.map || m.normalMap)) out.add(scene.geom[i]);
36
- }
37
- return out;
38
- }
39
-
40
- /** Stable, unique names for materials (OBJ usemtl / glTF material names). */
41
- export function materialNames(materials) {
42
- const used = new Set();
43
- return materials.map((m, i) => {
44
- let base = String(m.name || `material${i}`).replace(/[^A-Za-z0-9_.-]/g, '_') || `material${i}`;
45
- let name = base;
46
- let k = 1;
47
- while (used.has(name)) name = `${base}_${k++}`;
48
- used.add(name);
49
- return name;
50
- });
51
- }
52
-
53
- /** Growable flat instance table (no per-instance allocations for large imports). */
54
- export class InstanceTable {
55
- constructor(capacity = 64) {
56
- this.count = 0;
57
- this.geom = new Int32Array(capacity);
58
- this.mat = new Int32Array(capacity);
59
- this.matrices = new Float64Array(capacity * 16);
60
- }
61
-
62
- push(geom, mat, matrix) {
63
- if (this.count === this.geom.length) {
64
- const cap = this.geom.length * 2;
65
- const g = new Int32Array(cap), m = new Int32Array(cap), x = new Float64Array(cap * 16);
66
- g.set(this.geom);
67
- m.set(this.mat);
68
- x.set(this.matrices);
69
- this.geom = g;
70
- this.mat = m;
71
- this.matrices = x;
72
- }
73
- const i = this.count++;
74
- this.geom[i] = geom;
75
- this.mat[i] = mat;
76
- const o = i * 16, x = this.matrices;
77
- for (let k = 0; k < 16; k++) x[o + k] = matrix[k];
78
- }
79
- }
80
-
81
- /**
82
- * Build a RenderScene from already tessellated meshes (OBJ / glTF imports).
83
- * @param {{geometries: {mesh: import('../geometry/mesh.js').Mesh, key?: string}[],
84
- * materials: object[], instances: InstanceTable}} data
85
- * materials are parameter objects in the ES3D material model (sRGB colors)
86
- */
87
- export function buildExternalScene(data) {
88
- const table = data.instances;
89
- const scene = new RenderScene(Math.max(16, table.count));
90
- scene.environment = { background: [0.09, 0.1, 0.12, 1], sky: [0.8, 0.86, 0.95, 1], ground: [0.25, 0.22, 0.2, 1], intensity: 0.5, exposure: 1 };
91
- scene.materials = data.materials.map((p, i) => new Material({ ...DEFAULT_MATERIAL_PARAMS, ...p }, i, p.name || `material${i}`));
92
- if (!scene.materials.length) scene.materials.push(new Material({ ...DEFAULT_MATERIAL_PARAMS }, 0, 'default'));
93
- const entries = data.geometries.map((g, i) => ({
94
- id: i + 1,
95
- key: g.key || `external:${i}`,
96
- kind: g.mesh.kind,
97
- lods: 1,
98
- doubleSided: false,
99
- bounds: g.mesh.bounds,
100
- meshes: [g.mesh],
101
- }));
102
- const gIndex = entries.map((e) => scene.geometryIndex(e));
103
- const lastMat = scene.materials.length - 1;
104
- const world = new Float64Array(16);
105
- for (let i = 0; i < table.count; i++) {
106
- for (let k = 0; k < 16; k++) world[k] = table.matrices[i * 16 + k];
107
- scene.setInstance(i, gIndex[table.geom[i]], Math.min(table.mat[i], lastMat), world, 0, -1, 0);
108
- }
109
- scene.count = table.count;
110
- if (!(scene.bounds.min[0] <= scene.bounds.max[0])) scene.bounds = { min: [-1, -1, -1], max: [1, 1, 1] };
111
- scene.version++;
112
- return { scene, meshOf: (entry) => entry.meshes[0] };
113
- }
114
-
115
- /** Trimmed fixed-point number formatting (used by text exporters). */
116
- export function fixed(v, digits) {
117
- if (!Number.isFinite(v)) return '0';
118
- let s = v.toFixed(digits);
119
- if (s.indexOf('.') >= 0) {
120
- let e = s.length;
121
- while (s.charCodeAt(e - 1) === 48) e--;
122
- if (s.charCodeAt(e - 1) === 46) e--;
123
- s = s.slice(0, e);
124
- }
125
- if (s === '-0') return '0';
126
- return s;
127
- }
1
+ // Shared helpers for exporters and importers of conventional mesh formats.
2
+
3
+ import { RenderScene } from '../scene/flatten.js';
4
+ import { Material, DEFAULT_MATERIAL_PARAMS } from '../material/materials.js';
5
+
6
+ /**
7
+ * Instances that a static exporter should write: every instance outside <lod>
8
+ * switches, and the most detailed alternative (child 0) inside them.
9
+ */
10
+ export function exportableInstances(scene) {
11
+ const sw = scene.lodSwitches;
12
+ const visible = new Uint8Array(sw.length);
13
+ for (let s = 0; s < sw.length; s++) {
14
+ const w = sw[s];
15
+ visible[s] = w.parent < 0 ? 1 : visible[w.parent] && w.parentChild === 0 ? 1 : 0;
16
+ }
17
+ const out = [];
18
+ for (let i = 0; i < scene.count; i++) {
19
+ const ln = scene.lodNode[i];
20
+ if (ln >= 0 && !(visible[ln] && scene.lodChild[i] === 0)) continue;
21
+ out.push(i);
22
+ }
23
+ return out;
24
+ }
25
+
26
+ /**
27
+ * Geometry indices that are drawn with a textured material. Exporters write
28
+ * texture coordinates only for these (with `uvs: 'auto'`): UVs of untextured
29
+ * procedural geometry are dead weight in a mesh file.
30
+ */
31
+ export function texturedGeometries(scene) {
32
+ const out = new Set();
33
+ for (let i = 0; i < scene.count; i++) {
34
+ const m = scene.materials[scene.mat[i]];
35
+ if (m && (m.map || m.normalMap)) out.add(scene.geom[i]);
36
+ }
37
+ return out;
38
+ }
39
+
40
+ /** Stable, unique names for materials (OBJ usemtl / glTF material names). */
41
+ export function materialNames(materials) {
42
+ const used = new Set();
43
+ return materials.map((m, i) => {
44
+ let base = String(m.name || `material${i}`).replace(/[^A-Za-z0-9_.-]/g, '_') || `material${i}`;
45
+ let name = base;
46
+ let k = 1;
47
+ while (used.has(name)) name = `${base}_${k++}`;
48
+ used.add(name);
49
+ return name;
50
+ });
51
+ }
52
+
53
+ /** Growable flat instance table (no per-instance allocations for large imports). */
54
+ export class InstanceTable {
55
+ constructor(capacity = 64) {
56
+ this.count = 0;
57
+ this.geom = new Int32Array(capacity);
58
+ this.mat = new Int32Array(capacity);
59
+ this.matrices = new Float64Array(capacity * 16);
60
+ }
61
+
62
+ push(geom, mat, matrix) {
63
+ if (this.count === this.geom.length) {
64
+ const cap = this.geom.length * 2;
65
+ const g = new Int32Array(cap), m = new Int32Array(cap), x = new Float64Array(cap * 16);
66
+ g.set(this.geom);
67
+ m.set(this.mat);
68
+ x.set(this.matrices);
69
+ this.geom = g;
70
+ this.mat = m;
71
+ this.matrices = x;
72
+ }
73
+ const i = this.count++;
74
+ this.geom[i] = geom;
75
+ this.mat[i] = mat;
76
+ const o = i * 16, x = this.matrices;
77
+ for (let k = 0; k < 16; k++) x[o + k] = matrix[k];
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Build a RenderScene from already tessellated meshes (OBJ / glTF imports).
83
+ * @param {{geometries: {mesh: import('../geometry/mesh.js').Mesh, key?: string}[],
84
+ * materials: object[], instances: InstanceTable}} data
85
+ * materials are parameter objects in the ES3D material model (sRGB colors)
86
+ */
87
+ export function buildExternalScene(data) {
88
+ const table = data.instances;
89
+ const scene = new RenderScene(Math.max(16, table.count));
90
+ scene.environment = { background: [0.09, 0.1, 0.12, 1], sky: [0.8, 0.86, 0.95, 1], ground: [0.25, 0.22, 0.2, 1], intensity: 0.5, exposure: 1 };
91
+ scene.materials = data.materials.map((p, i) => new Material({ ...DEFAULT_MATERIAL_PARAMS, ...p }, i, p.name || `material${i}`));
92
+ if (!scene.materials.length) scene.materials.push(new Material({ ...DEFAULT_MATERIAL_PARAMS }, 0, 'default'));
93
+ const entries = data.geometries.map((g, i) => ({
94
+ id: i + 1,
95
+ key: g.key || `external:${i}`,
96
+ kind: g.mesh.kind,
97
+ lods: 1,
98
+ doubleSided: false,
99
+ bounds: g.mesh.bounds,
100
+ meshes: [g.mesh],
101
+ }));
102
+ const gIndex = entries.map((e) => scene.geometryIndex(e));
103
+ const lastMat = scene.materials.length - 1;
104
+ const world = new Float64Array(16);
105
+ for (let i = 0; i < table.count; i++) {
106
+ for (let k = 0; k < 16; k++) world[k] = table.matrices[i * 16 + k];
107
+ scene.setInstance(i, gIndex[table.geom[i]], Math.min(table.mat[i], lastMat), world, 0, -1, 0);
108
+ }
109
+ scene.count = table.count;
110
+ if (!(scene.bounds.min[0] <= scene.bounds.max[0])) scene.bounds = { min: [-1, -1, -1], max: [1, 1, 1] };
111
+ scene.version++;
112
+ return { scene, meshOf: (entry) => entry.meshes[0] };
113
+ }
114
+
115
+ /** Trimmed fixed-point number formatting (used by text exporters). */
116
+ export function fixed(v, digits) {
117
+ if (!Number.isFinite(v)) return '0';
118
+ let s = v.toFixed(digits);
119
+ if (s.indexOf('.') >= 0) {
120
+ let e = s.length;
121
+ while (s.charCodeAt(e - 1) === 48) e--;
122
+ if (s.charCodeAt(e - 1) === 46) e--;
123
+ s = s.slice(0, e);
124
+ }
125
+ if (s === '-0') return '0';
126
+ return s;
127
+ }
@@ -1,28 +1,28 @@
1
- // File loading for ES3D Studio: detects the content type from magic bytes and
2
- // extension, transparently decompresses gzip, and converts foreign formats
3
- // (SVG, OBJ, STL, STEP, glTF) into ES3D text with the same importers as the
4
- // converter page, so they can be inspected and edited like any other document.
5
-
6
- import { isGzip, gunzip } from './compression.js';
7
- import { detectKind, importFiles, INPUT_KINDS } from './convert.js';
8
-
9
- /**
10
- * @param {File|{name:string, arrayBuffer:()=>Promise<ArrayBuffer>}} file
11
- * @returns {Promise<{name:string, kind:string, text?:string, bytes:Uint8Array, originalSize:number, note?:string}>}
12
- */
13
- export async function loadFile(file) {
14
- let bytes = new Uint8Array(await file.arrayBuffer());
15
- const originalSize = bytes.length;
16
- let name = file.name || 'untitled.es3d';
17
- let note = '';
18
- if (isGzip(bytes)) {
19
- bytes = await gunzip(bytes);
20
- name = name.replace(/\.svgz$/i, '.svg').replace(/\.gz$/i, '');
21
- note = `decompressed from ${originalSize.toLocaleString('en-US')} gzip bytes`;
22
- }
23
- const kind = detectKind(name, bytes) || 'es3d';
24
- if (kind === 'es3d') return { name, kind, text: new TextDecoder('utf-8').decode(bytes), bytes, originalSize, note };
25
- const r = await importFiles([{ name, bytes }]);
26
- const warn = r.warnings.length ? ` (${r.warnings.join(' ')})` : '';
27
- return { name, kind, bytes, originalSize, text: r.text, note: (note ? note + '; ' : '') + `converted from ${INPUT_KINDS[kind]} to ES3D${warn}` };
28
- }
1
+ // File loading for ES3D Studio: detects the content type from magic bytes and
2
+ // extension, transparently decompresses gzip, and converts foreign formats
3
+ // (SVG, OBJ, STL, STEP, glTF) into ES3D text with the same importers as the
4
+ // converter page, so they can be inspected and edited like any other document.
5
+
6
+ import { isGzip, gunzip } from './compression.js';
7
+ import { detectKind, importFiles, INPUT_KINDS } from './convert.js';
8
+
9
+ /**
10
+ * @param {File|{name:string, arrayBuffer:()=>Promise<ArrayBuffer>}} file
11
+ * @returns {Promise<{name:string, kind:string, text?:string, bytes:Uint8Array, originalSize:number, note?:string}>}
12
+ */
13
+ export async function loadFile(file) {
14
+ let bytes = new Uint8Array(await file.arrayBuffer());
15
+ const originalSize = bytes.length;
16
+ let name = file.name || 'untitled.es3d';
17
+ let note = '';
18
+ if (isGzip(bytes)) {
19
+ bytes = await gunzip(bytes);
20
+ name = name.replace(/\.svgz$/i, '.svg').replace(/\.gz$/i, '');
21
+ note = `decompressed from ${originalSize.toLocaleString('en-US')} gzip bytes`;
22
+ }
23
+ const kind = detectKind(name, bytes) || 'es3d';
24
+ if (kind === 'es3d') return { name, kind, text: new TextDecoder('utf-8').decode(bytes), bytes, originalSize, note };
25
+ const r = await importFiles([{ name, bytes }]);
26
+ const warn = r.warnings.length ? ` (${r.warnings.join(' ')})` : '';
27
+ return { name, kind, bytes, originalSize, text: r.text, note: (note ? note + '; ' : '') + `converted from ${INPUT_KINDS[kind]} to ES3D${warn}` };
28
+ }