babylon.fireProceduralTexture.js 6.8 KB

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