babylon.fireMaterial.js 17 KB

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