@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,171 +1,171 @@
1
- // Parser for the ES3D compact syntax: tokens -> the same AST as the markup syntax.
2
- //
3
- // Grammar (newlines are whitespace):
4
- // document := comment* root
5
- // root := 'es3d' header ( '{' statement* '}' | statement* )
6
- // statement := element | comment | ';'
7
- // element := NAME header ( '{' statement* '}' )?
8
- // header := ('#' ID)? ('@' REF)? STRING? (NAME '=' VALUE)*
9
-
10
- import { Attr, Comment, Document, Element } from './ast.js';
11
- import { Diagnostic, ES3DError, LineIndex } from './errors.js';
12
- import { CTok, CompactTokenizer } from './compact-tokenizer.js';
13
-
14
- const ROOT_NAMES = new Set(['es3d', 'v3d']);
15
-
16
- export function parseCompact(text, lineIndex = new LineIndex(text)) {
17
- const tz = new CompactTokenizer(text, lineIndex);
18
- const buf = [];
19
- const peek = (k = 0) => {
20
- while (buf.length <= k) buf.push(tz.next());
21
- return buf[k];
22
- };
23
- const take = () => (buf.length ? buf.shift() : tz.next());
24
- const fail = (code, message, offset, length = 1, extra = {}) => {
25
- const d = new Diagnostic({ code, message, offset, length, ...extra });
26
- d.locate(lineIndex);
27
- throw new ES3DError([d], lineIndex);
28
- };
29
- const where = (offset) => {
30
- const p = lineIndex.position(offset);
31
- return `line ${p.line}, column ${p.column}`;
32
- };
33
-
34
- function parseHeader(el) {
35
- let last = el.nameEnd;
36
- for (;;) {
37
- const t = peek();
38
- if (t.type === CTok.ID || t.type === CTok.REF) {
39
- take();
40
- const name = t.type === CTok.ID ? 'id' : 'ref';
41
- if (el.attrNode(name)) fail('E_DUPLICATE_ATTRIBUTE', `Duplicate attribute '${name}' on ${el.name}.`, t.start, t.end - t.start);
42
- el.attrs.push(new Attr(name, t.value, t.start, t.start + 1, t.start + 1, t.end, t.type === CTok.ID ? '#' : '@'));
43
- last = t.end;
44
- } else if (t.type === CTok.STRING) {
45
- take();
46
- if (el.text !== null) fail('E_SYNTAX', `${el.name} already has text content.`, t.start, t.end - t.start);
47
- el.text = t.value;
48
- el.textStart = t.start;
49
- el.textEnd = t.end;
50
- last = t.end;
51
- } else if (t.type === CTok.NAME && peek(1).type === CTok.EQ) {
52
- take();
53
- take();
54
- const v = take();
55
- if (v.type !== CTok.VALUE) fail('E_MISSING_VALUE', `Expected a value for attribute '${t.value}'.`, v.start, 1);
56
- for (const prev of el.attrs) {
57
- if (prev.name === t.value) fail('E_DUPLICATE_ATTRIBUTE', `Duplicate attribute '${t.value}' on ${el.name}.`, t.start, t.end - t.start);
58
- }
59
- const a = new Attr(t.value, v.value, t.start, t.end, v.valueStart, v.valueEnd, v.quote);
60
- a.decoded = !!v.escaped;
61
- el.attrs.push(a);
62
- last = v.end;
63
- } else {
64
- break;
65
- }
66
- }
67
- el.openEnd = last;
68
- }
69
-
70
- function parseStatements(parent, closeTok) {
71
- for (;;) {
72
- const t = peek();
73
- if (t.type === CTok.EOF) {
74
- if (closeTok) {
75
- fail('E_UNCLOSED_ELEMENT', `Unclosed block of '${parent.name}' (opened at ${where(closeTok.start)}): missing '}'.`, closeTok.start, 1);
76
- }
77
- return;
78
- }
79
- if (t.type === CTok.RBRACE) {
80
- if (!closeTok) fail('E_UNEXPECTED_CLOSE', "Unexpected '}': no block is open.", t.start, 1);
81
- take();
82
- parent.closeStart = t.start;
83
- parent.end = t.end;
84
- return;
85
- }
86
- if (t.type === CTok.SEMI) {
87
- take();
88
- continue;
89
- }
90
- if (t.type === CTok.COMMENT) {
91
- take();
92
- parent.appendChild(new Comment(t.text, t.start, t.end));
93
- continue;
94
- }
95
- if (t.type === CTok.NAME) {
96
- if (peek(1).type === CTok.EQ) {
97
- fail('E_SYNTAX', `Attribute '${t.value}' must follow an element name.`, t.start, t.end - t.start, {
98
- hint: 'Attributes belong to the element written before them on the same statement.',
99
- });
100
- }
101
- parent.appendChild(parseElement());
102
- continue;
103
- }
104
- if (t.type === CTok.LBRACE) fail('E_SYNTAX', "Unexpected '{': a block must follow an element name.", t.start, 1);
105
- if (t.type === CTok.STRING || t.type === CTok.ID || t.type === CTok.REF) {
106
- fail('E_SYNTAX', 'Expected an element name.', t.start, t.end - t.start);
107
- }
108
- fail('E_SYNTAX', 'Unexpected token.', t.start, Math.max(1, t.end - t.start));
109
- }
110
- }
111
-
112
- function parseElement() {
113
- const t = take();
114
- const el = new Element(t.value, t.start);
115
- el.nameStart = t.start;
116
- el.nameEnd = t.end;
117
- parseHeader(el);
118
- el.end = el.openEnd;
119
- if (peek().type === CTok.LBRACE) {
120
- const open = take();
121
- el.selfClosing = false;
122
- parseStatements(el, open);
123
- } else {
124
- el.selfClosing = true;
125
- }
126
- return el;
127
- }
128
-
129
- const leading = [];
130
- while (peek().type === CTok.COMMENT || peek().type === CTok.SEMI) {
131
- const c = take();
132
- if (c.type === CTok.COMMENT) leading.push(new Comment(c.text, c.start, c.end));
133
- }
134
- const first = peek();
135
- if (first.type === CTok.EOF) fail('E_EMPTY_DOCUMENT', "Empty document: expected 'es3d version=1'.", first.start, 0);
136
- if (first.type !== CTok.NAME || !ROOT_NAMES.has(first.value)) {
137
- fail('E_COMPACT_ROOT', "An ES3D compact document must start with the root element 'es3d'.", first.start, Math.max(1, first.end - first.start), {
138
- hint: "Begin the file with: es3d version=1",
139
- });
140
- }
141
- const rt = take();
142
- const root = new Element(rt.value, rt.start);
143
- root.nameStart = rt.start;
144
- root.nameEnd = rt.end;
145
- parseHeader(root);
146
- root.end = root.openEnd;
147
- const trailing = [];
148
- if (peek().type === CTok.LBRACE) {
149
- const open = take();
150
- root.selfClosing = false;
151
- parseStatements(root, open);
152
- for (;;) {
153
- const t = peek();
154
- if (t.type === CTok.EOF) break;
155
- if (t.type === CTok.COMMENT) {
156
- take();
157
- trailing.push(new Comment(t.text, t.start, t.end));
158
- } else if (t.type === CTok.SEMI) take();
159
- else fail('E_MULTIPLE_ROOTS', 'Content after the end of the root block.', t.start, Math.max(1, t.end - t.start));
160
- }
161
- } else {
162
- // Header form: everything after the root header belongs to the root.
163
- root.headerForm = true;
164
- parseStatements(root, null);
165
- root.selfClosing = root.children.length === 0;
166
- root.end = text.length;
167
- }
168
- const doc = new Document({ root, syntax: 'compact', source: text, leading, trailing });
169
- doc.lineIndex = lineIndex;
170
- return doc;
171
- }
1
+ // Parser for the ES3D compact syntax: tokens -> the same AST as the markup syntax.
2
+ //
3
+ // Grammar (newlines are whitespace):
4
+ // document := comment* root
5
+ // root := 'es3d' header ( '{' statement* '}' | statement* )
6
+ // statement := element | comment | ';'
7
+ // element := NAME header ( '{' statement* '}' )?
8
+ // header := ('#' ID)? ('@' REF)? STRING? (NAME '=' VALUE)*
9
+
10
+ import { Attr, Comment, Document, Element } from './ast.js';
11
+ import { Diagnostic, ES3DError, LineIndex } from './errors.js';
12
+ import { CTok, CompactTokenizer } from './compact-tokenizer.js';
13
+
14
+ const ROOT_NAMES = new Set(['es3d', 'v3d']);
15
+
16
+ export function parseCompact(text, lineIndex = new LineIndex(text)) {
17
+ const tz = new CompactTokenizer(text, lineIndex);
18
+ const buf = [];
19
+ const peek = (k = 0) => {
20
+ while (buf.length <= k) buf.push(tz.next());
21
+ return buf[k];
22
+ };
23
+ const take = () => (buf.length ? buf.shift() : tz.next());
24
+ const fail = (code, message, offset, length = 1, extra = {}) => {
25
+ const d = new Diagnostic({ code, message, offset, length, ...extra });
26
+ d.locate(lineIndex);
27
+ throw new ES3DError([d], lineIndex);
28
+ };
29
+ const where = (offset) => {
30
+ const p = lineIndex.position(offset);
31
+ return `line ${p.line}, column ${p.column}`;
32
+ };
33
+
34
+ function parseHeader(el) {
35
+ let last = el.nameEnd;
36
+ for (;;) {
37
+ const t = peek();
38
+ if (t.type === CTok.ID || t.type === CTok.REF) {
39
+ take();
40
+ const name = t.type === CTok.ID ? 'id' : 'ref';
41
+ if (el.attrNode(name)) fail('E_DUPLICATE_ATTRIBUTE', `Duplicate attribute '${name}' on ${el.name}.`, t.start, t.end - t.start);
42
+ el.attrs.push(new Attr(name, t.value, t.start, t.start + 1, t.start + 1, t.end, t.type === CTok.ID ? '#' : '@'));
43
+ last = t.end;
44
+ } else if (t.type === CTok.STRING) {
45
+ take();
46
+ if (el.text !== null) fail('E_SYNTAX', `${el.name} already has text content.`, t.start, t.end - t.start);
47
+ el.text = t.value;
48
+ el.textStart = t.start;
49
+ el.textEnd = t.end;
50
+ last = t.end;
51
+ } else if (t.type === CTok.NAME && peek(1).type === CTok.EQ) {
52
+ take();
53
+ take();
54
+ const v = take();
55
+ if (v.type !== CTok.VALUE) fail('E_MISSING_VALUE', `Expected a value for attribute '${t.value}'.`, v.start, 1);
56
+ for (const prev of el.attrs) {
57
+ if (prev.name === t.value) fail('E_DUPLICATE_ATTRIBUTE', `Duplicate attribute '${t.value}' on ${el.name}.`, t.start, t.end - t.start);
58
+ }
59
+ const a = new Attr(t.value, v.value, t.start, t.end, v.valueStart, v.valueEnd, v.quote);
60
+ a.decoded = !!v.escaped;
61
+ el.attrs.push(a);
62
+ last = v.end;
63
+ } else {
64
+ break;
65
+ }
66
+ }
67
+ el.openEnd = last;
68
+ }
69
+
70
+ function parseStatements(parent, closeTok) {
71
+ for (;;) {
72
+ const t = peek();
73
+ if (t.type === CTok.EOF) {
74
+ if (closeTok) {
75
+ fail('E_UNCLOSED_ELEMENT', `Unclosed block of '${parent.name}' (opened at ${where(closeTok.start)}): missing '}'.`, closeTok.start, 1);
76
+ }
77
+ return;
78
+ }
79
+ if (t.type === CTok.RBRACE) {
80
+ if (!closeTok) fail('E_UNEXPECTED_CLOSE', "Unexpected '}': no block is open.", t.start, 1);
81
+ take();
82
+ parent.closeStart = t.start;
83
+ parent.end = t.end;
84
+ return;
85
+ }
86
+ if (t.type === CTok.SEMI) {
87
+ take();
88
+ continue;
89
+ }
90
+ if (t.type === CTok.COMMENT) {
91
+ take();
92
+ parent.appendChild(new Comment(t.text, t.start, t.end));
93
+ continue;
94
+ }
95
+ if (t.type === CTok.NAME) {
96
+ if (peek(1).type === CTok.EQ) {
97
+ fail('E_SYNTAX', `Attribute '${t.value}' must follow an element name.`, t.start, t.end - t.start, {
98
+ hint: 'Attributes belong to the element written before them on the same statement.',
99
+ });
100
+ }
101
+ parent.appendChild(parseElement());
102
+ continue;
103
+ }
104
+ if (t.type === CTok.LBRACE) fail('E_SYNTAX', "Unexpected '{': a block must follow an element name.", t.start, 1);
105
+ if (t.type === CTok.STRING || t.type === CTok.ID || t.type === CTok.REF) {
106
+ fail('E_SYNTAX', 'Expected an element name.', t.start, t.end - t.start);
107
+ }
108
+ fail('E_SYNTAX', 'Unexpected token.', t.start, Math.max(1, t.end - t.start));
109
+ }
110
+ }
111
+
112
+ function parseElement() {
113
+ const t = take();
114
+ const el = new Element(t.value, t.start);
115
+ el.nameStart = t.start;
116
+ el.nameEnd = t.end;
117
+ parseHeader(el);
118
+ el.end = el.openEnd;
119
+ if (peek().type === CTok.LBRACE) {
120
+ const open = take();
121
+ el.selfClosing = false;
122
+ parseStatements(el, open);
123
+ } else {
124
+ el.selfClosing = true;
125
+ }
126
+ return el;
127
+ }
128
+
129
+ const leading = [];
130
+ while (peek().type === CTok.COMMENT || peek().type === CTok.SEMI) {
131
+ const c = take();
132
+ if (c.type === CTok.COMMENT) leading.push(new Comment(c.text, c.start, c.end));
133
+ }
134
+ const first = peek();
135
+ if (first.type === CTok.EOF) fail('E_EMPTY_DOCUMENT', "Empty document: expected 'es3d version=1'.", first.start, 0);
136
+ if (first.type !== CTok.NAME || !ROOT_NAMES.has(first.value)) {
137
+ fail('E_COMPACT_ROOT', "An ES3D compact document must start with the root element 'es3d'.", first.start, Math.max(1, first.end - first.start), {
138
+ hint: "Begin the file with: es3d version=1",
139
+ });
140
+ }
141
+ const rt = take();
142
+ const root = new Element(rt.value, rt.start);
143
+ root.nameStart = rt.start;
144
+ root.nameEnd = rt.end;
145
+ parseHeader(root);
146
+ root.end = root.openEnd;
147
+ const trailing = [];
148
+ if (peek().type === CTok.LBRACE) {
149
+ const open = take();
150
+ root.selfClosing = false;
151
+ parseStatements(root, open);
152
+ for (;;) {
153
+ const t = peek();
154
+ if (t.type === CTok.EOF) break;
155
+ if (t.type === CTok.COMMENT) {
156
+ take();
157
+ trailing.push(new Comment(t.text, t.start, t.end));
158
+ } else if (t.type === CTok.SEMI) take();
159
+ else fail('E_MULTIPLE_ROOTS', 'Content after the end of the root block.', t.start, Math.max(1, t.end - t.start));
160
+ }
161
+ } else {
162
+ // Header form: everything after the root header belongs to the root.
163
+ root.headerForm = true;
164
+ parseStatements(root, null);
165
+ root.selfClosing = root.children.length === 0;
166
+ root.end = text.length;
167
+ }
168
+ const doc = new Document({ root, syntax: 'compact', source: text, leading, trailing });
169
+ doc.lineIndex = lineIndex;
170
+ return doc;
171
+ }