babylon.fireMaterial.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. 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 extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  16. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  17. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  18. 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;
  19. return c > 3 && r && Object.defineProperty(target, key, r), r;
  20. };
  21. var BABYLON;
  22. (function (BABYLON) {
  23. var FireMaterialDefines = /** @class */ (function (_super) {
  24. __extends(FireMaterialDefines, _super);
  25. function FireMaterialDefines() {
  26. var _this = _super.call(this) || this;
  27. _this.DIFFUSE = false;
  28. _this.CLIPPLANE = false;
  29. _this.CLIPPLANE2 = false;
  30. _this.CLIPPLANE3 = false;
  31. _this.CLIPPLANE4 = false;
  32. _this.ALPHATEST = false;
  33. _this.DEPTHPREPASS = false;
  34. _this.POINTSIZE = false;
  35. _this.FOG = false;
  36. _this.UV1 = false;
  37. _this.VERTEXCOLOR = false;
  38. _this.VERTEXALPHA = false;
  39. _this.BonesPerMesh = 0;
  40. _this.NUM_BONE_INFLUENCERS = 0;
  41. _this.INSTANCES = false;
  42. _this.rebuild();
  43. return _this;
  44. }
  45. return FireMaterialDefines;
  46. }(BABYLON.MaterialDefines));
  47. var FireMaterial = /** @class */ (function (_super) {
  48. __extends(FireMaterial, _super);
  49. function FireMaterial(name, scene) {
  50. var _this = _super.call(this, name, scene) || this;
  51. _this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  52. _this.speed = 1.0;
  53. _this._scaledDiffuse = new BABYLON.Color3();
  54. _this._lastTime = 0;
  55. return _this;
  56. }
  57. FireMaterial.prototype.needAlphaBlending = function () {
  58. return false;
  59. };
  60. FireMaterial.prototype.needAlphaTesting = function () {
  61. return true;
  62. };
  63. FireMaterial.prototype.getAlphaTestTexture = function () {
  64. return null;
  65. };
  66. // Methods
  67. FireMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  68. if (this.isFrozen) {
  69. if (this._wasPreviouslyReady && subMesh.effect) {
  70. return true;
  71. }
  72. }
  73. if (!subMesh._materialDefines) {
  74. subMesh._materialDefines = new FireMaterialDefines();
  75. }
  76. var defines = subMesh._materialDefines;
  77. var scene = this.getScene();
  78. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  79. if (this._renderId === scene.getRenderId()) {
  80. return true;
  81. }
  82. }
  83. var engine = scene.getEngine();
  84. // Textures
  85. if (defines._areTexturesDirty) {
  86. defines._needUVs = false;
  87. if (this._diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  88. if (!this._diffuseTexture.isReady()) {
  89. return false;
  90. }
  91. else {
  92. defines._needUVs = true;
  93. defines.DIFFUSE = true;
  94. }
  95. }
  96. }
  97. defines.ALPHATEST = this._opacityTexture ? true : false;
  98. // Misc.
  99. if (defines._areMiscDirty) {
  100. defines.POINTSIZE = (this.pointsCloud || scene.forcePointsCloud);
  101. defines.FOG = (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled);
  102. }
  103. // Values that need to be evaluated on every frame
  104. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  105. // Attribs
  106. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  107. // Get correct effect
  108. if (defines.isDirty) {
  109. defines.markAsProcessed();
  110. scene.resetCachedMaterial();
  111. // Fallbacks
  112. var fallbacks = new BABYLON.EffectFallbacks();
  113. if (defines.FOG) {
  114. fallbacks.addFallback(1, "FOG");
  115. }
  116. if (defines.NUM_BONE_INFLUENCERS > 0) {
  117. fallbacks.addCPUSkinningFallback(0, mesh);
  118. }
  119. //Attributes
  120. var attribs = [BABYLON.VertexBuffer.PositionKind];
  121. if (defines.UV1) {
  122. attribs.push(BABYLON.VertexBuffer.UVKind);
  123. }
  124. if (defines.VERTEXCOLOR) {
  125. attribs.push(BABYLON.VertexBuffer.ColorKind);
  126. }
  127. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  128. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  129. // Legacy browser patch
  130. var shaderName = "fire";
  131. var join = defines.toString();
  132. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  133. attributes: attribs,
  134. uniformsNames: ["world", "view", "viewProjection", "vEyePosition",
  135. "vFogInfos", "vFogColor", "pointSize",
  136. "vDiffuseInfos",
  137. "mBones",
  138. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "diffuseMatrix",
  139. // Fire
  140. "time", "speed"
  141. ],
  142. uniformBuffersNames: [],
  143. samplers: ["diffuseSampler",
  144. // Fire
  145. "distortionSampler", "opacitySampler"
  146. ],
  147. defines: join,
  148. fallbacks: fallbacks,
  149. onCompiled: this.onCompiled,
  150. onError: this.onError,
  151. indexParameters: null,
  152. maxSimultaneousLights: 4,
  153. transformFeedbackVaryings: null
  154. }, engine), defines);
  155. }
  156. if (!subMesh.effect || !subMesh.effect.isReady()) {
  157. return false;
  158. }
  159. this._renderId = scene.getRenderId();
  160. this._wasPreviouslyReady = true;
  161. return true;
  162. };
  163. FireMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  164. var scene = this.getScene();
  165. var defines = subMesh._materialDefines;
  166. if (!defines) {
  167. return;
  168. }
  169. var effect = subMesh.effect;
  170. if (!effect) {
  171. return;
  172. }
  173. this._activeEffect = effect;
  174. // Matrices
  175. this.bindOnlyWorldMatrix(world);
  176. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  177. // Bones
  178. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  179. if (this._mustRebind(scene, effect)) {
  180. // Textures
  181. if (this._diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  182. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  183. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  184. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  185. this._activeEffect.setTexture("distortionSampler", this._distortionTexture);
  186. this._activeEffect.setTexture("opacitySampler", this._opacityTexture);
  187. }
  188. // Clip plane
  189. BABYLON.MaterialHelper.BindClipPlane(this._activeEffect, scene);
  190. // Point size
  191. if (this.pointsCloud) {
  192. this._activeEffect.setFloat("pointSize", this.pointSize);
  193. }
  194. BABYLON.MaterialHelper.BindEyePosition(effect, scene);
  195. }
  196. this._activeEffect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  197. // View
  198. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  199. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  200. }
  201. // Fog
  202. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  203. // Time
  204. this._lastTime += scene.getEngine().getDeltaTime();
  205. this._activeEffect.setFloat("time", this._lastTime);
  206. // Speed
  207. this._activeEffect.setFloat("speed", this.speed);
  208. this._afterBind(mesh, this._activeEffect);
  209. };
  210. FireMaterial.prototype.getAnimatables = function () {
  211. var results = [];
  212. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  213. results.push(this._diffuseTexture);
  214. }
  215. if (this._distortionTexture && this._distortionTexture.animations && this._distortionTexture.animations.length > 0) {
  216. results.push(this._distortionTexture);
  217. }
  218. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  219. results.push(this._opacityTexture);
  220. }
  221. return results;
  222. };
  223. FireMaterial.prototype.getActiveTextures = function () {
  224. var activeTextures = _super.prototype.getActiveTextures.call(this);
  225. if (this._diffuseTexture) {
  226. activeTextures.push(this._diffuseTexture);
  227. }
  228. if (this._distortionTexture) {
  229. activeTextures.push(this._distortionTexture);
  230. }
  231. if (this._opacityTexture) {
  232. activeTextures.push(this._opacityTexture);
  233. }
  234. return activeTextures;
  235. };
  236. FireMaterial.prototype.hasTexture = function (texture) {
  237. if (_super.prototype.hasTexture.call(this, texture)) {
  238. return true;
  239. }
  240. if (this._diffuseTexture === texture) {
  241. return true;
  242. }
  243. if (this._distortionTexture === texture) {
  244. return true;
  245. }
  246. if (this._opacityTexture === texture) {
  247. return true;
  248. }
  249. return false;
  250. };
  251. FireMaterial.prototype.getClassName = function () {
  252. return "FireMaterial";
  253. };
  254. FireMaterial.prototype.dispose = function (forceDisposeEffect) {
  255. if (this._diffuseTexture) {
  256. this._diffuseTexture.dispose();
  257. }
  258. if (this._distortionTexture) {
  259. this._distortionTexture.dispose();
  260. }
  261. _super.prototype.dispose.call(this, forceDisposeEffect);
  262. };
  263. FireMaterial.prototype.clone = function (name) {
  264. var _this = this;
  265. return BABYLON.SerializationHelper.Clone(function () { return new FireMaterial(name, _this.getScene()); }, this);
  266. };
  267. FireMaterial.prototype.serialize = function () {
  268. var serializationObject = _super.prototype.serialize.call(this);
  269. serializationObject.customType = "BABYLON.FireMaterial";
  270. serializationObject.diffuseColor = this.diffuseColor.asArray();
  271. serializationObject.speed = this.speed;
  272. if (this._diffuseTexture) {
  273. serializationObject._diffuseTexture = this._diffuseTexture.serialize();
  274. }
  275. if (this._distortionTexture) {
  276. serializationObject._distortionTexture = this._distortionTexture.serialize();
  277. }
  278. if (this._opacityTexture) {
  279. serializationObject._opacityTexture = this._opacityTexture.serialize();
  280. }
  281. return serializationObject;
  282. };
  283. FireMaterial.Parse = function (source, scene, rootUrl) {
  284. var material = new FireMaterial(source.name, scene);
  285. material.diffuseColor = BABYLON.Color3.FromArray(source.diffuseColor);
  286. material.speed = source.speed;
  287. material.alpha = source.alpha;
  288. material.id = source.id;
  289. BABYLON.Tags.AddTagsTo(material, source.tags);
  290. material.backFaceCulling = source.backFaceCulling;
  291. material.wireframe = source.wireframe;
  292. if (source._diffuseTexture) {
  293. material._diffuseTexture = BABYLON.Texture.Parse(source._diffuseTexture, scene, rootUrl);
  294. }
  295. if (source._distortionTexture) {
  296. material._distortionTexture = BABYLON.Texture.Parse(source._distortionTexture, scene, rootUrl);
  297. }
  298. if (source._opacityTexture) {
  299. material._opacityTexture = BABYLON.Texture.Parse(source._opacityTexture, scene, rootUrl);
  300. }
  301. if (source.checkReadyOnlyOnce) {
  302. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  303. }
  304. return material;
  305. };
  306. __decorate([
  307. BABYLON.serializeAsTexture("diffuseTexture")
  308. ], FireMaterial.prototype, "_diffuseTexture", void 0);
  309. __decorate([
  310. BABYLON.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  311. ], FireMaterial.prototype, "diffuseTexture", void 0);
  312. __decorate([
  313. BABYLON.serializeAsTexture("distortionTexture")
  314. ], FireMaterial.prototype, "_distortionTexture", void 0);
  315. __decorate([
  316. BABYLON.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  317. ], FireMaterial.prototype, "distortionTexture", void 0);
  318. __decorate([
  319. BABYLON.serializeAsTexture("opacityTexture")
  320. ], FireMaterial.prototype, "_opacityTexture", void 0);
  321. __decorate([
  322. BABYLON.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  323. ], FireMaterial.prototype, "opacityTexture", void 0);
  324. __decorate([
  325. BABYLON.serializeAsColor3("diffuse")
  326. ], FireMaterial.prototype, "diffuseColor", void 0);
  327. __decorate([
  328. BABYLON.serialize()
  329. ], FireMaterial.prototype, "speed", void 0);
  330. return FireMaterial;
  331. }(BABYLON.PushMaterial));
  332. BABYLON.FireMaterial = FireMaterial;
  333. })(BABYLON || (BABYLON = {}));
  334. //# sourceMappingURL=babylon.fireMaterial.js.map
  335. BABYLON.Effect.ShadersStore['fireVertexShader'] = "precision highp float;\n\nattribute vec3 position;\n#ifdef UV1\nattribute vec2 uv;\n#endif\n#ifdef UV2\nattribute vec2 uv2;\n#endif\n#ifdef VERTEXCOLOR\nattribute vec4 color;\n#endif\n#include<bonesDeclaration>\n\n#include<instancesDeclaration>\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\n#endif\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n\nuniform float time;\nuniform float speed;\n#ifdef DIFFUSE\nvarying vec2 vDistortionCoords1;\nvarying vec2 vDistortionCoords2;\nvarying vec2 vDistortionCoords3;\n#endif\nvoid main(void) {\n#include<instancesVertex>\n#include<bonesVertex>\ngl_Position=viewProjection*finalWorld*vec4(position,1.0);\nvec4 worldPos=finalWorld*vec4(position,1.0);\nvPositionW=vec3(worldPos);\n\n#ifdef DIFFUSE\nvDiffuseUV=uv;\nvDiffuseUV.y-=0.2;\n#endif\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n#ifdef DIFFUSE\n\nvec3 layerSpeed=vec3(-0.2,-0.52,-0.1)*speed;\nvDistortionCoords1.x=uv.x;\nvDistortionCoords1.y=uv.y+layerSpeed.x*time/1000.0;\nvDistortionCoords2.x=uv.x;\nvDistortionCoords2.y=uv.y+layerSpeed.y*time/1000.0;\nvDistortionCoords3.x=uv.x;\nvDistortionCoords3.y=uv.y+layerSpeed.z*time/1000.0;\n#endif\n}\n";
  336. BABYLON.Effect.ShadersStore['firePixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\n\nvarying vec3 vPositionW;\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n\nuniform sampler2D distortionSampler;\nuniform sampler2D opacitySampler;\n#ifdef DIFFUSE\nvarying vec2 vDistortionCoords1;\nvarying vec2 vDistortionCoords2;\nvarying vec2 vDistortionCoords3;\n#endif\n#include<clipPlaneFragmentDeclaration>\n\n#include<fogFragmentDeclaration>\nvec4 bx2(vec4 x)\n{\nreturn vec4(2.0)*x-vec4(1.0);\n}\nvoid main(void) {\n\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\nvec4 baseColor=vec4(1.,1.,1.,1.);\n\nfloat alpha=1.0;\n#ifdef DIFFUSE\n\nconst float distortionAmount0=0.092;\nconst float distortionAmount1=0.092;\nconst float distortionAmount2=0.092;\nvec2 heightAttenuation=vec2(0.3,0.39);\nvec4 noise0=texture2D(distortionSampler,vDistortionCoords1);\nvec4 noise1=texture2D(distortionSampler,vDistortionCoords2);\nvec4 noise2=texture2D(distortionSampler,vDistortionCoords3);\nvec4 noiseSum=bx2(noise0)*distortionAmount0+bx2(noise1)*distortionAmount1+bx2(noise2)*distortionAmount2;\nvec4 perturbedBaseCoords=vec4(vDiffuseUV,0.0,1.0)+noiseSum*(vDiffuseUV.y*heightAttenuation.x+heightAttenuation.y);\nvec4 opacityColor=texture2D(opacitySampler,perturbedBaseCoords.xy);\n#ifdef ALPHATEST\nif (opacityColor.r<0.1)\ndiscard;\n#endif\n#include<depthPrePass>\nbaseColor=texture2D(diffuseSampler,perturbedBaseCoords.xy)*2.0;\nbaseColor*=opacityColor;\nbaseColor.rgb*=vDiffuseInfos.y;\n#endif\n#ifdef VERTEXCOLOR\nbaseColor.rgb*=vColor.rgb;\n#endif\n\nvec3 diffuseBase=vec3(1.0,1.0,1.0);\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\n\nvec4 color=vec4(baseColor.rgb,alpha);\n#include<fogFragment>\ngl_FragColor=color;\n}";