babylon.fireProceduralTexture.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  13. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  14. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  15. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  16. return c > 3 && r && Object.defineProperty(target, key, r), r;
  17. };
  18. var BABYLON;
  19. (function (BABYLON) {
  20. var FireProceduralTexture = /** @class */ (function (_super) {
  21. __extends(FireProceduralTexture, _super);
  22. function FireProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  23. var _this = _super.call(this, name, size, "fireProceduralTexture", scene, fallbackTexture, generateMipMaps) || this;
  24. _this._time = 0.0;
  25. _this._speed = new BABYLON.Vector2(0.5, 0.3);
  26. _this._autoGenerateTime = true;
  27. _this._alphaThreshold = 0.5;
  28. _this._fireColors = FireProceduralTexture.RedFireColors;
  29. _this.updateShaderUniforms();
  30. return _this;
  31. }
  32. FireProceduralTexture.prototype.updateShaderUniforms = function () {
  33. this.setFloat("time", this._time);
  34. this.setVector2("speed", this._speed);
  35. this.setColor3("c1", this._fireColors[0]);
  36. this.setColor3("c2", this._fireColors[1]);
  37. this.setColor3("c3", this._fireColors[2]);
  38. this.setColor3("c4", this._fireColors[3]);
  39. this.setColor3("c5", this._fireColors[4]);
  40. this.setColor3("c6", this._fireColors[5]);
  41. this.setFloat("alphaThreshold", this._alphaThreshold);
  42. };
  43. FireProceduralTexture.prototype.render = function (useCameraPostProcess) {
  44. var scene = this.getScene();
  45. if (this._autoGenerateTime && scene) {
  46. this._time += scene.getAnimationRatio() * 0.03;
  47. this.updateShaderUniforms();
  48. }
  49. _super.prototype.render.call(this, useCameraPostProcess);
  50. };
  51. Object.defineProperty(FireProceduralTexture, "PurpleFireColors", {
  52. get: function () {
  53. return [
  54. new BABYLON.Color3(0.5, 0.0, 1.0),
  55. new BABYLON.Color3(0.9, 0.0, 1.0),
  56. new BABYLON.Color3(0.2, 0.0, 1.0),
  57. new BABYLON.Color3(1.0, 0.9, 1.0),
  58. new BABYLON.Color3(0.1, 0.1, 1.0),
  59. new BABYLON.Color3(0.9, 0.9, 1.0)
  60. ];
  61. },
  62. enumerable: true,
  63. configurable: true
  64. });
  65. Object.defineProperty(FireProceduralTexture, "GreenFireColors", {
  66. get: function () {
  67. return [
  68. new BABYLON.Color3(0.5, 1.0, 0.0),
  69. new BABYLON.Color3(0.5, 1.0, 0.0),
  70. new BABYLON.Color3(0.3, 0.4, 0.0),
  71. new BABYLON.Color3(0.5, 1.0, 0.0),
  72. new BABYLON.Color3(0.2, 0.0, 0.0),
  73. new BABYLON.Color3(0.5, 1.0, 0.0)
  74. ];
  75. },
  76. enumerable: true,
  77. configurable: true
  78. });
  79. Object.defineProperty(FireProceduralTexture, "RedFireColors", {
  80. get: function () {
  81. return [
  82. new BABYLON.Color3(0.5, 0.0, 0.1),
  83. new BABYLON.Color3(0.9, 0.0, 0.0),
  84. new BABYLON.Color3(0.2, 0.0, 0.0),
  85. new BABYLON.Color3(1.0, 0.9, 0.0),
  86. new BABYLON.Color3(0.1, 0.1, 0.1),
  87. new BABYLON.Color3(0.9, 0.9, 0.9)
  88. ];
  89. },
  90. enumerable: true,
  91. configurable: true
  92. });
  93. Object.defineProperty(FireProceduralTexture, "BlueFireColors", {
  94. get: function () {
  95. return [
  96. new BABYLON.Color3(0.1, 0.0, 0.5),
  97. new BABYLON.Color3(0.0, 0.0, 0.5),
  98. new BABYLON.Color3(0.1, 0.0, 0.2),
  99. new BABYLON.Color3(0.0, 0.0, 1.0),
  100. new BABYLON.Color3(0.1, 0.2, 0.3),
  101. new BABYLON.Color3(0.0, 0.2, 0.9)
  102. ];
  103. },
  104. enumerable: true,
  105. configurable: true
  106. });
  107. Object.defineProperty(FireProceduralTexture.prototype, "fireColors", {
  108. get: function () {
  109. return this._fireColors;
  110. },
  111. set: function (value) {
  112. this._fireColors = value;
  113. this.updateShaderUniforms();
  114. },
  115. enumerable: true,
  116. configurable: true
  117. });
  118. Object.defineProperty(FireProceduralTexture.prototype, "time", {
  119. get: function () {
  120. return this._time;
  121. },
  122. set: function (value) {
  123. this._time = value;
  124. this.updateShaderUniforms();
  125. },
  126. enumerable: true,
  127. configurable: true
  128. });
  129. Object.defineProperty(FireProceduralTexture.prototype, "speed", {
  130. get: function () {
  131. return this._speed;
  132. },
  133. set: function (value) {
  134. this._speed = value;
  135. this.updateShaderUniforms();
  136. },
  137. enumerable: true,
  138. configurable: true
  139. });
  140. Object.defineProperty(FireProceduralTexture.prototype, "alphaThreshold", {
  141. get: function () {
  142. return this._alphaThreshold;
  143. },
  144. set: function (value) {
  145. this._alphaThreshold = value;
  146. this.updateShaderUniforms();
  147. },
  148. enumerable: true,
  149. configurable: true
  150. });
  151. /**
  152. * Serializes this fire procedural texture
  153. * @returns a serialized fire procedural texture object
  154. */
  155. FireProceduralTexture.prototype.serialize = function () {
  156. var serializationObject = BABYLON.SerializationHelper.Serialize(this, _super.prototype.serialize.call(this));
  157. serializationObject.customType = "BABYLON.FireProceduralTexture";
  158. return serializationObject;
  159. };
  160. /**
  161. * Creates a Fire Procedural Texture from parsed fire procedural texture data
  162. * @param parsedTexture defines parsed texture data
  163. * @param scene defines the current scene
  164. * @param rootUrl defines the root URL containing fire procedural texture information
  165. * @returns a parsed Fire Procedural Texture
  166. */
  167. FireProceduralTexture.Parse = function (parsedTexture, scene, rootUrl) {
  168. var texture = BABYLON.SerializationHelper.Parse(function () { return new FireProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps); }, parsedTexture, scene, rootUrl);
  169. return texture;
  170. };
  171. __decorate([
  172. BABYLON.serializeAsColor3()
  173. ], FireProceduralTexture.prototype, "fireColors", null);
  174. __decorate([
  175. BABYLON.serialize()
  176. ], FireProceduralTexture.prototype, "time", null);
  177. __decorate([
  178. BABYLON.serializeAsVector2()
  179. ], FireProceduralTexture.prototype, "speed", null);
  180. __decorate([
  181. BABYLON.serialize()
  182. ], FireProceduralTexture.prototype, "alphaThreshold", null);
  183. return FireProceduralTexture;
  184. }(BABYLON.ProceduralTexture));
  185. BABYLON.FireProceduralTexture = FireProceduralTexture;
  186. })(BABYLON || (BABYLON = {}));
  187. //# sourceMappingURL=babylon.fireProceduralTexture.js.map
  188. 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}";