fireMaterial.ts 13 KB

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