babylon.fireMaterial.js 17 KB

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