lavaMaterial.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 { IAnimatable } from 'babylonjs/Animations/animatable.interface';
  6. import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
  7. import { IEffectCreationOptions } from "babylonjs/Materials/effect";
  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 "./lava.fragment";
  19. import "./lava.vertex";
  20. import { EffectFallbacks } from 'babylonjs/Materials/effectFallbacks';
  21. class LavaMaterialDefines extends MaterialDefines {
  22. public DIFFUSE = false;
  23. public CLIPPLANE = false;
  24. public CLIPPLANE2 = false;
  25. public CLIPPLANE3 = false;
  26. public CLIPPLANE4 = false;
  27. public ALPHATEST = false;
  28. public DEPTHPREPASS = false;
  29. public POINTSIZE = false;
  30. public FOG = false;
  31. public LIGHT0 = false;
  32. public LIGHT1 = false;
  33. public LIGHT2 = false;
  34. public LIGHT3 = false;
  35. public SPOTLIGHT0 = false;
  36. public SPOTLIGHT1 = false;
  37. public SPOTLIGHT2 = false;
  38. public SPOTLIGHT3 = false;
  39. public HEMILIGHT0 = false;
  40. public HEMILIGHT1 = false;
  41. public HEMILIGHT2 = false;
  42. public HEMILIGHT3 = false;
  43. public DIRLIGHT0 = false;
  44. public DIRLIGHT1 = false;
  45. public DIRLIGHT2 = false;
  46. public DIRLIGHT3 = false;
  47. public POINTLIGHT0 = false;
  48. public POINTLIGHT1 = false;
  49. public POINTLIGHT2 = false;
  50. public POINTLIGHT3 = false;
  51. public SHADOW0 = false;
  52. public SHADOW1 = false;
  53. public SHADOW2 = false;
  54. public SHADOW3 = false;
  55. public SHADOWS = false;
  56. public SHADOWESM0 = false;
  57. public SHADOWESM1 = false;
  58. public SHADOWESM2 = false;
  59. public SHADOWESM3 = false;
  60. public SHADOWPOISSON0 = false;
  61. public SHADOWPOISSON1 = false;
  62. public SHADOWPOISSON2 = false;
  63. public SHADOWPOISSON3 = false;
  64. public SHADOWPCF0 = false;
  65. public SHADOWPCF1 = false;
  66. public SHADOWPCF2 = false;
  67. public SHADOWPCF3 = false;
  68. public SHADOWPCSS0 = false;
  69. public SHADOWPCSS1 = false;
  70. public SHADOWPCSS2 = false;
  71. public SHADOWPCSS3 = false;
  72. public NORMAL = false;
  73. public UV1 = false;
  74. public UV2 = false;
  75. public VERTEXCOLOR = false;
  76. public VERTEXALPHA = false;
  77. public NUM_BONE_INFLUENCERS = 0;
  78. public BonesPerMesh = 0;
  79. public INSTANCES = false;
  80. public UNLIT = false;
  81. constructor() {
  82. super();
  83. this.rebuild();
  84. }
  85. }
  86. export class LavaMaterial extends PushMaterial {
  87. @serializeAsTexture("diffuseTexture")
  88. private _diffuseTexture: BaseTexture;
  89. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  90. public diffuseTexture: BaseTexture;
  91. @serializeAsTexture()
  92. public noiseTexture: BaseTexture;
  93. @serializeAsColor3()
  94. public fogColor: Color3;
  95. @serialize()
  96. public speed: number = 1;
  97. @serialize()
  98. public movingSpeed: number = 1;
  99. @serialize()
  100. public lowFrequencySpeed: number = 1;
  101. @serialize()
  102. public fogDensity: number = 0.15;
  103. private _lastTime: number = 0;
  104. @serializeAsColor3()
  105. public diffuseColor = new Color3(1, 1, 1);
  106. @serialize("disableLighting")
  107. private _disableLighting = false;
  108. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  109. public disableLighting: boolean;
  110. @serialize("unlit")
  111. private _unlit = false;
  112. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  113. public unlit: boolean;
  114. @serialize("maxSimultaneousLights")
  115. private _maxSimultaneousLights = 4;
  116. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  117. public maxSimultaneousLights: number;
  118. private _scaledDiffuse = new Color3();
  119. private _renderId: number;
  120. constructor(name: string, scene: Scene) {
  121. super(name, scene);
  122. }
  123. public needAlphaBlending(): boolean {
  124. return (this.alpha < 1.0);
  125. }
  126. public needAlphaTesting(): boolean {
  127. return false;
  128. }
  129. public getAlphaTestTexture(): Nullable<BaseTexture> {
  130. return null;
  131. }
  132. // Methods
  133. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  134. if (this.isFrozen) {
  135. if (this._wasPreviouslyReady && subMesh.effect) {
  136. return true;
  137. }
  138. }
  139. if (!subMesh._materialDefines) {
  140. subMesh._materialDefines = new LavaMaterialDefines();
  141. }
  142. var defines = <LavaMaterialDefines>subMesh._materialDefines;
  143. var scene = this.getScene();
  144. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  145. if (this._renderId === scene.getRenderId()) {
  146. return true;
  147. }
  148. }
  149. var engine = scene.getEngine();
  150. // Textures
  151. if (defines._areTexturesDirty) {
  152. defines._needUVs = false;
  153. if (scene.texturesEnabled) {
  154. if (this._diffuseTexture && MaterialFlags.DiffuseTextureEnabled) {
  155. if (!this._diffuseTexture.isReady()) {
  156. return false;
  157. } else {
  158. defines._needUVs = true;
  159. defines.DIFFUSE = true;
  160. }
  161. }
  162. }
  163. }
  164. // Misc.
  165. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  166. // Lights
  167. defines._needNormals = true;
  168. MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  169. // Values that need to be evaluated on every frame
  170. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  171. // Attribs
  172. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  173. // Get correct effect
  174. if (defines.isDirty) {
  175. defines.markAsProcessed();
  176. scene.resetCachedMaterial();
  177. // Fallbacks
  178. var fallbacks = new EffectFallbacks();
  179. if (defines.FOG) {
  180. fallbacks.addFallback(1, "FOG");
  181. }
  182. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  183. if (defines.NUM_BONE_INFLUENCERS > 0) {
  184. fallbacks.addCPUSkinningFallback(0, mesh);
  185. }
  186. //Attributes
  187. var attribs = [VertexBuffer.PositionKind];
  188. if (defines.NORMAL) {
  189. attribs.push(VertexBuffer.NormalKind);
  190. }
  191. if (defines.UV1) {
  192. attribs.push(VertexBuffer.UVKind);
  193. }
  194. if (defines.UV2) {
  195. attribs.push(VertexBuffer.UV2Kind);
  196. }
  197. if (defines.VERTEXCOLOR) {
  198. attribs.push(VertexBuffer.ColorKind);
  199. }
  200. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  201. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  202. // Legacy browser patch
  203. var shaderName = "lava";
  204. var join = defines.toString();
  205. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  206. "vFogInfos", "vFogColor", "pointSize",
  207. "vDiffuseInfos",
  208. "mBones",
  209. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "diffuseMatrix",
  210. "time", "speed", "movingSpeed",
  211. "fogColor", "fogDensity", "lowFrequencySpeed"
  212. ];
  213. var samplers = ["diffuseSampler",
  214. "noiseTexture"
  215. ];
  216. var uniformBuffers = new Array<string>();
  217. MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
  218. uniformsNames: uniforms,
  219. uniformBuffersNames: uniformBuffers,
  220. samplers: samplers,
  221. defines: defines,
  222. maxSimultaneousLights: this.maxSimultaneousLights
  223. });
  224. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  225. <IEffectCreationOptions>{
  226. attributes: attribs,
  227. uniformsNames: uniforms,
  228. uniformBuffersNames: uniformBuffers,
  229. samplers: samplers,
  230. defines: join,
  231. fallbacks: fallbacks,
  232. onCompiled: this.onCompiled,
  233. onError: this.onError,
  234. indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights }
  235. }, engine), defines);
  236. }
  237. if (!subMesh.effect || !subMesh.effect.isReady()) {
  238. return false;
  239. }
  240. this._renderId = scene.getRenderId();
  241. this._wasPreviouslyReady = true;
  242. return true;
  243. }
  244. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  245. var scene = this.getScene();
  246. var defines = <LavaMaterialDefines>subMesh._materialDefines;
  247. if (!defines) {
  248. return;
  249. }
  250. var effect = subMesh.effect;
  251. if (!effect) {
  252. return;
  253. }
  254. this._activeEffect = effect;
  255. defines.UNLIT = this._unlit;
  256. // Matrices
  257. this.bindOnlyWorldMatrix(world);
  258. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  259. // Bones
  260. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  261. if (this._mustRebind(scene, effect)) {
  262. // Textures
  263. if (this.diffuseTexture && MaterialFlags.DiffuseTextureEnabled) {
  264. this._activeEffect.setTexture("diffuseSampler", this.diffuseTexture);
  265. this._activeEffect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  266. this._activeEffect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  267. }
  268. if (this.noiseTexture) {
  269. this._activeEffect.setTexture("noiseTexture", this.noiseTexture);
  270. }
  271. // Clip plane
  272. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  273. // Point size
  274. if (this.pointsCloud) {
  275. this._activeEffect.setFloat("pointSize", this.pointSize);
  276. }
  277. MaterialHelper.BindEyePosition(effect, scene);
  278. }
  279. this._activeEffect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  280. if (scene.lightsEnabled && !this.disableLighting) {
  281. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines);
  282. }
  283. // View
  284. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  285. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  286. }
  287. // Fog
  288. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  289. this._lastTime += scene.getEngine().getDeltaTime();
  290. this._activeEffect.setFloat("time", this._lastTime * this.speed / 1000);
  291. if (!this.fogColor) {
  292. this.fogColor = Color3.Black();
  293. }
  294. this._activeEffect.setColor3("fogColor", this.fogColor);
  295. this._activeEffect.setFloat("fogDensity", this.fogDensity);
  296. this._activeEffect.setFloat("lowFrequencySpeed", this.lowFrequencySpeed);
  297. this._activeEffect.setFloat("movingSpeed", this.movingSpeed);
  298. this._afterBind(mesh, this._activeEffect);
  299. }
  300. public getAnimatables(): IAnimatable[] {
  301. var results = [];
  302. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  303. results.push(this.diffuseTexture);
  304. }
  305. if (this.noiseTexture && this.noiseTexture.animations && this.noiseTexture.animations.length > 0) {
  306. results.push(this.noiseTexture);
  307. }
  308. return results;
  309. }
  310. public getActiveTextures(): BaseTexture[] {
  311. var activeTextures = super.getActiveTextures();
  312. if (this._diffuseTexture) {
  313. activeTextures.push(this._diffuseTexture);
  314. }
  315. return activeTextures;
  316. }
  317. public hasTexture(texture: BaseTexture): boolean {
  318. if (super.hasTexture(texture)) {
  319. return true;
  320. }
  321. if (this.diffuseTexture === texture) {
  322. return true;
  323. }
  324. return false;
  325. }
  326. public dispose(forceDisposeEffect?: boolean): void {
  327. if (this.diffuseTexture) {
  328. this.diffuseTexture.dispose();
  329. }
  330. if (this.noiseTexture) {
  331. this.noiseTexture.dispose();
  332. }
  333. super.dispose(forceDisposeEffect);
  334. }
  335. public clone(name: string): LavaMaterial {
  336. return SerializationHelper.Clone(() => new LavaMaterial(name, this.getScene()), this);
  337. }
  338. public serialize(): any {
  339. var serializationObject = SerializationHelper.Serialize(this);
  340. serializationObject.customType = "BABYLON.LavaMaterial";
  341. return serializationObject;
  342. }
  343. public getClassName(): string {
  344. return "LavaMaterial";
  345. }
  346. // Statics
  347. public static Parse(source: any, scene: Scene, rootUrl: string): LavaMaterial {
  348. return SerializationHelper.Parse(() => new LavaMaterial(source.name, scene), source, scene, rootUrl);
  349. }
  350. }
  351. _TypeStore.RegisteredTypes["BABYLON.LavaMaterial"] = LavaMaterial;