babylon.fireProceduralTexture.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = (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. if (this._autoGenerateTime) {
  39. this._time += this.getScene().getAnimationRatio() * 0.03;
  40. this.updateShaderUniforms();
  41. }
  42. _super.prototype.render.call(this, useCameraPostProcess);
  43. };
  44. Object.defineProperty(FireProceduralTexture, "PurpleFireColors", {
  45. get: function () {
  46. return [
  47. new BABYLON.Color3(0.5, 0.0, 1.0),
  48. new BABYLON.Color3(0.9, 0.0, 1.0),
  49. new BABYLON.Color3(0.2, 0.0, 1.0),
  50. new BABYLON.Color3(1.0, 0.9, 1.0),
  51. new BABYLON.Color3(0.1, 0.1, 1.0),
  52. new BABYLON.Color3(0.9, 0.9, 1.0)
  53. ];
  54. },
  55. enumerable: true,
  56. configurable: true
  57. });
  58. Object.defineProperty(FireProceduralTexture, "GreenFireColors", {
  59. get: function () {
  60. return [
  61. new BABYLON.Color3(0.5, 1.0, 0.0),
  62. new BABYLON.Color3(0.5, 1.0, 0.0),
  63. new BABYLON.Color3(0.3, 0.4, 0.0),
  64. new BABYLON.Color3(0.5, 1.0, 0.0),
  65. new BABYLON.Color3(0.2, 0.0, 0.0),
  66. new BABYLON.Color3(0.5, 1.0, 0.0)
  67. ];
  68. },
  69. enumerable: true,
  70. configurable: true
  71. });
  72. Object.defineProperty(FireProceduralTexture, "RedFireColors", {
  73. get: function () {
  74. return [
  75. new BABYLON.Color3(0.5, 0.0, 0.1),
  76. new BABYLON.Color3(0.9, 0.0, 0.0),
  77. new BABYLON.Color3(0.2, 0.0, 0.0),
  78. new BABYLON.Color3(1.0, 0.9, 0.0),
  79. new BABYLON.Color3(0.1, 0.1, 0.1),
  80. new BABYLON.Color3(0.9, 0.9, 0.9)
  81. ];
  82. },
  83. enumerable: true,
  84. configurable: true
  85. });
  86. Object.defineProperty(FireProceduralTexture, "BlueFireColors", {
  87. get: function () {
  88. return [
  89. new BABYLON.Color3(0.1, 0.0, 0.5),
  90. new BABYLON.Color3(0.0, 0.0, 0.5),
  91. new BABYLON.Color3(0.1, 0.0, 0.2),
  92. new BABYLON.Color3(0.0, 0.0, 1.0),
  93. new BABYLON.Color3(0.1, 0.2, 0.3),
  94. new BABYLON.Color3(0.0, 0.2, 0.9)
  95. ];
  96. },
  97. enumerable: true,
  98. configurable: true
  99. });
  100. Object.defineProperty(FireProceduralTexture.prototype, "fireColors", {
  101. get: function () {
  102. return this._fireColors;
  103. },
  104. set: function (value) {
  105. this._fireColors = value;
  106. this.updateShaderUniforms();
  107. },
  108. enumerable: true,
  109. configurable: true
  110. });
  111. Object.defineProperty(FireProceduralTexture.prototype, "time", {
  112. get: function () {
  113. return this._time;
  114. },
  115. set: function (value) {
  116. this._time = value;
  117. this.updateShaderUniforms();
  118. },
  119. enumerable: true,
  120. configurable: true
  121. });
  122. Object.defineProperty(FireProceduralTexture.prototype, "speed", {
  123. get: function () {
  124. return this._speed;
  125. },
  126. set: function (value) {
  127. this._speed = value;
  128. this.updateShaderUniforms();
  129. },
  130. enumerable: true,
  131. configurable: true
  132. });
  133. Object.defineProperty(FireProceduralTexture.prototype, "alphaThreshold", {
  134. get: function () {
  135. return this._alphaThreshold;
  136. },
  137. set: function (value) {
  138. this._alphaThreshold = value;
  139. this.updateShaderUniforms();
  140. },
  141. enumerable: true,
  142. configurable: true
  143. });
  144. return FireProceduralTexture;
  145. }(BABYLON.ProceduralTexture));
  146. BABYLON.FireProceduralTexture = FireProceduralTexture;
  147. })(BABYLON || (BABYLON = {}));
  148. //# sourceMappingURL=babylon.fireProceduralTexture.js.map
  149. 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}";