babylon.objFileLoader.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory(require("babylonjs"));
  4. else if(typeof define === 'function' && define.amd)
  5. define("babylonjs-loaders", ["babylonjs"], factory);
  6. else if(typeof exports === 'object')
  7. exports["babylonjs-loaders"] = factory(require("babylonjs"));
  8. else
  9. root["LOADERS"] = factory(root["BABYLON"]);
  10. })(window, function(__WEBPACK_EXTERNAL_MODULE_babylonjs__) {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId]) {
  20. /******/ return installedModules[moduleId].exports;
  21. /******/ }
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // define getter function for harmony exports
  47. /******/ __webpack_require__.d = function(exports, name, getter) {
  48. /******/ if(!__webpack_require__.o(exports, name)) {
  49. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  50. /******/ }
  51. /******/ };
  52. /******/
  53. /******/ // define __esModule on exports
  54. /******/ __webpack_require__.r = function(exports) {
  55. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  56. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  57. /******/ }
  58. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  59. /******/ };
  60. /******/
  61. /******/ // create a fake namespace object
  62. /******/ // mode & 1: value is a module id, require it
  63. /******/ // mode & 2: merge all properties of value into the ns
  64. /******/ // mode & 4: return value when already ns object
  65. /******/ // mode & 8|1: behave like require
  66. /******/ __webpack_require__.t = function(value, mode) {
  67. /******/ if(mode & 1) value = __webpack_require__(value);
  68. /******/ if(mode & 8) return value;
  69. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  70. /******/ var ns = Object.create(null);
  71. /******/ __webpack_require__.r(ns);
  72. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  73. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  74. /******/ return ns;
  75. /******/ };
  76. /******/
  77. /******/ // getDefaultExport function for compatibility with non-harmony modules
  78. /******/ __webpack_require__.n = function(module) {
  79. /******/ var getter = module && module.__esModule ?
  80. /******/ function getDefault() { return module['default']; } :
  81. /******/ function getModuleExports() { return module; };
  82. /******/ __webpack_require__.d(getter, 'a', getter);
  83. /******/ return getter;
  84. /******/ };
  85. /******/
  86. /******/ // Object.prototype.hasOwnProperty.call
  87. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  88. /******/
  89. /******/ // __webpack_public_path__
  90. /******/ __webpack_require__.p = "";
  91. /******/
  92. /******/
  93. /******/ // Load entry module and return exports
  94. /******/ return __webpack_require__(__webpack_require__.s = "./legacy/legacy-objFileLoader.ts");
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ({
  98. /***/ "../node_modules/webpack/buildin/global.js":
  99. /*!*************************************************!*\
  100. !*** ../node_modules/webpack/buildin/global.js ***!
  101. \*************************************************/
  102. /*! no static exports found */
  103. /***/ (function(module, exports) {
  104. var g;
  105. // This works in non-strict mode
  106. g = (function() {
  107. return this;
  108. })();
  109. try {
  110. // This works if eval is allowed (see CSP)
  111. g = g || Function("return this")() || (1, eval)("this");
  112. } catch (e) {
  113. // This works if the window reference is available
  114. if (typeof window === "object") g = window;
  115. }
  116. // g can still be undefined, but nothing to do about it...
  117. // We return undefined, instead of nothing here, so it's
  118. // easier to handle this case. if(!global) { ...}
  119. module.exports = g;
  120. /***/ }),
  121. /***/ "./legacy/legacy-objFileLoader.ts":
  122. /*!****************************************!*\
  123. !*** ./legacy/legacy-objFileLoader.ts ***!
  124. \****************************************/
  125. /*! no static exports found */
  126. /***/ (function(module, exports, __webpack_require__) {
  127. "use strict";
  128. /* WEBPACK VAR INJECTION */(function(global) {
  129. function __export(m) {
  130. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  131. }
  132. Object.defineProperty(exports, "__esModule", { value: true });
  133. var Loaders = __webpack_require__(/*! ../src/OBJ */ "./src/OBJ/index.ts");
  134. /**
  135. * This is the entry point for the UMD module.
  136. * The entry point for a future ESM package should be index.ts
  137. */
  138. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  139. if (typeof globalObject !== "undefined") {
  140. for (var key in Loaders) {
  141. globalObject.BABYLON[key] = Loaders[key];
  142. }
  143. }
  144. __export(__webpack_require__(/*! ../src/OBJ */ "./src/OBJ/index.ts"));
  145. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../node_modules/webpack/buildin/global.js */ "../node_modules/webpack/buildin/global.js")))
  146. /***/ }),
  147. /***/ "./src/OBJ/index.ts":
  148. /*!**************************!*\
  149. !*** ./src/OBJ/index.ts ***!
  150. \**************************/
  151. /*! no static exports found */
  152. /***/ (function(module, exports, __webpack_require__) {
  153. "use strict";
  154. function __export(m) {
  155. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  156. }
  157. Object.defineProperty(exports, "__esModule", { value: true });
  158. __export(__webpack_require__(/*! ./objFileLoader */ "./src/OBJ/objFileLoader.ts"));
  159. /***/ }),
  160. /***/ "./src/OBJ/objFileLoader.ts":
  161. /*!**********************************!*\
  162. !*** ./src/OBJ/objFileLoader.ts ***!
  163. \**********************************/
  164. /*! no static exports found */
  165. /***/ (function(module, exports, __webpack_require__) {
  166. "use strict";
  167. Object.defineProperty(exports, "__esModule", { value: true });
  168. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  169. /**
  170. * Class reading and parsing the MTL file bundled with the obj file.
  171. */
  172. var MTLFileLoader = /** @class */ (function () {
  173. function MTLFileLoader() {
  174. /**
  175. * All material loaded from the mtl will be set here
  176. */
  177. this.materials = [];
  178. }
  179. /**
  180. * This function will read the mtl file and create each material described inside
  181. * This function could be improve by adding :
  182. * -some component missing (Ni, Tf...)
  183. * -including the specific options available
  184. *
  185. * @param scene defines the scene the material will be created in
  186. * @param data defines the mtl data to parse
  187. * @param rootUrl defines the rooturl to use in order to load relative dependencies
  188. */
  189. MTLFileLoader.prototype.parseMTL = function (scene, data, rootUrl) {
  190. if (data instanceof ArrayBuffer) {
  191. return;
  192. }
  193. //Split the lines from the file
  194. var lines = data.split('\n');
  195. //Space char
  196. var delimiter_pattern = /\s+/;
  197. //Array with RGB colors
  198. var color;
  199. //New material
  200. var material = null;
  201. //Look at each line
  202. for (var i = 0; i < lines.length; i++) {
  203. var line = lines[i].trim();
  204. // Blank line or comment
  205. if (line.length === 0 || line.charAt(0) === '#') {
  206. continue;
  207. }
  208. //Get the first parameter (keyword)
  209. var pos = line.indexOf(' ');
  210. var key = (pos >= 0) ? line.substring(0, pos) : line;
  211. key = key.toLowerCase();
  212. //Get the data following the key
  213. var value = (pos >= 0) ? line.substring(pos + 1).trim() : "";
  214. //This mtl keyword will create the new material
  215. if (key === "newmtl") {
  216. //Check if it is the first material.
  217. // Materials specifications are described after this keyword.
  218. if (material) {
  219. //Add the previous material in the material array.
  220. this.materials.push(material);
  221. }
  222. //Create a new material.
  223. // value is the name of the material read in the mtl file
  224. material = new babylonjs_1.StandardMaterial(value, scene);
  225. }
  226. else if (key === "kd" && material) {
  227. // Diffuse color (color under white light) using RGB values
  228. //value = "r g b"
  229. color = value.split(delimiter_pattern, 3).map(parseFloat);
  230. //color = [r,g,b]
  231. //Set tghe color into the material
  232. material.diffuseColor = babylonjs_1.Color3.FromArray(color);
  233. }
  234. else if (key === "ka" && material) {
  235. // Ambient color (color under shadow) using RGB values
  236. //value = "r g b"
  237. color = value.split(delimiter_pattern, 3).map(parseFloat);
  238. //color = [r,g,b]
  239. //Set tghe color into the material
  240. material.ambientColor = babylonjs_1.Color3.FromArray(color);
  241. }
  242. else if (key === "ks" && material) {
  243. // Specular color (color when light is reflected from shiny surface) using RGB values
  244. //value = "r g b"
  245. color = value.split(delimiter_pattern, 3).map(parseFloat);
  246. //color = [r,g,b]
  247. //Set the color into the material
  248. material.specularColor = babylonjs_1.Color3.FromArray(color);
  249. }
  250. else if (key === "ke" && material) {
  251. // Emissive color using RGB values
  252. color = value.split(delimiter_pattern, 3).map(parseFloat);
  253. material.emissiveColor = babylonjs_1.Color3.FromArray(color);
  254. }
  255. else if (key === "ns" && material) {
  256. //value = "Integer"
  257. material.specularPower = parseFloat(value);
  258. }
  259. else if (key === "d" && material) {
  260. //d is dissolve for current material. It mean alpha for BABYLON
  261. material.alpha = parseFloat(value);
  262. //Texture
  263. //This part can be improved by adding the possible options of texture
  264. }
  265. else if (key === "map_ka" && material) {
  266. // ambient texture map with a loaded image
  267. //We must first get the folder of the image
  268. material.ambientTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  269. }
  270. else if (key === "map_kd" && material) {
  271. // Diffuse texture map with a loaded image
  272. material.diffuseTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  273. }
  274. else if (key === "map_ks" && material) {
  275. // Specular texture map with a loaded image
  276. //We must first get the folder of the image
  277. material.specularTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  278. }
  279. else if (key === "map_ns") {
  280. //Specular
  281. //Specular highlight component
  282. //We must first get the folder of the image
  283. //
  284. //Not supported by BABYLON
  285. //
  286. // continue;
  287. }
  288. else if (key === "map_bump" && material) {
  289. //The bump texture
  290. material.bumpTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  291. }
  292. else if (key === "map_d" && material) {
  293. // The dissolve of the material
  294. material.opacityTexture = MTLFileLoader._getTexture(rootUrl, value, scene);
  295. //Options for illumination
  296. }
  297. else if (key === "illum") {
  298. //Illumination
  299. if (value === "0") {
  300. //That mean Kd == Kd
  301. }
  302. else if (value === "1") {
  303. //Color on and Ambient on
  304. }
  305. else if (value === "2") {
  306. //Highlight on
  307. }
  308. else if (value === "3") {
  309. //Reflection on and Ray trace on
  310. }
  311. else if (value === "4") {
  312. //Transparency: Glass on, Reflection: Ray trace on
  313. }
  314. else if (value === "5") {
  315. //Reflection: Fresnel on and Ray trace on
  316. }
  317. else if (value === "6") {
  318. //Transparency: Refraction on, Reflection: Fresnel off and Ray trace on
  319. }
  320. else if (value === "7") {
  321. //Transparency: Refraction on, Reflection: Fresnel on and Ray trace on
  322. }
  323. else if (value === "8") {
  324. //Reflection on and Ray trace off
  325. }
  326. else if (value === "9") {
  327. //Transparency: Glass on, Reflection: Ray trace off
  328. }
  329. else if (value === "10") {
  330. //Casts shadows onto invisible surfaces
  331. }
  332. }
  333. else {
  334. // console.log("Unhandled expression at line : " + i +'\n' + "with value : " + line);
  335. }
  336. }
  337. //At the end of the file, add the last material
  338. if (material) {
  339. this.materials.push(material);
  340. }
  341. };
  342. /**
  343. * Gets the texture for the material.
  344. *
  345. * If the material is imported from input file,
  346. * We sanitize the url to ensure it takes the textre from aside the material.
  347. *
  348. * @param rootUrl The root url to load from
  349. * @param value The value stored in the mtl
  350. * @return The Texture
  351. */
  352. MTLFileLoader._getTexture = function (rootUrl, value, scene) {
  353. if (!value) {
  354. return null;
  355. }
  356. var url = rootUrl;
  357. // Load from input file.
  358. if (rootUrl === "file:") {
  359. var lastDelimiter = value.lastIndexOf("\\");
  360. if (lastDelimiter === -1) {
  361. lastDelimiter = value.lastIndexOf("/");
  362. }
  363. if (lastDelimiter > -1) {
  364. url += value.substr(lastDelimiter + 1);
  365. }
  366. else {
  367. url += value;
  368. }
  369. }
  370. // Not from input file.
  371. else {
  372. url += value;
  373. }
  374. return new babylonjs_1.Texture(url, scene);
  375. };
  376. return MTLFileLoader;
  377. }());
  378. exports.MTLFileLoader = MTLFileLoader;
  379. /**
  380. * OBJ file type loader.
  381. * This is a babylon scene loader plugin.
  382. */
  383. var OBJFileLoader = /** @class */ (function () {
  384. function OBJFileLoader() {
  385. /**
  386. * Defines the name of the plugin.
  387. */
  388. this.name = "obj";
  389. /**
  390. * Defines the extension the plugin is able to load.
  391. */
  392. this.extensions = ".obj";
  393. /** @hidden */
  394. this.obj = /^o/;
  395. /** @hidden */
  396. this.group = /^g/;
  397. /** @hidden */
  398. this.mtllib = /^mtllib /;
  399. /** @hidden */
  400. this.usemtl = /^usemtl /;
  401. /** @hidden */
  402. this.smooth = /^s /;
  403. /** @hidden */
  404. this.vertexPattern = /v( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
  405. // vn float float float
  406. /** @hidden */
  407. this.normalPattern = /vn( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
  408. // vt float float
  409. /** @hidden */
  410. this.uvPattern = /vt( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
  411. // f vertex vertex vertex ...
  412. /** @hidden */
  413. this.facePattern1 = /f\s+(([\d]{1,}[\s]?){3,})+/;
  414. // f vertex/uvs vertex/uvs vertex/uvs ...
  415. /** @hidden */
  416. this.facePattern2 = /f\s+((([\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
  417. // f vertex/uvs/normal vertex/uvs/normal vertex/uvs/normal ...
  418. /** @hidden */
  419. this.facePattern3 = /f\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
  420. // f vertex//normal vertex//normal vertex//normal ...
  421. /** @hidden */
  422. this.facePattern4 = /f\s+((([\d]{1,}\/\/[\d]{1,}[\s]?){3,})+)/;
  423. // f -vertex/-uvs/-normal -vertex/-uvs/-normal -vertex/-uvs/-normal ...
  424. /** @hidden */
  425. this.facePattern5 = /f\s+(((-[\d]{1,}\/-[\d]{1,}\/-[\d]{1,}[\s]?){3,})+)/;
  426. }
  427. /**
  428. * Calls synchronously the MTL file attached to this obj.
  429. * Load function or importMesh function don't enable to load 2 files in the same time asynchronously.
  430. * Without this function materials are not displayed in the first frame (but displayed after).
  431. * In consequence it is impossible to get material information in your HTML file
  432. *
  433. * @param url The URL of the MTL file
  434. * @param rootUrl
  435. * @param onSuccess Callback function to be called when the MTL file is loaded
  436. * @private
  437. */
  438. OBJFileLoader.prototype._loadMTL = function (url, rootUrl, onSuccess) {
  439. //The complete path to the mtl file
  440. var pathOfFile = babylonjs_1.Tools.BaseUrl + rootUrl + url;
  441. // Loads through the babylon tools to allow fileInput search.
  442. babylonjs_1.Tools.LoadFile(pathOfFile, onSuccess, undefined, undefined, false, function () { console.warn("Error - Unable to load " + pathOfFile); });
  443. };
  444. /**
  445. * Imports one or more meshes from the loaded glTF data and adds them to the scene
  446. * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
  447. * @param scene the scene the meshes should be added to
  448. * @param data the glTF data to load
  449. * @param rootUrl root url to load from
  450. * @param onProgress event that fires when loading progress has occured
  451. * @param fileName Defines the name of the file to load
  452. * @returns a promise containg the loaded meshes, particles, skeletons and animations
  453. */
  454. OBJFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress, fileName) {
  455. //get the meshes from OBJ file
  456. return this._parseSolid(meshesNames, scene, data, rootUrl).then(function (meshes) {
  457. return {
  458. meshes: meshes,
  459. particleSystems: [],
  460. skeletons: [],
  461. animationGroups: []
  462. };
  463. });
  464. };
  465. /**
  466. * Imports all objects from the loaded glTF data and adds them to the scene
  467. * @param scene the scene the objects should be added to
  468. * @param data the glTF data to load
  469. * @param rootUrl root url to load from
  470. * @param onProgress event that fires when loading progress has occured
  471. * @param fileName Defines the name of the file to load
  472. * @returns a promise which completes when objects have been loaded to the scene
  473. */
  474. OBJFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress, fileName) {
  475. //Get the 3D model
  476. return this.importMeshAsync(null, scene, data, rootUrl, onProgress).then(function () {
  477. // return void
  478. });
  479. };
  480. /**
  481. * Load into an asset container.
  482. * @param scene The scene to load into
  483. * @param data The data to import
  484. * @param rootUrl The root url for scene and resources
  485. * @param onProgress The callback when the load progresses
  486. * @param fileName Defines the name of the file to load
  487. * @returns The loaded asset container
  488. */
  489. OBJFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress, fileName) {
  490. return this.importMeshAsync(null, scene, data, rootUrl).then(function (result) {
  491. var container = new babylonjs_1.AssetContainer(scene);
  492. result.meshes.forEach(function (mesh) { return container.meshes.push(mesh); });
  493. container.removeAllFromScene();
  494. return container;
  495. });
  496. };
  497. /**
  498. * Read the OBJ file and create an Array of meshes.
  499. * Each mesh contains all information given by the OBJ and the MTL file.
  500. * i.e. vertices positions and indices, optional normals values, optional UV values, optional material
  501. *
  502. * @param meshesNames
  503. * @param scene Scene The scene where are displayed the data
  504. * @param data String The content of the obj file
  505. * @param rootUrl String The path to the folder
  506. * @returns Array<AbstractMesh>
  507. * @private
  508. */
  509. OBJFileLoader.prototype._parseSolid = function (meshesNames, scene, data, rootUrl) {
  510. var _this = this;
  511. var positions = []; //values for the positions of vertices
  512. var normals = []; //Values for the normals
  513. var uvs = []; //Values for the textures
  514. var meshesFromObj = []; //[mesh] Contains all the obj meshes
  515. var handledMesh; //The current mesh of meshes array
  516. var indicesForBabylon = []; //The list of indices for VertexData
  517. var wrappedPositionForBabylon = []; //The list of position in vectors
  518. var wrappedUvsForBabylon = []; //Array with all value of uvs to match with the indices
  519. var wrappedNormalsForBabylon = []; //Array with all value of normals to match with the indices
  520. var tuplePosNorm = []; //Create a tuple with indice of Position, Normal, UV [pos, norm, uvs]
  521. var curPositionInIndices = 0;
  522. var hasMeshes = false; //Meshes are defined in the file
  523. var unwrappedPositionsForBabylon = []; //Value of positionForBabylon w/o Vector3() [x,y,z]
  524. var unwrappedNormalsForBabylon = []; //Value of normalsForBabylon w/o Vector3() [x,y,z]
  525. var unwrappedUVForBabylon = []; //Value of uvsForBabylon w/o Vector3() [x,y,z]
  526. var triangles = []; //Indices from new triangles coming from polygons
  527. var materialNameFromObj = ""; //The name of the current material
  528. var fileToLoad = ""; //The name of the mtlFile to load
  529. var materialsFromMTLFile = new MTLFileLoader();
  530. var objMeshName = ""; //The name of the current obj mesh
  531. var increment = 1; //Id for meshes created by the multimaterial
  532. var isFirstMaterial = true;
  533. /**
  534. * Search for obj in the given array.
  535. * This function is called to check if a couple of data already exists in an array.
  536. *
  537. * If found, returns the index of the founded tuple index. Returns -1 if not found
  538. * @param arr Array<{ normals: Array<number>, idx: Array<number> }>
  539. * @param obj Array<number>
  540. * @returns {boolean}
  541. */
  542. var isInArray = function (arr, obj) {
  543. if (!arr[obj[0]]) {
  544. arr[obj[0]] = { normals: [], idx: [] };
  545. }
  546. var idx = arr[obj[0]].normals.indexOf(obj[1]);
  547. return idx === -1 ? -1 : arr[obj[0]].idx[idx];
  548. };
  549. var isInArrayUV = function (arr, obj) {
  550. if (!arr[obj[0]]) {
  551. arr[obj[0]] = { normals: [], idx: [], uv: [] };
  552. }
  553. var idx = arr[obj[0]].normals.indexOf(obj[1]);
  554. if (idx != 1 && (obj[2] == arr[obj[0]].uv[idx])) {
  555. return arr[obj[0]].idx[idx];
  556. }
  557. return -1;
  558. };
  559. /**
  560. * This function set the data for each triangle.
  561. * Data are position, normals and uvs
  562. * If a tuple of (position, normal) is not set, add the data into the corresponding array
  563. * If the tuple already exist, add only their indice
  564. *
  565. * @param indicePositionFromObj Integer The index in positions array
  566. * @param indiceUvsFromObj Integer The index in uvs array
  567. * @param indiceNormalFromObj Integer The index in normals array
  568. * @param positionVectorFromOBJ Vector3 The value of position at index objIndice
  569. * @param textureVectorFromOBJ Vector3 The value of uvs
  570. * @param normalsVectorFromOBJ Vector3 The value of normals at index objNormale
  571. */
  572. var setData = function (indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positionVectorFromOBJ, textureVectorFromOBJ, normalsVectorFromOBJ) {
  573. //Check if this tuple already exists in the list of tuples
  574. var _index;
  575. if (OBJFileLoader.OPTIMIZE_WITH_UV) {
  576. _index = isInArrayUV(tuplePosNorm, [
  577. indicePositionFromObj,
  578. indiceNormalFromObj,
  579. indiceUvsFromObj
  580. ]);
  581. }
  582. else {
  583. _index = isInArray(tuplePosNorm, [
  584. indicePositionFromObj,
  585. indiceNormalFromObj
  586. ]);
  587. }
  588. //If it not exists
  589. if (_index == -1) {
  590. //Add an new indice.
  591. //The array of indices is only an array with his length equal to the number of triangles - 1.
  592. //We add vertices data in this order
  593. indicesForBabylon.push(wrappedPositionForBabylon.length);
  594. //Push the position of vertice for Babylon
  595. //Each element is a Vector3(x,y,z)
  596. wrappedPositionForBabylon.push(positionVectorFromOBJ);
  597. //Push the uvs for Babylon
  598. //Each element is a Vector3(u,v)
  599. wrappedUvsForBabylon.push(textureVectorFromOBJ);
  600. //Push the normals for Babylon
  601. //Each element is a Vector3(x,y,z)
  602. wrappedNormalsForBabylon.push(normalsVectorFromOBJ);
  603. //Add the tuple in the comparison list
  604. tuplePosNorm[indicePositionFromObj].normals.push(indiceNormalFromObj);
  605. tuplePosNorm[indicePositionFromObj].idx.push(curPositionInIndices++);
  606. if (OBJFileLoader.OPTIMIZE_WITH_UV) {
  607. tuplePosNorm[indicePositionFromObj].uv.push(indiceUvsFromObj);
  608. }
  609. }
  610. else {
  611. //The tuple already exists
  612. //Add the index of the already existing tuple
  613. //At this index we can get the value of position, normal and uvs of vertex
  614. indicesForBabylon.push(_index);
  615. }
  616. };
  617. /**
  618. * Transform Vector() object onto 3 digits in an array
  619. */
  620. var unwrapData = function () {
  621. //Every array has the same length
  622. for (var l = 0; l < wrappedPositionForBabylon.length; l++) {
  623. //Push the x, y, z values of each element in the unwrapped array
  624. unwrappedPositionsForBabylon.push(wrappedPositionForBabylon[l].x, wrappedPositionForBabylon[l].y, wrappedPositionForBabylon[l].z);
  625. unwrappedNormalsForBabylon.push(wrappedNormalsForBabylon[l].x, wrappedNormalsForBabylon[l].y, wrappedNormalsForBabylon[l].z);
  626. unwrappedUVForBabylon.push(wrappedUvsForBabylon[l].x, wrappedUvsForBabylon[l].y); //z is an optional value not supported by BABYLON
  627. }
  628. // Reset arrays for the next new meshes
  629. wrappedPositionForBabylon = [];
  630. wrappedNormalsForBabylon = [];
  631. wrappedUvsForBabylon = [];
  632. tuplePosNorm = [];
  633. curPositionInIndices = 0;
  634. };
  635. /**
  636. * Create triangles from polygons by recursion
  637. * The best to understand how it works is to draw it in the same time you get the recursion.
  638. * It is important to notice that a triangle is a polygon
  639. * We get 5 patterns of face defined in OBJ File :
  640. * facePattern1 = ["1","2","3","4","5","6"]
  641. * facePattern2 = ["1/1","2/2","3/3","4/4","5/5","6/6"]
  642. * facePattern3 = ["1/1/1","2/2/2","3/3/3","4/4/4","5/5/5","6/6/6"]
  643. * facePattern4 = ["1//1","2//2","3//3","4//4","5//5","6//6"]
  644. * facePattern5 = ["-1/-1/-1","-2/-2/-2","-3/-3/-3","-4/-4/-4","-5/-5/-5","-6/-6/-6"]
  645. * Each pattern is divided by the same method
  646. * @param face Array[String] The indices of elements
  647. * @param v Integer The variable to increment
  648. */
  649. var getTriangles = function (face, v) {
  650. //Work for each element of the array
  651. if (v + 1 < face.length) {
  652. //Add on the triangle variable the indexes to obtain triangles
  653. triangles.push(face[0], face[v], face[v + 1]);
  654. //Incrementation for recursion
  655. v += 1;
  656. //Recursion
  657. getTriangles(face, v);
  658. }
  659. //Result obtained after 2 iterations:
  660. //Pattern1 => triangle = ["1","2","3","1","3","4"];
  661. //Pattern2 => triangle = ["1/1","2/2","3/3","1/1","3/3","4/4"];
  662. //Pattern3 => triangle = ["1/1/1","2/2/2","3/3/3","1/1/1","3/3/3","4/4/4"];
  663. //Pattern4 => triangle = ["1//1","2//2","3//3","1//1","3//3","4//4"];
  664. //Pattern5 => triangle = ["-1/-1/-1","-2/-2/-2","-3/-3/-3","-1/-1/-1","-3/-3/-3","-4/-4/-4"];
  665. };
  666. /**
  667. * Create triangles and push the data for each polygon for the pattern 1
  668. * In this pattern we get vertice positions
  669. * @param face
  670. * @param v
  671. */
  672. var setDataForCurrentFaceWithPattern1 = function (face, v) {
  673. //Get the indices of triangles for each polygon
  674. getTriangles(face, v);
  675. //For each element in the triangles array.
  676. //This var could contains 1 to an infinity of triangles
  677. for (var k = 0; k < triangles.length; k++) {
  678. // Set position indice
  679. var indicePositionFromObj = parseInt(triangles[k]) - 1;
  680. setData(indicePositionFromObj, 0, 0, //In the pattern 1, normals and uvs are not defined
  681. positions[indicePositionFromObj], //Get the vectors data
  682. babylonjs_1.Vector2.Zero(), babylonjs_1.Vector3.Up() //Create default vectors
  683. );
  684. }
  685. //Reset variable for the next line
  686. triangles = [];
  687. };
  688. /**
  689. * Create triangles and push the data for each polygon for the pattern 2
  690. * In this pattern we get vertice positions and uvsu
  691. * @param face
  692. * @param v
  693. */
  694. var setDataForCurrentFaceWithPattern2 = function (face, v) {
  695. //Get the indices of triangles for each polygon
  696. getTriangles(face, v);
  697. for (var k = 0; k < triangles.length; k++) {
  698. //triangle[k] = "1/1"
  699. //Split the data for getting position and uv
  700. var point = triangles[k].split("/"); // ["1", "1"]
  701. //Set position indice
  702. var indicePositionFromObj = parseInt(point[0]) - 1;
  703. //Set uv indice
  704. var indiceUvsFromObj = parseInt(point[1]) - 1;
  705. setData(indicePositionFromObj, indiceUvsFromObj, 0, //Default value for normals
  706. positions[indicePositionFromObj], //Get the values for each element
  707. uvs[indiceUvsFromObj], babylonjs_1.Vector3.Up() //Default value for normals
  708. );
  709. }
  710. //Reset variable for the next line
  711. triangles = [];
  712. };
  713. /**
  714. * Create triangles and push the data for each polygon for the pattern 3
  715. * In this pattern we get vertice positions, uvs and normals
  716. * @param face
  717. * @param v
  718. */
  719. var setDataForCurrentFaceWithPattern3 = function (face, v) {
  720. //Get the indices of triangles for each polygon
  721. getTriangles(face, v);
  722. for (var k = 0; k < triangles.length; k++) {
  723. //triangle[k] = "1/1/1"
  724. //Split the data for getting position, uv, and normals
  725. var point = triangles[k].split("/"); // ["1", "1", "1"]
  726. // Set position indice
  727. var indicePositionFromObj = parseInt(point[0]) - 1;
  728. // Set uv indice
  729. var indiceUvsFromObj = parseInt(point[1]) - 1;
  730. // Set normal indice
  731. var indiceNormalFromObj = parseInt(point[2]) - 1;
  732. setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positions[indicePositionFromObj], uvs[indiceUvsFromObj], normals[indiceNormalFromObj] //Set the vector for each component
  733. );
  734. }
  735. //Reset variable for the next line
  736. triangles = [];
  737. };
  738. /**
  739. * Create triangles and push the data for each polygon for the pattern 4
  740. * In this pattern we get vertice positions and normals
  741. * @param face
  742. * @param v
  743. */
  744. var setDataForCurrentFaceWithPattern4 = function (face, v) {
  745. getTriangles(face, v);
  746. for (var k = 0; k < triangles.length; k++) {
  747. //triangle[k] = "1//1"
  748. //Split the data for getting position and normals
  749. var point = triangles[k].split("//"); // ["1", "1"]
  750. // We check indices, and normals
  751. var indicePositionFromObj = parseInt(point[0]) - 1;
  752. var indiceNormalFromObj = parseInt(point[1]) - 1;
  753. setData(indicePositionFromObj, 1, //Default value for uv
  754. indiceNormalFromObj, positions[indicePositionFromObj], //Get each vector of data
  755. babylonjs_1.Vector2.Zero(), normals[indiceNormalFromObj]);
  756. }
  757. //Reset variable for the next line
  758. triangles = [];
  759. };
  760. /**
  761. * Create triangles and push the data for each polygon for the pattern 3
  762. * In this pattern we get vertice positions, uvs and normals
  763. * @param face
  764. * @param v
  765. */
  766. var setDataForCurrentFaceWithPattern5 = function (face, v) {
  767. //Get the indices of triangles for each polygon
  768. getTriangles(face, v);
  769. for (var k = 0; k < triangles.length; k++) {
  770. //triangle[k] = "-1/-1/-1"
  771. //Split the data for getting position, uv, and normals
  772. var point = triangles[k].split("/"); // ["-1", "-1", "-1"]
  773. // Set position indice
  774. var indicePositionFromObj = positions.length + parseInt(point[0]);
  775. // Set uv indice
  776. var indiceUvsFromObj = uvs.length + parseInt(point[1]);
  777. // Set normal indice
  778. var indiceNormalFromObj = normals.length + parseInt(point[2]);
  779. setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positions[indicePositionFromObj], uvs[indiceUvsFromObj], normals[indiceNormalFromObj] //Set the vector for each component
  780. );
  781. }
  782. //Reset variable for the next line
  783. triangles = [];
  784. };
  785. var addPreviousObjMesh = function () {
  786. //Check if it is not the first mesh. Otherwise we don't have data.
  787. if (meshesFromObj.length > 0) {
  788. //Get the previous mesh for applying the data about the faces
  789. //=> in obj file, faces definition append after the name of the mesh
  790. handledMesh = meshesFromObj[meshesFromObj.length - 1];
  791. //Set the data into Array for the mesh
  792. unwrapData();
  793. // Reverse tab. Otherwise face are displayed in the wrong sens
  794. indicesForBabylon.reverse();
  795. //Set the information for the mesh
  796. //Slice the array to avoid rewriting because of the fact this is the same var which be rewrited
  797. handledMesh.indices = indicesForBabylon.slice();
  798. handledMesh.positions = unwrappedPositionsForBabylon.slice();
  799. handledMesh.normals = unwrappedNormalsForBabylon.slice();
  800. handledMesh.uvs = unwrappedUVForBabylon.slice();
  801. //Reset the array for the next mesh
  802. indicesForBabylon = [];
  803. unwrappedPositionsForBabylon = [];
  804. unwrappedNormalsForBabylon = [];
  805. unwrappedUVForBabylon = [];
  806. }
  807. };
  808. //Main function
  809. //Split the file into lines
  810. var lines = data.split('\n');
  811. //Look at each line
  812. for (var i = 0; i < lines.length; i++) {
  813. var line = lines[i].trim();
  814. var result;
  815. //Comment or newLine
  816. if (line.length === 0 || line.charAt(0) === '#') {
  817. continue;
  818. //Get information about one position possible for the vertices
  819. }
  820. else if ((result = this.vertexPattern.exec(line)) !== null) {
  821. //Create a Vector3 with the position x, y, z
  822. //Value of result:
  823. // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  824. //Add the Vector in the list of positions
  825. positions.push(new babylonjs_1.Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
  826. }
  827. else if ((result = this.normalPattern.exec(line)) !== null) {
  828. //Create a Vector3 with the normals x, y, z
  829. //Value of result
  830. // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  831. //Add the Vector in the list of normals
  832. normals.push(new babylonjs_1.Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
  833. }
  834. else if ((result = this.uvPattern.exec(line)) !== null) {
  835. //Create a Vector2 with the normals u, v
  836. //Value of result
  837. // ["vt 0.1 0.2 0.3", "0.1", "0.2"]
  838. //Add the Vector in the list of uvs
  839. uvs.push(new babylonjs_1.Vector2(parseFloat(result[1]), parseFloat(result[2])));
  840. //Identify patterns of faces
  841. //Face could be defined in different type of pattern
  842. }
  843. else if ((result = this.facePattern3.exec(line)) !== null) {
  844. //Value of result:
  845. //["f 1/1/1 2/2/2 3/3/3", "1/1/1 2/2/2 3/3/3"...]
  846. //Set the data for this face
  847. setDataForCurrentFaceWithPattern3(result[1].trim().split(" "), // ["1/1/1", "2/2/2", "3/3/3"]
  848. 1);
  849. }
  850. else if ((result = this.facePattern4.exec(line)) !== null) {
  851. //Value of result:
  852. //["f 1//1 2//2 3//3", "1//1 2//2 3//3"...]
  853. //Set the data for this face
  854. setDataForCurrentFaceWithPattern4(result[1].trim().split(" "), // ["1//1", "2//2", "3//3"]
  855. 1);
  856. }
  857. else if ((result = this.facePattern5.exec(line)) !== null) {
  858. //Value of result:
  859. //["f -1/-1/-1 -2/-2/-2 -3/-3/-3", "-1/-1/-1 -2/-2/-2 -3/-3/-3"...]
  860. //Set the data for this face
  861. setDataForCurrentFaceWithPattern5(result[1].trim().split(" "), // ["-1/-1/-1", "-2/-2/-2", "-3/-3/-3"]
  862. 1);
  863. }
  864. else if ((result = this.facePattern2.exec(line)) !== null) {
  865. //Value of result:
  866. //["f 1/1 2/2 3/3", "1/1 2/2 3/3"...]
  867. //Set the data for this face
  868. setDataForCurrentFaceWithPattern2(result[1].trim().split(" "), // ["1/1", "2/2", "3/3"]
  869. 1);
  870. }
  871. else if ((result = this.facePattern1.exec(line)) !== null) {
  872. //Value of result
  873. //["f 1 2 3", "1 2 3"...]
  874. //Set the data for this face
  875. setDataForCurrentFaceWithPattern1(result[1].trim().split(" "), // ["1", "2", "3"]
  876. 1);
  877. //Define a mesh or an object
  878. //Each time this keyword is analysed, create a new Object with all data for creating a babylonMesh
  879. }
  880. else if (this.group.test(line) || this.obj.test(line)) {
  881. //Create a new mesh corresponding to the name of the group.
  882. //Definition of the mesh
  883. var objMesh =
  884. //Set the name of the current obj mesh
  885. {
  886. name: line.substring(2).trim(),
  887. indices: undefined,
  888. positions: undefined,
  889. normals: undefined,
  890. uvs: undefined,
  891. materialName: ""
  892. };
  893. addPreviousObjMesh();
  894. //Push the last mesh created with only the name
  895. meshesFromObj.push(objMesh);
  896. //Set this variable to indicate that now meshesFromObj has objects defined inside
  897. hasMeshes = true;
  898. isFirstMaterial = true;
  899. increment = 1;
  900. //Keyword for applying a material
  901. }
  902. else if (this.usemtl.test(line)) {
  903. //Get the name of the material
  904. materialNameFromObj = line.substring(7).trim();
  905. //If this new material is in the same mesh
  906. if (!isFirstMaterial) {
  907. //Set the data for the previous mesh
  908. addPreviousObjMesh();
  909. //Create a new mesh
  910. var objMesh =
  911. //Set the name of the current obj mesh
  912. {
  913. name: objMeshName + "_mm" + increment.toString(),
  914. indices: undefined,
  915. positions: undefined,
  916. normals: undefined,
  917. uvs: undefined,
  918. materialName: materialNameFromObj
  919. };
  920. increment++;
  921. //If meshes are already defined
  922. meshesFromObj.push(objMesh);
  923. }
  924. //Set the material name if the previous line define a mesh
  925. if (hasMeshes && isFirstMaterial) {
  926. //Set the material name to the previous mesh (1 material per mesh)
  927. meshesFromObj[meshesFromObj.length - 1].materialName = materialNameFromObj;
  928. isFirstMaterial = false;
  929. }
  930. //Keyword for loading the mtl file
  931. }
  932. else if (this.mtllib.test(line)) {
  933. //Get the name of mtl file
  934. fileToLoad = line.substring(7).trim();
  935. //Apply smoothing
  936. }
  937. else if (this.smooth.test(line)) {
  938. // smooth shading => apply smoothing
  939. //Toda y I don't know it work with babylon and with obj.
  940. //With the obj file an integer is set
  941. }
  942. else {
  943. //If there is another possibility
  944. console.log("Unhandled expression at line : " + line);
  945. }
  946. }
  947. //At the end of the file, add the last mesh into the meshesFromObj array
  948. if (hasMeshes) {
  949. //Set the data for the last mesh
  950. handledMesh = meshesFromObj[meshesFromObj.length - 1];
  951. //Reverse indices for displaying faces in the good sens
  952. indicesForBabylon.reverse();
  953. //Get the good array
  954. unwrapData();
  955. //Set array
  956. handledMesh.indices = indicesForBabylon;
  957. handledMesh.positions = unwrappedPositionsForBabylon;
  958. handledMesh.normals = unwrappedNormalsForBabylon;
  959. handledMesh.uvs = unwrappedUVForBabylon;
  960. }
  961. //If any o or g keyword found, create a mesj with a random id
  962. if (!hasMeshes) {
  963. // reverse tab of indices
  964. indicesForBabylon.reverse();
  965. //Get positions normals uvs
  966. unwrapData();
  967. //Set data for one mesh
  968. meshesFromObj.push({
  969. name: babylonjs_1.Geometry.RandomId(),
  970. indices: indicesForBabylon,
  971. positions: unwrappedPositionsForBabylon,
  972. normals: unwrappedNormalsForBabylon,
  973. uvs: unwrappedUVForBabylon,
  974. materialName: materialNameFromObj
  975. });
  976. }
  977. //Create a Mesh list
  978. var babylonMeshesArray = []; //The mesh for babylon
  979. var materialToUse = new Array();
  980. //Set data for each mesh
  981. for (var j = 0; j < meshesFromObj.length; j++) {
  982. //check meshesNames (stlFileLoader)
  983. if (meshesNames && meshesFromObj[j].name) {
  984. if (meshesNames instanceof Array) {
  985. if (meshesNames.indexOf(meshesFromObj[j].name) == -1) {
  986. continue;
  987. }
  988. }
  989. else {
  990. if (meshesFromObj[j].name !== meshesNames) {
  991. continue;
  992. }
  993. }
  994. }
  995. //Get the current mesh
  996. //Set the data with VertexBuffer for each mesh
  997. handledMesh = meshesFromObj[j];
  998. //Create a Mesh with the name of the obj mesh
  999. var babylonMesh = new babylonjs_1.Mesh(meshesFromObj[j].name, scene);
  1000. //Push the name of the material to an array
  1001. //This is indispensable for the importMesh function
  1002. materialToUse.push(meshesFromObj[j].materialName);
  1003. var vertexData = new babylonjs_1.VertexData(); //The container for the values
  1004. //Set the data for the babylonMesh
  1005. vertexData.positions = handledMesh.positions;
  1006. vertexData.normals = handledMesh.normals;
  1007. vertexData.uvs = handledMesh.uvs;
  1008. vertexData.indices = handledMesh.indices;
  1009. //Set the data from the VertexBuffer to the current Mesh
  1010. vertexData.applyToMesh(babylonMesh);
  1011. if (OBJFileLoader.INVERT_Y) {
  1012. babylonMesh.scaling.y *= -1;
  1013. }
  1014. //Push the mesh into an array
  1015. babylonMeshesArray.push(babylonMesh);
  1016. }
  1017. var mtlPromises = [];
  1018. //load the materials
  1019. //Check if we have a file to load
  1020. if (fileToLoad !== "") {
  1021. //Load the file synchronously
  1022. mtlPromises.push(new Promise(function (resolve, reject) {
  1023. _this._loadMTL(fileToLoad, rootUrl, function (dataLoaded) {
  1024. try {
  1025. //Create materials thanks MTLLoader function
  1026. materialsFromMTLFile.parseMTL(scene, dataLoaded, rootUrl);
  1027. //Look at each material loaded in the mtl file
  1028. for (var n = 0; n < materialsFromMTLFile.materials.length; n++) {
  1029. //Three variables to get all meshes with the same material
  1030. var startIndex = 0;
  1031. var _indices = [];
  1032. var _index;
  1033. //The material from MTL file is used in the meshes loaded
  1034. //Push the indice in an array
  1035. //Check if the material is not used for another mesh
  1036. while ((_index = materialToUse.indexOf(materialsFromMTLFile.materials[n].name, startIndex)) > -1) {
  1037. _indices.push(_index);
  1038. startIndex = _index + 1;
  1039. }
  1040. //If the material is not used dispose it
  1041. if (_index == -1 && _indices.length == 0) {
  1042. //If the material is not needed, remove it
  1043. materialsFromMTLFile.materials[n].dispose();
  1044. }
  1045. else {
  1046. for (var o = 0; o < _indices.length; o++) {
  1047. //Apply the material to the Mesh for each mesh with the material
  1048. babylonMeshesArray[_indices[o]].material = materialsFromMTLFile.materials[n];
  1049. }
  1050. }
  1051. }
  1052. resolve();
  1053. }
  1054. catch (e) {
  1055. reject(e);
  1056. }
  1057. });
  1058. }));
  1059. }
  1060. //Return an array with all Mesh
  1061. return Promise.all(mtlPromises).then(function () {
  1062. return babylonMeshesArray;
  1063. });
  1064. };
  1065. /**
  1066. * Defines if UVs are optimized by default during load.
  1067. */
  1068. OBJFileLoader.OPTIMIZE_WITH_UV = false;
  1069. /**
  1070. * Defines if Y is inverted by default during load.
  1071. */
  1072. OBJFileLoader.INVERT_Y = false;
  1073. return OBJFileLoader;
  1074. }());
  1075. exports.OBJFileLoader = OBJFileLoader;
  1076. if (babylonjs_1.SceneLoader) {
  1077. //Add this loader into the register plugin
  1078. babylonjs_1.SceneLoader.RegisterPlugin(new OBJFileLoader());
  1079. }
  1080. /***/ }),
  1081. /***/ "babylonjs":
  1082. /*!****************************************************************************************************!*\
  1083. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  1084. \****************************************************************************************************/
  1085. /*! no static exports found */
  1086. /***/ (function(module, exports) {
  1087. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  1088. /***/ })
  1089. /******/ });
  1090. });
  1091. //# sourceMappingURL=babylon.objFileLoader.js.map