babylon.fireMaterial.js 18 KB

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