fireMaterial.ts 12 KB

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