babylon.fireProceduralTexture.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var FireProceduralTexture = (function (_super) {
  5. __extends(FireProceduralTexture, _super);
  6. function FireProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  7. _super.call(this, name, size, "fireProceduralTexture", scene, fallbackTexture, generateMipMaps);
  8. this._time = 0.0;
  9. this._speed = new BABYLON.Vector2(0.5, 0.3);
  10. this._autoGenerateTime = true;
  11. this._alphaThreshold = 0.5;
  12. this._fireColors = FireProceduralTexture.RedFireColors;
  13. this.updateShaderUniforms();
  14. this.refreshRate = 1;
  15. }
  16. FireProceduralTexture.prototype.updateShaderUniforms = function () {
  17. this.setFloat("time", this._time);
  18. this.setVector2("speed", this._speed);
  19. this.setColor3("c1", this._fireColors[0]);
  20. this.setColor3("c2", this._fireColors[1]);
  21. this.setColor3("c3", this._fireColors[2]);
  22. this.setColor3("c4", this._fireColors[3]);
  23. this.setColor3("c5", this._fireColors[4]);
  24. this.setColor3("c6", this._fireColors[5]);
  25. this.setFloat("alphaThreshold", this._alphaThreshold);
  26. };
  27. FireProceduralTexture.prototype.render = function (useCameraPostProcess) {
  28. if (this._autoGenerateTime) {
  29. this._time += this.getScene().getAnimationRatio() * 0.03;
  30. this.updateShaderUniforms();
  31. }
  32. _super.prototype.render.call(this, useCameraPostProcess);
  33. };
  34. Object.defineProperty(FireProceduralTexture, "PurpleFireColors", {
  35. get: function () {
  36. return [
  37. new BABYLON.Color3(0.5, 0.0, 1.0),
  38. new BABYLON.Color3(0.9, 0.0, 1.0),
  39. new BABYLON.Color3(0.2, 0.0, 1.0),
  40. new BABYLON.Color3(1.0, 0.9, 1.0),
  41. new BABYLON.Color3(0.1, 0.1, 1.0),
  42. new BABYLON.Color3(0.9, 0.9, 1.0)
  43. ];
  44. },
  45. enumerable: true,
  46. configurable: true
  47. });
  48. Object.defineProperty(FireProceduralTexture, "GreenFireColors", {
  49. get: function () {
  50. return [
  51. new BABYLON.Color3(0.5, 1.0, 0.0),
  52. new BABYLON.Color3(0.5, 1.0, 0.0),
  53. new BABYLON.Color3(0.3, 0.4, 0.0),
  54. new BABYLON.Color3(0.5, 1.0, 0.0),
  55. new BABYLON.Color3(0.2, 0.0, 0.0),
  56. new BABYLON.Color3(0.5, 1.0, 0.0)
  57. ];
  58. },
  59. enumerable: true,
  60. configurable: true
  61. });
  62. Object.defineProperty(FireProceduralTexture, "RedFireColors", {
  63. get: function () {
  64. return [
  65. new BABYLON.Color3(0.5, 0.0, 0.1),
  66. new BABYLON.Color3(0.9, 0.0, 0.0),
  67. new BABYLON.Color3(0.2, 0.0, 0.0),
  68. new BABYLON.Color3(1.0, 0.9, 0.0),
  69. new BABYLON.Color3(0.1, 0.1, 0.1),
  70. new BABYLON.Color3(0.9, 0.9, 0.9)
  71. ];
  72. },
  73. enumerable: true,
  74. configurable: true
  75. });
  76. Object.defineProperty(FireProceduralTexture, "BlueFireColors", {
  77. get: function () {
  78. return [
  79. new BABYLON.Color3(0.1, 0.0, 0.5),
  80. new BABYLON.Color3(0.0, 0.0, 0.5),
  81. new BABYLON.Color3(0.1, 0.0, 0.2),
  82. new BABYLON.Color3(0.0, 0.0, 1.0),
  83. new BABYLON.Color3(0.1, 0.2, 0.3),
  84. new BABYLON.Color3(0.0, 0.2, 0.9)
  85. ];
  86. },
  87. enumerable: true,
  88. configurable: true
  89. });
  90. Object.defineProperty(FireProceduralTexture.prototype, "fireColors", {
  91. get: function () {
  92. return this._fireColors;
  93. },
  94. set: function (value) {
  95. this._fireColors = value;
  96. this.updateShaderUniforms();
  97. },
  98. enumerable: true,
  99. configurable: true
  100. });
  101. Object.defineProperty(FireProceduralTexture.prototype, "time", {
  102. get: function () {
  103. return this._time;
  104. },
  105. set: function (value) {
  106. this._time = value;
  107. this.updateShaderUniforms();
  108. },
  109. enumerable: true,
  110. configurable: true
  111. });
  112. Object.defineProperty(FireProceduralTexture.prototype, "speed", {
  113. get: function () {
  114. return this._speed;
  115. },
  116. set: function (value) {
  117. this._speed = value;
  118. this.updateShaderUniforms();
  119. },
  120. enumerable: true,
  121. configurable: true
  122. });
  123. Object.defineProperty(FireProceduralTexture.prototype, "alphaThreshold", {
  124. get: function () {
  125. return this._alphaThreshold;
  126. },
  127. set: function (value) {
  128. this._alphaThreshold = value;
  129. this.updateShaderUniforms();
  130. },
  131. enumerable: true,
  132. configurable: true
  133. });
  134. return FireProceduralTexture;
  135. })(BABYLON.ProceduralTexture);
  136. BABYLON.FireProceduralTexture = FireProceduralTexture;
  137. })(BABYLON || (BABYLON = {}));
  138. BABYLON.Effect.ShadersStore['fireProceduralTexturePixelShader'] = "precision highp float;\r\n\r\nuniform float time;\r\nuniform vec3 c1;\r\nuniform vec3 c2;\r\nuniform vec3 c3;\r\nuniform vec3 c4;\r\nuniform vec3 c5;\r\nuniform vec3 c6;\r\nuniform vec2 speed;\r\nuniform float shift;\r\nuniform float alphaThreshold;\r\n\r\nvarying vec2 vUV;\r\n\r\nfloat rand(vec2 n) {\r\n\treturn fract(cos(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);\r\n}\r\n\r\nfloat noise(vec2 n) {\r\n\tconst vec2 d = vec2(0.0, 1.0);\r\n\tvec2 b = floor(n), f = smoothstep(vec2(0.0), vec2(1.0), fract(n));\r\n\treturn mix(mix(rand(b), rand(b + d.yx), f.x), mix(rand(b + d.xy), rand(b + d.yy), f.x), f.y);\r\n}\r\n\r\nfloat fbm(vec2 n) {\r\n\tfloat total = 0.0, amplitude = 1.0;\r\n\tfor (int i = 0; i < 4; i++) {\r\n\t\ttotal += noise(n) * amplitude;\r\n\t\tn += n;\r\n\t\tamplitude *= 0.5;\r\n\t}\r\n\treturn total;\r\n}\r\n\r\nvoid main() {\r\n\tvec2 p = vUV * 8.0;\r\n\tfloat q = fbm(p - time * 0.1);\r\n\tvec2 r = vec2(fbm(p + q + time * speed.x - p.x - p.y), fbm(p + q - time * speed.y));\r\n\tvec3 c = mix(c1, c2, fbm(p + r)) + mix(c3, c4, r.x) - mix(c5, c6, r.y);\r\n\tvec3 color = c * cos(shift * vUV.y);\r\n\tfloat luminance = dot(color.rgb, vec3(0.3, 0.59, 0.11));\r\n\r\n\tgl_FragColor = vec4(color, luminance * alphaThreshold + (1.0 - alphaThreshold));\r\n}";