babylonjs.postProcess.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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 = "./src/legacy.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. /***/ "./src/asciiArt/asciiArtPostProcess.ts":
  122. /*!*********************************************!*\
  123. !*** ./src/asciiArt/asciiArtPostProcess.ts ***!
  124. \*********************************************/
  125. /*! no static exports found */
  126. /***/ (function(module, exports, __webpack_require__) {
  127. "use strict";
  128. var __extends = (this && this.__extends) || (function () {
  129. var extendStatics = function (d, b) {
  130. extendStatics = Object.setPrototypeOf ||
  131. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  132. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  133. return extendStatics(d, b);
  134. }
  135. return function (d, b) {
  136. extendStatics(d, b);
  137. function __() { this.constructor = d; }
  138. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  139. };
  140. })();
  141. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  142. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  143. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  144. 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;
  145. return c > 3 && r && Object.defineProperty(target, key, r), r;
  146. };
  147. Object.defineProperty(exports, "__esModule", { value: true });
  148. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  149. babylonjs_1.Effect.ShadersStore["asciiartPixelShader"] = __webpack_require__(/*! ./asciiart.fragment.fx */ "./src/asciiArt/asciiart.fragment.fx");
  150. /**
  151. * AsciiArtFontTexture is the helper class used to easily create your ascii art font texture.
  152. *
  153. * It basically takes care rendering the font front the given font size to a texture.
  154. * This is used later on in the postprocess.
  155. */
  156. var AsciiArtFontTexture = /** @class */ (function (_super) {
  157. __extends(AsciiArtFontTexture, _super);
  158. /**
  159. * Create a new instance of the Ascii Art FontTexture class
  160. * @param name the name of the texture
  161. * @param font the font to use, use the W3C CSS notation
  162. * @param text the caracter set to use in the rendering.
  163. * @param scene the scene that owns the texture
  164. */
  165. function AsciiArtFontTexture(name, font, text, scene) {
  166. if (scene === void 0) { scene = null; }
  167. var _this = _super.call(this, scene) || this;
  168. scene = _this.getScene();
  169. if (!scene) {
  170. return _this;
  171. }
  172. _this.name = name;
  173. _this._text == text;
  174. _this._font == font;
  175. _this.wrapU = babylonjs_1.Texture.CLAMP_ADDRESSMODE;
  176. _this.wrapV = babylonjs_1.Texture.CLAMP_ADDRESSMODE;
  177. //this.anisotropicFilteringLevel = 1;
  178. // Get the font specific info.
  179. var maxCharHeight = _this.getFontHeight(font);
  180. var maxCharWidth = _this.getFontWidth(font);
  181. _this._charSize = Math.max(maxCharHeight.height, maxCharWidth);
  182. // This is an approximate size, but should always be able to fit at least the maxCharCount.
  183. var textureWidth = Math.ceil(_this._charSize * text.length);
  184. var textureHeight = _this._charSize;
  185. // Create the texture that will store the font characters.
  186. _this._texture = scene.getEngine().createDynamicTexture(textureWidth, textureHeight, false, babylonjs_1.Texture.NEAREST_SAMPLINGMODE);
  187. //scene.getEngine().setclamp
  188. var textureSize = _this.getSize();
  189. // Create a canvas with the final size: the one matching the texture.
  190. var canvas = document.createElement("canvas");
  191. canvas.width = textureSize.width;
  192. canvas.height = textureSize.height;
  193. var context = canvas.getContext("2d");
  194. context.textBaseline = "top";
  195. context.font = font;
  196. context.fillStyle = "white";
  197. context.imageSmoothingEnabled = false;
  198. // Sets the text in the texture.
  199. for (var i = 0; i < text.length; i++) {
  200. context.fillText(text[i], i * _this._charSize, -maxCharHeight.offset);
  201. }
  202. // Flush the text in the dynamic texture.
  203. scene.getEngine().updateDynamicTexture(_this._texture, canvas, false, true);
  204. return _this;
  205. }
  206. Object.defineProperty(AsciiArtFontTexture.prototype, "charSize", {
  207. /**
  208. * Gets the size of one char in the texture (each char fits in size * size space in the texture).
  209. */
  210. get: function () {
  211. return this._charSize;
  212. },
  213. enumerable: true,
  214. configurable: true
  215. });
  216. /**
  217. * Gets the max char width of a font.
  218. * @param font the font to use, use the W3C CSS notation
  219. * @return the max char width
  220. */
  221. AsciiArtFontTexture.prototype.getFontWidth = function (font) {
  222. var fontDraw = document.createElement("canvas");
  223. var ctx = fontDraw.getContext('2d');
  224. ctx.fillStyle = 'white';
  225. ctx.font = font;
  226. return ctx.measureText("W").width;
  227. };
  228. // More info here: https://videlais.com/2014/03/16/the-many-and-varied-problems-with-measuring-font-height-for-html5-canvas/
  229. /**
  230. * Gets the max char height of a font.
  231. * @param font the font to use, use the W3C CSS notation
  232. * @return the max char height
  233. */
  234. AsciiArtFontTexture.prototype.getFontHeight = function (font) {
  235. var fontDraw = document.createElement("canvas");
  236. var ctx = fontDraw.getContext('2d');
  237. ctx.fillRect(0, 0, fontDraw.width, fontDraw.height);
  238. ctx.textBaseline = 'top';
  239. ctx.fillStyle = 'white';
  240. ctx.font = font;
  241. ctx.fillText('jH|', 0, 0);
  242. var pixels = ctx.getImageData(0, 0, fontDraw.width, fontDraw.height).data;
  243. var start = -1;
  244. var end = -1;
  245. for (var row = 0; row < fontDraw.height; row++) {
  246. for (var column = 0; column < fontDraw.width; column++) {
  247. var index = (row * fontDraw.width + column) * 4;
  248. if (pixels[index] === 0) {
  249. if (column === fontDraw.width - 1 && start !== -1) {
  250. end = row;
  251. row = fontDraw.height;
  252. break;
  253. }
  254. continue;
  255. }
  256. else {
  257. if (start === -1) {
  258. start = row;
  259. }
  260. break;
  261. }
  262. }
  263. }
  264. return { height: (end - start) + 1, offset: start - 1 };
  265. };
  266. /**
  267. * Clones the current AsciiArtTexture.
  268. * @return the clone of the texture.
  269. */
  270. AsciiArtFontTexture.prototype.clone = function () {
  271. return new AsciiArtFontTexture(this.name, this._font, this._text, this.getScene());
  272. };
  273. /**
  274. * Parses a json object representing the texture and returns an instance of it.
  275. * @param source the source JSON representation
  276. * @param scene the scene to create the texture for
  277. * @return the parsed texture
  278. */
  279. AsciiArtFontTexture.Parse = function (source, scene) {
  280. var texture = babylonjs_1.SerializationHelper.Parse(function () { return new AsciiArtFontTexture(source.name, source.font, source.text, scene); }, source, scene, null);
  281. return texture;
  282. };
  283. __decorate([
  284. babylonjs_1.serialize("font")
  285. ], AsciiArtFontTexture.prototype, "_font", void 0);
  286. __decorate([
  287. babylonjs_1.serialize("text")
  288. ], AsciiArtFontTexture.prototype, "_text", void 0);
  289. return AsciiArtFontTexture;
  290. }(babylonjs_1.BaseTexture));
  291. exports.AsciiArtFontTexture = AsciiArtFontTexture;
  292. /**
  293. * AsciiArtPostProcess helps rendering everithing in Ascii Art.
  294. *
  295. * Simmply add it to your scene and let the nerd that lives in you have fun.
  296. * Example usage: var pp = new AsciiArtPostProcess("myAscii", "20px Monospace", camera);
  297. */
  298. var AsciiArtPostProcess = /** @class */ (function (_super) {
  299. __extends(AsciiArtPostProcess, _super);
  300. /**
  301. * Instantiates a new Ascii Art Post Process.
  302. * @param name the name to give to the postprocess
  303. * @camera the camera to apply the post process to.
  304. * @param options can either be the font name or an option object following the IAsciiArtPostProcessOptions format
  305. */
  306. function AsciiArtPostProcess(name, camera, options) {
  307. var _this = _super.call(this, name, 'asciiart', ['asciiArtFontInfos', 'asciiArtOptions'], ['asciiArtFont'], {
  308. width: camera.getEngine().getRenderWidth(),
  309. height: camera.getEngine().getRenderHeight()
  310. }, camera, babylonjs_1.Texture.TRILINEAR_SAMPLINGMODE, camera.getEngine(), true) || this;
  311. /**
  312. * This defines the amount you want to mix the "tile" or caracter space colored in the ascii art.
  313. * This number is defined between 0 and 1;
  314. */
  315. _this.mixToTile = 0;
  316. /**
  317. * This defines the amount you want to mix the normal rendering pass in the ascii art.
  318. * This number is defined between 0 and 1;
  319. */
  320. _this.mixToNormal = 0;
  321. // Default values.
  322. var font = "40px Monospace";
  323. var characterSet = " `-.'_:,\"=^;<+!*?/cL\\zrs7TivJtC{3F)Il(xZfY5S2eajo14[nuyE]P6V9kXpKwGhqAUbOd8#HRDB0$mgMW&Q%N@";
  324. // Use options.
  325. if (options) {
  326. if (typeof (options) === "string") {
  327. font = options;
  328. }
  329. else {
  330. font = options.font || font;
  331. characterSet = options.characterSet || characterSet;
  332. _this.mixToTile = options.mixToTile || _this.mixToTile;
  333. _this.mixToNormal = options.mixToNormal || _this.mixToNormal;
  334. }
  335. }
  336. _this._asciiArtFontTexture = new AsciiArtFontTexture(name, font, characterSet, camera.getScene());
  337. var textureSize = _this._asciiArtFontTexture.getSize();
  338. _this.onApply = function (effect) {
  339. effect.setTexture("asciiArtFont", _this._asciiArtFontTexture);
  340. effect.setFloat4("asciiArtFontInfos", _this._asciiArtFontTexture.charSize, characterSet.length, textureSize.width, textureSize.height);
  341. effect.setFloat4("asciiArtOptions", _this.width, _this.height, _this.mixToNormal, _this.mixToTile);
  342. };
  343. return _this;
  344. }
  345. return AsciiArtPostProcess;
  346. }(babylonjs_1.PostProcess));
  347. exports.AsciiArtPostProcess = AsciiArtPostProcess;
  348. /***/ }),
  349. /***/ "./src/asciiArt/asciiart.fragment.fx":
  350. /*!*******************************************!*\
  351. !*** ./src/asciiArt/asciiart.fragment.fx ***!
  352. \*******************************************/
  353. /*! no static exports found */
  354. /***/ (function(module, exports) {
  355. 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}"
  356. /***/ }),
  357. /***/ "./src/asciiArt/index.ts":
  358. /*!*******************************!*\
  359. !*** ./src/asciiArt/index.ts ***!
  360. \*******************************/
  361. /*! no static exports found */
  362. /***/ (function(module, exports, __webpack_require__) {
  363. "use strict";
  364. function __export(m) {
  365. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  366. }
  367. Object.defineProperty(exports, "__esModule", { value: true });
  368. __export(__webpack_require__(/*! ./asciiArtPostProcess */ "./src/asciiArt/asciiArtPostProcess.ts"));
  369. /***/ }),
  370. /***/ "./src/digitalRain/digitalRainPostProcess.ts":
  371. /*!***************************************************!*\
  372. !*** ./src/digitalRain/digitalRainPostProcess.ts ***!
  373. \***************************************************/
  374. /*! no static exports found */
  375. /***/ (function(module, exports, __webpack_require__) {
  376. "use strict";
  377. var __extends = (this && this.__extends) || (function () {
  378. var extendStatics = function (d, b) {
  379. extendStatics = Object.setPrototypeOf ||
  380. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  381. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  382. return extendStatics(d, b);
  383. }
  384. return function (d, b) {
  385. extendStatics(d, b);
  386. function __() { this.constructor = d; }
  387. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  388. };
  389. })();
  390. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  391. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  392. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  393. 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;
  394. return c > 3 && r && Object.defineProperty(target, key, r), r;
  395. };
  396. Object.defineProperty(exports, "__esModule", { value: true });
  397. var babylonjs_1 = __webpack_require__(/*! babylonjs */ "babylonjs");
  398. babylonjs_1.Effect.ShadersStore["digitalrainPixelShader"] = __webpack_require__(/*! ./digitalrain.fragment.fx */ "./src/digitalRain/digitalrain.fragment.fx");
  399. /**
  400. * DigitalRainFontTexture is the helper class used to easily create your digital rain font texture.
  401. *
  402. * It basically takes care rendering the font front the given font size to a texture.
  403. * This is used later on in the postprocess.
  404. */
  405. var DigitalRainFontTexture = /** @class */ (function (_super) {
  406. __extends(DigitalRainFontTexture, _super);
  407. /**
  408. * Create a new instance of the Digital Rain FontTexture class
  409. * @param name the name of the texture
  410. * @param font the font to use, use the W3C CSS notation
  411. * @param text the caracter set to use in the rendering.
  412. * @param scene the scene that owns the texture
  413. */
  414. function DigitalRainFontTexture(name, font, text, scene) {
  415. if (scene === void 0) { scene = null; }
  416. var _this = _super.call(this, scene) || this;
  417. scene = _this.getScene();
  418. if (!scene) {
  419. return _this;
  420. }
  421. _this.name = name;
  422. _this._text == text;
  423. _this._font == font;
  424. _this.wrapU = babylonjs_1.Texture.CLAMP_ADDRESSMODE;
  425. _this.wrapV = babylonjs_1.Texture.CLAMP_ADDRESSMODE;
  426. // Get the font specific info.
  427. var maxCharHeight = _this.getFontHeight(font);
  428. var maxCharWidth = _this.getFontWidth(font);
  429. _this._charSize = Math.max(maxCharHeight.height, maxCharWidth);
  430. // This is an approximate size, but should always be able to fit at least the maxCharCount.
  431. var textureWidth = _this._charSize;
  432. var textureHeight = Math.ceil(_this._charSize * text.length);
  433. // Create the texture that will store the font characters.
  434. _this._texture = scene.getEngine().createDynamicTexture(textureWidth, textureHeight, false, babylonjs_1.Texture.NEAREST_SAMPLINGMODE);
  435. //scene.getEngine().setclamp
  436. var textureSize = _this.getSize();
  437. // Create a canvas with the final size: the one matching the texture.
  438. var canvas = document.createElement("canvas");
  439. canvas.width = textureSize.width;
  440. canvas.height = textureSize.height;
  441. var context = canvas.getContext("2d");
  442. context.textBaseline = "top";
  443. context.font = font;
  444. context.fillStyle = "white";
  445. context.imageSmoothingEnabled = false;
  446. // Sets the text in the texture.
  447. for (var i = 0; i < text.length; i++) {
  448. context.fillText(text[i], 0, i * _this._charSize - maxCharHeight.offset);
  449. }
  450. // Flush the text in the dynamic texture.
  451. scene.getEngine().updateDynamicTexture(_this._texture, canvas, false, true);
  452. return _this;
  453. }
  454. Object.defineProperty(DigitalRainFontTexture.prototype, "charSize", {
  455. /**
  456. * Gets the size of one char in the texture (each char fits in size * size space in the texture).
  457. */
  458. get: function () {
  459. return this._charSize;
  460. },
  461. enumerable: true,
  462. configurable: true
  463. });
  464. /**
  465. * Gets the max char width of a font.
  466. * @param font the font to use, use the W3C CSS notation
  467. * @return the max char width
  468. */
  469. DigitalRainFontTexture.prototype.getFontWidth = function (font) {
  470. var fontDraw = document.createElement("canvas");
  471. var ctx = fontDraw.getContext('2d');
  472. ctx.fillStyle = 'white';
  473. ctx.font = font;
  474. return ctx.measureText("W").width;
  475. };
  476. // More info here: https://videlais.com/2014/03/16/the-many-and-varied-problems-with-measuring-font-height-for-html5-canvas/
  477. /**
  478. * Gets the max char height of a font.
  479. * @param font the font to use, use the W3C CSS notation
  480. * @return the max char height
  481. */
  482. DigitalRainFontTexture.prototype.getFontHeight = function (font) {
  483. var fontDraw = document.createElement("canvas");
  484. var ctx = fontDraw.getContext('2d');
  485. ctx.fillRect(0, 0, fontDraw.width, fontDraw.height);
  486. ctx.textBaseline = 'top';
  487. ctx.fillStyle = 'white';
  488. ctx.font = font;
  489. ctx.fillText('jH|', 0, 0);
  490. var pixels = ctx.getImageData(0, 0, fontDraw.width, fontDraw.height).data;
  491. var start = -1;
  492. var end = -1;
  493. for (var row = 0; row < fontDraw.height; row++) {
  494. for (var column = 0; column < fontDraw.width; column++) {
  495. var index = (row * fontDraw.width + column) * 4;
  496. if (pixels[index] === 0) {
  497. if (column === fontDraw.width - 1 && start !== -1) {
  498. end = row;
  499. row = fontDraw.height;
  500. break;
  501. }
  502. continue;
  503. }
  504. else {
  505. if (start === -1) {
  506. start = row;
  507. }
  508. break;
  509. }
  510. }
  511. }
  512. return { height: (end - start) + 1, offset: start - 1 };
  513. };
  514. /**
  515. * Clones the current DigitalRainFontTexture.
  516. * @return the clone of the texture.
  517. */
  518. DigitalRainFontTexture.prototype.clone = function () {
  519. return new DigitalRainFontTexture(this.name, this._font, this._text, this.getScene());
  520. };
  521. /**
  522. * Parses a json object representing the texture and returns an instance of it.
  523. * @param source the source JSON representation
  524. * @param scene the scene to create the texture for
  525. * @return the parsed texture
  526. */
  527. DigitalRainFontTexture.Parse = function (source, scene) {
  528. var texture = babylonjs_1.SerializationHelper.Parse(function () { return new DigitalRainFontTexture(source.name, source.font, source.text, scene); }, source, scene, null);
  529. return texture;
  530. };
  531. __decorate([
  532. babylonjs_1.serialize("font")
  533. ], DigitalRainFontTexture.prototype, "_font", void 0);
  534. __decorate([
  535. babylonjs_1.serialize("text")
  536. ], DigitalRainFontTexture.prototype, "_text", void 0);
  537. return DigitalRainFontTexture;
  538. }(babylonjs_1.BaseTexture));
  539. exports.DigitalRainFontTexture = DigitalRainFontTexture;
  540. /**
  541. * DigitalRainPostProcess helps rendering everithing in digital rain.
  542. *
  543. * Simmply add it to your scene and let the nerd that lives in you have fun.
  544. * Example usage: var pp = new DigitalRainPostProcess("digitalRain", "20px Monospace", camera);
  545. */
  546. var DigitalRainPostProcess = /** @class */ (function (_super) {
  547. __extends(DigitalRainPostProcess, _super);
  548. /**
  549. * Instantiates a new Digital Rain Post Process.
  550. * @param name the name to give to the postprocess
  551. * @camera the camera to apply the post process to.
  552. * @param options can either be the font name or an option object following the IDigitalRainPostProcessOptions format
  553. */
  554. function DigitalRainPostProcess(name, camera, options) {
  555. var _this = _super.call(this, name, 'digitalrain', ['digitalRainFontInfos', 'digitalRainOptions', 'cosTimeZeroOne', 'matrixSpeed'], ['digitalRainFont'], {
  556. width: camera.getEngine().getRenderWidth(),
  557. height: camera.getEngine().getRenderHeight()
  558. }, camera, babylonjs_1.Texture.TRILINEAR_SAMPLINGMODE, camera.getEngine(), true) || this;
  559. /**
  560. * This defines the amount you want to mix the "tile" or caracter space colored in the digital rain.
  561. * This number is defined between 0 and 1;
  562. */
  563. _this.mixToTile = 0;
  564. /**
  565. * This defines the amount you want to mix the normal rendering pass in the digital rain.
  566. * This number is defined between 0 and 1;
  567. */
  568. _this.mixToNormal = 0;
  569. // Default values.
  570. var font = "15px Monospace";
  571. var characterSet = "古池や蛙飛び込む水の音ふるいけやかわずとびこむみずのおと初しぐれ猿も小蓑をほしげ也はつしぐれさるもこみのをほしげなり江戸の雨何石呑んだ時鳥えどのあめなんごくのんだほととぎす";
  572. // Use options.
  573. if (options) {
  574. if (typeof (options) === "string") {
  575. font = options;
  576. }
  577. else {
  578. font = options.font || font;
  579. _this.mixToTile = options.mixToTile || _this.mixToTile;
  580. _this.mixToNormal = options.mixToNormal || _this.mixToNormal;
  581. }
  582. }
  583. _this._digitalRainFontTexture = new DigitalRainFontTexture(name, font, characterSet, camera.getScene());
  584. var textureSize = _this._digitalRainFontTexture.getSize();
  585. var alpha = 0.0;
  586. var cosTimeZeroOne = 0.0;
  587. var matrix = babylonjs_1.Matrix.FromValues(Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random());
  588. _this.onApply = function (effect) {
  589. effect.setTexture("digitalRainFont", _this._digitalRainFontTexture);
  590. effect.setFloat4("digitalRainFontInfos", _this._digitalRainFontTexture.charSize, characterSet.length, textureSize.width, textureSize.height);
  591. effect.setFloat4("digitalRainOptions", _this.width, _this.height, _this.mixToNormal, _this.mixToTile);
  592. effect.setMatrix("matrixSpeed", matrix);
  593. alpha += 0.003;
  594. cosTimeZeroOne = alpha;
  595. effect.setFloat('cosTimeZeroOne', cosTimeZeroOne);
  596. };
  597. return _this;
  598. }
  599. return DigitalRainPostProcess;
  600. }(babylonjs_1.PostProcess));
  601. exports.DigitalRainPostProcess = DigitalRainPostProcess;
  602. /***/ }),
  603. /***/ "./src/digitalRain/digitalrain.fragment.fx":
  604. /*!*************************************************!*\
  605. !*** ./src/digitalRain/digitalrain.fragment.fx ***!
  606. \*************************************************/
  607. /*! no static exports found */
  608. /***/ (function(module, exports) {
  609. module.exports = "\nvarying vec2 vUV;\nuniform sampler2D textureSampler;\nuniform sampler2D digitalRainFont;\n\nuniform vec4 digitalRainFontInfos;\nuniform vec4 digitalRainOptions;\nuniform mat4 matrixSpeed;\nuniform float cosTimeZeroOne;\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=digitalRainFontInfos.x;\nfloat numChar=digitalRainFontInfos.y-1.0;\nfloat fontx=digitalRainFontInfos.z;\nfloat fonty=digitalRainFontInfos.w;\nfloat screenx=digitalRainOptions.x;\nfloat screeny=digitalRainOptions.y;\nfloat ratio=screeny/fonty;\nfloat columnx=float(floor((gl_FragCoord.x)/caracterSize));\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);\nint st=int(mod(columnx,4.0));\nfloat speed=cosTimeZeroOne*(sin(tileX*314.5)*0.5+0.6); \nfloat x=float(mod(gl_FragCoord.x,caracterSize))/fontx;\nfloat y=float(mod(speed+gl_FragCoord.y/screeny,1.0));\ny*=ratio;\nvec4 finalColor=texture2D(digitalRainFont,vec2(x,1.0-y));\nvec3 high=finalColor.rgb*(vec3(1.2,1.2,1.2)*pow(1.0-y,30.0));\nfinalColor.rgb*=vec3(pow(tileLuminance,5.0),pow(tileLuminance,1.5),pow(tileLuminance,3.0));\nfinalColor.rgb+=high;\nfinalColor.rgb=clamp(finalColor.rgb,0.,1.);\nfinalColor.a=1.0;\nfinalColor=mix(finalColor,tileColor,digitalRainOptions.w);\nfinalColor=mix(finalColor,baseColor,digitalRainOptions.z);\ngl_FragColor=finalColor;\n}"
  610. /***/ }),
  611. /***/ "./src/digitalRain/index.ts":
  612. /*!**********************************!*\
  613. !*** ./src/digitalRain/index.ts ***!
  614. \**********************************/
  615. /*! no static exports found */
  616. /***/ (function(module, exports, __webpack_require__) {
  617. "use strict";
  618. function __export(m) {
  619. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  620. }
  621. Object.defineProperty(exports, "__esModule", { value: true });
  622. __export(__webpack_require__(/*! ./digitalRainPostProcess */ "./src/digitalRain/digitalRainPostProcess.ts"));
  623. /***/ }),
  624. /***/ "./src/index.ts":
  625. /*!**********************!*\
  626. !*** ./src/index.ts ***!
  627. \**********************/
  628. /*! no static exports found */
  629. /***/ (function(module, exports, __webpack_require__) {
  630. "use strict";
  631. function __export(m) {
  632. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  633. }
  634. Object.defineProperty(exports, "__esModule", { value: true });
  635. __export(__webpack_require__(/*! ./asciiArt */ "./src/asciiArt/index.ts"));
  636. __export(__webpack_require__(/*! ./digitalRain */ "./src/digitalRain/index.ts"));
  637. /***/ }),
  638. /***/ "./src/legacy.ts":
  639. /*!***********************!*\
  640. !*** ./src/legacy.ts ***!
  641. \***********************/
  642. /*! no static exports found */
  643. /***/ (function(module, exports, __webpack_require__) {
  644. "use strict";
  645. /* WEBPACK VAR INJECTION */(function(global) {
  646. function __export(m) {
  647. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  648. }
  649. Object.defineProperty(exports, "__esModule", { value: true });
  650. var postProcessLibrary = __webpack_require__(/*! ./index */ "./src/index.ts");
  651. /**
  652. *
  653. * This is the entry point for the UMD module.
  654. * The entry point for a future ESM package should be index.ts
  655. */
  656. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : undefined);
  657. if (typeof globalObject !== "undefined") {
  658. for (var key in postProcessLibrary) {
  659. globalObject.BABYLON[key] = postProcessLibrary[key];
  660. }
  661. }
  662. __export(__webpack_require__(/*! ./index */ "./src/index.ts"));
  663. /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../../node_modules/webpack/buildin/global.js */ "../node_modules/webpack/buildin/global.js")))
  664. /***/ }),
  665. /***/ "babylonjs":
  666. /*!****************************************************************************************************!*\
  667. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  668. \****************************************************************************************************/
  669. /*! no static exports found */
  670. /***/ (function(module, exports) {
  671. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  672. /***/ })
  673. /******/ });
  674. });
  675. //# sourceMappingURL=babylonjs.postProcess.js.map