matrix-engine-wgpu 1.3.17 → 1.3.19

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 (273) hide show
  1. package/.codesandbox/tasks.json +46 -0
  2. package/.devcontainer/devcontainer.json +22 -0
  3. package/.github/dependabot.yml +12 -0
  4. package/REFERENCE.md +59 -0
  5. package/app-worker.js +45 -0
  6. package/dev.md +541 -0
  7. package/empty.js +17 -0
  8. package/examples/camera-texture.js +60 -0
  9. package/examples/games/jamb/html-content.js +128 -0
  10. package/examples/games/jamb/jamb.js +1341 -0
  11. package/examples/games/jamb/readme.md +3 -0
  12. package/examples/load-jamb.js +6 -0
  13. package/examples/load-obj-file.js +95 -0
  14. package/examples/load-objs-sequence.js +68 -0
  15. package/examples/unlit-textures.js +31 -0
  16. package/examples/video-texture.js +61 -0
  17. package/examples.js +73 -0
  18. package/main.js +635 -0
  19. package/non-project-files/cubebuffer-example.js +51 -0
  20. package/non-project-files/dev.txt +21 -0
  21. package/non-project-files/image1.png +0 -0
  22. package/non-project-files/image6.png +0 -0
  23. package/package.json +1 -4
  24. package/public/ammojs/ammo.js +957 -0
  25. package/public/ammojs/ammo.wasm.js +921 -0
  26. package/public/ammojs/ammo.wasm.wasm +0 -0
  27. package/public/app-worker.js +47 -0
  28. package/public/app.js +12744 -0
  29. package/public/css/style.css +711 -0
  30. package/public/empty.html +25 -0
  31. package/public/empty.js +10453 -0
  32. package/public/examples.html +30 -0
  33. package/public/examples.js +11217 -0
  34. package/public/index.html +20 -0
  35. package/public/manifest copy.web +35 -0
  36. package/public/manifest.web +25 -0
  37. package/public/res/audios/block.mp3 +0 -0
  38. package/public/res/audios/dice-roll.mp3 +0 -0
  39. package/public/res/audios/dice1.mp3 +0 -0
  40. package/public/res/audios/dice2.mp3 +0 -0
  41. package/public/res/audios/kenney/Kenney.url +2 -0
  42. package/public/res/audios/kenney/License.txt +22 -0
  43. package/public/res/audios/kenney/Patreon.url +2 -0
  44. package/public/res/audios/kenney/audios/back_001.ogg +0 -0
  45. package/public/res/audios/kenney/audios/back_002.ogg +0 -0
  46. package/public/res/audios/kenney/audios/back_003.ogg +0 -0
  47. package/public/res/audios/kenney/audios/back_004.ogg +0 -0
  48. package/public/res/audios/kenney/audios/bong_001.ogg +0 -0
  49. package/public/res/audios/kenney/audios/click_001.ogg +0 -0
  50. package/public/res/audios/kenney/audios/click_002.ogg +0 -0
  51. package/public/res/audios/kenney/audios/click_003.ogg +0 -0
  52. package/public/res/audios/kenney/audios/click_004.ogg +0 -0
  53. package/public/res/audios/kenney/audios/click_005.ogg +0 -0
  54. package/public/res/audios/kenney/audios/close_001.ogg +0 -0
  55. package/public/res/audios/kenney/audios/close_002.ogg +0 -0
  56. package/public/res/audios/kenney/audios/close_003.ogg +0 -0
  57. package/public/res/audios/kenney/audios/close_004.ogg +0 -0
  58. package/public/res/audios/kenney/audios/confirmation_001.ogg +0 -0
  59. package/public/res/audios/kenney/audios/confirmation_002.ogg +0 -0
  60. package/public/res/audios/kenney/audios/confirmation_003.ogg +0 -0
  61. package/public/res/audios/kenney/audios/confirmation_004.ogg +0 -0
  62. package/public/res/audios/kenney/audios/drop_001.ogg +0 -0
  63. package/public/res/audios/kenney/audios/drop_002.ogg +0 -0
  64. package/public/res/audios/kenney/audios/drop_003.ogg +0 -0
  65. package/public/res/audios/kenney/audios/drop_004.ogg +0 -0
  66. package/public/res/audios/kenney/audios/error_001.ogg +0 -0
  67. package/public/res/audios/kenney/audios/error_002.ogg +0 -0
  68. package/public/res/audios/kenney/audios/error_003.ogg +0 -0
  69. package/public/res/audios/kenney/audios/error_004.ogg +0 -0
  70. package/public/res/audios/kenney/audios/error_005.ogg +0 -0
  71. package/public/res/audios/kenney/audios/error_006.ogg +0 -0
  72. package/public/res/audios/kenney/audios/error_007.ogg +0 -0
  73. package/public/res/audios/kenney/audios/error_008.ogg +0 -0
  74. package/public/res/audios/kenney/audios/glass_001.ogg +0 -0
  75. package/public/res/audios/kenney/audios/glass_002.ogg +0 -0
  76. package/public/res/audios/kenney/audios/glass_003.ogg +0 -0
  77. package/public/res/audios/kenney/audios/glass_004.ogg +0 -0
  78. package/public/res/audios/kenney/audios/glass_005.ogg +0 -0
  79. package/public/res/audios/kenney/audios/glass_006.ogg +0 -0
  80. package/public/res/audios/kenney/audios/glitch_001.ogg +0 -0
  81. package/public/res/audios/kenney/audios/glitch_002.ogg +0 -0
  82. package/public/res/audios/kenney/audios/glitch_003.ogg +0 -0
  83. package/public/res/audios/kenney/audios/glitch_004.ogg +0 -0
  84. package/public/res/audios/kenney/audios/maximize_001.ogg +0 -0
  85. package/public/res/audios/kenney/audios/maximize_002.ogg +0 -0
  86. package/public/res/audios/kenney/audios/maximize_003.ogg +0 -0
  87. package/public/res/audios/kenney/audios/maximize_004.ogg +0 -0
  88. package/public/res/audios/kenney/audios/maximize_005.ogg +0 -0
  89. package/public/res/audios/kenney/audios/maximize_006.ogg +0 -0
  90. package/public/res/audios/kenney/audios/maximize_007.ogg +0 -0
  91. package/public/res/audios/kenney/audios/maximize_008.ogg +0 -0
  92. package/public/res/audios/kenney/audios/maximize_009.ogg +0 -0
  93. package/public/res/audios/kenney/audios/minimize_001.ogg +0 -0
  94. package/public/res/audios/kenney/audios/minimize_002.ogg +0 -0
  95. package/public/res/audios/kenney/audios/minimize_003.ogg +0 -0
  96. package/public/res/audios/kenney/audios/minimize_004.ogg +0 -0
  97. package/public/res/audios/kenney/audios/minimize_005.ogg +0 -0
  98. package/public/res/audios/kenney/audios/minimize_006.ogg +0 -0
  99. package/public/res/audios/kenney/audios/minimize_007.ogg +0 -0
  100. package/public/res/audios/kenney/audios/minimize_008.ogg +0 -0
  101. package/public/res/audios/kenney/audios/minimize_009.ogg +0 -0
  102. package/public/res/audios/kenney/audios/open_001.ogg +0 -0
  103. package/public/res/audios/kenney/audios/open_002.ogg +0 -0
  104. package/public/res/audios/kenney/audios/open_003.ogg +0 -0
  105. package/public/res/audios/kenney/audios/open_004.ogg +0 -0
  106. package/public/res/audios/kenney/audios/pluck_001.ogg +0 -0
  107. package/public/res/audios/kenney/audios/pluck_002.ogg +0 -0
  108. package/public/res/audios/kenney/audios/question_001.ogg +0 -0
  109. package/public/res/audios/kenney/audios/question_002.ogg +0 -0
  110. package/public/res/audios/kenney/audios/question_003.ogg +0 -0
  111. package/public/res/audios/kenney/audios/question_004.ogg +0 -0
  112. package/public/res/audios/kenney/audios/scratch_001.ogg +0 -0
  113. package/public/res/audios/kenney/audios/scratch_002.ogg +0 -0
  114. package/public/res/audios/kenney/audios/scratch_003.ogg +0 -0
  115. package/public/res/audios/kenney/audios/scratch_004.ogg +0 -0
  116. package/public/res/audios/kenney/audios/scratch_005.ogg +0 -0
  117. package/public/res/audios/kenney/audios/scroll_001.ogg +0 -0
  118. package/public/res/audios/kenney/audios/scroll_002.ogg +0 -0
  119. package/public/res/audios/kenney/audios/scroll_003.ogg +0 -0
  120. package/public/res/audios/kenney/audios/scroll_004.ogg +0 -0
  121. package/public/res/audios/kenney/audios/scroll_005.ogg +0 -0
  122. package/public/res/audios/kenney/audios/select_001.ogg +0 -0
  123. package/public/res/audios/kenney/audios/select_002.ogg +0 -0
  124. package/public/res/audios/kenney/audios/select_003.ogg +0 -0
  125. package/public/res/audios/kenney/audios/select_004.ogg +0 -0
  126. package/public/res/audios/kenney/audios/select_005.ogg +0 -0
  127. package/public/res/audios/kenney/audios/select_006.ogg +0 -0
  128. package/public/res/audios/kenney/audios/select_007.ogg +0 -0
  129. package/public/res/audios/kenney/audios/select_008.ogg +0 -0
  130. package/public/res/audios/kenney/audios/switch_001.ogg +0 -0
  131. package/public/res/audios/kenney/audios/switch_002.ogg +0 -0
  132. package/public/res/audios/kenney/audios/switch_003.ogg +0 -0
  133. package/public/res/audios/kenney/audios/switch_004.ogg +0 -0
  134. package/public/res/audios/kenney/audios/switch_005.ogg +0 -0
  135. package/public/res/audios/kenney/audios/switch_006.ogg +0 -0
  136. package/public/res/audios/kenney/audios/switch_007.ogg +0 -0
  137. package/public/res/audios/kenney/audios/tick_001.ogg +0 -0
  138. package/public/res/audios/kenney/audios/tick_002.ogg +0 -0
  139. package/public/res/audios/kenney/audios/tick_004.ogg +0 -0
  140. package/public/res/audios/kenney/audios/toggle_001.ogg +0 -0
  141. package/public/res/audios/kenney/audios/toggle_002.ogg +0 -0
  142. package/public/res/audios/kenney/audios/toggle_003.ogg +0 -0
  143. package/public/res/audios/kenney/audios/toggle_004.ogg +0 -0
  144. package/public/res/audios/start.mp3 +0 -0
  145. package/public/res/audios/toggle_002.mp3 +0 -0
  146. package/public/res/fonts/Accuratist.ttf +0 -0
  147. package/public/res/fonts/Closeness.ttf +0 -0
  148. package/public/res/fonts/WARGAMES.TTF +0 -0
  149. package/public/res/fonts/readme.txt +5 -0
  150. package/public/res/fonts/stormfaze.ttf +0 -0
  151. package/public/res/icons/512.png +0 -0
  152. package/public/res/icons/webgpu-horizontal.svg +45 -0
  153. package/public/res/meshes/blender/cube.blend +0 -0
  154. package/public/res/meshes/blender/cube.blend1 +0 -0
  155. package/public/res/meshes/blender/cube.mtl +12 -0
  156. package/public/res/meshes/blender/cube.obj +46 -0
  157. package/public/res/meshes/blender/cube.png +0 -0
  158. package/public/res/meshes/blender/cubeSmartUV.blend +0 -0
  159. package/public/res/meshes/blender/cubeSmartUV.mtl +12 -0
  160. package/public/res/meshes/blender/cubeSmartUV.obj +46 -0
  161. package/public/res/meshes/blender/lopta.mtl +10 -0
  162. package/public/res/meshes/blender/lopta.obj +3402 -0
  163. package/public/res/meshes/blender/piramyd.blend +0 -0
  164. package/public/res/meshes/blender/piramyd.blend1 +0 -0
  165. package/public/res/meshes/blender/piramyd.js +42 -0
  166. package/public/res/meshes/blender/piramyd.mtl +10 -0
  167. package/public/res/meshes/blender/piramyd.obj +18696 -0
  168. package/public/res/meshes/blender/piramyd1.js +42 -0
  169. package/public/res/meshes/blender/sphepe.blend +0 -0
  170. package/public/res/meshes/blender/sphepe.blend1 +0 -0
  171. package/public/res/meshes/blender/sphere.mtl +10 -0
  172. package/public/res/meshes/blender/sphere.obj +3402 -0
  173. package/public/res/meshes/blender/welcomeTextblend.blend +0 -0
  174. package/public/res/meshes/dragon/stanfordDragonData.js +5 -0
  175. package/public/res/meshes/jamb/bg.blend +0 -0
  176. package/public/res/meshes/jamb/bg.blend1 +0 -0
  177. package/public/res/meshes/jamb/bg.mtl +12 -0
  178. package/public/res/meshes/jamb/bg.obj +17 -0
  179. package/public/res/meshes/jamb/bg.png +0 -0
  180. package/public/res/meshes/jamb/dice-default.png +0 -0
  181. package/public/res/meshes/jamb/dice-mark.png +0 -0
  182. package/public/res/meshes/jamb/dice.mtl +12 -0
  183. package/public/res/meshes/jamb/dice.obj +40 -0
  184. package/public/res/meshes/jamb/dice.png +0 -0
  185. package/public/res/meshes/jamb/jamb-title.mtl +12 -0
  186. package/public/res/meshes/jamb/jamb-title.obj +26008 -0
  187. package/public/res/meshes/jamb/jamb.blend +0 -0
  188. package/public/res/meshes/jamb/jamb.blend1 +0 -0
  189. package/public/res/meshes/jamb/logo.png +0 -0
  190. package/public/res/meshes/jamb/nidzaDice.blend +0 -0
  191. package/public/res/meshes/jamb/nidzaDice.blend1 +0 -0
  192. package/public/res/meshes/jamb/pile.blend +0 -0
  193. package/public/res/meshes/jamb/simpleCube.blend +0 -0
  194. package/public/res/meshes/jamb/simpleCube.blend1 +0 -0
  195. package/public/res/meshes/jamb/sounds/roll1.wav +0 -0
  196. package/public/res/meshes/jamb/text.png +0 -0
  197. package/public/res/meshes/obj/armor.obj +319 -0
  198. package/public/res/meshes/obj/armor.png +0 -0
  199. package/public/res/meshes/objs-sequence/swat-walk-pistol_000001.mtl +22 -0
  200. package/public/res/meshes/objs-sequence/swat-walk-pistol_000001.obj +23264 -0
  201. package/public/res/meshes/objs-sequence/swat-walk-pistol_000002.mtl +22 -0
  202. package/public/res/meshes/objs-sequence/swat-walk-pistol_000002.obj +23261 -0
  203. package/public/res/meshes/objs-sequence/swat-walk-pistol_000003.mtl +22 -0
  204. package/public/res/meshes/objs-sequence/swat-walk-pistol_000003.obj +23264 -0
  205. package/public/res/meshes/objs-sequence/swat-walk-pistol_000004.mtl +22 -0
  206. package/public/res/meshes/objs-sequence/swat-walk-pistol_000004.obj +23261 -0
  207. package/public/res/meshes/objs-sequence/swat-walk-pistol_000005.mtl +22 -0
  208. package/public/res/meshes/objs-sequence/swat-walk-pistol_000005.obj +23261 -0
  209. package/public/res/meshes/objs-sequence/swat-walk-pistol_000006.mtl +22 -0
  210. package/public/res/meshes/objs-sequence/swat-walk-pistol_000006.obj +23261 -0
  211. package/public/res/meshes/objs-sequence/swat-walk-pistol_000007.mtl +22 -0
  212. package/public/res/meshes/objs-sequence/swat-walk-pistol_000007.obj +23264 -0
  213. package/public/res/meshes/objs-sequence/swat-walk-pistol_000008.mtl +22 -0
  214. package/public/res/meshes/objs-sequence/swat-walk-pistol_000008.obj +23263 -0
  215. package/public/res/meshes/objs-sequence/swat-walk-pistol_000009.mtl +22 -0
  216. package/public/res/meshes/objs-sequence/swat-walk-pistol_000009.obj +23264 -0
  217. package/public/res/meshes/objs-sequence/swat-walk-pistol_000010.mtl +22 -0
  218. package/public/res/meshes/objs-sequence/swat-walk-pistol_000010.obj +23260 -0
  219. package/public/res/meshes/objs-sequence/swat-walk-pistol_000011.mtl +22 -0
  220. package/public/res/meshes/objs-sequence/swat-walk-pistol_000011.obj +23262 -0
  221. package/public/res/meshes/objs-sequence/swat-walk-pistol_000012.mtl +22 -0
  222. package/public/res/meshes/objs-sequence/swat-walk-pistol_000012.obj +23262 -0
  223. package/public/res/meshes/objs-sequence/swat-walk-pistol_000013.mtl +22 -0
  224. package/public/res/meshes/objs-sequence/swat-walk-pistol_000013.obj +23263 -0
  225. package/public/res/meshes/objs-sequence/swat-walk-pistol_000014.mtl +22 -0
  226. package/public/res/meshes/objs-sequence/swat-walk-pistol_000014.obj +23262 -0
  227. package/public/res/meshes/objs-sequence/swat-walk-pistol_000015.mtl +22 -0
  228. package/public/res/meshes/objs-sequence/swat-walk-pistol_000015.obj +23263 -0
  229. package/public/res/meshes/objs-sequence/swat-walk-pistol_000016.mtl +22 -0
  230. package/public/res/meshes/objs-sequence/swat-walk-pistol_000016.obj +23264 -0
  231. package/public/res/meshes/objs-sequence/swat-walk-pistol_000017.mtl +22 -0
  232. package/public/res/meshes/objs-sequence/swat-walk-pistol_000017.obj +23263 -0
  233. package/public/res/meshes/objs-sequence/swat-walk-pistol_000018.mtl +22 -0
  234. package/public/res/meshes/objs-sequence/swat-walk-pistol_000018.obj +23261 -0
  235. package/public/res/meshes/objs-sequence/swat-walk-pistol_000019.mtl +22 -0
  236. package/public/res/meshes/objs-sequence/swat-walk-pistol_000019.obj +23263 -0
  237. package/public/res/meshes/objs-sequence/swat-walk-pistol_000020.mtl +22 -0
  238. package/public/res/meshes/objs-sequence/swat-walk-pistol_000020.obj +23261 -0
  239. package/public/res/meshes/shapes/star1.obj +60 -0
  240. package/public/res/multilang/en.json +39 -0
  241. package/public/res/multilang/sr.json +39 -0
  242. package/public/res/textures/default.png +0 -0
  243. package/public/res/textures/rust.jpg +0 -0
  244. package/public/res/textures/tex1.jpg +0 -0
  245. package/public/res/videos/readme.txt +2 -0
  246. package/public/res/videos/tunel.mp4 +0 -0
  247. package/public/test.html +636 -0
  248. package/public/three-test.js +165 -0
  249. package/public/worker.html +25 -0
  250. package/src/engine/ball.js +482 -0
  251. package/src/engine/cube.js +496 -0
  252. package/src/engine/engine.js +404 -0
  253. package/src/engine/final/adaptJSON1.js +53 -0
  254. package/src/engine/final/utils2.js +63 -0
  255. package/src/engine/lights.js +153 -0
  256. package/src/engine/loader-obj.js +473 -0
  257. package/src/engine/materials.js +295 -0
  258. package/src/engine/matrix-class.js +252 -0
  259. package/src/engine/mesh-obj.js +574 -0
  260. package/src/engine/raycast.js +218 -0
  261. package/src/engine/utils.js +881 -0
  262. package/src/libs/mat.js +0 -0
  263. package/src/multilang/lang.js +35 -0
  264. package/src/physics/matrix-ammo.js +363 -0
  265. package/src/shaders/fragment.video.wgsl.js +83 -0
  266. package/src/shaders/fragment.wgsl.js +75 -0
  267. package/src/shaders/shaders.js +51 -0
  268. package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-fs.glsl +56 -0
  269. package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-vs.glsl +75 -0
  270. package/src/shaders/vertex.wgsl.js +54 -0
  271. package/src/shaders/vertexShadow.wgsl.js +20 -0
  272. package/src/sounds/sounds.js +69 -0
  273. package/src/world.js +474 -0
@@ -0,0 +1,473 @@
1
+ /**
2
+ * The main Mesh class. The constructor will parse through the OBJ file data
3
+ * and collect the vertex, vertex normal, texture, and face information. This
4
+ * information can then be used later on when creating your VBOs. See
5
+ * OBJ.initMeshBuffers for an example of how to use the newly created Mesh
6
+ *
7
+ * Nidza Note:
8
+ * There is difference from me source obj loader and me-wgpu obj loader
9
+ * Here we need scele in comp x,y,z because we use also primitive [cube, sphere etc...]
10
+ * @class Mesh
11
+ * @constructor
12
+ *
13
+ * @param {String} objectData a string representation of an OBJ file with newlines preserved.
14
+ */
15
+
16
+ export class constructMesh {
17
+
18
+ constructor (objectData, inputArg) {
19
+ this.inputArg = inputArg;
20
+ this.objectData = objectData;
21
+ this.create(objectData, inputArg);
22
+ this.setScale = (s) => {
23
+ this.inputArg.scale = s;
24
+ this.create(this.objectData, this.inputArg)
25
+ };
26
+ this.updateBuffers = () => {
27
+ this.inputArg.scale = [0.1,0.1,0.1];
28
+ this.create(this.objectData, this.inputArg);
29
+ };
30
+ }
31
+
32
+ create = (objectData, inputArg, callback) => {
33
+
34
+ if (typeof callback === 'undefined') callback = function() {};
35
+
36
+ let initOrientation = [0,1,2];
37
+ /*
38
+ The OBJ file format does a sort of compression when saving a model in a
39
+ program like Blender. There are at least 3 sections (4 including textures)
40
+ within the file. Each line in a section begins with the same string:
41
+ * 'v': indicates vertex section
42
+ * 'vn': indicates vertex normal section
43
+ * 'f': indicates the faces section
44
+ * 'vt': indicates vertex texture section (if textures were used on the model)
45
+ Each of the above sections (except for the faces section) is a list/set of
46
+ unique vertices.
47
+ Each line of the faces section contains a list of
48
+ (vertex, [texture], normal) groups
49
+ Some examples:
50
+ // the texture index is optional, both formats are possible for models
51
+ // without a texture applied
52
+ f 1/25 18/46 12/31
53
+ f 1//25 18//46 12//31
54
+ // A 3 vertex face with texture indices
55
+ f 16/92/11 14/101/22 1/69/1
56
+ // A 4 vertex face
57
+ f 16/92/11 40/109/40 38/114/38 14/101/22
58
+ The first two lines are examples of a 3 vertex face without a texture applied.
59
+ The second is an example of a 3 vertex face with a texture applied.
60
+ The third is an example of a 4 vertex face. Note: a face can contain N
61
+ number of vertices.
62
+ Each number that appears in one of the groups is a 1-based index
63
+ corresponding to an item from the other sections (meaning that indexing
64
+ starts at one and *not* zero).
65
+ For example:
66
+ `f 16/92/11` is saying to
67
+ - take the 16th element from the [v] vertex array
68
+ - take the 92nd element from the [vt] texture array
69
+ - take the 11th element from the [vn] normal array
70
+ and together they make a unique vertex.
71
+ Using all 3+ unique Vertices from the face line will produce a polygon.
72
+ Now, you could just go through the OBJ file and create a new vertex for
73
+ each face line and WebGL will draw what appears to be the same model.
74
+ However, vertices will be overlapped and duplicated all over the place.
75
+ Consider a cube in 3D space centered about the origin and each side is
76
+ 2 units long. The front face (with the positive Z-axis pointing towards
77
+ you) would have a Top Right vertex (looking orthogonal to its normal)
78
+ mapped at (1,1,1) The right face would have a Top Left vertex (looking
79
+ orthogonal to its normal) at (1,1,1) and the top face would have a Bottom
80
+ Right vertex (looking orthogonal to its normal) at (1,1,1). Each face
81
+ has a vertex at the same coordinates, however, three distinct vertices
82
+ will be drawn at the same spot.
83
+ To solve the issue of duplicate Vertices (the `(vertex, [texture], normal)`
84
+ groups), while iterating through the face lines, when a group is encountered
85
+ the whole group string ('16/92/11') is checked to see if it exists in the
86
+ packed.hashindices object, and if it doesn't, the indices it specifies
87
+ are used to look up each attribute in the corresponding attribute arrays
88
+ already created. The values are then copied to the corresponding unpacked
89
+ array (flattened to play nice with WebGL's ELEMENT_ARRAY_BUFFER indexing),
90
+ the group string is added to the hashindices set and the current unpacked
91
+ index is used as this hashindices value so that the group of elements can
92
+ be reused. The unpacked index is incremented. If the group string already
93
+ exists in the hashindices object, its corresponding value is the index of
94
+ that group and is appended to the unpacked indices array.
95
+ */
96
+ var verts = [],
97
+ vertNormals = [],
98
+ textures = [],
99
+ unpacked = {};
100
+ // unpacking stuff
101
+ unpacked.verts = [];
102
+ unpacked.norms = [];
103
+ unpacked.textures = [];
104
+ unpacked.hashindices = {};
105
+ unpacked.indices = [];
106
+ unpacked.index = 0;
107
+ // array of lines separated by the newline
108
+ var lines = objectData.split('\n');
109
+
110
+ // update swap orientation
111
+ if (inputArg.swap[0] !== null) {
112
+ swap(inputArg.swap[0], inputArg.swap[1], initOrientation);
113
+ }
114
+
115
+ var VERTEX_RE = /^v\s/;
116
+ var NORMAL_RE = /^vn\s/;
117
+ var TEXTURE_RE = /^vt\s/;
118
+ var FACE_RE = /^f\s/;
119
+ var WHITESPACE_RE = /\s+/;
120
+
121
+ for (var i = 0; i < lines.length; i++) {
122
+ var line = lines[i].trim();
123
+ var elements = line.split(WHITESPACE_RE);
124
+ elements.shift();
125
+
126
+ if (VERTEX_RE.test(line)) {
127
+ // if this is a vertex
128
+ verts.push.apply(verts, elements);
129
+ } else if (NORMAL_RE.test(line)) {
130
+ // if this is a vertex normal
131
+ vertNormals.push.apply(vertNormals, elements);
132
+ } else if (TEXTURE_RE.test(line)) {
133
+ // if this is a texture
134
+ textures.push.apply(textures, elements);
135
+ } else if (FACE_RE.test(line)) {
136
+ // if this is a face
137
+ /*
138
+ split this face into an array of vertex groups
139
+ for example:
140
+ f 16/92/11 14/101/22 1/69/1
141
+ becomes:
142
+ ['16/92/11', '14/101/22', '1/69/1'];
143
+ */
144
+ var quad = false;
145
+ for (var j = 0, eleLen = elements.length; j < eleLen; j++) {
146
+ // Triangulating quads
147
+ // quad: 'f v0/t0/vn0 v1/t1/vn1 v2/t2/vn2 v3/t3/vn3/'
148
+ // corresponding triangles:
149
+ // 'f v0/t0/vn0 v1/t1/vn1 v2/t2/vn2'
150
+ // 'f v2/t2/vn2 v3/t3/vn3 v0/t0/vn0'
151
+ if (j === 3 && !quad) {
152
+ // add v2/t2/vn2 in again before continuing to 3
153
+ j = 2;
154
+ quad = true;
155
+ }
156
+ if (elements[j] in unpacked.hashindices) {
157
+ unpacked.indices.push(unpacked.hashindices[elements[j]]);
158
+ } else {
159
+ /*
160
+ Each element of the face line array is a vertex which has its
161
+ attributes delimited by a forward slash. This will separate
162
+ each attribute into another array:
163
+ '19/92/11'
164
+ becomes:
165
+ vertex = ['19', '92', '11'];
166
+ where
167
+ vertex[0] is the vertex index
168
+ vertex[1] is the texture index
169
+ vertex[2] is the normal index
170
+ Think of faces having Vertices which are comprised of the
171
+ attributes location (v), texture (vt), and normal (vn).
172
+ */
173
+ var vertex = elements[j].split('/');
174
+ /*
175
+ The verts, textures, and vertNormals arrays each contain a
176
+ flattend array of coordinates.
177
+ Because it gets confusing by referring to vertex and then
178
+ vertex (both are different in my descriptions) I will explain
179
+ what's going on using the vertexNormals array:
180
+ vertex[2] will contain the one-based index of the vertexNormals
181
+ section (vn). One is subtracted from this index number to play
182
+ nice with javascript's zero-based array indexing.
183
+ Because vertexNormal is a flattened array of x, y, z values,
184
+ simple pointer arithmetic is used to skip to the start of the
185
+ vertexNormal, then the offset is added to get the correct
186
+ component: +0 is x, +1 is y, +2 is z.
187
+ This same process is repeated for verts and textures.
188
+ */
189
+ // vertex position
190
+ unpacked.verts.push(+verts[(vertex[0] - 1) * 3 + initOrientation[0]] * inputArg.scale[0]);
191
+ unpacked.verts.push(+verts[(vertex[0] - 1) * 3 + initOrientation[1]] * inputArg.scale[1]);
192
+ unpacked.verts.push(+verts[(vertex[0] - 1) * 3 + initOrientation[2]] * inputArg.scale[2]);
193
+
194
+ // vertex textures
195
+ if (textures.length) {
196
+ unpacked.textures.push(+textures[(vertex[1] - 1) * 2 + 0]);
197
+ unpacked.textures.push(+textures[(vertex[1] - 1) * 2 + 1]);
198
+ }
199
+ // vertex normals
200
+ unpacked.norms.push(+vertNormals[(vertex[2] - 1) * 3 + 0]);
201
+ unpacked.norms.push(+vertNormals[(vertex[2] - 1) * 3 + 1]);
202
+ unpacked.norms.push(+vertNormals[(vertex[2] - 1) * 3 + 2]);
203
+ // add the newly created vertex to the list of indices
204
+ unpacked.hashindices[elements[j]] = unpacked.index;
205
+ unpacked.indices.push(unpacked.index);
206
+ // increment the counter
207
+ unpacked.index += 1;
208
+ }
209
+ if (j === 3 && quad) {
210
+ // add v0/t0/vn0 onto the second triangle
211
+ unpacked.indices.push(unpacked.hashindices[elements[0]]);
212
+ }
213
+ }
214
+ }
215
+ }
216
+ this.vertices = unpacked.verts;
217
+ this.vertexNormals = unpacked.norms;
218
+ this.textures = unpacked.textures;
219
+ this.indices = unpacked.indices;
220
+ callback();
221
+ return this;
222
+ };
223
+
224
+ }
225
+
226
+ var Ajax = function () {
227
+ // this is just a helper class to ease ajax calls
228
+ var _this = this;
229
+ this.xmlhttp = new XMLHttpRequest();
230
+
231
+ this.get = function (url, callback) {
232
+ _this.xmlhttp.onreadystatechange = function () {
233
+ if (_this.xmlhttp.readyState === 4) {
234
+ callback(_this.xmlhttp.responseText, _this.xmlhttp.status);
235
+ }
236
+ };
237
+ _this.xmlhttp.open('GET', url, true);
238
+ _this.xmlhttp.send();
239
+ };
240
+ };
241
+
242
+ /**
243
+ * Takes in an object of `mesh_name`, `'/url/to/OBJ/file'` pairs and a callback
244
+ * function. Each OBJ file will be ajaxed in and automatically converted to
245
+ * an OBJ.Mesh. When all files have successfully downloaded the callback
246
+ * function provided will be called and passed in an object containing
247
+ * the newly created meshes.
248
+ *
249
+ * **Note:** In order to use this function as a way to download meshes, a
250
+ * webserver of some sort must be used.
251
+ *
252
+ * @param {Object} nameAndURLs an object where the key is the name of the mesh and the value is the url to that mesh's OBJ file
253
+ *
254
+ * @param {Function} completionCallback should contain a function that will take one parameter: an object array where the keys will be the unique object name and the value will be a Mesh object
255
+ *
256
+ * @param {Object} meshes In case other meshes are loaded separately or if a previously declared variable is desired to be used, pass in a (possibly empty) json object of the pattern: { '<mesh_name>': OBJ.Mesh }
257
+ *
258
+ */
259
+ export var downloadMeshes = function (nameAndURLs, completionCallback, inputArg) {
260
+ // the total number of meshes. this is used to implement "blocking"
261
+ var semaphore = Object.keys(nameAndURLs).length;
262
+ // if error is true, an alert will given
263
+ var error = false;
264
+ // this is used to check if all meshes have been downloaded
265
+ // if meshes is supplied, then it will be populated, otherwise
266
+ // a new object is created. this will be passed into the completionCallback
267
+ if (typeof inputArg === 'undefined') {
268
+ var inputArg = {
269
+ scale: [0.1,0.1,0.1],
270
+ swap: [null]
271
+ };
272
+ }
273
+ if (typeof inputArg.scale === 'undefined') inputArg.scale = [0.1,0.1,0.1];
274
+ if (typeof inputArg.swap === 'undefined') inputArg.swap = [null];
275
+
276
+ var meshes = {};
277
+
278
+ // loop over the mesh_name,url key,value pairs
279
+ for (var mesh_name in nameAndURLs) {
280
+ if (nameAndURLs.hasOwnProperty(mesh_name)) {
281
+ new Ajax().get(
282
+ nameAndURLs[mesh_name],
283
+ (function (name) {
284
+ return function (data, status) {
285
+ if (status === 200) {
286
+ meshes[name] = new constructMesh(data, inputArg);
287
+ } else {
288
+ error = true;
289
+ console.error('An error has occurred and the mesh "' + name + '" could not be downloaded.');
290
+ }
291
+ // the request has finished, decrement the counter
292
+ semaphore--;
293
+ if (semaphore === 0) {
294
+ if (error) {
295
+ // if an error has occurred, the user is notified here and the
296
+ // callback is not called
297
+ console.error('An error has occurred and one or meshes has not been ' + 'downloaded. The execution of the script has terminated.');
298
+ throw '';
299
+ }
300
+ // there haven't been any errors in retrieving the meshes
301
+ // call the callback
302
+ completionCallback(meshes);
303
+ }
304
+ };
305
+ })(mesh_name)
306
+ );
307
+ }
308
+ }
309
+ };
310
+
311
+ /**
312
+ * Takes in the WebGL context and a Mesh, then creates and appends the buffers
313
+ * to the mesh object as attributes.
314
+ *
315
+ * @param {WebGLRenderingContext} gl the `canvas.getContext('webgl')` context instance
316
+ * @param {Mesh} mesh a single `OBJ.Mesh` instance
317
+ *
318
+ * The newly created mesh attributes are:
319
+ *
320
+ * Attrbute | Description
321
+ * :--- | ---
322
+ * **normalBuffer** |contains the model&#39;s Vertex Normals
323
+ * normalBuffer.itemSize |set to 3 items
324
+ * normalBuffer.numItems |the total number of vertex normals
325
+ * |
326
+ * **textureBuffer** |contains the model&#39;s Texture Coordinates
327
+ * textureBuffer.itemSize |set to 2 items
328
+ * textureBuffer.numItems |the number of texture coordinates
329
+ * |
330
+ * **vertexBuffer** |contains the model&#39;s Vertex Position Coordinates (does not include w)
331
+ * vertexBuffer.itemSize |set to 3 items
332
+ * vertexBuffer.numItems |the total number of vertices
333
+ * |
334
+ * **indexBuffer** |contains the indices of the faces
335
+ * indexBuffer.itemSize |is set to 1
336
+ * indexBuffer.numItems |the total number of indices
337
+ *
338
+ * A simple example (a lot of steps are missing, so don't copy and paste):
339
+ *
340
+ * var gl = canvas.getContext('webgl'),
341
+ * mesh = OBJ.Mesh(obj_file_data);
342
+ * // compile the shaders and create a shader program
343
+ * var shaderProgram = gl.createProgram();
344
+ * // compilation stuff here
345
+ * ...
346
+ * // make sure you have vertex, vertex normal, and texture coordinate
347
+ * // attributes located in your shaders and attach them to the shader program
348
+ * shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
349
+ * gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
350
+ *
351
+ * shaderProgram.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "aVertexNormal");
352
+ * gl.enableVertexAttribArray(shaderProgram.vertexNormalAttribute);
353
+ *
354
+ * shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
355
+ * gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
356
+ *
357
+ * // create and initialize the vertex, vertex normal, and texture coordinate buffers
358
+ * // and save on to the mesh object
359
+ * OBJ.initMeshBuffers(gl, mesh);
360
+ *
361
+ * // now to render the mesh
362
+ * gl.bindBuffer(gl.ARRAY_BUFFER, mesh.vertexBuffer);
363
+ * gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, mesh.vertexBuffer.itemSize, gl.FLOAT, false, 0, 0);
364
+ * // it's possible that the mesh doesn't contain
365
+ * // any texture coordinates (e.g. suzanne.obj in the development branch).
366
+ * // in this case, the texture vertexAttribArray will need to be disabled
367
+ * // before the call to drawElements
368
+ * if(!mesh.textures.length){
369
+ * gl.disableVertexAttribArray(shaderProgram.textureCoordAttribute);
370
+ * }
371
+ * else{
372
+ * // if the texture vertexAttribArray has been previously
373
+ * // disabled, then it needs to be re-enabled
374
+ * gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
375
+ * gl.bindBuffer(gl.ARRAY_BUFFER, mesh.textureBuffer);
376
+ * gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, mesh.textureBuffer.itemSize, gl.FLOAT, false, 0, 0);
377
+ * }
378
+ *
379
+ * gl.bindBuffer(gl.ARRAY_BUFFER, mesh.normalBuffer);
380
+ * gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute, mesh.normalBuffer.itemSize, gl.FLOAT, false, 0, 0);
381
+ *
382
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, model.mesh.indexBuffer);
383
+ * gl.drawElements(gl.TRIANGLES, model.mesh.indexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
384
+ */
385
+ export var initMeshBuffers = function (gl, mesh) {
386
+ // mesh.vertexNormals
387
+ // mesh.textures
388
+ // mesh.vertices
389
+ // mesh.indices
390
+ };
391
+
392
+ /**
393
+ * @description
394
+ * Construct sequence list argument for downloadMeshes.
395
+ * This is adaptation for blender obj animation export.
396
+ * For example:
397
+ * matrixEngine.objLoader.downloadMeshes(
398
+ matrixEngine.objLoader.makeObjSeqArg(
399
+ {
400
+ id: objName,
401
+ joinMultiPahts: [
402
+ {
403
+ path: "res/bvh-skeletal-base/swat-guy/seq-walk/low/swat",
404
+ id: objName,
405
+ from: 1, to: 34
406
+ },
407
+ {
408
+ path: "res/bvh-skeletal-base/swat-guy/seq-walk-pistol/low/swat-walk-pistol",
409
+ id: objName,
410
+ from: 35, to: 54
411
+ }
412
+ ]
413
+ }),
414
+ onLoadObj
415
+ );
416
+ */
417
+ export const makeObjSeqArg = (arg) => {
418
+ // Adaptation for blender (animation) obj exporter.
419
+ var local = {};
420
+
421
+ function localCalc(arg, noInitial = false) {
422
+ var zeros = '00000';
423
+ var l = {};
424
+ var helper = arg.from;
425
+ for (let j = arg.from, z=1; j <= arg.to;j++ ) {
426
+
427
+ if (z > 9 && z < 99) {
428
+ zeros = '0000';
429
+ } else if (z > 99 && z < 999) {
430
+ zeros = '000';
431
+ } // no need more then 999
432
+
433
+ if (helper == arg.from && noInitial === false) {
434
+ l[arg.id] = arg.path + '_' + zeros + z + '.obj';
435
+ } else {
436
+ l[arg.id + (helper-1)] = arg.path + '_' + zeros + z + '.obj';
437
+ }
438
+ helper++;
439
+ z++;
440
+ }
441
+ return l;
442
+ }
443
+
444
+ if (typeof arg.path === 'string' ) {
445
+ local = localCalc(arg);
446
+ } else if (typeof arg.path === 'undefined') {
447
+ if (typeof arg.joinMultiPahts !== 'undefined') {
448
+ console.log("ITS joinMultiPahts!");
449
+ var localFinal = {};
450
+ arg.joinMultiPahts.forEach((arg, index)=> {
451
+ if (index === 0) {
452
+ localFinal = Object.assign(local, localCalc(arg));
453
+ } else {
454
+ localFinal = Object.assign(local, localCalc(arg, true));
455
+ }
456
+ });
457
+ console.log("joinMultiPahts LOCAL => ", localFinal);
458
+ return localFinal;
459
+ }
460
+ }
461
+
462
+ return local;
463
+ }
464
+
465
+ /**
466
+ * @description
467
+ * Switching obj seq animations frames range.
468
+ */
469
+ export function play (nameAni) {
470
+ this.animations.active = nameAni;
471
+ this.currentAni = this.animations[this.animations.active].from;
472
+ this.playing = true;
473
+ }