babylon.digitalRainPostProcess.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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-digitalRain.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-digitalRain.ts":
  122. /*!**************************************!*\
  123. !*** ./legacy/legacy-digitalRain.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/digitalRain/index */ "./src/digitalRain/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/digitalRain/index */ "./src/digitalRain/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/digitalRain/digitalRainPostProcess.ts":
  148. /*!***************************************************!*\
  149. !*** ./src/digitalRain/digitalRainPostProcess.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["digitalrainPixelShader"] = __webpack_require__(/*! ./digitalrain.fragment.fx */ "./src/digitalRain/digitalrain.fragment.fx");
  176. /**
  177. * DigitalRainFontTexture is the helper class used to easily create your digital rain 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 DigitalRainFontTexture = /** @class */ (function (_super) {
  183. __extends(DigitalRainFontTexture, _super);
  184. /**
  185. * Create a new instance of the Digital Rain 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 DigitalRainFontTexture(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. // Get the font specific info.
  204. var maxCharHeight = _this.getFontHeight(font);
  205. var maxCharWidth = _this.getFontWidth(font);
  206. _this._charSize = Math.max(maxCharHeight.height, maxCharWidth);
  207. // This is an approximate size, but should always be able to fit at least the maxCharCount.
  208. var textureWidth = _this._charSize;
  209. var textureHeight = Math.ceil(_this._charSize * text.length);
  210. // Create the texture that will store the font characters.
  211. _this._texture = scene.getEngine().createDynamicTexture(textureWidth, textureHeight, false, babylonjs_1.Texture.NEAREST_SAMPLINGMODE);
  212. //scene.getEngine().setclamp
  213. var textureSize = _this.getSize();
  214. // Create a canvas with the final size: the one matching the texture.
  215. var canvas = document.createElement("canvas");
  216. canvas.width = textureSize.width;
  217. canvas.height = textureSize.height;
  218. var context = canvas.getContext("2d");
  219. context.textBaseline = "top";
  220. context.font = font;
  221. context.fillStyle = "white";
  222. context.imageSmoothingEnabled = false;
  223. // Sets the text in the texture.
  224. for (var i = 0; i < text.length; i++) {
  225. context.fillText(text[i], 0, i * _this._charSize - maxCharHeight.offset);
  226. }
  227. // Flush the text in the dynamic texture.
  228. scene.getEngine().updateDynamicTexture(_this._texture, canvas, false, true);
  229. return _this;
  230. }
  231. Object.defineProperty(DigitalRainFontTexture.prototype, "charSize", {
  232. /**
  233. * Gets the size of one char in the texture (each char fits in size * size space in the texture).
  234. */
  235. get: function () {
  236. return this._charSize;
  237. },
  238. enumerable: true,
  239. configurable: true
  240. });
  241. /**
  242. * Gets the max char width of a font.
  243. * @param font the font to use, use the W3C CSS notation
  244. * @return the max char width
  245. */
  246. DigitalRainFontTexture.prototype.getFontWidth = function (font) {
  247. var fontDraw = document.createElement("canvas");
  248. var ctx = fontDraw.getContext('2d');
  249. ctx.fillStyle = 'white';
  250. ctx.font = font;
  251. return ctx.measureText("W").width;
  252. };
  253. // More info here: https://videlais.com/2014/03/16/the-many-and-varied-problems-with-measuring-font-height-for-html5-canvas/
  254. /**
  255. * Gets the max char height of a font.
  256. * @param font the font to use, use the W3C CSS notation
  257. * @return the max char height
  258. */
  259. DigitalRainFontTexture.prototype.getFontHeight = function (font) {
  260. var fontDraw = document.createElement("canvas");
  261. var ctx = fontDraw.getContext('2d');
  262. ctx.fillRect(0, 0, fontDraw.width, fontDraw.height);
  263. ctx.textBaseline = 'top';
  264. ctx.fillStyle = 'white';
  265. ctx.font = font;
  266. ctx.fillText('jH|', 0, 0);
  267. var pixels = ctx.getImageData(0, 0, fontDraw.width, fontDraw.height).data;
  268. var start = -1;
  269. var end = -1;
  270. for (var row = 0; row < fontDraw.height; row++) {
  271. for (var column = 0; column < fontDraw.width; column++) {
  272. var index = (row * fontDraw.width + column) * 4;
  273. if (pixels[index] === 0) {
  274. if (column === fontDraw.width - 1 && start !== -1) {
  275. end = row;
  276. row = fontDraw.height;
  277. break;
  278. }
  279. continue;
  280. }
  281. else {
  282. if (start === -1) {
  283. start = row;
  284. }
  285. break;
  286. }
  287. }
  288. }
  289. return { height: (end - start) + 1, offset: start - 1 };
  290. };
  291. /**
  292. * Clones the current DigitalRainFontTexture.
  293. * @return the clone of the texture.
  294. */
  295. DigitalRainFontTexture.prototype.clone = function () {
  296. return new DigitalRainFontTexture(this.name, this._font, this._text, this.getScene());
  297. };
  298. /**
  299. * Parses a json object representing the texture and returns an instance of it.
  300. * @param source the source JSON representation
  301. * @param scene the scene to create the texture for
  302. * @return the parsed texture
  303. */
  304. DigitalRainFontTexture.Parse = function (source, scene) {
  305. var texture = babylonjs_1.SerializationHelper.Parse(function () { return new DigitalRainFontTexture(source.name, source.font, source.text, scene); }, source, scene, null);
  306. return texture;
  307. };
  308. __decorate([
  309. babylonjs_1.serialize("font")
  310. ], DigitalRainFontTexture.prototype, "_font", void 0);
  311. __decorate([
  312. babylonjs_1.serialize("text")
  313. ], DigitalRainFontTexture.prototype, "_text", void 0);
  314. return DigitalRainFontTexture;
  315. }(babylonjs_1.BaseTexture));
  316. exports.DigitalRainFontTexture = DigitalRainFontTexture;
  317. /**
  318. * DigitalRainPostProcess helps rendering everithing in digital rain.
  319. *
  320. * Simmply add it to your scene and let the nerd that lives in you have fun.
  321. * Example usage: var pp = new DigitalRainPostProcess("digitalRain", "20px Monospace", camera);
  322. */
  323. var DigitalRainPostProcess = /** @class */ (function (_super) {
  324. __extends(DigitalRainPostProcess, _super);
  325. /**
  326. * Instantiates a new Digital Rain Post Process.
  327. * @param name the name to give to the postprocess
  328. * @camera the camera to apply the post process to.
  329. * @param options can either be the font name or an option object following the IDigitalRainPostProcessOptions format
  330. */
  331. function DigitalRainPostProcess(name, camera, options) {
  332. var _this = _super.call(this, name, 'digitalrain', ['digitalRainFontInfos', 'digitalRainOptions', 'cosTimeZeroOne', 'matrixSpeed'], ['digitalRainFont'], {
  333. width: camera.getEngine().getRenderWidth(),
  334. height: camera.getEngine().getRenderHeight()
  335. }, camera, babylonjs_1.Texture.TRILINEAR_SAMPLINGMODE, camera.getEngine(), true) || this;
  336. /**
  337. * This defines the amount you want to mix the "tile" or caracter space colored in the digital rain.
  338. * This number is defined between 0 and 1;
  339. */
  340. _this.mixToTile = 0;
  341. /**
  342. * This defines the amount you want to mix the normal rendering pass in the digital rain.
  343. * This number is defined between 0 and 1;
  344. */
  345. _this.mixToNormal = 0;
  346. // Default values.
  347. var font = "15px Monospace";
  348. var characterSet = "古池や蛙飛び込む水の音ふるいけやかわずとびこむみずのおと初しぐれ猿も小蓑をほしげ也はつしぐれさるもこみのをほしげなり江戸の雨何石呑んだ時鳥えどのあめなんごくのんだほととぎす";
  349. // Use options.
  350. if (options) {
  351. if (typeof (options) === "string") {
  352. font = options;
  353. }
  354. else {
  355. font = options.font || font;
  356. _this.mixToTile = options.mixToTile || _this.mixToTile;
  357. _this.mixToNormal = options.mixToNormal || _this.mixToNormal;
  358. }
  359. }
  360. _this._digitalRainFontTexture = new DigitalRainFontTexture(name, font, characterSet, camera.getScene());
  361. var textureSize = _this._digitalRainFontTexture.getSize();
  362. var alpha = 0.0;
  363. var cosTimeZeroOne = 0.0;
  364. 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());
  365. _this.onApply = function (effect) {
  366. effect.setTexture("digitalRainFont", _this._digitalRainFontTexture);
  367. effect.setFloat4("digitalRainFontInfos", _this._digitalRainFontTexture.charSize, characterSet.length, textureSize.width, textureSize.height);
  368. effect.setFloat4("digitalRainOptions", _this.width, _this.height, _this.mixToNormal, _this.mixToTile);
  369. effect.setMatrix("matrixSpeed", matrix);
  370. alpha += 0.003;
  371. cosTimeZeroOne = alpha;
  372. effect.setFloat('cosTimeZeroOne', cosTimeZeroOne);
  373. };
  374. return _this;
  375. }
  376. return DigitalRainPostProcess;
  377. }(babylonjs_1.PostProcess));
  378. exports.DigitalRainPostProcess = DigitalRainPostProcess;
  379. /***/ }),
  380. /***/ "./src/digitalRain/digitalrain.fragment.fx":
  381. /*!*************************************************!*\
  382. !*** ./src/digitalRain/digitalrain.fragment.fx ***!
  383. \*************************************************/
  384. /*! no static exports found */
  385. /***/ (function(module, exports) {
  386. 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}"
  387. /***/ }),
  388. /***/ "./src/digitalRain/index.ts":
  389. /*!**********************************!*\
  390. !*** ./src/digitalRain/index.ts ***!
  391. \**********************************/
  392. /*! no static exports found */
  393. /***/ (function(module, exports, __webpack_require__) {
  394. "use strict";
  395. function __export(m) {
  396. for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
  397. }
  398. Object.defineProperty(exports, "__esModule", { value: true });
  399. __export(__webpack_require__(/*! ./digitalRainPostProcess */ "./src/digitalRain/digitalRainPostProcess.ts"));
  400. /***/ }),
  401. /***/ "babylonjs":
  402. /*!****************************************************************************************************!*\
  403. !*** external {"root":"BABYLON","commonjs":"babylonjs","commonjs2":"babylonjs","amd":"babylonjs"} ***!
  404. \****************************************************************************************************/
  405. /*! no static exports found */
  406. /***/ (function(module, exports) {
  407. module.exports = __WEBPACK_EXTERNAL_MODULE_babylonjs__;
  408. /***/ })
  409. /******/ });
  410. });
  411. //# sourceMappingURL=babylon.digitalRainPostProcess.js.map