digitalRainPostProcess.ts 11 KB

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