x_ite-off-parser 1.0.0

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.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ # The MIT License (MIT)
2
+
3
+ Copyright (c) 2009 Holger Seelig and other contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # x_ite-off-parser
2
+
3
+ OFF 3D File Format Parser for [X_ITE](https://create3000.github.io/x_ite/)
4
+
5
+ ## Usage
6
+
7
+ Include the script after X_ITE:
8
+
9
+ ```html
10
+ <script defer src="https://cdn.jsdelivr.net/npm/x_ite@15.0.3/dist/x_ite.min.js"></script>
11
+ <script defer src="https://cdn.jsdelivr.net/npm/x_ite-off-parser@1.0.0/dist/x_ite-off-parser.js"></script>
12
+ <!-- or as ES module -->
13
+ <script type="module" src="https://cdn.jsdelivr.net/npm/x_ite@15.0.3/dist/x_ite.min.mjs"></script>
14
+ <script type="module" src="../dist/x_ite-off-parser.js"></script>
15
+ ```
16
+
17
+ Now you can load OFF files:
18
+
19
+ ```html
20
+ <x3d-canvas src="cube.off"></x3d-canvas>
21
+ ```
22
+
23
+ You can also install it from npm:
24
+
25
+ ```sh
26
+ npm i x_ite-off-parser
27
+ ```
28
+
29
+ ## License
30
+
31
+ x_ite-off-parser is free software and licensed under the [MIT License](LICENSE.md).
@@ -0,0 +1,279 @@
1
+ const X3D = window [Symbol .for ("X_ITE.X3D")];
2
+
3
+ /*
4
+ * Grammar
5
+ */
6
+
7
+ // Lexical elements
8
+ const Grammar = X3D .Expressions ({
9
+ // General
10
+ whitespaces: /[\x20\n\t\r,]+/y,
11
+ comment: /#[^\r\n]*(?=[\r\n]|$)/y,
12
+ header: /OFF/y,
13
+
14
+ // Values
15
+ int32: /(?:0[xX][\da-fA-F]+)|(?:[+-]?\d+)/y,
16
+ double: /[+-]?(?:(?:(?:\d*\.\d+)|(?:\d+(?:\.)?))(?:[eE][+-]?\d+)?)/y,
17
+ });
18
+
19
+ /*
20
+ * Parser
21
+ */
22
+
23
+ class OffParser extends X3D .X3DParser
24
+ {
25
+ constructor (scene)
26
+ {
27
+ super (scene);
28
+
29
+ this .coordIndex = [ ];
30
+ this .colors = [ ];
31
+ this .points = [ ];
32
+ }
33
+
34
+ getEncoding ()
35
+ {
36
+ return "STRING";
37
+ }
38
+
39
+ setInput (input)
40
+ {
41
+ this .input = input;
42
+ }
43
+
44
+ isValid ()
45
+ {
46
+ return this .input .match (/OFF\r?\n/);
47
+ }
48
+
49
+ parseIntoScene (resolve, reject)
50
+ {
51
+ this .off ()
52
+ .then (resolve)
53
+ .catch (reject);
54
+ }
55
+
56
+ async off ()
57
+ {
58
+ const
59
+ browser = this .getBrowser (),
60
+ scene = this .getScene ();
61
+
62
+ if (!this .statements ())
63
+ throw new Error ("Invalid file structure.");
64
+
65
+ scene .setEncoding ("OFF");
66
+ scene .setProfile (browser .getProfile ("Interchange"));
67
+
68
+ await this .loadComponents ();
69
+
70
+ // Geometry
71
+
72
+ const
73
+ shapeNode = scene .createNode ("Shape"),
74
+ appearanceNode = scene .createNode ("Appearance"),
75
+ materialNode = scene .createNode ("Material"),
76
+ geometry = scene .createNode ("IndexedFaceSet"),
77
+ coordinate = scene .createNode ("Coordinate");
78
+
79
+ if (this .colors .length)
80
+ {
81
+ const color = scene .createNode ("Color");
82
+
83
+ color .color = this .colors;
84
+ geometry .colorPerVertex = false;
85
+ geometry .color = color;
86
+ }
87
+
88
+ coordinate .point = this .points;
89
+ geometry .coordIndex = this .coordIndex;
90
+ geometry .coord = coordinate;
91
+
92
+ appearanceNode .material = materialNode;
93
+
94
+ shapeNode .appearance = appearanceNode;
95
+ shapeNode .geometry = geometry;
96
+
97
+ scene .rootNodes .push (shapeNode);
98
+
99
+ return scene;
100
+ }
101
+
102
+ statements ()
103
+ {
104
+ this .header ();
105
+
106
+ if (this .counts ())
107
+ {
108
+ if (this .listOfVertices ())
109
+ {
110
+ if (this .listOfFaces ())
111
+ return true;
112
+ }
113
+ }
114
+
115
+ return false;
116
+ }
117
+
118
+ comments ()
119
+ {
120
+ while (this .comment ())
121
+ ;
122
+ }
123
+
124
+ comment ()
125
+ {
126
+ this .whitespaces ();
127
+
128
+ if (Grammar .comment .parse (this))
129
+ return true;
130
+
131
+ return false;
132
+ }
133
+
134
+ whitespaces ()
135
+ {
136
+ Grammar .whitespaces .parse (this);
137
+ }
138
+
139
+ header ()
140
+ {
141
+ return Grammar .header .parse (this);
142
+ }
143
+
144
+ counts ()
145
+ {
146
+ this .comments ();
147
+
148
+ if (this .int32 ())
149
+ {
150
+ this .numVertices = this .value;
151
+
152
+ if (this .int32 ())
153
+ {
154
+ this .numFaces = this .value;
155
+
156
+ if (this .int32 ())
157
+ {
158
+ this .numEdges = this .value;
159
+
160
+ return true;
161
+ }
162
+ }
163
+ }
164
+
165
+ return false;
166
+ }
167
+
168
+ listOfVertices ()
169
+ {
170
+ const
171
+ numVertices = this .numVertices,
172
+ points = this .points;
173
+
174
+ for (let v = 0; v < numVertices; ++ v)
175
+ {
176
+ this .comments ();
177
+
178
+ if (this .double ())
179
+ {
180
+ points .push (this .value);
181
+
182
+ if (this .double ())
183
+ {
184
+ points .push (this .value);
185
+
186
+ if (this .double ())
187
+ {
188
+ points .push (this .value);
189
+ continue;
190
+ }
191
+ }
192
+ }
193
+
194
+ return false;
195
+ }
196
+
197
+ return true;
198
+ }
199
+
200
+ listOfFaces ()
201
+ {
202
+ const
203
+ coordIndex = this .coordIndex,
204
+ colors = this .colors,
205
+ numFaces = this .numFaces;
206
+
207
+ for (let f = 0; f < numFaces; ++ f)
208
+ {
209
+ this .comments ();
210
+
211
+ if (this .int32 ())
212
+ {
213
+ const numIndices = this .value;
214
+
215
+ for (let i = 0; i < numIndices; ++ i)
216
+ {
217
+ if (this .int32 ())
218
+ {
219
+ coordIndex .push (this .value);
220
+ continue;
221
+ }
222
+
223
+ return false;
224
+ }
225
+
226
+ coordIndex .push (-1);
227
+
228
+ if (this .int32 ())
229
+ {
230
+ colors .push (this .value / 255);
231
+
232
+ if (this .int32 ())
233
+ {
234
+ colors .push (this .value / 255);
235
+
236
+ if (this .int32 ())
237
+ colors .push (this .value / 255);
238
+ }
239
+ }
240
+
241
+ continue;
242
+ }
243
+
244
+ return false;
245
+ }
246
+
247
+ return true;
248
+ }
249
+
250
+ int32 ()
251
+ {
252
+ this .whitespaces ();
253
+
254
+ if (Grammar .int32 .parse (this))
255
+ {
256
+ this .value = parseInt (this .result [0]);
257
+
258
+ return true;
259
+ }
260
+
261
+ return false;
262
+ }
263
+
264
+ double ()
265
+ {
266
+ this .whitespaces ();
267
+
268
+ if (Grammar .double .parse (this))
269
+ {
270
+ this .value = parseFloat (this .result [0]);
271
+
272
+ return true;
273
+ }
274
+
275
+ return false;
276
+ }
277
+ }
278
+
279
+ X3D .GoldenGate .addParsers (OffParser);
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "x_ite-off-parser",
3
+ "version": "1.0.0",
4
+ "description": "OFF 3D File Format Parser for X_ITE",
5
+ "main": "dist/x_ite-off-parser.js",
6
+ "module": "dist/x_ite-off-parser.js",
7
+ "files": [
8
+ "dist/*"
9
+ ],
10
+ "scripts": {
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/create3000/x_ite-off-parser.git"
15
+ },
16
+ "keywords": [
17
+ "3D",
18
+ "Browser",
19
+ "JavaScript",
20
+ "Node",
21
+ "OFF",
22
+ "x_ite",
23
+ "X3D"
24
+ ],
25
+ "author": "Holger Seelig <holger.seelig@gmail.com>",
26
+ "license": "MIT",
27
+ "bugs": {
28
+ "url": "https://github.com/create3000/x_ite-off-parser/issues"
29
+ },
30
+ "homepage": "https://github.com/create3000/x_ite-off-parser#readme",
31
+ "funding": {
32
+ "type": "patreon",
33
+ "url": "https://patreon.com/X_ITE"
34
+ }
35
+ }