babylon.fireMaterial.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
  4. switch (arguments.length) {
  5. case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
  6. case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
  7. case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
  8. }
  9. };
  10. var BABYLON;
  11. (function (BABYLON) {
  12. var maxSimultaneousLights = 4;
  13. var FireMaterialDefines = (function (_super) {
  14. __extends(FireMaterialDefines, _super);
  15. function FireMaterialDefines() {
  16. _super.call(this);
  17. this.DIFFUSE = false;
  18. this.CLIPPLANE = false;
  19. this.ALPHATEST = false;
  20. this.POINTSIZE = false;
  21. this.FOG = false;
  22. this.UV1 = false;
  23. this.VERTEXCOLOR = false;
  24. this.VERTEXALPHA = false;
  25. this.BonesPerMesh = 0;
  26. this.NUM_BONE_INFLUENCERS = 0;
  27. this.INSTANCES = false;
  28. this._keys = Object.keys(this);
  29. }
  30. return FireMaterialDefines;
  31. })(BABYLON.MaterialDefines);
  32. var FireMaterial = (function (_super) {
  33. __extends(FireMaterial, _super);
  34. function FireMaterial(name, scene) {
  35. _super.call(this, name, scene);
  36. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  37. this.speed = 1.0;
  38. this._scaledDiffuse = new BABYLON.Color3();
  39. this._defines = new FireMaterialDefines();
  40. this._cachedDefines = new FireMaterialDefines();
  41. this._lastTime = 0;
  42. this._cachedDefines.BonesPerMesh = -1;
  43. }
  44. FireMaterial.prototype.needAlphaBlending = function () {
  45. return (this.alpha < 1.0);
  46. };
  47. FireMaterial.prototype.needAlphaTesting = function () {
  48. return false;
  49. };
  50. FireMaterial.prototype.getAlphaTestTexture = function () {
  51. return null;
  52. };
  53. // Methods
  54. FireMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  55. if (!mesh) {
  56. return true;
  57. }
  58. if (this._defines.INSTANCES !== useInstances) {
  59. return false;
  60. }
  61. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  62. return true;
  63. }
  64. return false;
  65. };
  66. FireMaterial.prototype.isReady = function (mesh, useInstances) {
  67. if (this.checkReadyOnlyOnce) {
  68. if (this._wasPreviouslyReady) {
  69. return true;
  70. }
  71. }
  72. var scene = this.getScene();
  73. if (!this.checkReadyOnEveryCall) {
  74. if (this._renderId === scene.getRenderId()) {
  75. if (this._checkCache(scene, mesh, useInstances)) {
  76. return true;
  77. }
  78. }
  79. }
  80. var engine = scene.getEngine();
  81. var needNormals = false;
  82. var needUVs = false;
  83. this._defines.reset();
  84. // Textures
  85. if (scene.texturesEnabled) {
  86. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  87. if (!this.diffuseTexture.isReady()) {
  88. return false;
  89. }
  90. else {
  91. needUVs = true;
  92. this._defines.DIFFUSE = true;
  93. }
  94. }
  95. }
  96. // Effect
  97. if (scene.clipPlane) {
  98. this._defines.CLIPPLANE = true;
  99. }
  100. this._defines.ALPHATEST = true;
  101. // Point size
  102. if (this.pointsCloud || scene.forcePointsCloud) {
  103. this._defines.POINTSIZE = true;
  104. }
  105. // Fog
  106. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  107. this._defines.FOG = true;
  108. }
  109. // Attribs
  110. if (mesh) {
  111. if (needUVs) {
  112. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  113. this._defines.UV1 = true;
  114. }
  115. }
  116. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  117. this._defines.VERTEXCOLOR = true;
  118. if (mesh.hasVertexAlpha) {
  119. this._defines.VERTEXALPHA = true;
  120. }
  121. }
  122. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  123. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  124. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  125. }
  126. // Instances
  127. if (useInstances) {
  128. this._defines.INSTANCES = true;
  129. }
  130. }
  131. // Get correct effect
  132. if (!this._defines.isEqual(this._cachedDefines)) {
  133. this._defines.cloneTo(this._cachedDefines);
  134. scene.resetCachedMaterial();
  135. // Fallbacks
  136. var fallbacks = new BABYLON.EffectFallbacks();
  137. if (this._defines.FOG) {
  138. fallbacks.addFallback(1, "FOG");
  139. }
  140. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  141. fallbacks.addCPUSkinningFallback(0, mesh);
  142. }
  143. //Attributes
  144. var attribs = [BABYLON.VertexBuffer.PositionKind];
  145. if (this._defines.UV1) {
  146. attribs.push(BABYLON.VertexBuffer.UVKind);
  147. }
  148. if (this._defines.VERTEXCOLOR) {
  149. attribs.push(BABYLON.VertexBuffer.ColorKind);
  150. }
  151. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  152. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  153. // Legacy browser patch
  154. var shaderName = "fire";
  155. var join = this._defines.toString();
  156. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition",
  157. "vFogInfos", "vFogColor", "pointSize",
  158. "vDiffuseInfos",
  159. "mBones",
  160. "vClipPlane", "diffuseMatrix",
  161. // Fire
  162. "time", "speed"
  163. ], ["diffuseSampler",
  164. // Fire
  165. "distortionSampler", "opacitySampler"
  166. ], join, fallbacks, this.onCompiled, this.onError);
  167. }
  168. if (!this._effect.isReady()) {
  169. return false;
  170. }
  171. this._renderId = scene.getRenderId();
  172. this._wasPreviouslyReady = true;
  173. if (mesh) {
  174. if (!mesh._materialDefines) {
  175. mesh._materialDefines = new FireMaterialDefines();
  176. }
  177. this._defines.cloneTo(mesh._materialDefines);
  178. }
  179. return true;
  180. };
  181. FireMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  182. this._effect.setMatrix("world", world);
  183. };
  184. FireMaterial.prototype.bind = function (world, mesh) {
  185. var scene = this.getScene();
  186. // Matrices
  187. this.bindOnlyWorldMatrix(world);
  188. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  189. // Bones
  190. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  191. if (scene.getCachedMaterial() !== this) {
  192. // Textures
  193. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  194. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  195. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  196. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  197. this._effect.setTexture("distortionSampler", this.distortionTexture);
  198. this._effect.setTexture("opacitySampler", this.opacityTexture);
  199. }
  200. // Clip plane
  201. if (scene.clipPlane) {
  202. var clipPlane = scene.clipPlane;
  203. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  204. }
  205. // Point size
  206. if (this.pointsCloud) {
  207. this._effect.setFloat("pointSize", this.pointSize);
  208. }
  209. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  210. }
  211. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  212. // View
  213. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  214. this._effect.setMatrix("view", scene.getViewMatrix());
  215. }
  216. // Fog
  217. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  218. // Time
  219. this._lastTime += scene.getEngine().getDeltaTime();
  220. this._effect.setFloat("time", this._lastTime);
  221. // Speed
  222. this._effect.setFloat("speed", this.speed);
  223. _super.prototype.bind.call(this, world, mesh);
  224. };
  225. FireMaterial.prototype.getAnimatables = function () {
  226. var results = [];
  227. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  228. results.push(this.diffuseTexture);
  229. }
  230. if (this.distortionTexture && this.distortionTexture.animations && this.distortionTexture.animations.length > 0) {
  231. results.push(this.distortionTexture);
  232. }
  233. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  234. results.push(this.opacityTexture);
  235. }
  236. return results;
  237. };
  238. FireMaterial.prototype.dispose = function (forceDisposeEffect) {
  239. if (this.diffuseTexture) {
  240. this.diffuseTexture.dispose();
  241. }
  242. if (this.distortionTexture) {
  243. this.distortionTexture.dispose();
  244. }
  245. _super.prototype.dispose.call(this, forceDisposeEffect);
  246. };
  247. FireMaterial.prototype.clone = function (name) {
  248. var _this = this;
  249. return BABYLON.SerializationHelper.Clone(function () { return new FireMaterial(name, _this.getScene()); }, this);
  250. };
  251. FireMaterial.prototype.serialize = function () {
  252. var serializationObject = _super.prototype.serialize.call(this);
  253. serializationObject.customType = "BABYLON.FireMaterial";
  254. serializationObject.diffuseColor = this.diffuseColor.asArray();
  255. serializationObject.speed = this.speed;
  256. if (this.diffuseTexture) {
  257. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  258. }
  259. if (this.distortionTexture) {
  260. serializationObject.distortionTexture = this.distortionTexture.serialize();
  261. }
  262. if (this.opacityTexture) {
  263. serializationObject.opacityTexture = this.opacityTexture.serialize();
  264. }
  265. return serializationObject;
  266. };
  267. FireMaterial.Parse = function (source, scene, rootUrl) {
  268. var material = new FireMaterial(source.name, scene);
  269. material.diffuseColor = BABYLON.Color3.FromArray(source.diffuseColor);
  270. material.speed = source.speed;
  271. material.alpha = source.alpha;
  272. material.id = source.id;
  273. BABYLON.Tags.AddTagsTo(material, source.tags);
  274. material.backFaceCulling = source.backFaceCulling;
  275. material.wireframe = source.wireframe;
  276. if (source.diffuseTexture) {
  277. material.diffuseTexture = BABYLON.Texture.Parse(source.diffuseTexture, scene, rootUrl);
  278. }
  279. if (source.distortionTexture) {
  280. material.distortionTexture = BABYLON.Texture.Parse(source.distortionTexture, scene, rootUrl);
  281. }
  282. if (source.opacityTexture) {
  283. material.opacityTexture = BABYLON.Texture.Parse(source.opacityTexture, scene, rootUrl);
  284. }
  285. if (source.checkReadyOnlyOnce) {
  286. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  287. }
  288. return material;
  289. };
  290. __decorate([
  291. BABYLON.serializeAsTexture()
  292. ], FireMaterial.prototype, "diffuseTexture");
  293. __decorate([
  294. BABYLON.serializeAsTexture()
  295. ], FireMaterial.prototype, "distortionTexture");
  296. __decorate([
  297. BABYLON.serializeAsTexture()
  298. ], FireMaterial.prototype, "opacityTexture");
  299. __decorate([
  300. BABYLON.serialize("diffuseColor")
  301. ], FireMaterial.prototype, "diffuseColor");
  302. __decorate([
  303. BABYLON.serialize()
  304. ], FireMaterial.prototype, "speed");
  305. return FireMaterial;
  306. })(BABYLON.Material);
  307. BABYLON.FireMaterial = FireMaterial;
  308. })(BABYLON || (BABYLON = {}));
  309. 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#include<shadowsVertexDeclaration>\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";
  310. 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\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}";