babylon.fireProceduralTexture.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, "autoGenerateTime", {
  108. get: function () {
  109. return this._autoGenerateTime;
  110. },
  111. set: function (value) {
  112. this._autoGenerateTime = value;
  113. },
  114. enumerable: true,
  115. configurable: true
  116. });
  117. Object.defineProperty(FireProceduralTexture.prototype, "fireColors", {
  118. get: function () {
  119. return this._fireColors;
  120. },
  121. set: function (value) {
  122. this._fireColors = value;
  123. this.updateShaderUniforms();
  124. },
  125. enumerable: true,
  126. configurable: true
  127. });
  128. Object.defineProperty(FireProceduralTexture.prototype, "time", {
  129. get: function () {
  130. return this._time;
  131. },
  132. set: function (value) {
  133. this._time = value;
  134. this.updateShaderUniforms();
  135. },
  136. enumerable: true,
  137. configurable: true
  138. });
  139. Object.defineProperty(FireProceduralTexture.prototype, "speed", {
  140. get: function () {
  141. return this._speed;
  142. },
  143. set: function (value) {
  144. this._speed = value;
  145. this.updateShaderUniforms();
  146. },
  147. enumerable: true,
  148. configurable: true
  149. });
  150. Object.defineProperty(FireProceduralTexture.prototype, "alphaThreshold", {
  151. get: function () {
  152. return this._alphaThreshold;
  153. },
  154. set: function (value) {
  155. this._alphaThreshold = value;
  156. this.updateShaderUniforms();
  157. },
  158. enumerable: true,
  159. configurable: true
  160. });
  161. /**
  162. * Serializes this fire procedural texture
  163. * @returns a serialized fire procedural texture object
  164. */
  165. FireProceduralTexture.prototype.serialize = function () {
  166. var serializationObject = BABYLON.SerializationHelper.Serialize(this, _super.prototype.serialize.call(this));
  167. serializationObject.customType = "BABYLON.FireProceduralTexture";
  168. serializationObject.fireColors = [];
  169. for (var i = 0; i < this._fireColors.length; i++) {
  170. serializationObject.fireColors.push(this._fireColors[i].asArray());
  171. }
  172. return serializationObject;
  173. };
  174. /**
  175. * Creates a Fire Procedural Texture from parsed fire procedural texture data
  176. * @param parsedTexture defines parsed texture data
  177. * @param scene defines the current scene
  178. * @param rootUrl defines the root URL containing fire procedural texture information
  179. * @returns a parsed Fire Procedural Texture
  180. */
  181. FireProceduralTexture.Parse = function (parsedTexture, scene, rootUrl) {
  182. var texture = BABYLON.SerializationHelper.Parse(function () { return new FireProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps); }, parsedTexture, scene, rootUrl);
  183. var colors = [];
  184. for (var i = 0; i < parsedTexture.fireColors.length; i++) {
  185. colors.push(BABYLON.Color3.FromArray(parsedTexture.fireColors[i]));
  186. }
  187. texture.fireColors = colors;
  188. return texture;
  189. };
  190. __decorate([
  191. BABYLON.serialize()
  192. ], FireProceduralTexture.prototype, "autoGenerateTime", null);
  193. __decorate([
  194. BABYLON.serialize()
  195. ], FireProceduralTexture.prototype, "time", null);
  196. __decorate([
  197. BABYLON.serializeAsVector2()
  198. ], FireProceduralTexture.prototype, "speed", null);
  199. __decorate([
  200. BABYLON.serialize()
  201. ], FireProceduralTexture.prototype, "alphaThreshold", null);
  202. return FireProceduralTexture;
  203. }(BABYLON.ProceduralTexture));
  204. BABYLON.FireProceduralTexture = FireProceduralTexture;
  205. })(BABYLON || (BABYLON = {}));
  206. //# sourceMappingURL=babylon.fireProceduralTexture.js.map
  207. 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}";