babylon.asciiArtPostProcess.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1.  /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. /**
  4. * AsciiArtFontTexture is the helper class used to easily create your ascii art 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 AsciiArtFontTexture 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 Ascii Art 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 = Math.ceil(this._charSize * text.length);
  42. var textureHeight = this._charSize;
  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], 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 AsciiArtTexture.
  115. * @return the clone of the texture.
  116. */
  117. public clone(): AsciiArtFontTexture {
  118. return new AsciiArtFontTexture(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): AsciiArtFontTexture {
  127. var texture = SerializationHelper.Parse(() => new AsciiArtFontTexture(source.name, source.font, source.text, scene),
  128. source, scene, null);
  129. return texture;
  130. }
  131. }
  132. /**
  133. * Option available in the Ascii Art Post Process.
  134. */
  135. export interface IAsciiArtPostProcessOptions {
  136. /**
  137. * The font to use following the w3c font definition.
  138. */
  139. font?: string;
  140. /**
  141. * The caracter set to use in the postprocess.
  142. */
  143. caracterSet?: string;
  144. /**
  145. * This defines the amount you want to mix the "tile" or caracter space colored in the ascii art.
  146. * This number is defined between 0 and 1;
  147. */
  148. mixToTile?:number;
  149. /**
  150. * This defines the amount you want to mix the normal rendering pass in the ascii art.
  151. * This number is defined between 0 and 1;
  152. */
  153. mixToNormal?:number;
  154. }
  155. /**
  156. * AsciiArtPostProcess helps rendering everithing in Ascii Art.
  157. *
  158. * Simmply add it to your scene and let the nerd that lives in you have fun.
  159. * Example usage: var pp = new AsciiArtPostProcess("myAscii", "20px Monospace", camera);
  160. */
  161. export class AsciiArtPostProcess extends PostProcess {
  162. /**
  163. * The font texture used to render the char in the post process.
  164. */
  165. private _asciiArtFontTexture: AsciiArtFontTexture;
  166. /**
  167. * This defines the amount you want to mix the "tile" or caracter space colored in the ascii art.
  168. * This number is defined between 0 and 1;
  169. */
  170. public mixToTile:number = 0;
  171. /**
  172. * This defines the amount you want to mix the normal rendering pass in the ascii art.
  173. * This number is defined between 0 and 1;
  174. */
  175. public mixToNormal:number = 0;
  176. /**
  177. * Instantiates a new Ascii Art Post Process.
  178. * @param name the name to give to the postprocess
  179. * @camera the camera to apply the post process to.
  180. * @param options can either be the font name or an option object following the IAsciiArtPostProcessOptions format
  181. */
  182. constructor(name: string, camera: Camera, options?: string | IAsciiArtPostProcessOptions) {
  183. super(name,
  184. 'asciiart',
  185. ['asciiArtFontInfos', 'asciiArtOptions'],
  186. ['asciiArtFont'],
  187. {
  188. width: camera.getEngine().getRenderWidth(),
  189. height: camera.getEngine().getRenderHeight()
  190. },
  191. camera,
  192. Texture.TRILINEAR_SAMPLINGMODE,
  193. camera.getEngine(),
  194. true);
  195. // Default values.
  196. var font = "40px Monospace";
  197. var caracterSet = " `-.'_:,\"=^;<+!*?/cL\\zrs7TivJtC{3F)Il(xZfY5S2eajo14[nuyE]P6V9kXpKwGhqAUbOd8#HRDB0$mgMW&Q%N@";
  198. // Use options.
  199. if (options) {
  200. if (typeof(options) === "string") {
  201. font = <string>options;
  202. }
  203. else {
  204. font = (<IAsciiArtPostProcessOptions>options).font || font;
  205. caracterSet = (<IAsciiArtPostProcessOptions>options).caracterSet || caracterSet;
  206. this.mixToTile = (<IAsciiArtPostProcessOptions>options).mixToTile || this.mixToTile;
  207. this.mixToNormal = (<IAsciiArtPostProcessOptions>options).mixToNormal || this.mixToNormal;
  208. }
  209. }
  210. this._asciiArtFontTexture = new AsciiArtFontTexture(name, font, caracterSet, camera.getScene());
  211. var textureSize = this._asciiArtFontTexture.getSize();
  212. this.onApply = (effect: Effect) => {
  213. effect.setTexture("asciiArtFont", this._asciiArtFontTexture);
  214. effect.setFloat4("asciiArtFontInfos",
  215. this._asciiArtFontTexture.charSize,
  216. caracterSet.length,
  217. textureSize.width,
  218. textureSize.height);
  219. effect.setFloat4("asciiArtOptions",
  220. this.width,
  221. this.height,
  222. this.mixToNormal,
  223. this.mixToTile);
  224. };
  225. }
  226. }
  227. }