lavaMaterial.ts 14 KB

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