babylon.digitalRainPostProcess.js 12 KB

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