pixospritz-core 0.10.1 → 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 (157) hide show
  1. package/README.md +36 -286
  2. package/dist/bundle.js +13 -3
  3. package/dist/bundle.js.map +1 -1
  4. package/dist/style.css +1 -0
  5. package/package.json +43 -44
  6. package/src/components/WebGLView.jsx +318 -0
  7. package/src/css/pixos.css +372 -0
  8. package/src/engine/actions/animate.js +41 -0
  9. package/src/engine/actions/changezone.js +135 -0
  10. package/src/engine/actions/chat.js +109 -0
  11. package/src/engine/actions/dialogue.js +90 -0
  12. package/src/engine/actions/face.js +22 -0
  13. package/src/engine/actions/greeting.js +28 -0
  14. package/src/engine/actions/interact.js +86 -0
  15. package/src/engine/actions/move.js +67 -0
  16. package/src/engine/actions/patrol.js +109 -0
  17. package/src/engine/actions/prompt.js +185 -0
  18. package/src/engine/actions/script.js +42 -0
  19. package/src/engine/core/audio/AudioSystem.js +543 -0
  20. package/src/engine/core/cutscene/PxcPlayer.js +956 -0
  21. package/src/engine/core/cutscene/manager.js +243 -0
  22. package/src/engine/core/database/index.js +75 -0
  23. package/src/engine/core/debug/index.js +371 -0
  24. package/src/engine/core/hud/index.js +765 -0
  25. package/src/engine/core/index.js +540 -0
  26. package/src/engine/core/input/gamepad/Controller.js +71 -0
  27. package/src/engine/core/input/gamepad/ControllerButtons.js +231 -0
  28. package/src/engine/core/input/gamepad/ControllerStick.js +173 -0
  29. package/src/engine/core/input/gamepad/index.js +592 -0
  30. package/src/engine/core/input/keyboard.js +196 -0
  31. package/src/engine/core/input/manager.js +485 -0
  32. package/src/engine/core/input/mouse.js +203 -0
  33. package/src/engine/core/input/touch.js +175 -0
  34. package/src/engine/core/mode/manager.js +199 -0
  35. package/src/engine/core/net/manager.js +535 -0
  36. package/src/engine/core/queue/action.js +83 -0
  37. package/src/engine/core/queue/event.js +82 -0
  38. package/src/engine/core/queue/index.js +44 -0
  39. package/src/engine/core/queue/loadable.js +33 -0
  40. package/src/engine/core/render/CameraEffects.js +494 -0
  41. package/src/engine/core/render/FrustumCuller.js +417 -0
  42. package/src/engine/core/render/LODManager.js +285 -0
  43. package/src/engine/core/render/ParticleManager.js +529 -0
  44. package/src/engine/core/render/TextureAtlas.js +465 -0
  45. package/src/engine/core/render/camera.js +338 -0
  46. package/src/engine/core/render/light.js +197 -0
  47. package/src/engine/core/render/manager.js +1079 -0
  48. package/src/engine/core/render/shaders.js +110 -0
  49. package/src/engine/core/render/skybox.js +342 -0
  50. package/src/engine/core/resource/manager.js +133 -0
  51. package/src/engine/core/resource/object.js +611 -0
  52. package/src/engine/core/resource/texture.js +103 -0
  53. package/src/engine/core/resource/tileset.js +177 -0
  54. package/src/engine/core/scene/avatar.js +215 -0
  55. package/src/engine/core/scene/speech.js +138 -0
  56. package/src/engine/core/scene/sprite.js +702 -0
  57. package/src/engine/core/scene/spritz.js +189 -0
  58. package/src/engine/core/scene/world.js +681 -0
  59. package/src/engine/core/scene/zone.js +1167 -0
  60. package/src/engine/core/store/index.js +110 -0
  61. package/src/engine/dynamic/animatedSprite.js +64 -0
  62. package/src/engine/dynamic/animatedTile.js +98 -0
  63. package/src/engine/dynamic/avatar.js +110 -0
  64. package/src/engine/dynamic/map.js +174 -0
  65. package/src/engine/dynamic/sprite.js +255 -0
  66. package/src/engine/dynamic/spritz.js +119 -0
  67. package/src/engine/events/EventSystem.js +609 -0
  68. package/src/engine/events/camera.js +142 -0
  69. package/src/engine/events/chat.js +75 -0
  70. package/src/engine/events/menu.js +186 -0
  71. package/src/engine/scripting/CallbackManager.js +514 -0
  72. package/src/engine/scripting/PixoScriptInterpreter.js +81 -0
  73. package/src/engine/scripting/PixoScriptLibrary.js +704 -0
  74. package/src/engine/shaders/effects/index.js +450 -0
  75. package/src/engine/shaders/fs.js +222 -0
  76. package/src/engine/shaders/particles/fs.js +41 -0
  77. package/src/engine/shaders/particles/vs.js +61 -0
  78. package/src/engine/shaders/picker/fs.js +34 -0
  79. package/src/engine/shaders/picker/init.js +62 -0
  80. package/src/engine/shaders/picker/vs.js +42 -0
  81. package/src/engine/shaders/pxsl/README.md +250 -0
  82. package/src/engine/shaders/pxsl/index.js +25 -0
  83. package/src/engine/shaders/pxsl/library.js +608 -0
  84. package/src/engine/shaders/pxsl/manager.js +338 -0
  85. package/src/engine/shaders/pxsl/specification.js +363 -0
  86. package/src/engine/shaders/pxsl/transpiler.js +753 -0
  87. package/src/engine/shaders/skybox/cosmic/fs.js +147 -0
  88. package/src/engine/shaders/skybox/cosmic/vs.js +23 -0
  89. package/src/engine/shaders/skybox/matrix/fs.js +127 -0
  90. package/src/engine/shaders/skybox/matrix/vs.js +23 -0
  91. package/src/engine/shaders/skybox/morning/fs.js +109 -0
  92. package/src/engine/shaders/skybox/morning/vs.js +23 -0
  93. package/src/engine/shaders/skybox/neon/fs.js +119 -0
  94. package/src/engine/shaders/skybox/neon/vs.js +23 -0
  95. package/src/engine/shaders/skybox/sky/fs.js +114 -0
  96. package/src/engine/shaders/skybox/sky/vs.js +23 -0
  97. package/src/engine/shaders/skybox/sunset/fs.js +101 -0
  98. package/src/engine/shaders/skybox/sunset/vs.js +23 -0
  99. package/src/engine/shaders/transition/blur/fs.js +42 -0
  100. package/src/engine/shaders/transition/blur/vs.js +26 -0
  101. package/src/engine/shaders/transition/cross/fs.js +36 -0
  102. package/src/engine/shaders/transition/cross/vs.js +26 -0
  103. package/src/engine/shaders/transition/crossBlur/fs.js +41 -0
  104. package/src/engine/shaders/transition/crossBlur/vs.js +25 -0
  105. package/src/engine/shaders/transition/dissolve/fs.js +78 -0
  106. package/src/engine/shaders/transition/dissolve/vs.js +24 -0
  107. package/src/engine/shaders/transition/fade/fs.js +31 -0
  108. package/src/engine/shaders/transition/fade/vs.js +27 -0
  109. package/src/engine/shaders/transition/iris/fs.js +52 -0
  110. package/src/engine/shaders/transition/iris/vs.js +24 -0
  111. package/src/engine/shaders/transition/pixelate/fs.js +44 -0
  112. package/src/engine/shaders/transition/pixelate/vs.js +24 -0
  113. package/src/engine/shaders/transition/slide/fs.js +53 -0
  114. package/src/engine/shaders/transition/slide/vs.js +24 -0
  115. package/src/engine/shaders/transition/swirl/fs.js +39 -0
  116. package/src/engine/shaders/transition/swirl/vs.js +26 -0
  117. package/src/engine/shaders/transition/wipe/fs.js +50 -0
  118. package/src/engine/shaders/transition/wipe/vs.js +24 -0
  119. package/src/engine/shaders/vs.js +60 -0
  120. package/src/engine/utils/CameraController.js +506 -0
  121. package/src/engine/utils/ObjHelper.js +551 -0
  122. package/src/engine/utils/debug-logger.js +110 -0
  123. package/src/engine/utils/enums.js +305 -0
  124. package/src/engine/utils/generator.js +156 -0
  125. package/src/engine/utils/index.js +21 -0
  126. package/src/engine/utils/loaders/ActionLoader.js +77 -0
  127. package/src/engine/utils/loaders/AudioLoader.js +157 -0
  128. package/src/engine/utils/loaders/EventLoader.js +66 -0
  129. package/src/engine/utils/loaders/ObjectLoader.js +67 -0
  130. package/src/engine/utils/loaders/SpriteLoader.js +77 -0
  131. package/src/engine/utils/loaders/TilesetLoader.js +103 -0
  132. package/src/engine/utils/loaders/index.js +21 -0
  133. package/src/engine/utils/math/matrix4.js +367 -0
  134. package/src/engine/utils/math/vector.js +458 -0
  135. package/src/engine/utils/obj/_old_js/index.js +46 -0
  136. package/src/engine/utils/obj/_old_js/layout.js +308 -0
  137. package/src/engine/utils/obj/_old_js/material.js +711 -0
  138. package/src/engine/utils/obj/_old_js/mesh.js +761 -0
  139. package/src/engine/utils/obj/_old_js/utils.js +647 -0
  140. package/src/engine/utils/obj/index.js +24 -0
  141. package/src/engine/utils/obj/js/index.js +277 -0
  142. package/src/engine/utils/obj/js/loader.js +232 -0
  143. package/src/engine/utils/obj/layout.js +246 -0
  144. package/src/engine/utils/obj/material.js +665 -0
  145. package/src/engine/utils/obj/mesh.js +657 -0
  146. package/src/engine/utils/obj/ts/index.ts +72 -0
  147. package/src/engine/utils/obj/ts/layout.ts +265 -0
  148. package/src/engine/utils/obj/ts/material.ts +760 -0
  149. package/src/engine/utils/obj/ts/mesh.ts +785 -0
  150. package/src/engine/utils/obj/ts/utils.ts +501 -0
  151. package/src/engine/utils/obj/utils.js +428 -0
  152. package/src/engine/utils/resources.js +18 -0
  153. package/src/index.jsx +55 -0
  154. package/src/spritz/player.js +18 -0
  155. package/src/spritz/readme.md +18 -0
  156. package/LICENSE +0 -437
  157. package/dist/bundle.js.LICENSE.txt +0 -31
@@ -0,0 +1,665 @@
1
+ /**
2
+ * The Material class.
3
+ */
4
+ export class Material {
5
+ constructor(name) {
6
+ this.name = name;
7
+ /**
8
+ * Constructor
9
+ * @param {String} name the unique name of the material
10
+ */
11
+ // The values for the following attibutes
12
+ // are an array of R, G, B normalized values.
13
+ // Ka - Ambient Reflectivity
14
+ this.ambient = [0, 0, 0];
15
+ // Kd - Defuse Reflectivity
16
+ this.diffuse = [0, 0, 0];
17
+ // Ks
18
+ this.specular = [0, 0, 0];
19
+ // Ke
20
+ this.emissive = [0, 0, 0];
21
+ // Tf
22
+ this.transmissionFilter = [0, 0, 0];
23
+ // d
24
+ this.dissolve = 0;
25
+ // valid range is between 0 and 1000
26
+ this.specularExponent = 0;
27
+ // either d or Tr; valid values are normalized
28
+ this.transparency = 0;
29
+ // illum - the enum of the illumination model to use
30
+ this.illumination = 0;
31
+ // Ni - Set to "normal" (air).
32
+ this.refractionIndex = 1;
33
+ // sharpness
34
+ this.sharpness = 0;
35
+ // map_Kd
36
+ this.mapDiffuse = emptyTextureOptions();
37
+ // map_Ka
38
+ this.mapAmbient = emptyTextureOptions();
39
+ // map_Ks
40
+ this.mapSpecular = emptyTextureOptions();
41
+ // map_Ns
42
+ this.mapSpecularExponent = emptyTextureOptions();
43
+ // map_d
44
+ this.mapDissolve = emptyTextureOptions();
45
+ // map_aat
46
+ this.antiAliasing = false;
47
+ // map_bump or bump
48
+ this.mapBump = emptyTextureOptions();
49
+ // disp
50
+ this.mapDisplacement = emptyTextureOptions();
51
+ // decal
52
+ this.mapDecal = emptyTextureOptions();
53
+ // map_Ke
54
+ this.mapEmissive = emptyTextureOptions();
55
+ // refl - when the reflection type is a cube, there will be multiple refl
56
+ // statements for each side of the cube. If it's a spherical
57
+ // reflection, there should only ever be one.
58
+ this.mapReflections = [];
59
+ }
60
+ }
61
+ const SENTINEL_MATERIAL = new Material("sentinel");
62
+ /**
63
+ * https://en.wikipedia.org/wiki/Wavefront_.obj_file
64
+ * http://paulbourke.net/dataformats/mtl/
65
+ */
66
+ export class MaterialLibrary {
67
+ constructor(data) {
68
+ this.data = data;
69
+ /**
70
+ * Constructs the Material Parser
71
+ * @param mtlData the MTL file contents
72
+ */
73
+ this.currentMaterial = SENTINEL_MATERIAL;
74
+ this.materials = {};
75
+ this.parse();
76
+ }
77
+ /* eslint-disable camelcase */
78
+ /* the function names here disobey camelCase conventions
79
+ to make parsing/routing easier. see the parse function
80
+ documentation for more information. */
81
+ /**
82
+ * Creates a new Material object and adds to the registry.
83
+ * @param tokens the tokens associated with the directive
84
+ */
85
+ parse_newmtl(tokens) {
86
+ const name = tokens[0];
87
+ // console.info('Parsing new Material:', name);
88
+ this.currentMaterial = new Material(name);
89
+ this.materials[name] = this.currentMaterial;
90
+ }
91
+ /**
92
+ * See the documenation for parse_Ka below for a better understanding.
93
+ *
94
+ * Given a list of possible color tokens, returns an array of R, G, and B
95
+ * color values.
96
+ *
97
+ * @param tokens the tokens associated with the directive
98
+ * @return {*} a 3 element array containing the R, G, and B values
99
+ * of the color.
100
+ */
101
+ parseColor(tokens) {
102
+ if (tokens[0] == "spectral") {
103
+ throw new Error("The MTL parser does not support spectral curve files. You will " +
104
+ "need to convert the MTL colors to either RGB or CIEXYZ.");
105
+ }
106
+ if (tokens[0] == "xyz") {
107
+ throw new Error("The MTL parser does not currently support XYZ colors. Either convert the " +
108
+ "XYZ values to RGB or create an issue to add support for XYZ");
109
+ }
110
+ // from my understanding of the spec, RGB values at this point
111
+ // will either be 3 floats or exactly 1 float, so that's the check
112
+ // that i'm going to perform here
113
+ if (tokens.length == 3) {
114
+ const [x, y, z] = tokens;
115
+ return [parseFloat(x), parseFloat(y), parseFloat(z)];
116
+ }
117
+ // Since tokens at this point has a length of 3, we're going to assume
118
+ // it's exactly 1, skipping the check for 2.
119
+ const value = parseFloat(tokens[0]);
120
+ // in this case, all values are equivalent
121
+ return [value, value, value];
122
+ }
123
+ /**
124
+ * Parse the ambient reflectivity
125
+ *
126
+ * A Ka directive can take one of three forms:
127
+ * - Ka r g b
128
+ * - Ka spectral file.rfl
129
+ * - Ka xyz x y z
130
+ * These three forms are mutually exclusive in that only one
131
+ * declaration can exist per material. It is considered a syntax
132
+ * error otherwise.
133
+ *
134
+ * The "Ka" form specifies the ambient reflectivity using RGB values.
135
+ * The "g" and "b" values are optional. If only the "r" value is
136
+ * specified, then the "g" and "b" values are assigned the value of
137
+ * "r". Values are normally in the range 0.0 to 1.0. Values outside
138
+ * of this range increase or decrease the reflectivity accordingly.
139
+ *
140
+ * The "Ka spectral" form specifies the ambient reflectivity using a
141
+ * spectral curve. "file.rfl" is the name of the ".rfl" file containing
142
+ * the curve data. "factor" is an optional argument which is a multiplier
143
+ * for the values in the .rfl file and defaults to 1.0 if not specified.
144
+ *
145
+ * The "Ka xyz" form specifies the ambient reflectivity using CIEXYZ values.
146
+ * "x y z" are the values of the CIEXYZ color space. The "y" and "z" arguments
147
+ * are optional and take on the value of the "x" component if only "x" is
148
+ * specified. The "x y z" values are normally in the range of 0.0 to 1.0 and
149
+ * increase or decrease ambient reflectivity accordingly outside of that
150
+ * range.
151
+ *
152
+ * @param tokens the tokens associated with the directive
153
+ */
154
+ parse_Ka(tokens) {
155
+ this.currentMaterial.ambient = this.parseColor(tokens);
156
+ }
157
+ /**
158
+ * Diffuse Reflectivity
159
+ *
160
+ * Similar to the Ka directive. Simply replace "Ka" with "Kd" and the rules
161
+ * are the same
162
+ *
163
+ * @param tokens the tokens associated with the directive
164
+ */
165
+ parse_Kd(tokens) {
166
+ this.currentMaterial.diffuse = this.parseColor(tokens);
167
+ }
168
+ /**
169
+ * Spectral Reflectivity
170
+ *
171
+ * Similar to the Ka directive. Simply replace "Ks" with "Kd" and the rules
172
+ * are the same
173
+ *
174
+ * @param tokens the tokens associated with the directive
175
+ */
176
+ parse_Ks(tokens) {
177
+ this.currentMaterial.specular = this.parseColor(tokens);
178
+ }
179
+ /**
180
+ * Emissive
181
+ *
182
+ * The amount and color of light emitted by the object.
183
+ *
184
+ * @param tokens the tokens associated with the directive
185
+ */
186
+ parse_Ke(tokens) {
187
+ this.currentMaterial.emissive = this.parseColor(tokens);
188
+ }
189
+ /**
190
+ * Transmission Filter
191
+ *
192
+ * Any light passing through the object is filtered by the transmission
193
+ * filter, which only allows specific colors to pass through. For example, Tf
194
+ * 0 1 0 allows all of the green to pass through and filters out all of the
195
+ * red and blue.
196
+ *
197
+ * Similar to the Ka directive. Simply replace "Ks" with "Tf" and the rules
198
+ * are the same
199
+ *
200
+ * @param tokens the tokens associated with the directive
201
+ */
202
+ parse_Tf(tokens) {
203
+ this.currentMaterial.transmissionFilter = this.parseColor(tokens);
204
+ }
205
+ /**
206
+ * Specifies the dissolve for the current material.
207
+ *
208
+ * Statement: d [-halo] `factor`
209
+ *
210
+ * Example: "d 0.5"
211
+ *
212
+ * The factor is the amount this material dissolves into the background. A
213
+ * factor of 1.0 is fully opaque. This is the default when a new material is
214
+ * created. A factor of 0.0 is fully dissolved (completely transparent).
215
+ *
216
+ * Unlike a real transparent material, the dissolve does not depend upon
217
+ * material thickness nor does it have any spectral character. Dissolve works
218
+ * on all illumination models.
219
+ *
220
+ * The dissolve statement allows for an optional "-halo" flag which indicates
221
+ * that a dissolve is dependent on the surface orientation relative to the
222
+ * viewer. For example, a sphere with the following dissolve, "d -halo 0.0",
223
+ * will be fully dissolved at its center and will appear gradually more opaque
224
+ * toward its edge.
225
+ *
226
+ * "factor" is the minimum amount of dissolve applied to the material. The
227
+ * amount of dissolve will vary between 1.0 (fully opaque) and the specified
228
+ * "factor". The formula is:
229
+ *
230
+ * dissolve = 1.0 - (N*v)(1.0-factor)
231
+ *
232
+ * @param tokens the tokens associated with the directive
233
+ */
234
+ parse_d(tokens) {
235
+ // this ignores the -halo option as I can't find any documentation on what
236
+ // it's supposed to be.
237
+ this.currentMaterial.dissolve = parseFloat(tokens.pop() || "0");
238
+ }
239
+ /**
240
+ * The "illum" statement specifies the illumination model to use in the
241
+ * material. Illumination models are mathematical equations that represent
242
+ * various material lighting and shading effects.
243
+ *
244
+ * The illumination number can be a number from 0 to 10. The following are
245
+ * the list of illumination enumerations and their summaries:
246
+ * 0. Color on and Ambient off
247
+ * 1. Color on and Ambient on
248
+ * 2. Highlight on
249
+ * 3. Reflection on and Ray trace on
250
+ * 4. Transparency: Glass on, Reflection: Ray trace on
251
+ * 5. Reflection: Fresnel on and Ray trace on
252
+ * 6. Transparency: Refraction on, Reflection: Fresnel off and Ray trace on
253
+ * 7. Transparency: Refraction on, Reflection: Fresnel on and Ray trace on
254
+ * 8. Reflection on and Ray trace off
255
+ * 9. Transparency: Glass on, Reflection: Ray trace off
256
+ * 10. Casts shadows onto invisible surfaces
257
+ *
258
+ * Example: "illum 2" to specify the "Highlight on" model
259
+ *
260
+ * @param tokens the tokens associated with the directive
261
+ */
262
+ parse_illum(tokens) {
263
+ this.currentMaterial.illumination = parseInt(tokens[0]);
264
+ }
265
+ /**
266
+ * Optical Density (AKA Index of Refraction)
267
+ *
268
+ * Statement: Ni `index`
269
+ *
270
+ * Example: Ni 1.0
271
+ *
272
+ * Specifies the optical density for the surface. `index` is the value
273
+ * for the optical density. The values can range from 0.001 to 10. A value of
274
+ * 1.0 means that light does not bend as it passes through an object.
275
+ * Increasing the optical_density increases the amount of bending. Glass has
276
+ * an index of refraction of about 1.5. Values of less than 1.0 produce
277
+ * bizarre results and are not recommended
278
+ *
279
+ * @param tokens the tokens associated with the directive
280
+ */
281
+ parse_Ni(tokens) {
282
+ this.currentMaterial.refractionIndex = parseFloat(tokens[0]);
283
+ }
284
+ /**
285
+ * Specifies the specular exponent for the current material. This defines the
286
+ * focus of the specular highlight.
287
+ *
288
+ * Statement: Ns `exponent`
289
+ *
290
+ * Example: "Ns 250"
291
+ *
292
+ * `exponent` is the value for the specular exponent. A high exponent results
293
+ * in a tight, concentrated highlight. Ns Values normally range from 0 to
294
+ * 1000.
295
+ *
296
+ * @param tokens the tokens associated with the directive
297
+ */
298
+ parse_Ns(tokens) {
299
+ this.currentMaterial.specularExponent = parseInt(tokens[0]);
300
+ }
301
+ /**
302
+ * Specifies the sharpness of the reflections from the local reflection map.
303
+ *
304
+ * Statement: sharpness `value`
305
+ *
306
+ * Example: "sharpness 100"
307
+ *
308
+ * If a material does not have a local reflection map defined in its material
309
+ * defintions, sharpness will apply to the global reflection map defined in
310
+ * PreView.
311
+ *
312
+ * `value` can be a number from 0 to 1000. The default is 60. A high value
313
+ * results in a clear reflection of objects in the reflection map.
314
+ *
315
+ * Tip: sharpness values greater than 100 introduce aliasing effects in
316
+ * flat surfaces that are viewed at a sharp angle.
317
+ *
318
+ * @param tokens the tokens associated with the directive
319
+ */
320
+ parse_sharpness(tokens) {
321
+ this.currentMaterial.sharpness = parseInt(tokens[0]);
322
+ }
323
+ /**
324
+ * Parses the -cc flag and updates the options object with the values.
325
+ *
326
+ * @param values the values passed to the -cc flag
327
+ * @param options the Object of all image options
328
+ */
329
+ parse_cc(values, options) {
330
+ options.colorCorrection = values[0] == "on";
331
+ }
332
+ /**
333
+ * Parses the -blendu flag and updates the options object with the values.
334
+ *
335
+ * @param values the values passed to the -blendu flag
336
+ * @param options the Object of all image options
337
+ */
338
+ parse_blendu(values, options) {
339
+ options.horizontalBlending = values[0] == "on";
340
+ }
341
+ /**
342
+ * Parses the -blendv flag and updates the options object with the values.
343
+ *
344
+ * @param values the values passed to the -blendv flag
345
+ * @param options the Object of all image options
346
+ */
347
+ parse_blendv(values, options) {
348
+ options.verticalBlending = values[0] == "on";
349
+ }
350
+ /**
351
+ * Parses the -boost flag and updates the options object with the values.
352
+ *
353
+ * @param values the values passed to the -boost flag
354
+ * @param options the Object of all image options
355
+ */
356
+ parse_boost(values, options) {
357
+ options.boostMipMapSharpness = parseFloat(values[0]);
358
+ }
359
+ /**
360
+ * Parses the -mm flag and updates the options object with the values.
361
+ *
362
+ * @param values the values passed to the -mm flag
363
+ * @param options the Object of all image options
364
+ */
365
+ parse_mm(values, options) {
366
+ options.modifyTextureMap.brightness = parseFloat(values[0]);
367
+ options.modifyTextureMap.contrast = parseFloat(values[1]);
368
+ }
369
+ /**
370
+ * Parses and sets the -o, -s, and -t u, v, and w values
371
+ *
372
+ * @param values the values passed to the -o, -s, -t flag
373
+ * @param {Object} option the Object of either the -o, -s, -t option
374
+ * @param {Integer} defaultValue the Object of all image options
375
+ */
376
+ parse_ost(values, option, defaultValue) {
377
+ while (values.length < 3) {
378
+ values.push(defaultValue.toString());
379
+ }
380
+ option.u = parseFloat(values[0]);
381
+ option.v = parseFloat(values[1]);
382
+ option.w = parseFloat(values[2]);
383
+ }
384
+ /**
385
+ * Parses the -o flag and updates the options object with the values.
386
+ *
387
+ * @param values the values passed to the -o flag
388
+ * @param options the Object of all image options
389
+ */
390
+ parse_o(values, options) {
391
+ this.parse_ost(values, options.offset, 0);
392
+ }
393
+ /**
394
+ * Parses the -s flag and updates the options object with the values.
395
+ *
396
+ * @param values the values passed to the -s flag
397
+ * @param options the Object of all image options
398
+ */
399
+ parse_s(values, options) {
400
+ this.parse_ost(values, options.scale, 1);
401
+ }
402
+ /**
403
+ * Parses the -t flag and updates the options object with the values.
404
+ *
405
+ * @param values the values passed to the -t flag
406
+ * @param options the Object of all image options
407
+ */
408
+ parse_t(values, options) {
409
+ this.parse_ost(values, options.turbulence, 0);
410
+ }
411
+ /**
412
+ * Parses the -texres flag and updates the options object with the values.
413
+ *
414
+ * @param values the values passed to the -texres flag
415
+ * @param options the Object of all image options
416
+ */
417
+ parse_texres(values, options) {
418
+ options.textureResolution = parseFloat(values[0]);
419
+ }
420
+ /**
421
+ * Parses the -clamp flag and updates the options object with the values.
422
+ *
423
+ * @param values the values passed to the -clamp flag
424
+ * @param options the Object of all image options
425
+ */
426
+ parse_clamp(values, options) {
427
+ options.clamp = values[0] == "on";
428
+ }
429
+ /**
430
+ * Parses the -bm flag and updates the options object with the values.
431
+ *
432
+ * @param values the values passed to the -bm flag
433
+ * @param options the Object of all image options
434
+ */
435
+ parse_bm(values, options) {
436
+ options.bumpMultiplier = parseFloat(values[0]);
437
+ }
438
+ /**
439
+ * Parses the -imfchan flag and updates the options object with the values.
440
+ *
441
+ * @param values the values passed to the -imfchan flag
442
+ * @param options the Object of all image options
443
+ */
444
+ parse_imfchan(values, options) {
445
+ options.imfChan = values[0];
446
+ }
447
+ /**
448
+ * This only exists for relection maps and denotes the type of reflection.
449
+ *
450
+ * @param values the values passed to the -type flag
451
+ * @param options the Object of all image options
452
+ */
453
+ parse_type(values, options) {
454
+ options.reflectionType = values[0];
455
+ }
456
+ /**
457
+ * Parses the texture's options and returns an options object with the info
458
+ *
459
+ * @param tokens all of the option tokens to pass to the texture
460
+ * @return {Object} a complete object of objects to apply to the texture
461
+ */
462
+ parseOptions(tokens) {
463
+ const options = emptyTextureOptions();
464
+ let option;
465
+ let values;
466
+ const optionsToValues = {};
467
+ tokens.reverse();
468
+ while (tokens.length) {
469
+ // token is guaranteed to exists here, hence the explicit "as"
470
+ const token = tokens.pop();
471
+ if (token.startsWith("-")) {
472
+ option = token.substr(1);
473
+ optionsToValues[option] = [];
474
+ }
475
+ else if (option) {
476
+ optionsToValues[option].push(token);
477
+ }
478
+ }
479
+ for (option in optionsToValues) {
480
+ if (!optionsToValues.hasOwnProperty(option)) {
481
+ continue;
482
+ }
483
+ values = optionsToValues[option];
484
+ const optionMethod = this[`parse_${option}`];
485
+ if (optionMethod) {
486
+ optionMethod.bind(this)(values, options);
487
+ }
488
+ }
489
+ return options;
490
+ }
491
+ /**
492
+ * Parses the given texture map line.
493
+ *
494
+ * @param tokens all of the tokens representing the texture
495
+ * @return a complete object of objects to apply to the texture
496
+ */
497
+ parseMap(tokens) {
498
+ // according to wikipedia:
499
+ // (https://en.wikipedia.org/wiki/Wavefront_.obj_file#Vendor_specific_alterations)
500
+ // there is at least one vendor that places the filename before the options
501
+ // rather than after (which is to spec). All options start with a '-'
502
+ // so if the first token doesn't start with a '-', we're going to assume
503
+ // it's the name of the map file.
504
+ let optionsString;
505
+ let filename = "";
506
+ if (!tokens[0].startsWith("-")) {
507
+ [filename, ...optionsString] = tokens;
508
+ }
509
+ else {
510
+ filename = tokens.pop();
511
+ optionsString = tokens;
512
+ }
513
+ const options = this.parseOptions(optionsString);
514
+ options.filename = filename.replace(/\\/g, "/");
515
+ return options;
516
+ }
517
+ /**
518
+ * Parses the ambient map.
519
+ *
520
+ * @param tokens list of tokens for the map_Ka direcive
521
+ */
522
+ parse_map_Ka(tokens) {
523
+ this.currentMaterial.mapAmbient = this.parseMap(tokens);
524
+ }
525
+ /**
526
+ * Parses the diffuse map.
527
+ *
528
+ * @param tokens list of tokens for the map_Kd direcive
529
+ */
530
+ parse_map_Kd(tokens) {
531
+ this.currentMaterial.mapDiffuse = this.parseMap(tokens);
532
+ }
533
+ /**
534
+ * Parses the specular map.
535
+ *
536
+ * @param tokens list of tokens for the map_Ks direcive
537
+ */
538
+ parse_map_Ks(tokens) {
539
+ this.currentMaterial.mapSpecular = this.parseMap(tokens);
540
+ }
541
+ /**
542
+ * Parses the emissive map.
543
+ *
544
+ * @param tokens list of tokens for the map_Ke direcive
545
+ */
546
+ parse_map_Ke(tokens) {
547
+ this.currentMaterial.mapEmissive = this.parseMap(tokens);
548
+ }
549
+ /**
550
+ * Parses the specular exponent map.
551
+ *
552
+ * @param tokens list of tokens for the map_Ns direcive
553
+ */
554
+ parse_map_Ns(tokens) {
555
+ this.currentMaterial.mapSpecularExponent = this.parseMap(tokens);
556
+ }
557
+ /**
558
+ * Parses the dissolve map.
559
+ *
560
+ * @param tokens list of tokens for the map_d direcive
561
+ */
562
+ parse_map_d(tokens) {
563
+ this.currentMaterial.mapDissolve = this.parseMap(tokens);
564
+ }
565
+ /**
566
+ * Parses the anti-aliasing option.
567
+ *
568
+ * @param tokens list of tokens for the map_aat direcive
569
+ */
570
+ parse_map_aat(tokens) {
571
+ this.currentMaterial.antiAliasing = tokens[0] == "on";
572
+ }
573
+ /**
574
+ * Parses the bump map.
575
+ *
576
+ * @param tokens list of tokens for the map_bump direcive
577
+ */
578
+ parse_map_bump(tokens) {
579
+ this.currentMaterial.mapBump = this.parseMap(tokens);
580
+ }
581
+ /**
582
+ * Parses the bump map.
583
+ *
584
+ * @param tokens list of tokens for the bump direcive
585
+ */
586
+ parse_bump(tokens) {
587
+ this.parse_map_bump(tokens);
588
+ }
589
+ /**
590
+ * Parses the disp map.
591
+ *
592
+ * @param tokens list of tokens for the disp direcive
593
+ */
594
+ parse_disp(tokens) {
595
+ this.currentMaterial.mapDisplacement = this.parseMap(tokens);
596
+ }
597
+ /**
598
+ * Parses the decal map.
599
+ *
600
+ * @param tokens list of tokens for the map_decal direcive
601
+ */
602
+ parse_decal(tokens) {
603
+ this.currentMaterial.mapDecal = this.parseMap(tokens);
604
+ }
605
+ /**
606
+ * Parses the refl map.
607
+ *
608
+ * @param tokens list of tokens for the refl direcive
609
+ */
610
+ parse_refl(tokens) {
611
+ this.currentMaterial.mapReflections.push(this.parseMap(tokens));
612
+ }
613
+ /**
614
+ * Parses the MTL file.
615
+ *
616
+ * Iterates line by line parsing each MTL directive.
617
+ *
618
+ * This function expects the first token in the line
619
+ * to be a valid MTL directive. That token is then used
620
+ * to try and run a method on this class. parse_[directive]
621
+ * E.g., the `newmtl` directive would try to call the method
622
+ * parse_newmtl. Each parsing function takes in the remaining
623
+ * list of tokens and updates the currentMaterial class with
624
+ * the attributes provided.
625
+ */
626
+ parse() {
627
+ const lines = this.data.split(/\r?\n/);
628
+ for (let line of lines) {
629
+ line = line.trim();
630
+ if (!line || line.startsWith("#")) {
631
+ continue;
632
+ }
633
+ const [directive, ...tokens] = line.split(/\s/);
634
+ const parseMethod = this[`parse_${directive}`];
635
+ if (!parseMethod) {
636
+ console.warn(`Don't know how to parse the directive: "${directive}"`);
637
+ continue;
638
+ }
639
+ parseMethod.bind(this)(tokens);
640
+ }
641
+ // some cleanup. These don't need to be exposed as public data.
642
+ delete this.data;
643
+ this.currentMaterial = SENTINEL_MATERIAL;
644
+ }
645
+ }
646
+ function emptyTextureOptions() {
647
+ return {
648
+ colorCorrection: false,
649
+ horizontalBlending: true,
650
+ verticalBlending: true,
651
+ boostMipMapSharpness: 0,
652
+ modifyTextureMap: {
653
+ brightness: 0,
654
+ contrast: 1,
655
+ },
656
+ offset: { u: 0, v: 0, w: 0 },
657
+ scale: { u: 1, v: 1, w: 1 },
658
+ turbulence: { u: 0, v: 0, w: 0 },
659
+ clamp: false,
660
+ textureResolution: null,
661
+ bumpMultiplier: 1,
662
+ imfChan: null,
663
+ filename: "",
664
+ };
665
+ }