babylon.digitalRainPostProcess.ts 11 KB

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