babylon.fireMaterial.js 18 KB

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