babylon.asciiArtPostProcess.ts 10 KB

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