babylon.standardProceduralTexture.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. module BABYLON {
  2. export class WoodProceduralTexture extends ProceduralTexture {
  3. private _ampScale: number = 0.03;
  4. private _ringScale: number = 5;
  5. private _woodColor1: BABYLON.Color3 = new BABYLON.Color3(0.80, 0.55, 0.01);
  6. private _woodColor2: BABYLON.Color3 = new BABYLON.Color3(0.60, 0.41, 0.0);
  7. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  8. super(name, size, "wood", scene, fallbackTexture, generateMipMaps);
  9. this.updateShaderUniforms();
  10. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  11. this.refreshRate = 0;
  12. }
  13. public updateShaderUniforms() {
  14. this.setFloat("ampScale", this._ampScale);
  15. this.setFloat("ringScale", this._ringScale);
  16. this.setColor3("woodColor1", this._woodColor1);
  17. this.setColor3("woodColor2", this._woodColor2);
  18. }
  19. public get ampScale(): number {
  20. return this._ampScale;
  21. }
  22. public set ampScale(value: number) {
  23. this._ampScale = value;
  24. this.updateShaderUniforms();
  25. }
  26. public get ringScale(): number {
  27. return this._ringScale;
  28. }
  29. public set ringScale(value: number) {
  30. this._ringScale = value;
  31. this.updateShaderUniforms();
  32. }
  33. public get woodColor1(): BABYLON.Color3 {
  34. return this._woodColor1;
  35. }
  36. public set woodColor1(value: BABYLON.Color3) {
  37. this._woodColor1 = value;
  38. this.updateShaderUniforms();
  39. }
  40. public get woodColor2(): BABYLON.Color3 {
  41. return this._woodColor2;
  42. }
  43. public set woodColor2(value: BABYLON.Color3) {
  44. this._woodColor2 = value;
  45. this.updateShaderUniforms();
  46. }
  47. }
  48. export class FireProceduralTexture extends ProceduralTexture {
  49. private _time: number = 0.0;
  50. private _speed: BABYLON.Vector2 = new BABYLON.Vector2(0.5, 0.3);
  51. private _shift: number = 1.6;
  52. private _alpha: number = 1.0;
  53. private _autoGenerateTime: boolean = true;
  54. private _fireColors: number[][];
  55. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  56. super(name, size, "fire", scene, fallbackTexture, generateMipMaps);
  57. this._fireColors = FireProceduralTexture.RedFireColors;
  58. this.updateShaderUniforms();
  59. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  60. this.refreshRate = 1;
  61. }
  62. public updateShaderUniforms() {
  63. this.setFloat("iGlobalTime", this._time);
  64. this.setVector2("speed", this._speed);
  65. this.setFloat("shift", this._shift);
  66. this.setFloat("alpha", this._alpha);
  67. this.setColor3("c1", new BABYLON.Color3(this._fireColors[0][0], this._fireColors[0][1], this._fireColors[0][2]));
  68. this.setColor3("c2", new BABYLON.Color3(this._fireColors[1][0], this._fireColors[1][1], this._fireColors[1][2]));
  69. this.setColor3("c3", new BABYLON.Color3(this._fireColors[2][0], this._fireColors[2][1], this._fireColors[2][2]));
  70. this.setColor3("c4", new BABYLON.Color3(this._fireColors[3][0], this._fireColors[3][1], this._fireColors[3][2]));
  71. this.setColor3("c5", new BABYLON.Color3(this._fireColors[4][0], this._fireColors[4][1], this._fireColors[4][2]));
  72. this.setColor3("c6", new BABYLON.Color3(this._fireColors[5][0], this._fireColors[5][1], this._fireColors[5][2]));
  73. }
  74. public render(useCameraPostProcess?: boolean) {
  75. if (this._autoGenerateTime) {
  76. this._time += this.getScene().getAnimationRatio() * 0.03;
  77. this.updateShaderUniforms();
  78. }
  79. super.render(useCameraPostProcess);
  80. }
  81. public static get PurpleFireColors(): number[][] {
  82. return [
  83. [0.5, 0.0, 1.0],
  84. [0.9, 0.0, 1.0],
  85. [0.2, 0.0, 1.0],
  86. [1.0, 0.9, 1.0],
  87. [0.1, 0.1, 1.0],
  88. [0.9, 0.9, 1.0]
  89. ];
  90. }
  91. public static get GreenFireColors(): number[][] {
  92. return [
  93. [0.5, 1.0, 0.0],
  94. [0.5, 1.0, 0.0],
  95. [0.3, 0.4, 0.0],
  96. [0.5, 1.0, 0.0],
  97. [0.2, 0.0, 0.0],
  98. [0.5, 1.0, 0.0]
  99. ];
  100. }
  101. public static get RedFireColors(): number[][] {
  102. return [
  103. [0.5, 0.0, 0.1],
  104. [0.9, 0.0, 0.0],
  105. [0.2, 0.0, 0.0],
  106. [1.0, 0.9, 0.0],
  107. [0.1, 0.1, 0.1],
  108. [0.9, 0.9, 0.9]
  109. ];
  110. }
  111. public static get BlueFireColors(): number[][] {
  112. return [
  113. [0.1, 0.0, 0.5],
  114. [0.0, 0.0, 0.5],
  115. [0.1, 0.0, 0.2],
  116. [0.0, 0.0, 1.0],
  117. [0.1, 0.2, 0.3],
  118. [0.0, 0.2, 0.9]
  119. ];
  120. }
  121. public get fireColors(): number[][] {
  122. return this._fireColors;
  123. }
  124. public set fireColors(value: number[][]) {
  125. this._fireColors = value;
  126. this.updateShaderUniforms();
  127. }
  128. public get time(): number {
  129. return this._time;
  130. }
  131. public set time(value: number) {
  132. this._time = value;
  133. this.updateShaderUniforms();
  134. }
  135. public get speed(): BABYLON.Vector2 {
  136. return this._speed;
  137. }
  138. public set speed(value: BABYLON.Vector2) {
  139. this._speed = value;
  140. this.updateShaderUniforms();
  141. }
  142. public get shift(): number {
  143. return this._shift;
  144. }
  145. public set shift(value: number) {
  146. this._shift = value;
  147. this.updateShaderUniforms();
  148. }
  149. public get alpha(): number {
  150. return this._alpha;
  151. }
  152. public set alpha(value: number) {
  153. this._alpha = value;
  154. this.updateShaderUniforms();
  155. }
  156. }
  157. export class CloudProceduralTexture extends ProceduralTexture {
  158. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  159. super(name, size, "cloud", scene, fallbackTexture, generateMipMaps);
  160. // Use 0 to render just once, 1 to render on every frame, 2 to render every two frames and so on...
  161. this.refreshRate = 0;
  162. }
  163. }
  164. }