babylon.standardProceduralTexture.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. module BABYLON {
  2. export class WoodProceduralTexture extends ProceduralTexture {
  3. private _ampScale: number = 100.0;
  4. private _woodColor: Color3 = new 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. this.refreshRate = 0;
  9. }
  10. public updateShaderUniforms() {
  11. this.setFloat("ampScale", this._ampScale);
  12. this.setColor3("woodColor", this._woodColor);
  13. }
  14. public get ampScale(): number {
  15. return this._ampScale;
  16. }
  17. public set ampScale(value: number) {
  18. this._ampScale = value;
  19. this.updateShaderUniforms();
  20. }
  21. public get woodColor(): Color3 {
  22. return this._woodColor;
  23. }
  24. public set woodColor(value: Color3) {
  25. this._woodColor = value;
  26. this.updateShaderUniforms();
  27. }
  28. }
  29. export class FireProceduralTexture extends ProceduralTexture {
  30. private _time: number = 0.0;
  31. private _speed = new Vector2(0.5, 0.3);
  32. private _shift: number = 1.6;
  33. private _autoGenerateTime: boolean = true;
  34. private _fireColors: Color3[];
  35. private _alphaThreshold: number = 0.5;
  36. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  37. super(name, size, "fire", scene, fallbackTexture, generateMipMaps);
  38. this._fireColors = FireProceduralTexture.RedFireColors;
  39. this.updateShaderUniforms();
  40. this.refreshRate = 1;
  41. }
  42. public updateShaderUniforms() {
  43. this.setFloat("time", this._time);
  44. this.setVector2("speed", this._speed);
  45. this.setFloat("shift", this._shift);
  46. this.setColor3("c1", this._fireColors[0]);
  47. this.setColor3("c2", this._fireColors[1]);
  48. this.setColor3("c3", this._fireColors[2]);
  49. this.setColor3("c4", this._fireColors[3]);
  50. this.setColor3("c5", this._fireColors[4]);
  51. this.setColor3("c6", this._fireColors[5]);
  52. this.setFloat("alphaThreshold", this._alphaThreshold);
  53. }
  54. public render(useCameraPostProcess?: boolean) {
  55. if (this._autoGenerateTime) {
  56. this._time += this.getScene().getAnimationRatio() * 0.03;
  57. this.updateShaderUniforms();
  58. }
  59. super.render(useCameraPostProcess);
  60. }
  61. public static get PurpleFireColors(): Color3[] {
  62. return [
  63. new Color3(0.5, 0.0, 1.0),
  64. new Color3(0.9, 0.0, 1.0),
  65. new Color3(0.2, 0.0, 1.0),
  66. new Color3(1.0, 0.9, 1.0),
  67. new Color3(0.1, 0.1, 1.0),
  68. new Color3(0.9, 0.9, 1.0)
  69. ];
  70. }
  71. public static get GreenFireColors(): Color3[] {
  72. return [
  73. new Color3(0.5, 1.0, 0.0),
  74. new Color3(0.5, 1.0, 0.0),
  75. new Color3(0.3, 0.4, 0.0),
  76. new Color3(0.5, 1.0, 0.0),
  77. new Color3(0.2, 0.0, 0.0),
  78. new Color3(0.5, 1.0, 0.0)
  79. ];
  80. }
  81. public static get RedFireColors(): Color3[] {
  82. return [
  83. new Color3(0.5, 0.0, 0.1),
  84. new Color3(0.9, 0.0, 0.0),
  85. new Color3(0.2, 0.0, 0.0),
  86. new Color3(1.0, 0.9, 0.0),
  87. new Color3(0.1, 0.1, 0.1),
  88. new Color3(0.9, 0.9, 0.9)
  89. ];
  90. }
  91. public static get BlueFireColors(): Color3[] {
  92. return [
  93. new Color3(0.1, 0.0, 0.5),
  94. new Color3(0.0, 0.0, 0.5),
  95. new Color3(0.1, 0.0, 0.2),
  96. new Color3(0.0, 0.0, 1.0),
  97. new Color3(0.1, 0.2, 0.3),
  98. new Color3(0.0, 0.2, 0.9)
  99. ];
  100. }
  101. public get fireColors(): Color3[] {
  102. return this._fireColors;
  103. }
  104. public set fireColors(value: Color3[]) {
  105. this._fireColors = value;
  106. this.updateShaderUniforms();
  107. }
  108. public get time(): number {
  109. return this._time;
  110. }
  111. public set time(value: number) {
  112. this._time = value;
  113. this.updateShaderUniforms();
  114. }
  115. public get speed(): Vector2 {
  116. return this._speed;
  117. }
  118. public set speed(value: Vector2) {
  119. this._speed = value;
  120. this.updateShaderUniforms();
  121. }
  122. public get shift(): number {
  123. return this._shift;
  124. }
  125. public set shift(value: number) {
  126. this._shift = value;
  127. this.updateShaderUniforms();
  128. }
  129. public get alphaThreshold(): number {
  130. return this._alphaThreshold;
  131. }
  132. public set alphaThreshold(value: number) {
  133. this._alphaThreshold = value;
  134. this.updateShaderUniforms();
  135. }
  136. }
  137. export class CloudProceduralTexture extends ProceduralTexture {
  138. private _skyColor = new Color3(0.15, 0.68, 1.0);
  139. private _cloudColor = new Color3(1, 1, 1);
  140. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  141. super(name, size, "cloud", scene, fallbackTexture, generateMipMaps);
  142. this.updateShaderUniforms();
  143. this.refreshRate = 0;
  144. }
  145. public updateShaderUniforms() {
  146. this.setColor3("skyColor", this._skyColor);
  147. this.setColor3("cloudColor", this._cloudColor);
  148. }
  149. public get skyColor(): Color3 {
  150. return this._skyColor;
  151. }
  152. public set skyColor(value: Color3) {
  153. this._skyColor = value;
  154. this.updateShaderUniforms();
  155. }
  156. public get cloudColor(): Color3 {
  157. return this._cloudColor;
  158. }
  159. public set cloudColor(value: Color3) {
  160. this._cloudColor = value;
  161. this.updateShaderUniforms();
  162. }
  163. }
  164. export class GrassProceduralTexture extends ProceduralTexture {
  165. private _grassColors: Color3[];
  166. private _herb1 = new Color3(0.29, 0.38, 0.02);
  167. private _herb2 = new Color3(0.36, 0.49, 0.09);
  168. private _herb3 = new Color3(0.51, 0.6, 0.28);
  169. private _groundColor = new Color3(1, 1, 1);
  170. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  171. super(name, size, "grass", scene, fallbackTexture, generateMipMaps);
  172. this._grassColors = [
  173. new Color3(0.29, 0.38, 0.02),
  174. new Color3(0.36, 0.49, 0.09),
  175. new Color3(0.51, 0.6, 0.28)
  176. ];
  177. this.updateShaderUniforms();
  178. this.refreshRate = 0;
  179. }
  180. public updateShaderUniforms() {
  181. this.setColor3("herb1Color", this._grassColors[0]);
  182. this.setColor3("herb2Color", this._grassColors[1]);
  183. this.setColor3("herb3Color", this._grassColors[2]);
  184. this.setColor3("groundColor", this._groundColor);
  185. }
  186. public get grassColors(): Color3[] {
  187. return this._grassColors;
  188. }
  189. public set grassColors(value: Color3[]) {
  190. this._grassColors = value;
  191. this.updateShaderUniforms();
  192. }
  193. public get groundColor(): Color3 {
  194. return this._groundColor;
  195. }
  196. public set groundColor(value: Color3) {
  197. this.groundColor = value;
  198. this.updateShaderUniforms();
  199. }
  200. }
  201. export class RoadProceduralTexture extends ProceduralTexture {
  202. private _roadColor = new Color3(0.53, 0.53, 0.53);
  203. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  204. super(name, size, "road", scene, fallbackTexture, generateMipMaps);
  205. this.updateShaderUniforms();
  206. this.refreshRate = 0;
  207. }
  208. public updateShaderUniforms() {
  209. this.setColor3("roadColor", this._roadColor);
  210. }
  211. public get roadColor(): Color3 {
  212. return this._roadColor;
  213. }
  214. public set roadColor(value: Color3) {
  215. this._roadColor = value;
  216. this.updateShaderUniforms();
  217. }
  218. }
  219. export class BrickProceduralTexture extends ProceduralTexture {
  220. private _numberOfBricksHeight: number = 15;
  221. private _numberOfBricksWidth: number = 5;
  222. private _jointColor = new Color3(0.72, 0.72, 0.72);
  223. private _brickColor = new Color3(0.77, 0.47, 0.40);
  224. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  225. super(name, size, "brick", scene, fallbackTexture, generateMipMaps);
  226. this.updateShaderUniforms();
  227. this.refreshRate = 0;
  228. }
  229. public updateShaderUniforms() {
  230. this.setFloat("numberOfBricksHeight", this._numberOfBricksHeight);
  231. this.setFloat("numberOfBricksWidth", this._numberOfBricksWidth);
  232. this.setColor3("brickColor", this._brickColor);
  233. this.setColor3("jointColor", this._jointColor);
  234. }
  235. public get numberOfBricksHeight(): number {
  236. return this._numberOfBricksHeight;
  237. }
  238. public set numberOfBricksHeight(value: number) {
  239. this._numberOfBricksHeight = value;
  240. this.updateShaderUniforms();
  241. }
  242. public get numberOfBricksWidth(): number {
  243. return this._numberOfBricksWidth;
  244. }
  245. public set numberOfBricksWidth(value: number) {
  246. this._numberOfBricksHeight = value;
  247. this.updateShaderUniforms();
  248. }
  249. public get jointColor(): Color3 {
  250. return this._jointColor;
  251. }
  252. public set jointColor(value: Color3) {
  253. this._jointColor = value;
  254. this.updateShaderUniforms();
  255. }
  256. public get brickColor(): Color3 {
  257. return this._brickColor;
  258. }
  259. public set brickColor(value: Color3) {
  260. this._brickColor = value;
  261. this.updateShaderUniforms();
  262. }
  263. }
  264. export class MarbleProceduralTexture extends ProceduralTexture {
  265. private _numberOfTilesHeight: number = 3;
  266. private _numberOfTilesWidth: number = 3;
  267. private _amplitude: number = 9.0;
  268. private _marbleColor = new Color3(0.77, 0.47, 0.40);
  269. private _jointColor = new Color3(0.72, 0.72, 0.72);
  270. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  271. super(name, size, "marble", scene, fallbackTexture, generateMipMaps);
  272. this.updateShaderUniforms();
  273. this.refreshRate = 0;
  274. }
  275. public updateShaderUniforms() {
  276. this.setFloat("numberOfTilesHeight", this._numberOfTilesHeight);
  277. this.setFloat("numberOfTilesWidth", this._numberOfTilesWidth);
  278. this.setFloat("amplitude", this._amplitude);
  279. this.setColor3("marbleColor", this._marbleColor);
  280. this.setColor3("jointColor", this._jointColor);
  281. }
  282. public get numberOfTilesHeight(): number {
  283. return this._numberOfTilesHeight;
  284. }
  285. public set numberOfTilesHeight(value: number) {
  286. this._numberOfTilesHeight = value;
  287. this.updateShaderUniforms();
  288. }
  289. public get numberOfTilesWidth(): number {
  290. return this._numberOfTilesWidth;
  291. }
  292. public set numberOfTilesWidth(value: number) {
  293. this._numberOfTilesWidth = value;
  294. this.updateShaderUniforms();
  295. }
  296. public get jointColor(): Color3 {
  297. return this._jointColor;
  298. }
  299. public set jointColor(value: Color3) {
  300. this._jointColor = value;
  301. this.updateShaderUniforms();
  302. }
  303. public get marbleColor(): Color3 {
  304. return this._marbleColor;
  305. }
  306. public set marbleColor(value: Color3) {
  307. this._marbleColor = value;
  308. this.updateShaderUniforms();
  309. }
  310. }
  311. }