babylon.fireMaterial.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class FireMaterialDefines extends MaterialDefines {
  5. public DIFFUSE = false;
  6. public CLIPPLANE = false;
  7. public ALPHATEST = false;
  8. public POINTSIZE = false;
  9. public FOG = false;
  10. public UV1 = false;
  11. public VERTEXCOLOR = false;
  12. public VERTEXALPHA = false;
  13. public BonesPerMesh = 0;
  14. public NUM_BONE_INFLUENCERS = 0;
  15. public INSTANCES = false;
  16. constructor() {
  17. super();
  18. this._keys = Object.keys(this);
  19. }
  20. }
  21. export class FireMaterial extends Material {
  22. @serializeAsTexture()
  23. public diffuseTexture: BaseTexture;
  24. @serializeAsTexture()
  25. public distortionTexture: BaseTexture;
  26. @serializeAsTexture()
  27. public opacityTexture: BaseTexture;
  28. @serialize("diffuseColor")
  29. public diffuseColor = new Color3(1, 1, 1);
  30. @serialize()
  31. public speed = 1.0;
  32. private _scaledDiffuse = new Color3();
  33. private _renderId: number;
  34. private _defines = new FireMaterialDefines();
  35. private _cachedDefines = new FireMaterialDefines();
  36. private _lastTime: number = 0;
  37. constructor(name: string, scene: Scene) {
  38. super(name, scene);
  39. this._cachedDefines.BonesPerMesh = -1;
  40. }
  41. public needAlphaBlending(): boolean {
  42. return (this.alpha < 1.0);
  43. }
  44. public needAlphaTesting(): boolean {
  45. return false;
  46. }
  47. public getAlphaTestTexture(): BaseTexture {
  48. return null;
  49. }
  50. // Methods
  51. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  52. if (!mesh) {
  53. return true;
  54. }
  55. if (this._defines.INSTANCES !== useInstances) {
  56. return false;
  57. }
  58. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  64. if (this.checkReadyOnlyOnce) {
  65. if (this._wasPreviouslyReady) {
  66. return true;
  67. }
  68. }
  69. var scene = this.getScene();
  70. if (!this.checkReadyOnEveryCall) {
  71. if (this._renderId === scene.getRenderId()) {
  72. if (this._checkCache(scene, mesh, useInstances)) {
  73. return true;
  74. }
  75. }
  76. }
  77. var engine = scene.getEngine();
  78. var needNormals = false;
  79. var needUVs = false;
  80. this._defines.reset();
  81. // Textures
  82. if (scene.texturesEnabled) {
  83. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  84. if (!this.diffuseTexture.isReady()) {
  85. return false;
  86. } else {
  87. needUVs = true;
  88. this._defines.DIFFUSE = true;
  89. }
  90. }
  91. }
  92. // Effect
  93. if (scene.clipPlane) {
  94. this._defines.CLIPPLANE = true;
  95. }
  96. this._defines.ALPHATEST = true;
  97. // Point size
  98. if (this.pointsCloud || scene.forcePointsCloud) {
  99. this._defines.POINTSIZE = true;
  100. }
  101. // Fog
  102. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  103. this._defines.FOG = true;
  104. }
  105. // Attribs
  106. if (mesh) {
  107. if (needUVs) {
  108. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  109. this._defines.UV1 = true;
  110. }
  111. }
  112. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  113. this._defines.VERTEXCOLOR = true;
  114. if (mesh.hasVertexAlpha) {
  115. this._defines.VERTEXALPHA = true;
  116. }
  117. }
  118. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  119. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  120. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  121. }
  122. // Instances
  123. if (useInstances) {
  124. this._defines.INSTANCES = true;
  125. }
  126. }
  127. // Get correct effect
  128. if (!this._defines.isEqual(this._cachedDefines)) {
  129. this._defines.cloneTo(this._cachedDefines);
  130. scene.resetCachedMaterial();
  131. // Fallbacks
  132. var fallbacks = new EffectFallbacks();
  133. if (this._defines.FOG) {
  134. fallbacks.addFallback(1, "FOG");
  135. }
  136. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  137. fallbacks.addCPUSkinningFallback(0, mesh);
  138. }
  139. //Attributes
  140. var attribs = [VertexBuffer.PositionKind];
  141. if (this._defines.UV1) {
  142. attribs.push(VertexBuffer.UVKind);
  143. }
  144. if (this._defines.VERTEXCOLOR) {
  145. attribs.push(VertexBuffer.ColorKind);
  146. }
  147. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  148. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  149. // Legacy browser patch
  150. var shaderName = "fire";
  151. var join = this._defines.toString();
  152. this._effect = scene.getEngine().createEffect(shaderName,
  153. attribs,
  154. ["world", "view", "viewProjection", "vEyePosition",
  155. "vFogInfos", "vFogColor", "pointSize",
  156. "vDiffuseInfos",
  157. "mBones",
  158. "vClipPlane", "diffuseMatrix",
  159. // Fire
  160. "time", "speed"
  161. ],
  162. ["diffuseSampler",
  163. // Fire
  164. "distortionSampler", "opacitySampler"
  165. ],
  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. public bindOnlyWorldMatrix(world: Matrix): void {
  182. this._effect.setMatrix("world", world);
  183. }
  184. public bind(world: Matrix, mesh?: Mesh): void {
  185. var scene = this.getScene();
  186. // Matrices
  187. this.bindOnlyWorldMatrix(world);
  188. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  189. // Bones
  190. MaterialHelper.BindBonesParameters(mesh, this._effect);
  191. if (scene.getCachedMaterial() !== this) {
  192. // Textures
  193. if (this.diffuseTexture && 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 !== Scene.FOGMODE_NONE) {
  214. this._effect.setMatrix("view", scene.getViewMatrix());
  215. }
  216. // Fog
  217. 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.bind(world, mesh);
  224. }
  225. public getAnimatables(): IAnimatable[] {
  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. public dispose(forceDisposeEffect?: boolean): void {
  239. if (this.diffuseTexture) {
  240. this.diffuseTexture.dispose();
  241. }
  242. if (this.distortionTexture) {
  243. this.distortionTexture.dispose();
  244. }
  245. super.dispose(forceDisposeEffect);
  246. }
  247. public clone(name: string): FireMaterial {
  248. return SerializationHelper.Clone<FireMaterial>(() => new FireMaterial(name, this.getScene()), this);
  249. }
  250. public serialize(): any {
  251. var serializationObject = super.serialize();
  252. serializationObject.customType = "BABYLON.FireMaterial";
  253. serializationObject.diffuseColor = this.diffuseColor.asArray();
  254. serializationObject.speed = this.speed;
  255. if (this.diffuseTexture) {
  256. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  257. }
  258. if (this.distortionTexture) {
  259. serializationObject.distortionTexture = this.distortionTexture.serialize();
  260. }
  261. if (this.opacityTexture) {
  262. serializationObject.opacityTexture = this.opacityTexture.serialize();
  263. }
  264. return serializationObject;
  265. }
  266. public static Parse(source: any, scene: Scene, rootUrl: string): FireMaterial {
  267. var material = new FireMaterial(source.name, scene);
  268. material.diffuseColor = Color3.FromArray(source.diffuseColor);
  269. material.speed = source.speed;
  270. material.alpha = source.alpha;
  271. material.id = source.id;
  272. Tags.AddTagsTo(material, source.tags);
  273. material.backFaceCulling = source.backFaceCulling;
  274. material.wireframe = source.wireframe;
  275. if (source.diffuseTexture) {
  276. material.diffuseTexture = Texture.Parse(source.diffuseTexture, scene, rootUrl);
  277. }
  278. if (source.distortionTexture) {
  279. material.distortionTexture = Texture.Parse(source.distortionTexture, scene, rootUrl);
  280. }
  281. if (source.opacityTexture) {
  282. material.opacityTexture = Texture.Parse(source.opacityTexture, scene, rootUrl);
  283. }
  284. if (source.checkReadyOnlyOnce) {
  285. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  286. }
  287. return material;
  288. }
  289. }
  290. }