babylon.digitalRainPostProcess.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  16. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  17. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  18. 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;
  19. return c > 3 && r && Object.defineProperty(target, key, r), r;
  20. };
  21. var BABYLON;
  22. (function (BABYLON) {
  23. /**
  24. * DigitalRainFontTexture is the helper class used to easily create your digital rain font texture.
  25. *
  26. * It basically takes care rendering the font front the given font size to a texture.
  27. * This is used later on in the postprocess.
  28. */
  29. var DigitalRainFontTexture = /** @class */ (function (_super) {
  30. __extends(DigitalRainFontTexture, _super);
  31. /**
  32. * Create a new instance of the Digital Rain FontTexture class
  33. * @param name the name of the texture
  34. * @param font the font to use, use the W3C CSS notation
  35. * @param text the caracter set to use in the rendering.
  36. * @param scene the scene that owns the texture
  37. */
  38. function DigitalRainFontTexture(name, font, text, scene) {
  39. if (scene === void 0) { scene = null; }
  40. var _this = _super.call(this, scene) || this;
  41. scene = _this.getScene();
  42. if (!scene) {
  43. return _this;
  44. }
  45. _this.name = name;
  46. _this._text == text;
  47. _this._font == font;
  48. _this.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  49. _this.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  50. //this.anisotropicFilteringLevel = 1;
  51. // Get the font specific info.
  52. var maxCharHeight = _this.getFontHeight(font);
  53. var maxCharWidth = _this.getFontWidth(font);
  54. _this._charSize = Math.max(maxCharHeight.height, maxCharWidth);
  55. // This is an approximate size, but should always be able to fit at least the maxCharCount.
  56. var textureWidth = _this._charSize;
  57. var textureHeight = Math.ceil(_this._charSize * text.length);
  58. // Create the texture that will store the font characters.
  59. _this._texture = scene.getEngine().createDynamicTexture(textureWidth, textureHeight, false, BABYLON.Texture.NEAREST_SAMPLINGMODE);
  60. //scene.getEngine().setclamp
  61. var textureSize = _this.getSize();
  62. // Create a canvas with the final size: the one matching the texture.
  63. var canvas = document.createElement("canvas");
  64. canvas.width = textureSize.width;
  65. canvas.height = textureSize.height;
  66. var context = canvas.getContext("2d");
  67. context.textBaseline = "top";
  68. context.font = font;
  69. context.fillStyle = "white";
  70. context.imageSmoothingEnabled = false;
  71. // Sets the text in the texture.
  72. for (var i = 0; i < text.length; i++) {
  73. context.fillText(text[i], 0, i * _this._charSize - maxCharHeight.offset);
  74. }
  75. // Flush the text in the dynamic texture.
  76. scene.getEngine().updateDynamicTexture(_this._texture, canvas, false, true);
  77. return _this;
  78. }
  79. Object.defineProperty(DigitalRainFontTexture.prototype, "charSize", {
  80. /**
  81. * Gets the size of one char in the texture (each char fits in size * size space in the texture).
  82. */
  83. get: function () {
  84. return this._charSize;
  85. },
  86. enumerable: true,
  87. configurable: true
  88. });
  89. /**
  90. * Gets the max char width of a font.
  91. * @param font the font to use, use the W3C CSS notation
  92. * @return the max char width
  93. */
  94. DigitalRainFontTexture.prototype.getFontWidth = function (font) {
  95. var fontDraw = document.createElement("canvas");
  96. var ctx = fontDraw.getContext('2d');
  97. ctx.fillStyle = 'white';
  98. ctx.font = font;
  99. return ctx.measureText("W").width;
  100. };
  101. // More info here: https://videlais.com/2014/03/16/the-many-and-varied-problems-with-measuring-font-height-for-html5-canvas/
  102. /**
  103. * Gets the max char height of a font.
  104. * @param font the font to use, use the W3C CSS notation
  105. * @return the max char height
  106. */
  107. DigitalRainFontTexture.prototype.getFontHeight = function (font) {
  108. var fontDraw = document.createElement("canvas");
  109. var ctx = fontDraw.getContext('2d');
  110. ctx.fillRect(0, 0, fontDraw.width, fontDraw.height);
  111. ctx.textBaseline = 'top';
  112. ctx.fillStyle = 'white';
  113. ctx.font = font;
  114. ctx.fillText('jH|', 0, 0);
  115. var pixels = ctx.getImageData(0, 0, fontDraw.width, fontDraw.height).data;
  116. var start = -1;
  117. var end = -1;
  118. for (var row = 0; row < fontDraw.height; row++) {
  119. for (var column = 0; column < fontDraw.width; column++) {
  120. var index = (row * fontDraw.width + column) * 4;
  121. if (pixels[index] === 0) {
  122. if (column === fontDraw.width - 1 && start !== -1) {
  123. end = row;
  124. row = fontDraw.height;
  125. break;
  126. }
  127. continue;
  128. }
  129. else {
  130. if (start === -1) {
  131. start = row;
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. return { height: (end - start) + 1, offset: start - 1 };
  138. };
  139. /**
  140. * Clones the current DigitalRainFontTexture.
  141. * @return the clone of the texture.
  142. */
  143. DigitalRainFontTexture.prototype.clone = function () {
  144. return new DigitalRainFontTexture(this.name, this._font, this._text, this.getScene());
  145. };
  146. /**
  147. * Parses a json object representing the texture and returns an instance of it.
  148. * @param source the source JSON representation
  149. * @param scene the scene to create the texture for
  150. * @return the parsed texture
  151. */
  152. DigitalRainFontTexture.Parse = function (source, scene) {
  153. var texture = BABYLON.SerializationHelper.Parse(function () { return new DigitalRainFontTexture(source.name, source.font, source.text, scene); }, source, scene, null);
  154. return texture;
  155. };
  156. __decorate([
  157. BABYLON.serialize("font")
  158. ], DigitalRainFontTexture.prototype, "_font", void 0);
  159. __decorate([
  160. BABYLON.serialize("text")
  161. ], DigitalRainFontTexture.prototype, "_text", void 0);
  162. return DigitalRainFontTexture;
  163. }(BABYLON.BaseTexture));
  164. BABYLON.DigitalRainFontTexture = DigitalRainFontTexture;
  165. /**
  166. * DigitalRainPostProcess helps rendering everithing in digital rain.
  167. *
  168. * Simmply add it to your scene and let the nerd that lives in you have fun.
  169. * Example usage: var pp = new DigitalRainPostProcess("digitalRain", "20px Monospace", camera);
  170. */
  171. var DigitalRainPostProcess = /** @class */ (function (_super) {
  172. __extends(DigitalRainPostProcess, _super);
  173. /**
  174. * Instantiates a new Digital Rain Post Process.
  175. * @param name the name to give to the postprocess
  176. * @camera the camera to apply the post process to.
  177. * @param options can either be the font name or an option object following the IDigitalRainPostProcessOptions format
  178. */
  179. function DigitalRainPostProcess(name, camera, options) {
  180. var _this = _super.call(this, name, 'digitalrain', ['digitalRainFontInfos', 'digitalRainOptions', 'cosTimeZeroOne', 'matrixSpeed'], ['digitalRainFont'], {
  181. width: camera.getEngine().getRenderWidth(),
  182. height: camera.getEngine().getRenderHeight()
  183. }, camera, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, camera.getEngine(), true) || this;
  184. /**
  185. * This defines the amount you want to mix the "tile" or caracter space colored in the digital rain.
  186. * This number is defined between 0 and 1;
  187. */
  188. _this.mixToTile = 0;
  189. /**
  190. * This defines the amount you want to mix the normal rendering pass in the digital rain.
  191. * This number is defined between 0 and 1;
  192. */
  193. _this.mixToNormal = 0;
  194. // Default values.
  195. var font = "15px Monospace";
  196. var characterSet = "古池や蛙飛び込む水の音ふるいけやかわずとびこむみずのおと初しぐれ猿も小蓑をほしげ也はつしぐれさるもこみのをほしげなり江戸の雨何石呑んだ時鳥えどのあめなんごくのんだほととぎす";
  197. // Use options.
  198. if (options) {
  199. if (typeof (options) === "string") {
  200. font = options;
  201. }
  202. else {
  203. font = options.font || font;
  204. _this.mixToTile = options.mixToTile || _this.mixToTile;
  205. _this.mixToNormal = options.mixToNormal || _this.mixToNormal;
  206. }
  207. }
  208. _this._digitalRainFontTexture = new DigitalRainFontTexture(name, font, characterSet, camera.getScene());
  209. var textureSize = _this._digitalRainFontTexture.getSize();
  210. var alpha = 0.0;
  211. var cosTimeZeroOne = 0.0;
  212. var matrix = BABYLON.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());
  213. _this.onApply = function (effect) {
  214. effect.setTexture("digitalRainFont", _this._digitalRainFontTexture);
  215. effect.setFloat4("digitalRainFontInfos", _this._digitalRainFontTexture.charSize, characterSet.length, textureSize.width, textureSize.height);
  216. effect.setFloat4("digitalRainOptions", _this.width, _this.height, _this.mixToNormal, _this.mixToTile);
  217. effect.setMatrix("matrixSpeed", matrix);
  218. alpha += 0.003;
  219. cosTimeZeroOne = alpha;
  220. effect.setFloat('cosTimeZeroOne', cosTimeZeroOne);
  221. };
  222. return _this;
  223. }
  224. return DigitalRainPostProcess;
  225. }(BABYLON.PostProcess));
  226. BABYLON.DigitalRainPostProcess = DigitalRainPostProcess;
  227. })(BABYLON || (BABYLON = {}));
  228. //# sourceMappingURL=babylon.digitalRainPostProcess.js.map
  229. BABYLON.Effect.ShadersStore['digitalrainPixelShader'] = "\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}";