babylon.fireMaterial.js 18 KB

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