babylon.standardProceduralTexture.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  145. this.refreshRate = 0;
  146. }
  147. public updateShaderUniforms() {
  148. this.setColor3("skyColor", this._skyColor);
  149. this.setColor3("cloudColor", this._cloudColor);
  150. }
  151. public get skyColor(): BABYLON.Color3 {
  152. return this._skyColor;
  153. }
  154. public set skyColor(value: BABYLON.Color3) {
  155. this._skyColor = value;
  156. this.updateShaderUniforms();
  157. }
  158. public get cloudColor(): BABYLON.Color3 {
  159. return this._cloudColor;
  160. }
  161. public set cloudColor(value: BABYLON.Color3) {
  162. this._cloudColor = value;
  163. this.updateShaderUniforms();
  164. }
  165. }
  166. export class GrassProceduralTexture extends ProceduralTexture {
  167. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  168. super(name, size, "grass", scene, fallbackTexture, generateMipMaps);
  169. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  170. this.refreshRate = 0;
  171. }
  172. }
  173. }