babylon.lavaMaterial.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class LavaMaterialDefines extends MaterialDefines {
  5. public DIFFUSE = false;
  6. public CLIPPLANE = false;
  7. public ALPHATEST = false;
  8. public POINTSIZE = false;
  9. public FOG = false;
  10. public LIGHT0 = false;
  11. public LIGHT1 = false;
  12. public LIGHT2 = false;
  13. public LIGHT3 = false;
  14. public SPOTLIGHT0 = false;
  15. public SPOTLIGHT1 = false;
  16. public SPOTLIGHT2 = false;
  17. public SPOTLIGHT3 = false;
  18. public HEMILIGHT0 = false;
  19. public HEMILIGHT1 = false;
  20. public HEMILIGHT2 = false;
  21. public HEMILIGHT3 = false;
  22. public DIRLIGHT0 = false;
  23. public DIRLIGHT1 = false;
  24. public DIRLIGHT2 = false;
  25. public DIRLIGHT3 = false;
  26. public POINTLIGHT0 = false;
  27. public POINTLIGHT1 = false;
  28. public POINTLIGHT2 = false;
  29. public POINTLIGHT3 = false;
  30. public SHADOW0 = false;
  31. public SHADOW1 = false;
  32. public SHADOW2 = false;
  33. public SHADOW3 = false;
  34. public SHADOWS = false;
  35. public SHADOWVSM0 = false;
  36. public SHADOWVSM1 = false;
  37. public SHADOWVSM2 = false;
  38. public SHADOWVSM3 = false;
  39. public SHADOWPCF0 = false;
  40. public SHADOWPCF1 = false;
  41. public SHADOWPCF2 = false;
  42. public SHADOWPCF3 = false;
  43. public NORMAL = false;
  44. public UV1 = false;
  45. public UV2 = false;
  46. public VERTEXCOLOR = false;
  47. public VERTEXALPHA = false;
  48. public NUM_BONE_INFLUENCERS = 0;
  49. public BonesPerMesh = 0;
  50. public INSTANCES = false;
  51. constructor() {
  52. super();
  53. this._keys = Object.keys(this);
  54. }
  55. }
  56. export class LavaMaterial extends Material {
  57. public diffuseTexture: BaseTexture;
  58. public noiseTexture: BaseTexture;
  59. public fogColor: Color3;
  60. public speed : number = 1;
  61. public movingSpeed : number = 1;
  62. public lowFrequencySpeed : number = 1;
  63. public fogDensity : number = 0.15;
  64. private _lastTime : number = 0;
  65. public diffuseColor = new Color3(1, 1, 1);
  66. public disableLighting = false;
  67. private _worldViewProjectionMatrix = Matrix.Zero();
  68. private _scaledDiffuse = new Color3();
  69. private _renderId: number;
  70. private _defines = new LavaMaterialDefines();
  71. private _cachedDefines = new LavaMaterialDefines();
  72. constructor(name: string, scene: Scene) {
  73. super(name, scene);
  74. this._cachedDefines.BonesPerMesh = -1;
  75. }
  76. public needAlphaBlending(): boolean {
  77. return (this.alpha < 1.0);
  78. }
  79. public needAlphaTesting(): boolean {
  80. return false;
  81. }
  82. public getAlphaTestTexture(): BaseTexture {
  83. return null;
  84. }
  85. // Methods
  86. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  87. if (!mesh) {
  88. return true;
  89. }
  90. if (this._defines.INSTANCES !== useInstances) {
  91. return false;
  92. }
  93. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  94. return true;
  95. }
  96. return false;
  97. }
  98. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  99. if (this.checkReadyOnlyOnce) {
  100. if (this._wasPreviouslyReady) {
  101. return true;
  102. }
  103. }
  104. var scene = this.getScene();
  105. if (!this.checkReadyOnEveryCall) {
  106. if (this._renderId === scene.getRenderId()) {
  107. if (this._checkCache(scene, mesh, useInstances)) {
  108. return true;
  109. }
  110. }
  111. }
  112. var engine = scene.getEngine();
  113. var needUVs = false;
  114. this._defines.reset();
  115. // Textures
  116. if (scene.texturesEnabled) {
  117. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  118. if (!this.diffuseTexture.isReady()) {
  119. return false;
  120. } else {
  121. needUVs = true;
  122. this._defines.DIFFUSE = true;
  123. }
  124. }
  125. }
  126. // Effect
  127. if (scene.clipPlane) {
  128. this._defines.CLIPPLANE = true;
  129. }
  130. if (engine.getAlphaTesting()) {
  131. this._defines.ALPHATEST = true;
  132. }
  133. // Point size
  134. if (this.pointsCloud || scene.forcePointsCloud) {
  135. this._defines.POINTSIZE = true;
  136. }
  137. // Fog
  138. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  139. this._defines.FOG = true;
  140. }
  141. var lightIndex = 0;
  142. if (scene.lightsEnabled && !this.disableLighting) {
  143. MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  144. }
  145. // Attribs
  146. if (mesh) {
  147. if (mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  148. this._defines.NORMAL = true;
  149. }
  150. if (needUVs) {
  151. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  152. this._defines.UV1 = true;
  153. }
  154. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  155. this._defines.UV2 = true;
  156. }
  157. }
  158. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  159. this._defines.VERTEXCOLOR = true;
  160. if (mesh.hasVertexAlpha) {
  161. this._defines.VERTEXALPHA = true;
  162. }
  163. }
  164. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  165. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  166. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  167. }
  168. // Instances
  169. if (useInstances) {
  170. this._defines.INSTANCES = true;
  171. }
  172. }
  173. // Get correct effect
  174. if (!this._defines.isEqual(this._cachedDefines)) {
  175. this._defines.cloneTo(this._cachedDefines);
  176. scene.resetCachedMaterial();
  177. // Fallbacks
  178. var fallbacks = new EffectFallbacks();
  179. if (this._defines.FOG) {
  180. fallbacks.addFallback(1, "FOG");
  181. }
  182. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  183. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  184. fallbacks.addCPUSkinningFallback(0, mesh);
  185. }
  186. //Attributes
  187. var attribs = [VertexBuffer.PositionKind];
  188. if (this._defines.NORMAL) {
  189. attribs.push(VertexBuffer.NormalKind);
  190. }
  191. if (this._defines.UV1) {
  192. attribs.push(VertexBuffer.UVKind);
  193. }
  194. if (this._defines.UV2) {
  195. attribs.push(VertexBuffer.UV2Kind);
  196. }
  197. if (this._defines.VERTEXCOLOR) {
  198. attribs.push(VertexBuffer.ColorKind);
  199. }
  200. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  201. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  202. // Legacy browser patch
  203. var shaderName = "lava";
  204. var join = this._defines.toString();
  205. this._effect = scene.getEngine().createEffect(shaderName,
  206. attribs,
  207. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  208. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  209. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  210. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  211. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  212. "vFogInfos", "vFogColor", "pointSize",
  213. "vDiffuseInfos",
  214. "mBones",
  215. "vClipPlane", "diffuseMatrix",
  216. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues",
  217. "time", "speed","movingSpeed",
  218. "fogColor","fogDensity", "lowFrequencySpeed"
  219. ],
  220. ["diffuseSampler",
  221. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3", "noiseTexture"
  222. ],
  223. join, fallbacks, this.onCompiled, this.onError);
  224. }
  225. if (!this._effect.isReady()) {
  226. return false;
  227. }
  228. this._renderId = scene.getRenderId();
  229. this._wasPreviouslyReady = true;
  230. if (mesh) {
  231. if (!mesh._materialDefines) {
  232. mesh._materialDefines = new LavaMaterialDefines();
  233. }
  234. this._defines.cloneTo(mesh._materialDefines);
  235. }
  236. return true;
  237. }
  238. public bindOnlyWorldMatrix(world: Matrix): void {
  239. this._effect.setMatrix("world", world);
  240. }
  241. public bind(world: Matrix, mesh?: Mesh): void {
  242. var scene = this.getScene();
  243. // Matrices
  244. this.bindOnlyWorldMatrix(world);
  245. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  246. // Bones
  247. MaterialHelper.BindBonesParameters(mesh, this._effect);
  248. if (scene.getCachedMaterial() !== this) {
  249. // Textures
  250. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  251. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  252. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  253. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  254. }
  255. if (this.noiseTexture) {
  256. this._effect.setTexture("noiseTexture", this.noiseTexture);
  257. }
  258. // Clip plane
  259. MaterialHelper.BindClipPlane(this._effect, scene);
  260. // Point size
  261. if (this.pointsCloud) {
  262. this._effect.setFloat("pointSize", this.pointSize);
  263. }
  264. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  265. }
  266. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  267. if (scene.lightsEnabled && !this.disableLighting) {
  268. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  269. }
  270. // View
  271. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  272. this._effect.setMatrix("view", scene.getViewMatrix());
  273. }
  274. // Fog
  275. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  276. this._lastTime += scene.getEngine().getDeltaTime();
  277. this._effect.setFloat("time", this._lastTime * this.speed / 1000);
  278. if (! this.fogColor) {
  279. this.fogColor = Color3.Black();
  280. }
  281. this._effect.setColor3("fogColor", this.fogColor);
  282. this._effect.setFloat("fogDensity", this.fogDensity);
  283. this._effect.setFloat("lowFrequencySpeed", this.lowFrequencySpeed);
  284. this._effect.setFloat("movingSpeed", this.movingSpeed);
  285. super.bind(world, mesh);
  286. }
  287. public getAnimatables(): IAnimatable[] {
  288. var results = [];
  289. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  290. results.push(this.diffuseTexture);
  291. }
  292. if (this.noiseTexture && this.noiseTexture.animations && this.noiseTexture.animations.length > 0) {
  293. results.push(this.noiseTexture);
  294. }
  295. return results;
  296. }
  297. public dispose(forceDisposeEffect?: boolean): void {
  298. if (this.diffuseTexture) {
  299. this.diffuseTexture.dispose();
  300. }
  301. if (this.noiseTexture) {
  302. this.noiseTexture.dispose();
  303. }
  304. super.dispose(forceDisposeEffect);
  305. }
  306. public clone(name: string): LavaMaterial {
  307. var newMaterial = new LavaMaterial(name, this.getScene());
  308. // Base material
  309. this.copyTo(newMaterial);
  310. // Lava material
  311. if (this.diffuseTexture && this.diffuseTexture.clone) {
  312. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  313. }
  314. if (this.noiseTexture && this.noiseTexture.clone) {
  315. newMaterial.noiseTexture = this.noiseTexture.clone();
  316. }
  317. if (this.fogColor && this.fogColor.clone) {
  318. newMaterial.fogColor = this.fogColor.clone();
  319. }
  320. return newMaterial;
  321. }
  322. public serialize(): any {
  323. var serializationObject = super.serialize();
  324. serializationObject.customType = "BABYLON.LavaMaterial";
  325. serializationObject.diffuseColor = this.diffuseColor.asArray();
  326. serializationObject.fogColor = this.fogColor.asArray();
  327. serializationObject.speed = this.speed;
  328. serializationObject.movingSpeed = this.movingSpeed;
  329. serializationObject.lowFrequencySpeed = this.lowFrequencySpeed;
  330. serializationObject.fogDensity = this.fogDensity;
  331. serializationObject.checkReadyOnlyOnce = this.checkReadyOnlyOnce;
  332. if (this.diffuseTexture) {
  333. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  334. }
  335. if (this.noiseTexture) {
  336. serializationObject.noiseTexture = this.noiseTexture.serialize();
  337. }
  338. return serializationObject;
  339. }
  340. public static Parse(source: any, scene: Scene, rootUrl: string): LavaMaterial {
  341. var material = new LavaMaterial(source.name, scene);
  342. material.diffuseColor = Color3.FromArray(source.diffuseColor);
  343. material.speed = source.speed;
  344. material.fogColor = Color3.FromArray(source.fogColor);
  345. material.movingSpeed = source.movingSpeed;
  346. material.lowFrequencySpeed = source.lowFrequencySpeed;
  347. material.fogDensity = source.lowFrequencySpeed;
  348. material.alpha = source.alpha;
  349. material.id = source.id;
  350. Tags.AddTagsTo(material, source.tags);
  351. material.backFaceCulling = source.backFaceCulling;
  352. material.wireframe = source.wireframe;
  353. if (source.diffuseTexture) {
  354. material.diffuseTexture = Texture.Parse(source.diffuseTexture, scene, rootUrl);
  355. }
  356. if (source.noiseTexture) {
  357. material.noiseTexture = Texture.Parse(source.noiseTexture, scene, rootUrl);
  358. }
  359. if (source.checkReadyOnlyOnce) {
  360. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  361. }
  362. return material;
  363. }
  364. }
  365. }