babylon.fireProceduralTexture.js 7.1 KB

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