babylon.stlFileLoader.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. })((typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : this), function(__WEBPACK_EXTERNAL_MODULE_babylonjs_Misc_observable__) {
  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-stlFileLoader.ts");
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ({
  98. /***/ "../../node_modules/webpack/buildin/global.js":
  99. /*!***********************************!*\
  100. !*** (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 || new Function("return 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. /***/ "./STL/index.ts":
  122. /*!**********************!*\
  123. !*** ./STL/index.ts ***!
  124. \**********************/
  125. /*! exports provided: STLFileLoader */
  126. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  127. "use strict";
  128. __webpack_require__.r(__webpack_exports__);
  129. /* harmony import */ var _stlFileLoader__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./stlFileLoader */ "./STL/stlFileLoader.ts");
  130. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "STLFileLoader", function() { return _stlFileLoader__WEBPACK_IMPORTED_MODULE_0__["STLFileLoader"]; });
  131. /***/ }),
  132. /***/ "./STL/stlFileLoader.ts":
  133. /*!******************************!*\
  134. !*** ./STL/stlFileLoader.ts ***!
  135. \******************************/
  136. /*! exports provided: STLFileLoader */
  137. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  138. "use strict";
  139. __webpack_require__.r(__webpack_exports__);
  140. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "STLFileLoader", function() { return STLFileLoader; });
  141. /* harmony import */ var babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! babylonjs/Misc/tools */ "babylonjs/Misc/observable");
  142. /* harmony import */ var babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__);
  143. /**
  144. * STL file type loader.
  145. * This is a babylon scene loader plugin.
  146. */
  147. var STLFileLoader = /** @class */ (function () {
  148. function STLFileLoader() {
  149. /** @hidden */
  150. this.solidPattern = /solid (\S*)([\S\s]*)endsolid[ ]*(\S*)/g;
  151. /** @hidden */
  152. this.facetsPattern = /facet([\s\S]*?)endfacet/g;
  153. /** @hidden */
  154. this.normalPattern = /normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
  155. /** @hidden */
  156. this.vertexPattern = /vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
  157. /**
  158. * Defines the name of the plugin.
  159. */
  160. this.name = "stl";
  161. /**
  162. * Defines the extensions the stl loader is able to load.
  163. * force data to come in as an ArrayBuffer
  164. * we'll convert to string if it looks like it's an ASCII .stl
  165. */
  166. this.extensions = {
  167. ".stl": { isBinary: true },
  168. };
  169. }
  170. /**
  171. * Import meshes into a scene.
  172. * @param meshesNames An array of mesh names, a single mesh name, or empty string for all meshes that filter what meshes are imported
  173. * @param scene The scene to import into
  174. * @param data The data to import
  175. * @param rootUrl The root url for scene and resources
  176. * @param meshes The meshes array to import into
  177. * @param particleSystems The particle systems array to import into
  178. * @param skeletons The skeletons array to import into
  179. * @param onError The callback when import fails
  180. * @returns True if successful or false otherwise
  181. */
  182. STLFileLoader.prototype.importMesh = function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  183. var matches;
  184. if (typeof data !== "string") {
  185. if (this._isBinary(data)) {
  186. // binary .stl
  187. var babylonMesh = new babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["Mesh"]("stlmesh", scene);
  188. this._parseBinary(babylonMesh, data);
  189. if (meshes) {
  190. meshes.push(babylonMesh);
  191. }
  192. return true;
  193. }
  194. // ASCII .stl
  195. // convert to string
  196. var array_buffer = new Uint8Array(data);
  197. var str = '';
  198. for (var i = 0; i < data.byteLength; i++) {
  199. str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
  200. }
  201. data = str;
  202. }
  203. //if arrived here, data is a string, containing the STLA data.
  204. while (matches = this.solidPattern.exec(data)) {
  205. var meshName = matches[1];
  206. var meshNameFromEnd = matches[3];
  207. if (meshName != meshNameFromEnd) {
  208. babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["Tools"].Error("Error in STL, solid name != endsolid name");
  209. return false;
  210. }
  211. // check meshesNames
  212. if (meshesNames && meshName) {
  213. if (meshesNames instanceof Array) {
  214. if (!meshesNames.indexOf(meshName)) {
  215. continue;
  216. }
  217. }
  218. else {
  219. if (meshName !== meshesNames) {
  220. continue;
  221. }
  222. }
  223. }
  224. // stl mesh name can be empty as well
  225. meshName = meshName || "stlmesh";
  226. var babylonMesh = new babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["Mesh"](meshName, scene);
  227. this._parseASCII(babylonMesh, matches[2]);
  228. if (meshes) {
  229. meshes.push(babylonMesh);
  230. }
  231. }
  232. return true;
  233. };
  234. /**
  235. * Load into a scene.
  236. * @param scene The scene to load into
  237. * @param data The data to import
  238. * @param rootUrl The root url for scene and resources
  239. * @param onError The callback when import fails
  240. * @returns true if successful or false otherwise
  241. */
  242. STLFileLoader.prototype.load = function (scene, data, rootUrl) {
  243. var result = this.importMesh(null, scene, data, rootUrl, null, null, null);
  244. if (result) {
  245. scene.createDefaultLight();
  246. scene.createDefaultCameraOrLight();
  247. }
  248. return result;
  249. };
  250. /**
  251. * Load into an asset container.
  252. * @param scene The scene to load into
  253. * @param data The data to import
  254. * @param rootUrl The root url for scene and resources
  255. * @param onError The callback when import fails
  256. * @returns The loaded asset container
  257. */
  258. STLFileLoader.prototype.loadAssetContainer = function (scene, data, rootUrl, onError) {
  259. var container = new babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["AssetContainer"](scene);
  260. this.importMesh(null, scene, data, rootUrl, container.meshes, null, null);
  261. container.removeAllFromScene();
  262. return container;
  263. };
  264. STLFileLoader.prototype._isBinary = function (data) {
  265. // check if file size is correct for binary stl
  266. var faceSize, nFaces, reader;
  267. reader = new DataView(data);
  268. faceSize = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
  269. nFaces = reader.getUint32(80, true);
  270. if (80 + (32 / 8) + (nFaces * faceSize) === reader.byteLength) {
  271. return true;
  272. }
  273. // check characters higher than ASCII to confirm binary
  274. var fileLength = reader.byteLength;
  275. for (var index = 0; index < fileLength; index++) {
  276. if (reader.getUint8(index) > 127) {
  277. return true;
  278. }
  279. }
  280. return false;
  281. };
  282. STLFileLoader.prototype._parseBinary = function (mesh, data) {
  283. var reader = new DataView(data);
  284. var faces = reader.getUint32(80, true);
  285. var dataOffset = 84;
  286. var faceLength = 12 * 4 + 2;
  287. var offset = 0;
  288. var positions = new Float32Array(faces * 3 * 3);
  289. var normals = new Float32Array(faces * 3 * 3);
  290. var indices = new Uint32Array(faces * 3);
  291. var indicesCount = 0;
  292. for (var face = 0; face < faces; face++) {
  293. var start = dataOffset + face * faceLength;
  294. var normalX = reader.getFloat32(start, true);
  295. var normalY = reader.getFloat32(start + 4, true);
  296. var normalZ = reader.getFloat32(start + 8, true);
  297. for (var i = 1; i <= 3; i++) {
  298. var vertexstart = start + i * 12;
  299. // ordering is intentional to match ascii import
  300. positions[offset] = reader.getFloat32(vertexstart, true);
  301. positions[offset + 2] = reader.getFloat32(vertexstart + 4, true);
  302. positions[offset + 1] = reader.getFloat32(vertexstart + 8, true);
  303. normals[offset] = normalX;
  304. normals[offset + 2] = normalY;
  305. normals[offset + 1] = normalZ;
  306. offset += 3;
  307. }
  308. indices[indicesCount] = indicesCount++;
  309. indices[indicesCount] = indicesCount++;
  310. indices[indicesCount] = indicesCount++;
  311. }
  312. mesh.setVerticesData(babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind, positions);
  313. mesh.setVerticesData(babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind, normals);
  314. mesh.setIndices(indices);
  315. mesh.computeWorldMatrix(true);
  316. };
  317. STLFileLoader.prototype._parseASCII = function (mesh, solidData) {
  318. var positions = [];
  319. var normals = [];
  320. var indices = [];
  321. var indicesCount = 0;
  322. //load facets, ignoring loop as the standard doesn't define it can contain more than vertices
  323. var matches;
  324. while (matches = this.facetsPattern.exec(solidData)) {
  325. var facet = matches[1];
  326. //one normal per face
  327. var normalMatches = this.normalPattern.exec(facet);
  328. this.normalPattern.lastIndex = 0;
  329. if (!normalMatches) {
  330. continue;
  331. }
  332. var normal = [Number(normalMatches[1]), Number(normalMatches[5]), Number(normalMatches[3])];
  333. var vertexMatch;
  334. while (vertexMatch = this.vertexPattern.exec(facet)) {
  335. positions.push(Number(vertexMatch[1]), Number(vertexMatch[5]), Number(vertexMatch[3]));
  336. normals.push(normal[0], normal[1], normal[2]);
  337. }
  338. indices.push(indicesCount++, indicesCount++, indicesCount++);
  339. this.vertexPattern.lastIndex = 0;
  340. }
  341. this.facetsPattern.lastIndex = 0;
  342. mesh.setVerticesData(babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].PositionKind, positions);
  343. mesh.setVerticesData(babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["VertexBuffer"].NormalKind, normals);
  344. mesh.setIndices(indices);
  345. mesh.computeWorldMatrix(true);
  346. };
  347. return STLFileLoader;
  348. }());
  349. if (babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["SceneLoader"]) {
  350. babylonjs_Misc_tools__WEBPACK_IMPORTED_MODULE_0__["SceneLoader"].RegisterPlugin(new STLFileLoader());
  351. }
  352. /***/ }),
  353. /***/ "./legacy/legacy-stlFileLoader.ts":
  354. /*!****************************************!*\
  355. !*** ./legacy/legacy-stlFileLoader.ts ***!
  356. \****************************************/
  357. /*! exports provided: STLFileLoader */
  358. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  359. "use strict";
  360. __webpack_require__.r(__webpack_exports__);
  361. /* WEBPACK VAR INJECTION */(function(global) {/* harmony import */ var _STL__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../STL */ "./STL/index.ts");
  362. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "STLFileLoader", function() { return _STL__WEBPACK_IMPORTED_MODULE_0__["STLFileLoader"]; });
  363. /**
  364. * This is the entry point for the UMD module.
  365. * The entry point for a future ESM package should be index.ts
  366. */
  367. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  368. if (typeof globalObject !== "undefined") {
  369. for (var key in _STL__WEBPACK_IMPORTED_MODULE_0__) {
  370. globalObject.BABYLON[key] = _STL__WEBPACK_IMPORTED_MODULE_0__[key];
  371. }
  372. }
  373. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../../node_modules/webpack/buildin/global.js */ "../../node_modules/webpack/buildin/global.js")))
  374. /***/ }),
  375. /***/ "babylonjs/Misc/observable":
  376. /*!****************************************************************************************************!*\
  377. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  378. \****************************************************************************************************/
  379. /*! no static exports found */
  380. /***/ (function(module, exports) {
  381. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs_Misc_observable__;
  382. /***/ })
  383. /******/ });
  384. });
  385. //# sourceMappingURL=babylon.stlFileLoader.js.map