babylon.standardProceduralTexture.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. module BABYLON {
  2. export class WoodProceduralTexture extends ProceduralTexture {
  3. private _ampScale: number = 100.0;
  4. private _woodColor: BABYLON.Color3 = new BABYLON.Color3(0.32, 0.17, 0.09);
  5. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  6. super(name, size, "wood", scene, fallbackTexture, generateMipMaps);
  7. this.updateShaderUniforms();
  8. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  9. this.refreshRate = 0;
  10. }
  11. public updateShaderUniforms() {
  12. this.setFloat("ampScale", this._ampScale);
  13. this.setColor3("woodColor", this._woodColor);
  14. }
  15. public get ampScale(): number {
  16. return this._ampScale;
  17. }
  18. public set ampScale(value: number) {
  19. this._ampScale = value;
  20. this.updateShaderUniforms();
  21. }
  22. public get woodColor(): BABYLON.Color3 {
  23. return this._woodColor;
  24. }
  25. public set woodColor(value: BABYLON.Color3) {
  26. this._woodColor = value;
  27. this.updateShaderUniforms();
  28. }
  29. }
  30. export class FireProceduralTexture extends ProceduralTexture {
  31. private _time: number = 0.0;
  32. private _speed: BABYLON.Vector2 = new BABYLON.Vector2(0.5, 0.3);
  33. private _shift: number = 1.6;
  34. private _alpha: number = 1.0;
  35. private _autoGenerateTime: boolean = true;
  36. private _fireColors: number[][];
  37. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  38. super(name, size, "fire", scene, fallbackTexture, generateMipMaps);
  39. this._fireColors = FireProceduralTexture.RedFireColors;
  40. this.updateShaderUniforms();
  41. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  42. this.refreshRate = 1;
  43. }
  44. public updateShaderUniforms() {
  45. this.setFloat("iGlobalTime", this._time);
  46. this.setVector2("speed", this._speed);
  47. this.setFloat("shift", this._shift);
  48. this.setFloat("alpha", this._alpha);
  49. this.setColor3("c1", new BABYLON.Color3(this._fireColors[0][0], this._fireColors[0][1], this._fireColors[0][2]));
  50. this.setColor3("c2", new BABYLON.Color3(this._fireColors[1][0], this._fireColors[1][1], this._fireColors[1][2]));
  51. this.setColor3("c3", new BABYLON.Color3(this._fireColors[2][0], this._fireColors[2][1], this._fireColors[2][2]));
  52. this.setColor3("c4", new BABYLON.Color3(this._fireColors[3][0], this._fireColors[3][1], this._fireColors[3][2]));
  53. this.setColor3("c5", new BABYLON.Color3(this._fireColors[4][0], this._fireColors[4][1], this._fireColors[4][2]));
  54. this.setColor3("c6", new BABYLON.Color3(this._fireColors[5][0], this._fireColors[5][1], this._fireColors[5][2]));
  55. }
  56. public render(useCameraPostProcess?: boolean) {
  57. if (this._autoGenerateTime) {
  58. this._time += this.getScene().getAnimationRatio() * 0.03;
  59. this.updateShaderUniforms();
  60. }
  61. super.render(useCameraPostProcess);
  62. }
  63. public static get PurpleFireColors(): number[][] {
  64. return [
  65. [0.5, 0.0, 1.0],
  66. [0.9, 0.0, 1.0],
  67. [0.2, 0.0, 1.0],
  68. [1.0, 0.9, 1.0],
  69. [0.1, 0.1, 1.0],
  70. [0.9, 0.9, 1.0]
  71. ];
  72. }
  73. public static get GreenFireColors(): number[][] {
  74. return [
  75. [0.5, 1.0, 0.0],
  76. [0.5, 1.0, 0.0],
  77. [0.3, 0.4, 0.0],
  78. [0.5, 1.0, 0.0],
  79. [0.2, 0.0, 0.0],
  80. [0.5, 1.0, 0.0]
  81. ];
  82. }
  83. public static get RedFireColors(): number[][] {
  84. return [
  85. [0.5, 0.0, 0.1],
  86. [0.9, 0.0, 0.0],
  87. [0.2, 0.0, 0.0],
  88. [1.0, 0.9, 0.0],
  89. [0.1, 0.1, 0.1],
  90. [0.9, 0.9, 0.9]
  91. ];
  92. }
  93. public static get BlueFireColors(): number[][] {
  94. return [
  95. [0.1, 0.0, 0.5],
  96. [0.0, 0.0, 0.5],
  97. [0.1, 0.0, 0.2],
  98. [0.0, 0.0, 1.0],
  99. [0.1, 0.2, 0.3],
  100. [0.0, 0.2, 0.9]
  101. ];
  102. }
  103. public get fireColors(): number[][] {
  104. return this._fireColors;
  105. }
  106. public set fireColors(value: number[][]) {
  107. this._fireColors = value;
  108. this.updateShaderUniforms();
  109. }
  110. public get time(): number {
  111. return this._time;
  112. }
  113. public set time(value: number) {
  114. this._time = value;
  115. this.updateShaderUniforms();
  116. }
  117. public get speed(): BABYLON.Vector2 {
  118. return this._speed;
  119. }
  120. public set speed(value: BABYLON.Vector2) {
  121. this._speed = value;
  122. this.updateShaderUniforms();
  123. }
  124. public get shift(): number {
  125. return this._shift;
  126. }
  127. public set shift(value: number) {
  128. this._shift = value;
  129. this.updateShaderUniforms();
  130. }
  131. public get alpha(): number {
  132. return this._alpha;
  133. }
  134. public set alpha(value: number) {
  135. this._alpha = value;
  136. this.updateShaderUniforms();
  137. }
  138. }
  139. export class CloudProceduralTexture extends ProceduralTexture {
  140. private _skyColor: BABYLON.Color3 = new BABYLON.Color3(0.15, 0.68, 1.0);
  141. private _cloudColor: BABYLON.Color3 = new BABYLON.Color3(1, 1, 1);
  142. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  143. super(name, size, "cloud", scene, fallbackTexture, generateMipMaps);
  144. this.updateShaderUniforms();
  145. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  146. this.refreshRate = 0;
  147. // https://www.shadertoy.com/view/XsjSRt
  148. }
  149. public updateShaderUniforms() {
  150. this.setColor3("skyColor", this._skyColor);
  151. this.setColor3("cloudColor", this._cloudColor);
  152. }
  153. public get skyColor(): BABYLON.Color3 {
  154. return this._skyColor;
  155. }
  156. public set skyColor(value: BABYLON.Color3) {
  157. this._skyColor = value;
  158. this.updateShaderUniforms();
  159. }
  160. public get cloudColor(): BABYLON.Color3 {
  161. return this._cloudColor;
  162. }
  163. public set cloudColor(value: BABYLON.Color3) {
  164. this._cloudColor = value;
  165. this.updateShaderUniforms();
  166. }
  167. }
  168. export class GrassProceduralTexture extends ProceduralTexture {
  169. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  170. super(name, size, "grass", scene, fallbackTexture, generateMipMaps);
  171. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  172. this.refreshRate = 0;
  173. }
  174. }
  175. export class RockProceduralTexture extends ProceduralTexture {
  176. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  177. super(name, size, "rock", scene, fallbackTexture, generateMipMaps);
  178. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  179. this.refreshRate = 0;
  180. }
  181. }
  182. export class RoadProceduralTexture extends ProceduralTexture {
  183. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  184. super(name, size, "road", scene, fallbackTexture, generateMipMaps);
  185. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  186. this.refreshRate = 0;
  187. }
  188. }
  189. export class BrickProceduralTexture extends ProceduralTexture {
  190. private _numberOfBricksHeight: number = 15;
  191. private _numberOfBricksWidth: number = 5;
  192. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  193. super(name, size, "brick", scene, fallbackTexture, generateMipMaps);
  194. this.updateShaderUniforms();
  195. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  196. this.refreshRate = 0;
  197. }
  198. public updateShaderUniforms() {
  199. this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
  200. this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
  201. }
  202. public get numberOfBricksHeight(): number {
  203. return this._numberOfBricksHeight;
  204. }
  205. public set cloudColor(value: number) {
  206. this._numberOfBricksHeight = value;
  207. this.updateShaderUniforms();
  208. }
  209. public get numberOfBricksWidth(): number {
  210. return this._numberOfBricksWidth;
  211. }
  212. public set numberOfBricksWidth(value: number) {
  213. this._numberOfBricksHeight = value;
  214. this.updateShaderUniforms();
  215. }
  216. }
  217. export class MarbleProceduralTexture extends ProceduralTexture {
  218. private _numberOfBricksHeight: number = 3;
  219. private _numberOfBricksWidth: number = 3;
  220. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  221. super(name, size, "marble", scene, fallbackTexture, generateMipMaps);
  222. this.updateShaderUniforms();
  223. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  224. this.refreshRate = 0;
  225. }
  226. public updateShaderUniforms() {
  227. this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
  228. this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
  229. }
  230. public get numberOfBricksHeight(): number {
  231. return this._numberOfBricksHeight;
  232. }
  233. public set cloudColor(value: number) {
  234. this._numberOfBricksHeight = value;
  235. this.updateShaderUniforms();
  236. }
  237. public get numberOfBricksWidth(): number {
  238. return this._numberOfBricksWidth;
  239. }
  240. public set numberOfBricksWidth(value: number) {
  241. this._numberOfBricksHeight = value;
  242. this.updateShaderUniforms();
  243. }
  244. }
  245. }