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