babylon.fireProceduralTexture.js 7.1 KB

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