babylon.fireProceduralTexture.js 6.8 KB

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