@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,92 +1,132 @@
1
- // Entry point for reading ES3D documents in any of the surface syntaxes:
2
- // markup (XML-like, canonical) <es3d version="1"> ... </es3d>
3
- // compact (ES3D-C) es3d version=1 box s=2 ...
4
- // JSON (JsonML-style) ["es3d", {"version":"1"}, ...]
5
- // All of them produce the same AST, which is then validated against the schema.
6
-
7
- import { Document } from './ast.js';
8
- import { Diagnostic, ES3DError, LineIndex } from './errors.js';
9
- import { parseXml } from './xml-parser.js';
10
- import { parseCompact } from './compact-parser.js';
11
- import { validate } from './validator.js';
12
- import { fromJsonTree } from './json.js';
13
-
14
- /** Detect the surface syntax of a text document. */
15
- export function detectSyntax(text) {
16
- let i = 0;
17
- const n = text.length;
18
- if (text.charCodeAt(0) === 0xfeff) i = 1;
19
- for (;;) {
20
- while (i < n && /\s/.test(text[i])) i++;
21
- if (text.startsWith('//', i)) {
22
- const e = text.indexOf('\n', i);
23
- i = e < 0 ? n : e + 1;
24
- continue;
25
- }
26
- if (text.startsWith('/*', i)) {
27
- const e = text.indexOf('*/', i + 2);
28
- i = e < 0 ? n : e + 2;
29
- continue;
30
- }
31
- break;
32
- }
33
- const c = text[i];
34
- if (c === '<') return 'xml';
35
- if (c === '[' || c === '{') return 'json';
36
- return 'compact';
37
- }
38
-
39
- function failedDocument(err, syntax, text, lineIndex) {
40
- const doc = new Document({ root: null, syntax, source: text });
41
- doc.lineIndex = lineIndex;
42
- doc.diagnostics = err instanceof ES3DError ? err.diagnostics : [new Diagnostic({ code: 'E_INTERNAL', message: String(err && err.message ? err.message : err) })];
43
- return doc;
44
- }
45
-
46
- /**
47
- * Parse (and by default validate) an ES3D document.
48
- * Never throws for malformed input: problems are reported in doc.diagnostics.
49
- * @param {string|ArrayBuffer|Uint8Array} input
50
- * @param {{syntax?: 'xml'|'compact'|'json', validate?: boolean}} [opts]
51
- * @returns {Document}
52
- */
53
- export function parse(input, opts = {}) {
54
- if (typeof input !== 'string') input = new TextDecoder('utf-8').decode(input instanceof Uint8Array ? input : new Uint8Array(input));
55
- const text = input;
56
- const syntax = opts.syntax || detectSyntax(text);
57
- const lineIndex = new LineIndex(text);
58
- let doc;
59
- try {
60
- if (syntax === 'xml') doc = parseXml(text, lineIndex);
61
- else if (syntax === 'compact') doc = parseCompact(text, lineIndex);
62
- else if (syntax === 'json') {
63
- let tree;
64
- try {
65
- tree = JSON.parse(text);
66
- } catch (e) {
67
- const m = /position (\d+)/.exec(e.message);
68
- const d = new Diagnostic({ code: 'E_JSON', message: `Invalid JSON: ${e.message}`, offset: m ? Number(m[1]) : 0, length: 1 });
69
- d.locate(lineIndex);
70
- throw new ES3DError([d], lineIndex);
71
- }
72
- doc = fromJsonTree(tree);
73
- doc.source = text;
74
- } else throw new Error(`Unknown syntax '${syntax}'`);
75
- } catch (e) {
76
- return failedDocument(e, syntax, text, lineIndex);
77
- }
78
- if (!doc.lineIndex && syntax !== 'json') doc.lineIndex = lineIndex;
79
- return opts.validate === false ? doc : validate(doc);
80
- }
81
-
82
- /** Parse and throw an ES3DError when the document has errors. */
83
- export function parseStrict(input, opts = {}) {
84
- const doc = parse(input, opts);
85
- if (!doc.ok) throw new ES3DError(doc.errors, doc.lineIndex);
86
- return doc;
87
- }
88
-
89
- /** Human readable report of all diagnostics. */
90
- export function formatDiagnostics(doc) {
91
- return doc.diagnostics.map((d) => d.format(doc.lineIndex)).join('\n\n');
92
- }
1
+ // Entry point for reading ES3D documents in any of the surface syntaxes:
2
+ // markup (XML-like, canonical) <es3d version="1"> ... </es3d>
3
+ // compact (ES3D-C) es3d version=1 box s=2 ...
4
+ // JSON (JsonML-style) ["es3d", {"version":"1"}, ...]
5
+ // All of them produce the same AST, which is then validated against the schema.
6
+
7
+ import { Document } from './ast.js';
8
+ import { Diagnostic, ES3DError, LineIndex } from './errors.js';
9
+ import { parseXml } from './xml-parser.js';
10
+ import { parseCompact } from './compact-parser.js';
11
+ import { validate } from './validator.js';
12
+ import { fromJsonTree } from './json.js';
13
+
14
+ /** Detect the surface syntax of a text document. */
15
+ export function detectSyntax(text) {
16
+ let i = 0;
17
+ const n = text.length;
18
+ if (text.charCodeAt(0) === 0xfeff) i = 1;
19
+ for (;;) {
20
+ while (i < n && /\s/.test(text[i])) i++;
21
+ if (text.startsWith('//', i)) {
22
+ const e = text.indexOf('\n', i);
23
+ i = e < 0 ? n : e + 1;
24
+ continue;
25
+ }
26
+ if (text.startsWith('/*', i)) {
27
+ const e = text.indexOf('*/', i + 2);
28
+ i = e < 0 ? n : e + 2;
29
+ continue;
30
+ }
31
+ break;
32
+ }
33
+ const c = text[i];
34
+ if (c === '<') return 'xml';
35
+ if (c === '[' || c === '{') return 'json';
36
+ return 'compact';
37
+ }
38
+
39
+ function failedDocument(err, syntax, text, lineIndex) {
40
+ const doc = new Document({ root: null, syntax, source: text });
41
+ doc.lineIndex = lineIndex;
42
+ doc.diagnostics = err instanceof ES3DError ? err.diagnostics : [new Diagnostic({ code: 'E_INTERNAL', message: String(err && err.message ? err.message : err) })];
43
+ return doc;
44
+ }
45
+
46
+ /**
47
+ * Parse (and by default validate) an ES3D document.
48
+ * Never throws for malformed input: problems are reported in doc.diagnostics.
49
+ * @param {string|ArrayBuffer|Uint8Array} input
50
+ * @param {{syntax?: 'xml'|'compact'|'json', validate?: boolean}} [opts]
51
+ * @returns {Document}
52
+ */
53
+ export function parse(input, opts = {}) {
54
+ if (typeof input !== 'string') input = new TextDecoder('utf-8').decode(input instanceof Uint8Array ? input : new Uint8Array(input));
55
+ const text = input;
56
+ const syntax = opts.syntax || detectSyntax(text);
57
+ const lineIndex = new LineIndex(text);
58
+ let doc;
59
+ try {
60
+ if (syntax === 'xml') doc = parseXml(text, lineIndex);
61
+ else if (syntax === 'compact') doc = parseCompact(text, lineIndex);
62
+ else if (syntax === 'json') {
63
+ let tree;
64
+ try {
65
+ tree = JSON.parse(text);
66
+ } catch (e) {
67
+ const m = /position (\d+)/.exec(e.message);
68
+ const d = new Diagnostic({ code: 'E_JSON', message: `Invalid JSON: ${e.message}`, offset: m ? Number(m[1]) : 0, length: 1 });
69
+ d.locate(lineIndex);
70
+ throw new ES3DError([d], lineIndex);
71
+ }
72
+ doc = fromJsonTree(tree);
73
+ doc.source = text;
74
+ } else throw new Error(`Unknown syntax '${syntax}'`);
75
+ } catch (e) {
76
+ return failedDocument(e, syntax, text, lineIndex);
77
+ }
78
+ if (!doc.lineIndex && syntax !== 'json') doc.lineIndex = lineIndex;
79
+ return opts.validate === false ? doc : validate(doc);
80
+ }
81
+
82
+ /** Parse and throw an ES3DError when the document has errors. */
83
+ export function parseStrict(input, opts = {}) {
84
+ const doc = parse(input, opts);
85
+ if (!doc.ok) throw new ES3DError(doc.errors, doc.lineIndex);
86
+ return doc;
87
+ }
88
+
89
+ /** Element name written in a message, if it has one (`<x-anchors>`). */
90
+ function mentionedElement(message) {
91
+ const m = /<([^<>\s]+)>/.exec(message || '');
92
+ return m ? m[1] : '';
93
+ }
94
+
95
+ /**
96
+ * Human-readable report of diagnostics.
97
+ * Repeated warnings that share a code, the element name in the message, and
98
+ * the same message are shown once, at the first location, with a count.
99
+ * Errors are listed one by one. The diagnostic objects are not changed.
100
+ * @param {object|Array} source a document, a compile result, or a diagnostic list
101
+ * @param {import('./errors.js').LineIndex} [lineIndex]
102
+ */
103
+ export function formatDiagnostics(source, lineIndex) {
104
+ if (!source) return '';
105
+ const list = Array.isArray(source) ? source : source.diagnostics || [];
106
+ const index = lineIndex || source.lineIndex || (source.doc && source.doc.lineIndex) || null;
107
+ const seen = new Map();
108
+ const items = [];
109
+ for (const d of list) {
110
+ if (!d || d.severity !== 'warning') {
111
+ items.push(d);
112
+ continue;
113
+ }
114
+ const key = `${d.code}\0${mentionedElement(d.message)}\0${d.message}`;
115
+ const prev = seen.get(key);
116
+ if (prev) {
117
+ prev.count++;
118
+ continue;
119
+ }
120
+ const slot = { d, count: 1 };
121
+ seen.set(key, slot);
122
+ items.push(slot);
123
+ }
124
+ return items
125
+ .map((item) => {
126
+ if (!item || !item.d) return item.format(index);
127
+ const text = item.d.format(index);
128
+ if (item.count < 2) return text;
129
+ return `${text}\n\nReported ${item.count} times. Showing the first.`;
130
+ })
131
+ .join('\n\n');
132
+ }