babylon.fireProceduralTexture.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  15. FireProceduralTexture.prototype.updateShaderUniforms = function () {
  16. this.setFloat("time", this._time);
  17. this.setVector2("speed", this._speed);
  18. this.setColor3("c1", this._fireColors[0]);
  19. this.setColor3("c2", this._fireColors[1]);
  20. this.setColor3("c3", this._fireColors[2]);
  21. this.setColor3("c4", this._fireColors[3]);
  22. this.setColor3("c5", this._fireColors[4]);
  23. this.setColor3("c6", this._fireColors[5]);
  24. this.setFloat("alphaThreshold", this._alphaThreshold);
  25. };
  26. FireProceduralTexture.prototype.render = function (useCameraPostProcess) {
  27. if (this._autoGenerateTime) {
  28. this._time += this.getScene().getAnimationRatio() * 0.03;
  29. this.updateShaderUniforms();
  30. }
  31. _super.prototype.render.call(this, useCameraPostProcess);
  32. };
  33. Object.defineProperty(FireProceduralTexture, "PurpleFireColors", {
  34. get: function () {
  35. return [
  36. new BABYLON.Color3(0.5, 0.0, 1.0),
  37. new BABYLON.Color3(0.9, 0.0, 1.0),
  38. new BABYLON.Color3(0.2, 0.0, 1.0),
  39. new BABYLON.Color3(1.0, 0.9, 1.0),
  40. new BABYLON.Color3(0.1, 0.1, 1.0),
  41. new BABYLON.Color3(0.9, 0.9, 1.0)
  42. ];
  43. },
  44. enumerable: true,
  45. configurable: true
  46. });
  47. Object.defineProperty(FireProceduralTexture, "GreenFireColors", {
  48. get: function () {
  49. return [
  50. new BABYLON.Color3(0.5, 1.0, 0.0),
  51. new BABYLON.Color3(0.5, 1.0, 0.0),
  52. new BABYLON.Color3(0.3, 0.4, 0.0),
  53. new BABYLON.Color3(0.5, 1.0, 0.0),
  54. new BABYLON.Color3(0.2, 0.0, 0.0),
  55. new BABYLON.Color3(0.5, 1.0, 0.0)
  56. ];
  57. },
  58. enumerable: true,
  59. configurable: true
  60. });
  61. Object.defineProperty(FireProceduralTexture, "RedFireColors", {
  62. get: function () {
  63. return [
  64. new BABYLON.Color3(0.5, 0.0, 0.1),
  65. new BABYLON.Color3(0.9, 0.0, 0.0),
  66. new BABYLON.Color3(0.2, 0.0, 0.0),
  67. new BABYLON.Color3(1.0, 0.9, 0.0),
  68. new BABYLON.Color3(0.1, 0.1, 0.1),
  69. new BABYLON.Color3(0.9, 0.9, 0.9)
  70. ];
  71. },
  72. enumerable: true,
  73. configurable: true
  74. });
  75. Object.defineProperty(FireProceduralTexture, "BlueFireColors", {
  76. get: function () {
  77. return [
  78. new BABYLON.Color3(0.1, 0.0, 0.5),
  79. new BABYLON.Color3(0.0, 0.0, 0.5),
  80. new BABYLON.Color3(0.1, 0.0, 0.2),
  81. new BABYLON.Color3(0.0, 0.0, 1.0),
  82. new BABYLON.Color3(0.1, 0.2, 0.3),
  83. new BABYLON.Color3(0.0, 0.2, 0.9)
  84. ];
  85. },
  86. enumerable: true,
  87. configurable: true
  88. });
  89. Object.defineProperty(FireProceduralTexture.prototype, "fireColors", {
  90. get: function () {
  91. return this._fireColors;
  92. },
  93. set: function (value) {
  94. this._fireColors = value;
  95. this.updateShaderUniforms();
  96. },
  97. enumerable: true,
  98. configurable: true
  99. });
  100. Object.defineProperty(FireProceduralTexture.prototype, "time", {
  101. get: function () {
  102. return this._time;
  103. },
  104. set: function (value) {
  105. this._time = value;
  106. this.updateShaderUniforms();
  107. },
  108. enumerable: true,
  109. configurable: true
  110. });
  111. Object.defineProperty(FireProceduralTexture.prototype, "speed", {
  112. get: function () {
  113. return this._speed;
  114. },
  115. set: function (value) {
  116. this._speed = value;
  117. this.updateShaderUniforms();
  118. },
  119. enumerable: true,
  120. configurable: true
  121. });
  122. Object.defineProperty(FireProceduralTexture.prototype, "alphaThreshold", {
  123. get: function () {
  124. return this._alphaThreshold;
  125. },
  126. set: function (value) {
  127. this._alphaThreshold = value;
  128. this.updateShaderUniforms();
  129. },
  130. enumerable: true,
  131. configurable: true
  132. });
  133. return FireProceduralTexture;
  134. })(BABYLON.ProceduralTexture);
  135. BABYLON.FireProceduralTexture = FireProceduralTexture;
  136. })(BABYLON || (BABYLON = {}));
  137. 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}";