babylon.asciiArtpostProcess.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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-postProcessesLibrary", ["babylonjs"], factory);
  6. else if(typeof exports === 'object')
  7. exports["babylonjs-postProcessesLibrary"] = factory(require("babylonjs"));
  8. else
  9. root["PPLIB"] = 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-asciiArt.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-asciiArt.ts":
  122. /*!***********************************!*\
  123. !*** ./legacy/legacy-asciiArt.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 postProcessLibrary = __webpack_require__(/*! ../src/asciiArt/index */ "./src/asciiArt/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 postProcessLibrary) {
  141. globalObject.BABYLON[key] = postProcessLibrary[key];
  142. }
  143. }
  144. __export(__webpack_require__(/*! ../src/asciiArt/index */ "./src/asciiArt/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/asciiArt/asciiArtPostProcess.ts":
  148. /*!*********************************************!*\
  149. !*** ./src/asciiArt/asciiArtPostProcess.ts ***!
  150. \*********************************************/
  151. /*! no static exports found */
  152. /***/ (function(module, exports, __webpack_require__) {
  153. "use strict";
  154. var __extends = (this && this.__extends) || (function () {
  155. var extendStatics = function (d, b) {
  156. extendStatics = Object.setPrototypeOf ||
  157. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  158. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  159. return extendStatics(d, b);
  160. }
  161. return function (d, b) {
  162. extendStatics(d, b);
  163. function __() { this.constructor = d; }
  164. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  165. };
  166. })();
  167. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  168. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  169. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  170. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  171. return c > 3 && r && Object.defineProperty(target, key, r), r;
  172. };
  173. Object.defineProperty(exports, "__esModule", { value: true });
  174. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  175. babylonjs_1.Effect.ShadersStore["asciiartPixelShader"] = __webpack_require__(/*! ./asciiart.fragment.fx */ "./src/asciiArt/asciiart.fragment.fx");
  176. /**
  177. * AsciiArtFontTexture is the helper class used to easily create your ascii art font texture.
  178. *
  179. * It basically takes care rendering the font front the given font size to a texture.
  180. * This is used later on in the postprocess.
  181. */
  182. var AsciiArtFontTexture = /** @class */ (function (_super) {
  183. __extends(AsciiArtFontTexture, _super);
  184. /**
  185. * Create a new instance of the Ascii Art FontTexture class
  186. * @param name the name of the texture
  187. * @param font the font to use, use the W3C CSS notation
  188. * @param text the caracter set to use in the rendering.
  189. * @param scene the scene that owns the texture
  190. */
  191. function AsciiArtFontTexture(name, font, text, scene) {
  192. if (scene === void 0) { scene = null; }
  193. var _this = _super.call(this, scene) || this;
  194. scene = _this.getScene();
  195. if (!scene) {
  196. return _this;
  197. }
  198. _this.name = name;
  199. _this._text == text;
  200. _this._font == font;
  201. _this.wrapU = babylonjs_1.Texture.CLAMP_ADDRESSMODE;
  202. _this.wrapV = babylonjs_1.Texture.CLAMP_ADDRESSMODE;
  203. //this.anisotropicFilteringLevel = 1;
  204. // Get the font specific info.
  205. var maxCharHeight = _this.getFontHeight(font);
  206. var maxCharWidth = _this.getFontWidth(font);
  207. _this._charSize = Math.max(maxCharHeight.height, maxCharWidth);
  208. // This is an approximate size, but should always be able to fit at least the maxCharCount.
  209. var textureWidth = Math.ceil(_this._charSize * text.length);
  210. var textureHeight = _this._charSize;
  211. // Create the texture that will store the font characters.
  212. _this._texture = scene.getEngine().createDynamicTexture(textureWidth, textureHeight, false, babylonjs_1.Texture.NEAREST_SAMPLINGMODE);
  213. //scene.getEngine().setclamp
  214. var textureSize = _this.getSize();
  215. // Create a canvas with the final size: the one matching the texture.
  216. var canvas = document.createElement("canvas");
  217. canvas.width = textureSize.width;
  218. canvas.height = textureSize.height;
  219. var context = canvas.getContext("2d");
  220. context.textBaseline = "top";
  221. context.font = font;
  222. context.fillStyle = "white";
  223. context.imageSmoothingEnabled = false;
  224. // Sets the text in the texture.
  225. for (var i = 0; i < text.length; i++) {
  226. context.fillText(text[i], i * _this._charSize, -maxCharHeight.offset);
  227. }
  228. // Flush the text in the dynamic texture.
  229. scene.getEngine().updateDynamicTexture(_this._texture, canvas, false, true);
  230. return _this;
  231. }
  232. Object.defineProperty(AsciiArtFontTexture.prototype, "charSize", {
  233. /**
  234. * Gets the size of one char in the texture (each char fits in size * size space in the texture).
  235. */
  236. get: function () {
  237. return this._charSize;
  238. },
  239. enumerable: true,
  240. configurable: true
  241. });
  242. /**
  243. * Gets the max char width of a font.
  244. * @param font the font to use, use the W3C CSS notation
  245. * @return the max char width
  246. */
  247. AsciiArtFontTexture.prototype.getFontWidth = function (font) {
  248. var fontDraw = document.createElement("canvas");
  249. var ctx = fontDraw.getContext('2d');
  250. ctx.fillStyle = 'white';
  251. ctx.font = font;
  252. return ctx.measureText("W").width;
  253. };
  254. // More info here: https://videlais.com/2014/03/16/the-many-and-varied-problems-with-measuring-font-height-for-html5-canvas/
  255. /**
  256. * Gets the max char height of a font.
  257. * @param font the font to use, use the W3C CSS notation
  258. * @return the max char height
  259. */
  260. AsciiArtFontTexture.prototype.getFontHeight = function (font) {
  261. var fontDraw = document.createElement("canvas");
  262. var ctx = fontDraw.getContext('2d');
  263. ctx.fillRect(0, 0, fontDraw.width, fontDraw.height);
  264. ctx.textBaseline = 'top';
  265. ctx.fillStyle = 'white';
  266. ctx.font = font;
  267. ctx.fillText('jH|', 0, 0);
  268. var pixels = ctx.getImageData(0, 0, fontDraw.width, fontDraw.height).data;
  269. var start = -1;
  270. var end = -1;
  271. for (var row = 0; row < fontDraw.height; row++) {
  272. for (var column = 0; column < fontDraw.width; column++) {
  273. var index = (row * fontDraw.width + column) * 4;
  274. if (pixels[index] === 0) {
  275. if (column === fontDraw.width - 1 && start !== -1) {
  276. end = row;
  277. row = fontDraw.height;
  278. break;
  279. }
  280. continue;
  281. }
  282. else {
  283. if (start === -1) {
  284. start = row;
  285. }
  286. break;
  287. }
  288. }
  289. }
  290. return { height: (end - start) + 1, offset: start - 1 };
  291. };
  292. /**
  293. * Clones the current AsciiArtTexture.
  294. * @return the clone of the texture.
  295. */
  296. AsciiArtFontTexture.prototype.clone = function () {
  297. return new AsciiArtFontTexture(this.name, this._font, this._text, this.getScene());
  298. };
  299. /**
  300. * Parses a json object representing the texture and returns an instance of it.
  301. * @param source the source JSON representation
  302. * @param scene the scene to create the texture for
  303. * @return the parsed texture
  304. */
  305. AsciiArtFontTexture.Parse = function (source, scene) {
  306. var texture = babylonjs_1.SerializationHelper.Parse(function () { return new AsciiArtFontTexture(source.name, source.font, source.text, scene); }, source, scene, null);
  307. return texture;
  308. };
  309. __decorate([
  310. babylonjs_1.serialize("font")
  311. ], AsciiArtFontTexture.prototype, "_font", void 0);
  312. __decorate([
  313. babylonjs_1.serialize("text")
  314. ], AsciiArtFontTexture.prototype, "_text", void 0);
  315. return AsciiArtFontTexture;
  316. }(babylonjs_1.BaseTexture));
  317. exports.AsciiArtFontTexture = AsciiArtFontTexture;
  318. /**
  319. * AsciiArtPostProcess helps rendering everithing in Ascii Art.
  320. *
  321. * Simmply add it to your scene and let the nerd that lives in you have fun.
  322. * Example usage: var pp = new AsciiArtPostProcess("myAscii", "20px Monospace", camera);
  323. */
  324. var AsciiArtPostProcess = /** @class */ (function (_super) {
  325. __extends(AsciiArtPostProcess, _super);
  326. /**
  327. * Instantiates a new Ascii Art Post Process.
  328. * @param name the name to give to the postprocess
  329. * @camera the camera to apply the post process to.
  330. * @param options can either be the font name or an option object following the IAsciiArtPostProcessOptions format
  331. */
  332. function AsciiArtPostProcess(name, camera, options) {
  333. var _this = _super.call(this, name, 'asciiart', ['asciiArtFontInfos', 'asciiArtOptions'], ['asciiArtFont'], {
  334. width: camera.getEngine().getRenderWidth(),
  335. height: camera.getEngine().getRenderHeight()
  336. }, camera, babylonjs_1.Texture.TRILINEAR_SAMPLINGMODE, camera.getEngine(), true) || this;
  337. /**
  338. * This defines the amount you want to mix the "tile" or caracter space colored in the ascii art.
  339. * This number is defined between 0 and 1;
  340. */
  341. _this.mixToTile = 0;
  342. /**
  343. * This defines the amount you want to mix the normal rendering pass in the ascii art.
  344. * This number is defined between 0 and 1;
  345. */
  346. _this.mixToNormal = 0;
  347. // Default values.
  348. var font = "40px Monospace";
  349. var characterSet = " `-.'_:,\"=^;<+!*?/cL\\zrs7TivJtC{3F)Il(xZfY5S2eajo14[nuyE]P6V9kXpKwGhqAUbOd8#HRDB0$mgMW&Q%N@";
  350. // Use options.
  351. if (options) {
  352. if (typeof (options) === "string") {
  353. font = options;
  354. }
  355. else {
  356. font = options.font || font;
  357. characterSet = options.characterSet || characterSet;
  358. _this.mixToTile = options.mixToTile || _this.mixToTile;
  359. _this.mixToNormal = options.mixToNormal || _this.mixToNormal;
  360. }
  361. }
  362. _this._asciiArtFontTexture = new AsciiArtFontTexture(name, font, characterSet, camera.getScene());
  363. var textureSize = _this._asciiArtFontTexture.getSize();
  364. _this.onApply = function (effect) {
  365. effect.setTexture("asciiArtFont", _this._asciiArtFontTexture);
  366. effect.setFloat4("asciiArtFontInfos", _this._asciiArtFontTexture.charSize, characterSet.length, textureSize.width, textureSize.height);
  367. effect.setFloat4("asciiArtOptions", _this.width, _this.height, _this.mixToNormal, _this.mixToTile);
  368. };
  369. return _this;
  370. }
  371. return AsciiArtPostProcess;
  372. }(babylonjs_1.PostProcess));
  373. exports.AsciiArtPostProcess = AsciiArtPostProcess;
  374. /***/ }),
  375. /***/ "./src/asciiArt/asciiart.fragment.fx":
  376. /*!*******************************************!*\
  377. !*** ./src/asciiArt/asciiart.fragment.fx ***!
  378. \*******************************************/
  379. /*! no static exports found */
  380. /***/ (function(module, exports) {
  381. module.exports = "\nvarying vec2 vUV;\nuniform sampler2D textureSampler;\nuniform sampler2D asciiArtFont;\n\nuniform vec4 asciiArtFontInfos;\nuniform vec4 asciiArtOptions;\n\nfloat getLuminance(vec3 color)\n{\nreturn clamp(dot(color,vec3(0.2126,0.7152,0.0722)),0.,1.);\n}\n\nvoid main(void) \n{\nfloat caracterSize=asciiArtFontInfos.x;\nfloat numChar=asciiArtFontInfos.y-1.0;\nfloat fontx=asciiArtFontInfos.z;\nfloat fonty=asciiArtFontInfos.w;\nfloat screenx=asciiArtOptions.x;\nfloat screeny=asciiArtOptions.y;\nfloat tileX=float(floor((gl_FragCoord.x)/caracterSize))*caracterSize/screenx;\nfloat tileY=float(floor((gl_FragCoord.y)/caracterSize))*caracterSize/screeny;\nvec2 tileUV=vec2(tileX,tileY);\nvec4 tileColor=texture2D(textureSampler,tileUV);\nvec4 baseColor=texture2D(textureSampler,vUV);\nfloat tileLuminance=getLuminance(tileColor.rgb);\nfloat offsetx=(float(floor(tileLuminance*numChar)))*caracterSize/fontx;\nfloat offsety=0.0;\nfloat x=float(mod(gl_FragCoord.x,caracterSize))/fontx;\nfloat y=float(mod(gl_FragCoord.y,caracterSize))/fonty;\nvec4 finalColor=texture2D(asciiArtFont,vec2(offsetx+x,offsety+(caracterSize/fonty-y)));\nfinalColor.rgb*=tileColor.rgb;\nfinalColor.a=1.0;\nfinalColor=mix(finalColor,tileColor,asciiArtOptions.w);\nfinalColor=mix(finalColor,baseColor,asciiArtOptions.z);\ngl_FragColor=finalColor;\n}"
  382. /***/ }),
  383. /***/ "./src/asciiArt/index.ts":
  384. /*!*******************************!*\
  385. !*** ./src/asciiArt/index.ts ***!
  386. \*******************************/
  387. /*! no static exports found */
  388. /***/ (function(module, exports, __webpack_require__) {
  389. "use strict";
  390. function __export(m) {
  391. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  392. }
  393. Object.defineProperty(exports, "__esModule", { value: true });
  394. __export(__webpack_require__(/*! ./asciiArtPostProcess */ "./src/asciiArt/asciiArtPostProcess.ts"));
  395. /***/ }),
  396. /***/ "babylonjs":
  397. /*!****************************************************************************************************!*\
  398. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  399. \****************************************************************************************************/
  400. /*! no static exports found */
  401. /***/ (function(module, exports) {
  402. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  403. /***/ })
  404. /******/ });
  405. });
  406. //# sourceMappingURL=babylon.asciiArtPostProcess.js.map