babylon.fireMaterial.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. class FireMaterialDefines extends MaterialDefines {
  4. public DIFFUSE = false;
  5. public CLIPPLANE = false;
  6. public ALPHATEST = false;
  7. public DEPTHPREPASS = 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.rebuild();
  19. }
  20. }
  21. export class FireMaterial extends PushMaterial {
  22. @serializeAsTexture("diffuseTexture")
  23. private _diffuseTexture: Nullable<BaseTexture>;
  24. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  25. public diffuseTexture: Nullable<BaseTexture>;
  26. @serializeAsTexture("distortionTexture")
  27. private _distortionTexture: Nullable<BaseTexture>;
  28. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  29. public distortionTexture: Nullable<BaseTexture>;
  30. @serializeAsTexture("opacityTexture")
  31. private _opacityTexture: Nullable<BaseTexture>;
  32. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  33. public opacityTexture: Nullable<BaseTexture>;
  34. @serialize("diffuse")
  35. public diffuseColor = new Color3(1, 1, 1);
  36. @serialize()
  37. public speed = 1.0;
  38. private _scaledDiffuse = new Color3();
  39. private _renderId: number;
  40. private _lastTime: number = 0;
  41. constructor(name: string, scene: Scene) {
  42. super(name, scene);
  43. }
  44. public needAlphaBlending(): boolean {
  45. return false;
  46. }
  47. public needAlphaTesting(): boolean {
  48. return true;
  49. }
  50. public getAlphaTestTexture(): Nullable<BaseTexture> {
  51. return null;
  52. }
  53. // Methods
  54. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  55. if (this.isFrozen) {
  56. if (this._wasPreviouslyReady && subMesh.effect) {
  57. return true;
  58. }
  59. }
  60. if (!subMesh._materialDefines) {
  61. subMesh._materialDefines = new FireMaterialDefines();
  62. }
  63. var defines = <FireMaterialDefines>subMesh._materialDefines;
  64. var scene = this.getScene();
  65. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  66. if (this._renderId === scene.getRenderId()) {
  67. return true;
  68. }
  69. }
  70. var engine = scene.getEngine();
  71. // Textures
  72. if (defines._areTexturesDirty) {
  73. defines._needUVs = false;
  74. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  75. if (!this._diffuseTexture.isReady()) {
  76. return false;
  77. } else {
  78. defines._needUVs = true;
  79. defines.DIFFUSE = true;
  80. }
  81. }
  82. }
  83. // Misc.
  84. if (defines._areMiscDirty) {
  85. defines.POINTSIZE = (this.pointsCloud || scene.forcePointsCloud);
  86. defines.FOG = (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled);
  87. }
  88. // Values that need to be evaluated on every frame
  89. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  90. // Attribs
  91. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  92. // Get correct effect
  93. if (defines.isDirty) {
  94. defines.markAsProcessed();
  95. scene.resetCachedMaterial();
  96. // Fallbacks
  97. var fallbacks = new EffectFallbacks();
  98. if (defines.FOG) {
  99. fallbacks.addFallback(1, "FOG");
  100. }
  101. if (defines.NUM_BONE_INFLUENCERS > 0) {
  102. fallbacks.addCPUSkinningFallback(0, mesh);
  103. }
  104. //Attributes
  105. var attribs = [VertexBuffer.PositionKind];
  106. if (defines.UV1) {
  107. attribs.push(VertexBuffer.UVKind);
  108. }
  109. if (defines.VERTEXCOLOR) {
  110. attribs.push(VertexBuffer.ColorKind);
  111. }
  112. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  113. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  114. // Legacy browser patch
  115. var shaderName = "fire";
  116. var join = defines.toString();
  117. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  118. {
  119. attributes: attribs,
  120. uniformsNames: ["world", "view", "viewProjection", "vEyePosition",
  121. "vFogInfos", "vFogColor", "pointSize",
  122. "vDiffuseInfos",
  123. "mBones",
  124. "vClipPlane", "diffuseMatrix",
  125. // Fire
  126. "time", "speed"
  127. ],
  128. uniformBuffersNames: [],
  129. samplers: ["diffuseSampler",
  130. // Fire
  131. "distortionSampler", "opacitySampler"
  132. ],
  133. defines: join,
  134. fallbacks: fallbacks,
  135. onCompiled: this.onCompiled,
  136. onError: this.onError,
  137. indexParameters: null,
  138. maxSimultaneousLights: 4
  139. }, engine), defines);
  140. }
  141. if (!subMesh.effect || !subMesh.effect.isReady()) {
  142. return false;
  143. }
  144. this._renderId = scene.getRenderId();
  145. this._wasPreviouslyReady = true;
  146. return true;
  147. }
  148. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  149. var scene = this.getScene();
  150. var defines = <FireMaterialDefines>subMesh._materialDefines;
  151. if (!defines) {
  152. return;
  153. }
  154. var effect = subMesh.effect;
  155. if (!effect) {
  156. return;
  157. }
  158. this._activeEffect = effect;
  159. // Matrices
  160. this.bindOnlyWorldMatrix(world);
  161. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  162. // Bones
  163. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  164. if (this._mustRebind(scene, effect)) {
  165. // Textures
  166. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  167. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  168. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  169. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  170. this._activeEffect.setTexture("distortionSampler", this._distortionTexture);
  171. this._activeEffect.setTexture("opacitySampler", this._opacityTexture);
  172. }
  173. // Clip plane
  174. if (scene.clipPlane) {
  175. var clipPlane = scene.clipPlane;
  176. this._activeEffect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  177. }
  178. // Point size
  179. if (this.pointsCloud) {
  180. this._activeEffect.setFloat("pointSize", this.pointSize);
  181. }
  182. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : (<Camera>scene.activeCamera).position);
  183. }
  184. this._activeEffect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  185. // View
  186. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  187. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  188. }
  189. // Fog
  190. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  191. // Time
  192. this._lastTime += scene.getEngine().getDeltaTime();
  193. this._activeEffect.setFloat("time", this._lastTime);
  194. // Speed
  195. this._activeEffect.setFloat("speed", this.speed);
  196. this._afterBind(mesh, this._activeEffect);
  197. }
  198. public getAnimatables(): IAnimatable[] {
  199. var results = [];
  200. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  201. results.push(this._diffuseTexture);
  202. }
  203. if (this._distortionTexture && this._distortionTexture.animations && this._distortionTexture.animations.length > 0) {
  204. results.push(this._distortionTexture);
  205. }
  206. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  207. results.push(this._opacityTexture);
  208. }
  209. return results;
  210. }
  211. public getActiveTextures(): BaseTexture[] {
  212. var activeTextures = super.getActiveTextures();
  213. if (this._diffuseTexture) {
  214. activeTextures.push(this._diffuseTexture);
  215. }
  216. if (this._distortionTexture) {
  217. activeTextures.push(this._distortionTexture);
  218. }
  219. if (this._opacityTexture) {
  220. activeTextures.push(this._opacityTexture);
  221. }
  222. return activeTextures;
  223. }
  224. public hasTexture(texture: BaseTexture): boolean {
  225. if (super.hasTexture(texture)) {
  226. return true;
  227. }
  228. if (this._diffuseTexture === texture) {
  229. return true;
  230. }
  231. if (this._distortionTexture === texture) {
  232. return true;
  233. }
  234. if (this._opacityTexture === texture) {
  235. return true;
  236. }
  237. return false;
  238. }
  239. public getClassName(): string {
  240. return "FireMaterial";
  241. }
  242. public dispose(forceDisposeEffect?: boolean): void {
  243. if (this._diffuseTexture) {
  244. this._diffuseTexture.dispose();
  245. }
  246. if (this._distortionTexture) {
  247. this._distortionTexture.dispose();
  248. }
  249. super.dispose(forceDisposeEffect);
  250. }
  251. public clone(name: string): FireMaterial {
  252. return SerializationHelper.Clone<FireMaterial>(() => new FireMaterial(name, this.getScene()), this);
  253. }
  254. public serialize(): any {
  255. var serializationObject = super.serialize();
  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. public static Parse(source: any, scene: Scene, rootUrl: string): FireMaterial {
  271. var material = new FireMaterial(source.name, scene);
  272. material.diffuseColor = Color3.FromArray(source.diffuseColor);
  273. material.speed = source.speed;
  274. material.alpha = source.alpha;
  275. material.id = source.id;
  276. Tags.AddTagsTo(material, source.tags);
  277. material.backFaceCulling = source.backFaceCulling;
  278. material.wireframe = source.wireframe;
  279. if (source._diffuseTexture) {
  280. material._diffuseTexture = Texture.Parse(source._diffuseTexture, scene, rootUrl);
  281. }
  282. if (source._distortionTexture) {
  283. material._distortionTexture = Texture.Parse(source._distortionTexture, scene, rootUrl);
  284. }
  285. if (source._opacityTexture) {
  286. material._opacityTexture = Texture.Parse(source._opacityTexture, scene, rootUrl);
  287. }
  288. if (source.checkReadyOnlyOnce) {
  289. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  290. }
  291. return material;
  292. }
  293. }
  294. }