babylon.fireMaterial.js 17 KB

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