babylon.digitalRainPostProcess.ts 11 KB

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