babylon.lavaMaterial.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var maxSimultaneousLights = 4;
  5. var LavaMaterialDefines = (function (_super) {
  6. __extends(LavaMaterialDefines, _super);
  7. function LavaMaterialDefines() {
  8. _super.call(this);
  9. this.DIFFUSE = false;
  10. this.CLIPPLANE = false;
  11. this.ALPHATEST = false;
  12. this.POINTSIZE = false;
  13. this.FOG = false;
  14. this.LIGHT0 = false;
  15. this.LIGHT1 = false;
  16. this.LIGHT2 = false;
  17. this.LIGHT3 = false;
  18. this.SPOTLIGHT0 = false;
  19. this.SPOTLIGHT1 = false;
  20. this.SPOTLIGHT2 = false;
  21. this.SPOTLIGHT3 = false;
  22. this.HEMILIGHT0 = false;
  23. this.HEMILIGHT1 = false;
  24. this.HEMILIGHT2 = false;
  25. this.HEMILIGHT3 = false;
  26. this.DIRLIGHT0 = false;
  27. this.DIRLIGHT1 = false;
  28. this.DIRLIGHT2 = false;
  29. this.DIRLIGHT3 = false;
  30. this.POINTLIGHT0 = false;
  31. this.POINTLIGHT1 = false;
  32. this.POINTLIGHT2 = false;
  33. this.POINTLIGHT3 = false;
  34. this.SHADOW0 = false;
  35. this.SHADOW1 = false;
  36. this.SHADOW2 = false;
  37. this.SHADOW3 = false;
  38. this.SHADOWS = false;
  39. this.SHADOWVSM0 = false;
  40. this.SHADOWVSM1 = false;
  41. this.SHADOWVSM2 = false;
  42. this.SHADOWVSM3 = false;
  43. this.SHADOWPCF0 = false;
  44. this.SHADOWPCF1 = false;
  45. this.SHADOWPCF2 = false;
  46. this.SHADOWPCF3 = false;
  47. this.NORMAL = false;
  48. this.UV1 = false;
  49. this.UV2 = false;
  50. this.VERTEXCOLOR = false;
  51. this.VERTEXALPHA = false;
  52. this.BONES = false;
  53. this.BONES4 = false;
  54. this.BonesPerMesh = 0;
  55. this.INSTANCES = false;
  56. this._keys = Object.keys(this);
  57. }
  58. return LavaMaterialDefines;
  59. })(BABYLON.MaterialDefines);
  60. var LavaMaterial = (function (_super) {
  61. __extends(LavaMaterial, _super);
  62. function LavaMaterial(name, scene) {
  63. _super.call(this, name, scene);
  64. this.speed = 1;
  65. this._lastTime = 0;
  66. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  67. this.disableLighting = false;
  68. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  69. this._scaledDiffuse = new BABYLON.Color3();
  70. this._defines = new LavaMaterialDefines();
  71. this._cachedDefines = new LavaMaterialDefines();
  72. this._cachedDefines.BonesPerMesh = -1;
  73. }
  74. LavaMaterial.prototype.needAlphaBlending = function () {
  75. return (this.alpha < 1.0);
  76. };
  77. LavaMaterial.prototype.needAlphaTesting = function () {
  78. return false;
  79. };
  80. LavaMaterial.prototype.getAlphaTestTexture = function () {
  81. return null;
  82. };
  83. // Methods
  84. LavaMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  85. if (!mesh) {
  86. return true;
  87. }
  88. if (this._defines.INSTANCES !== useInstances) {
  89. return false;
  90. }
  91. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  92. return true;
  93. }
  94. return false;
  95. };
  96. LavaMaterial.prototype.isReady = function (mesh, useInstances) {
  97. if (this.checkReadyOnlyOnce) {
  98. if (this._wasPreviouslyReady) {
  99. return true;
  100. }
  101. }
  102. var scene = this.getScene();
  103. if (!this.checkReadyOnEveryCall) {
  104. if (this._renderId === scene.getRenderId()) {
  105. if (this._checkCache(scene, mesh, useInstances)) {
  106. return true;
  107. }
  108. }
  109. }
  110. var engine = scene.getEngine();
  111. var needNormals = false;
  112. var needUVs = false;
  113. this._defines.reset();
  114. // Textures
  115. if (scene.texturesEnabled) {
  116. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  117. if (!this.diffuseTexture.isReady()) {
  118. return false;
  119. }
  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 !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  139. this._defines.FOG = true;
  140. }
  141. var lightIndex = 0;
  142. if (scene.lightsEnabled && !this.disableLighting) {
  143. for (var index = 0; index < scene.lights.length; index++) {
  144. var light = scene.lights[index];
  145. if (!light.isEnabled()) {
  146. continue;
  147. }
  148. // Excluded check
  149. if (light._excludedMeshesIds.length > 0) {
  150. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  151. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  152. if (excludedMesh) {
  153. light.excludedMeshes.push(excludedMesh);
  154. }
  155. }
  156. light._excludedMeshesIds = [];
  157. }
  158. // Included check
  159. if (light._includedOnlyMeshesIds.length > 0) {
  160. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  161. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  162. if (includedOnlyMesh) {
  163. light.includedOnlyMeshes.push(includedOnlyMesh);
  164. }
  165. }
  166. light._includedOnlyMeshesIds = [];
  167. }
  168. if (!light.canAffectMesh(mesh)) {
  169. continue;
  170. }
  171. needNormals = true;
  172. this._defines["LIGHT" + lightIndex] = true;
  173. var type;
  174. if (light instanceof BABYLON.SpotLight) {
  175. type = "SPOTLIGHT" + lightIndex;
  176. }
  177. else if (light instanceof BABYLON.HemisphericLight) {
  178. type = "HEMILIGHT" + lightIndex;
  179. }
  180. else if (light instanceof BABYLON.PointLight) {
  181. type = "POINTLIGHT" + lightIndex;
  182. }
  183. else {
  184. type = "DIRLIGHT" + lightIndex;
  185. }
  186. this._defines[type] = true;
  187. // Shadows
  188. if (scene.shadowsEnabled) {
  189. var shadowGenerator = light.getShadowGenerator();
  190. if (mesh && mesh.receiveShadows && shadowGenerator) {
  191. this._defines["SHADOW" + lightIndex] = true;
  192. this._defines.SHADOWS = true;
  193. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  194. this._defines["SHADOWVSM" + lightIndex] = true;
  195. }
  196. if (shadowGenerator.usePoissonSampling) {
  197. this._defines["SHADOWPCF" + lightIndex] = true;
  198. }
  199. }
  200. }
  201. lightIndex++;
  202. if (lightIndex === maxSimultaneousLights)
  203. break;
  204. }
  205. }
  206. // Attribs
  207. if (mesh) {
  208. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  209. this._defines.NORMAL = true;
  210. }
  211. if (needUVs) {
  212. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  213. this._defines.UV1 = true;
  214. }
  215. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  216. this._defines.UV2 = true;
  217. }
  218. }
  219. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  220. this._defines.VERTEXCOLOR = true;
  221. if (mesh.hasVertexAlpha) {
  222. this._defines.VERTEXALPHA = true;
  223. }
  224. }
  225. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  226. this._defines.BONES = true;
  227. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  228. this._defines.BONES4 = true;
  229. }
  230. // Instances
  231. if (useInstances) {
  232. this._defines.INSTANCES = true;
  233. }
  234. }
  235. // Get correct effect
  236. if (!this._defines.isEqual(this._cachedDefines)) {
  237. this._defines.cloneTo(this._cachedDefines);
  238. scene.resetCachedMaterial();
  239. // Fallbacks
  240. var fallbacks = new BABYLON.EffectFallbacks();
  241. if (this._defines.FOG) {
  242. fallbacks.addFallback(1, "FOG");
  243. }
  244. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  245. if (!this._defines["LIGHT" + lightIndex]) {
  246. continue;
  247. }
  248. if (lightIndex > 0) {
  249. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  250. }
  251. if (this._defines["SHADOW" + lightIndex]) {
  252. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  253. }
  254. if (this._defines["SHADOWPCF" + lightIndex]) {
  255. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  256. }
  257. if (this._defines["SHADOWVSM" + lightIndex]) {
  258. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  259. }
  260. }
  261. if (this._defines.BONES4) {
  262. fallbacks.addFallback(0, "BONES4");
  263. }
  264. //Attributes
  265. var attribs = [BABYLON.VertexBuffer.PositionKind];
  266. if (this._defines.NORMAL) {
  267. attribs.push(BABYLON.VertexBuffer.NormalKind);
  268. }
  269. if (this._defines.UV1) {
  270. attribs.push(BABYLON.VertexBuffer.UVKind);
  271. }
  272. if (this._defines.UV2) {
  273. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  274. }
  275. if (this._defines.VERTEXCOLOR) {
  276. attribs.push(BABYLON.VertexBuffer.ColorKind);
  277. }
  278. if (this._defines.BONES) {
  279. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  280. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  281. }
  282. if (this._defines.INSTANCES) {
  283. attribs.push("world0");
  284. attribs.push("world1");
  285. attribs.push("world2");
  286. attribs.push("world3");
  287. }
  288. // Legacy browser patch
  289. var shaderName = "lava";
  290. var join = this._defines.toString();
  291. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  292. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  293. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  294. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  295. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  296. "vFogInfos", "vFogColor", "pointSize",
  297. "vDiffuseInfos",
  298. "mBones",
  299. "vClipPlane", "diffuseMatrix",
  300. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "time", "speed", "fogColor"
  301. ], ["diffuseSampler",
  302. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3", "noiseTexture"
  303. ], join, fallbacks, this.onCompiled, this.onError);
  304. }
  305. if (!this._effect.isReady()) {
  306. return false;
  307. }
  308. this._renderId = scene.getRenderId();
  309. this._wasPreviouslyReady = true;
  310. if (mesh) {
  311. if (!mesh._materialDefines) {
  312. mesh._materialDefines = new LavaMaterialDefines();
  313. }
  314. this._defines.cloneTo(mesh._materialDefines);
  315. }
  316. return true;
  317. };
  318. LavaMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  319. this._effect.setMatrix("world", world);
  320. };
  321. LavaMaterial.prototype.bind = function (world, mesh) {
  322. var scene = this.getScene();
  323. // Matrices
  324. this.bindOnlyWorldMatrix(world);
  325. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  326. // Bones
  327. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  328. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  329. }
  330. if (scene.getCachedMaterial() !== this) {
  331. // Textures
  332. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  333. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  334. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  335. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  336. }
  337. if (this.noiseTexture) {
  338. this._effect.setTexture("noiseTexture", this.noiseTexture);
  339. }
  340. // Clip plane
  341. if (scene.clipPlane) {
  342. var clipPlane = scene.clipPlane;
  343. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  344. }
  345. // Point size
  346. if (this.pointsCloud) {
  347. this._effect.setFloat("pointSize", this.pointSize);
  348. }
  349. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  350. }
  351. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  352. if (scene.lightsEnabled && !this.disableLighting) {
  353. var lightIndex = 0;
  354. for (var index = 0; index < scene.lights.length; index++) {
  355. var light = scene.lights[index];
  356. if (!light.isEnabled()) {
  357. continue;
  358. }
  359. if (!light.canAffectMesh(mesh)) {
  360. continue;
  361. }
  362. if (light instanceof BABYLON.PointLight) {
  363. // Point Light
  364. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  365. }
  366. else if (light instanceof BABYLON.DirectionalLight) {
  367. // Directional Light
  368. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  369. }
  370. else if (light instanceof BABYLON.SpotLight) {
  371. // Spot Light
  372. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  373. }
  374. else if (light instanceof BABYLON.HemisphericLight) {
  375. // Hemispheric Light
  376. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  377. }
  378. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  379. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  380. // Shadows
  381. if (scene.shadowsEnabled) {
  382. var shadowGenerator = light.getShadowGenerator();
  383. if (mesh.receiveShadows && shadowGenerator) {
  384. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  385. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  386. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  387. }
  388. }
  389. lightIndex++;
  390. if (lightIndex === maxSimultaneousLights)
  391. break;
  392. }
  393. }
  394. // View
  395. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  396. this._effect.setMatrix("view", scene.getViewMatrix());
  397. }
  398. // Fog
  399. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  400. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  401. this._effect.setColor3("vFogColor", scene.fogColor);
  402. }
  403. this._lastTime += scene.getEngine().getDeltaTime();
  404. this._effect.setFloat("time", this._lastTime * this.speed / 1000);
  405. if (!this.fogColor) {
  406. this.fogColor = BABYLON.Color3.Black();
  407. }
  408. this._effect.setColor3("fogColor", this.fogColor);
  409. _super.prototype.bind.call(this, world, mesh);
  410. };
  411. LavaMaterial.prototype.getAnimatables = function () {
  412. var results = [];
  413. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  414. results.push(this.diffuseTexture);
  415. }
  416. if (this.noiseTexture && this.noiseTexture.animations && this.noiseTexture.animations.length > 0) {
  417. results.push(this.noiseTexture);
  418. }
  419. return results;
  420. };
  421. LavaMaterial.prototype.dispose = function (forceDisposeEffect) {
  422. if (this.diffuseTexture) {
  423. this.diffuseTexture.dispose();
  424. }
  425. if (this.noiseTexture) {
  426. this.noiseTexture.dispose();
  427. }
  428. _super.prototype.dispose.call(this, forceDisposeEffect);
  429. };
  430. LavaMaterial.prototype.clone = function (name) {
  431. var newMaterial = new LavaMaterial(name, this.getScene());
  432. // Base material
  433. this.copyTo(newMaterial);
  434. // Lava material
  435. if (this.diffuseTexture && this.diffuseTexture.clone) {
  436. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  437. }
  438. if (this.noiseTexture && this.noiseTexture.clone) {
  439. newMaterial.noiseTexture = this.noiseTexture.clone();
  440. }
  441. if (this.fogColor && this.fogColor.clone) {
  442. newMaterial.fogColor = this.fogColor.clone();
  443. }
  444. return newMaterial;
  445. };
  446. return LavaMaterial;
  447. })(BABYLON.Material);
  448. BABYLON.LavaMaterial = LavaMaterial;
  449. })(BABYLON || (BABYLON = {}));
  450. BABYLON.Effect.ShadersStore['lavaVertexShader'] = "precision highp float;\r\n// Inputs\r\nuniform float time;\r\nuniform float speed;\r\n// Varying\r\nvarying float noise;\r\n\r\n// Attributes\r\nattribute vec3 position;\r\n#ifdef NORMAL\r\nattribute vec3 normal;\r\n#endif\r\n#ifdef UV1\r\nattribute vec2 uv;\r\n#endif\r\n#ifdef UV2\r\nattribute vec2 uv2;\r\n#endif\r\n#ifdef VERTEXCOLOR\r\nattribute vec4 color;\r\n#endif\r\n#ifdef BONES\r\nattribute vec4 matricesIndices;\r\nattribute vec4 matricesWeights;\r\n#endif\r\n\r\n// Uniforms\r\n\r\n#ifdef INSTANCES\r\nattribute vec4 world0;\r\nattribute vec4 world1;\r\nattribute vec4 world2;\r\nattribute vec4 world3;\r\n#else\r\nuniform mat4 world;\r\n#endif\r\n\r\nuniform mat4 view;\r\nuniform mat4 viewProjection;\r\n\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform mat4 diffuseMatrix;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#ifdef BONES\r\nuniform mat4 mBones[BonesPerMesh];\r\n#endif\r\n\r\n#ifdef POINTSIZE\r\nuniform float pointSize;\r\n#endif\r\n\r\n// Output\r\nvarying vec3 vPositionW;\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n#ifdef CLIPPLANE\r\nuniform vec4 vClipPlane;\r\nvarying float fClipDistance;\r\n#endif\r\n\r\n#ifdef FOG\r\nvarying float fFogDistance;\r\n#endif\r\n\r\n#ifdef SHADOWS\r\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\r\nuniform mat4 lightMatrix0;\r\nvarying vec4 vPositionFromLight0;\r\n#endif\r\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\r\nuniform mat4 lightMatrix1;\r\nvarying vec4 vPositionFromLight1;\r\n#endif\r\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\r\nuniform mat4 lightMatrix2;\r\nvarying vec4 vPositionFromLight2;\r\n#endif\r\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\r\nuniform mat4 lightMatrix3;\r\nvarying vec4 vPositionFromLight3;\r\n#endif\r\n#endif\r\n\r\n/* NOISE FUNCTIONS */\r\n////// ASHIMA webgl noise\r\n////// https://github.com/ashima/webgl-noise/blob/master/src/classicnoise3D.glsl\r\nvec3 mod289(vec3 x)\r\n{\r\n return x - floor(x * (1.0 / 289.0)) * 289.0;\r\n}\r\n\r\nvec4 mod289(vec4 x)\r\n{\r\n return x - floor(x * (1.0 / 289.0)) * 289.0;\r\n}\r\n\r\nvec4 permute(vec4 x)\r\n{\r\n return mod289(((x*34.0)+1.0)*x);\r\n}\r\n\r\nvec4 taylorInvSqrt(vec4 r)\r\n{\r\n return 1.79284291400159 - 0.85373472095314 * r;\r\n}\r\n\r\nvec3 fade(vec3 t) {\r\n return t*t*t*(t*(t*6.0-15.0)+10.0);\r\n}\r\n\r\n// Classic Perlin noise, periodic variant\r\nfloat pnoise(vec3 P, vec3 rep)\r\n{\r\n vec3 Pi0 = mod(floor(P), rep); // Integer part, modulo period\r\n vec3 Pi1 = mod(Pi0 + vec3(1.0), rep); // Integer part + 1, mod period\r\n Pi0 = mod289(Pi0);\r\n Pi1 = mod289(Pi1);\r\n vec3 Pf0 = fract(P); // Fractional part for interpolation\r\n vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0\r\n vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);\r\n vec4 iy = vec4(Pi0.yy, Pi1.yy);\r\n vec4 iz0 = Pi0.zzzz;\r\n vec4 iz1 = Pi1.zzzz;\r\n\r\n vec4 ixy = permute(permute(ix) + iy);\r\n vec4 ixy0 = permute(ixy + iz0);\r\n vec4 ixy1 = permute(ixy + iz1);\r\n\r\n vec4 gx0 = ixy0 * (1.0 / 7.0);\r\n vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;\r\n gx0 = fract(gx0);\r\n vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);\r\n vec4 sz0 = step(gz0, vec4(0.0));\r\n gx0 -= sz0 * (step(0.0, gx0) - 0.5);\r\n gy0 -= sz0 * (step(0.0, gy0) - 0.5);\r\n\r\n vec4 gx1 = ixy1 * (1.0 / 7.0);\r\n vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;\r\n gx1 = fract(gx1);\r\n vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);\r\n vec4 sz1 = step(gz1, vec4(0.0));\r\n gx1 -= sz1 * (step(0.0, gx1) - 0.5);\r\n gy1 -= sz1 * (step(0.0, gy1) - 0.5);\r\n\r\n vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);\r\n vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);\r\n vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);\r\n vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);\r\n vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);\r\n vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);\r\n vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);\r\n vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);\r\n\r\n vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));\r\n g000 *= norm0.x;\r\n g010 *= norm0.y;\r\n g100 *= norm0.z;\r\n g110 *= norm0.w;\r\n vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));\r\n g001 *= norm1.x;\r\n g011 *= norm1.y;\r\n g101 *= norm1.z;\r\n g111 *= norm1.w;\r\n\r\n float n000 = dot(g000, Pf0);\r\n float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));\r\n float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));\r\n float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));\r\n float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));\r\n float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));\r\n float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));\r\n float n111 = dot(g111, Pf1);\r\n\r\n vec3 fade_xyz = fade(Pf0);\r\n vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);\r\n vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);\r\n float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);\r\n return 2.2 * n_xyz;\r\n}\r\n/* END FUNCTION */\r\n\r\nfloat turbulence( vec3 p ) {\r\n float w = 100.0;\r\n float t = -.5;\r\n for (float f = 1.0 ; f <= 10.0 ; f++ ){\r\n float power = pow( 2.0, f );\r\n t += abs( pnoise( vec3( power * p ), vec3( 10.0, 10.0, 10.0 ) ) / power );\r\n }\r\n return t;\r\n}\r\n\r\nvoid main(void) {\r\n\tmat4 finalWorld;\r\n\r\n#ifdef INSTANCES\r\n\tfinalWorld = mat4(world0, world1, world2, world3);\r\n#else\r\n\tfinalWorld = world;\r\n#endif\r\n\r\n#ifdef BONES\r\n\tmat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;\r\n\tmat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;\r\n\tmat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;\r\n\r\n#ifdef BONES4\r\n\tmat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;\r\n\tfinalWorld = finalWorld * (m0 + m1 + m2 + m3);\r\n#else\r\n\tfinalWorld = finalWorld * (m0 + m1 + m2);\r\n#endif \r\n\r\n#endif\r\n\r\n\r\n // get a turbulent 3d noise using the normal, normal to high freq\r\n noise = 10.0 * -.10 * turbulence( .5 * normal + time*1.15 );\r\n // get a 3d noise using the position, low frequency\r\n float b = 5.0 * pnoise( 0.05 * position +vec3(time*1.025), vec3( 100.0 ) );\r\n // compose both noises\r\n float displacement = - 1.5 * noise + b;\r\n\r\n // move the position along the normal and transform it\r\n vec3 newPosition = position + normal * displacement;\r\n gl_Position = viewProjection * finalWorld * vec4( newPosition, 1.0 );\r\n\r\n\r\n\tvec4 worldPos = finalWorld * vec4(newPosition, 1.0);\r\n\tvPositionW = vec3(worldPos);\r\n\r\n#ifdef NORMAL\r\n\tvNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\r\n#endif\r\n\r\n\t// Texture coordinates\r\n#ifndef UV1\r\n\tvec2 uv = vec2(0., 0.);\r\n#endif\r\n#ifndef UV2\r\n\tvec2 uv2 = vec2(0., 0.);\r\n#endif\r\n\r\n#ifdef DIFFUSE\r\n\tif (vDiffuseInfos.x == 0.)\r\n\t{\r\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));\r\n\t}\r\n\telse\r\n\t{\r\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));\r\n\t}\r\n#endif\r\n\r\n\t// Clip plane\r\n#ifdef CLIPPLANE\r\n\tfClipDistance = dot(worldPos, vClipPlane);\r\n#endif\r\n\r\n\t// Fog\r\n#ifdef FOG\r\n\tfFogDistance = (view * worldPos).z;\r\n#endif\r\n\r\n\t// Shadows\r\n#ifdef SHADOWS\r\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\r\n\tvPositionFromLight0 = lightMatrix0 * worldPos;\r\n#endif\r\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\r\n\tvPositionFromLight1 = lightMatrix1 * worldPos;\r\n#endif\r\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\r\n\tvPositionFromLight2 = lightMatrix2 * worldPos;\r\n#endif\r\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\r\n\tvPositionFromLight3 = lightMatrix3 * worldPos;\r\n#endif\r\n#endif\r\n\r\n\t// Vertex color\r\n#ifdef VERTEXCOLOR\r\n\tvColor = color;\r\n#endif\r\n\r\n\t// Point size\r\n#ifdef POINTSIZE\r\n\tgl_PointSize = pointSize;\r\n#endif\r\n}\r\n";
  451. BABYLON.Effect.ShadersStore['lavaPixelShader'] = "precision highp float;\r\n\r\n// Constants\r\nuniform vec3 vEyePosition;\r\nuniform vec4 vDiffuseColor;\r\n\r\n// Input\r\nvarying vec3 vPositionW;\r\n\r\n// MAGMAAAA\r\nuniform float time;\r\nuniform float speed;\r\nuniform vec3 fogColor;\r\nuniform sampler2D noiseTexture;\r\n\r\n// Varying\r\nvarying float noise;\r\n\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n// Lights\r\n#ifdef LIGHT0\r\nuniform vec4 vLightData0;\r\nuniform vec4 vLightDiffuse0;\r\n#ifdef SHADOW0\r\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\r\nvarying vec4 vPositionFromLight0;\r\nuniform sampler2D shadowSampler0;\r\n#else\r\nuniform samplerCube shadowSampler0;\r\n#endif\r\nuniform vec3 shadowsInfo0;\r\n#endif\r\n#ifdef SPOTLIGHT0\r\nuniform vec4 vLightDirection0;\r\n#endif\r\n#ifdef HEMILIGHT0\r\nuniform vec3 vLightGround0;\r\n#endif\r\n#endif\r\n\r\n#ifdef LIGHT1\r\nuniform vec4 vLightData1;\r\nuniform vec4 vLightDiffuse1;\r\n#ifdef SHADOW1\r\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\r\nvarying vec4 vPositionFromLight1;\r\nuniform sampler2D shadowSampler1;\r\n#else\r\nuniform samplerCube shadowSampler1;\r\n#endif\r\nuniform vec3 shadowsInfo1;\r\n#endif\r\n#ifdef SPOTLIGHT1\r\nuniform vec4 vLightDirection1;\r\n#endif\r\n#ifdef HEMILIGHT1\r\nuniform vec3 vLightGround1;\r\n#endif\r\n#endif\r\n\r\n#ifdef LIGHT2\r\nuniform vec4 vLightData2;\r\nuniform vec4 vLightDiffuse2;\r\n#ifdef SHADOW2\r\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\r\nvarying vec4 vPositionFromLight2;\r\nuniform sampler2D shadowSampler2;\r\n#else\r\nuniform samplerCube shadowSampler2;\r\n#endif\r\nuniform vec3 shadowsInfo2;\r\n#endif\r\n#ifdef SPOTLIGHT2\r\nuniform vec4 vLightDirection2;\r\n#endif\r\n#ifdef HEMILIGHT2\r\nuniform vec3 vLightGround2;\r\n#endif\r\n#endif\r\n\r\n#ifdef LIGHT3\r\nuniform vec4 vLightData3;\r\nuniform vec4 vLightDiffuse3;\r\n#ifdef SHADOW3\r\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\r\nvarying vec4 vPositionFromLight3;\r\nuniform sampler2D shadowSampler3;\r\n#else\r\nuniform samplerCube shadowSampler3;\r\n#endif\r\nuniform vec3 shadowsInfo3;\r\n#endif\r\n#ifdef SPOTLIGHT3\r\nuniform vec4 vLightDirection3;\r\n#endif\r\n#ifdef HEMILIGHT3\r\nuniform vec3 vLightGround3;\r\n#endif\r\n#endif\r\n\r\n// Samplers\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform sampler2D diffuseSampler;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n// Shadows\r\n#ifdef SHADOWS\r\n\r\nfloat unpack(vec4 color)\r\n{\r\n\tconst vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);\r\n\treturn dot(color, bit_shift);\r\n}\r\n\r\n#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)\r\nfloat computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)\r\n{\r\n\tvec3 directionToLight = vPositionW - lightPosition;\r\n\tfloat depth = length(directionToLight);\r\n\r\n\tdepth = clamp(depth, 0., 1.);\r\n\r\n\tdirectionToLight.y = 1.0 - directionToLight.y;\r\n\r\n\tfloat shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;\r\n\r\n\tif (depth > shadow)\r\n\t{\r\n\t\treturn darkness;\r\n\t}\r\n\treturn 1.0;\r\n}\r\n\r\nfloat computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float bias, float darkness)\r\n{\r\n\tvec3 directionToLight = vPositionW - lightPosition;\r\n\tfloat depth = length(directionToLight);\r\n\r\n\tdepth = clamp(depth, 0., 1.);\r\n\r\n\tdirectionToLight.y = 1.0 - directionToLight.y;\r\n\r\n\tfloat visibility = 1.;\r\n\r\n\tvec3 poissonDisk[4];\r\n\tpoissonDisk[0] = vec3(-0.094201624, 0.04, -0.039906216);\r\n\tpoissonDisk[1] = vec3(0.094558609, -0.04, -0.076890725);\r\n\tpoissonDisk[2] = vec3(-0.094184101, 0.01, -0.092938870);\r\n\tpoissonDisk[3] = vec3(0.034495938, -0.01, 0.029387760);\r\n\r\n\t// Poisson Sampling\r\n\tfloat biasedDepth = depth - bias;\r\n\r\n\tif (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0])) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1])) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2])) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3])) < biasedDepth) visibility -= 0.25;\r\n\r\n\treturn min(1.0, visibility + darkness);\r\n}\r\n#endif\r\n\r\n#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)\r\nfloat computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)\r\n{\r\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\r\n\tdepth = 0.5 * depth + vec3(0.5);\r\n\tvec2 uv = depth.xy;\r\n\r\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\r\n\t{\r\n\t\treturn 1.0;\r\n\t}\r\n\r\n\tfloat shadow = unpack(texture2D(shadowSampler, uv)) + bias;\r\n\r\n\tif (depth.z > shadow)\r\n\t{\r\n\t\treturn darkness;\r\n\t}\r\n\treturn 1.;\r\n}\r\n\r\nfloat computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)\r\n{\r\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\r\n\tdepth = 0.5 * depth + vec3(0.5);\r\n\tvec2 uv = depth.xy;\r\n\r\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\r\n\t{\r\n\t\treturn 1.0;\r\n\t}\r\n\r\n\tfloat visibility = 1.;\r\n\r\n\tvec2 poissonDisk[4];\r\n\tpoissonDisk[0] = vec2(-0.94201624, -0.39906216);\r\n\tpoissonDisk[1] = vec2(0.94558609, -0.76890725);\r\n\tpoissonDisk[2] = vec2(-0.094184101, -0.92938870);\r\n\tpoissonDisk[3] = vec2(0.34495938, 0.29387760);\r\n\r\n\t// Poisson Sampling\r\n\tfloat biasedDepth = depth.z - bias;\r\n\r\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;\r\n\r\n\treturn min(1.0, visibility + darkness);\r\n}\r\n\r\n// Thanks to http://devmaster.net/\r\nfloat unpackHalf(vec2 color)\r\n{\r\n\treturn color.x + (color.y / 255.0);\r\n}\r\n\r\nfloat linstep(float low, float high, float v) {\r\n\treturn clamp((v - low) / (high - low), 0.0, 1.0);\r\n}\r\n\r\nfloat ChebychevInequality(vec2 moments, float compare, float bias)\r\n{\r\n\tfloat p = smoothstep(compare - bias, compare, moments.x);\r\n\tfloat variance = max(moments.y - moments.x * moments.x, 0.02);\r\n\tfloat d = compare - moments.x;\r\n\tfloat p_max = linstep(0.2, 1.0, variance / (variance + d * d));\r\n\r\n\treturn clamp(max(p, p_max), 0.0, 1.0);\r\n}\r\n\r\nfloat computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)\r\n{\r\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\r\n\tdepth = 0.5 * depth + vec3(0.5);\r\n\tvec2 uv = depth.xy;\r\n\r\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)\r\n\t{\r\n\t\treturn 1.0;\r\n\t}\r\n\r\n\tvec4 texel = texture2D(shadowSampler, uv);\r\n\r\n\tvec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));\r\n\treturn min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);\r\n}\r\n#endif\r\n#endif\r\n\r\n\r\n#ifdef CLIPPLANE\r\nvarying float fClipDistance;\r\n#endif\r\n\r\n// Fog\r\n#ifdef FOG\r\n\r\n#define FOGMODE_NONE 0.\r\n#define FOGMODE_EXP 1.\r\n#define FOGMODE_EXP2 2.\r\n#define FOGMODE_LINEAR 3.\r\n#define E 2.71828\r\n\r\nuniform vec4 vFogInfos;\r\nuniform vec3 vFogColor;\r\nvarying float fFogDistance;\r\n\r\nfloat CalcFogFactor()\r\n{\r\n\tfloat fogCoeff = 1.0;\r\n\tfloat fogStart = vFogInfos.y;\r\n\tfloat fogEnd = vFogInfos.z;\r\n\tfloat fogDensity = vFogInfos.w;\r\n\r\n\tif (FOGMODE_LINEAR == vFogInfos.x)\r\n\t{\r\n\t\tfogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);\r\n\t}\r\n\telse if (FOGMODE_EXP == vFogInfos.x)\r\n\t{\r\n\t\tfogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);\r\n\t}\r\n\telse if (FOGMODE_EXP2 == vFogInfos.x)\r\n\t{\r\n\t\tfogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);\r\n\t}\r\n\r\n\treturn clamp(fogCoeff, 0.0, 1.0);\r\n}\r\n#endif\r\n\r\n// Light Computing\r\nstruct lightingInfo\r\n{\r\n\tvec3 diffuse;\r\n};\r\n\r\nlightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, float range) {\r\n\tlightingInfo result;\r\n\r\n\tvec3 lightVectorW;\r\n\tfloat attenuation = 1.0;\r\n\tif (lightData.w == 0.)\r\n\t{\r\n\t\tvec3 direction = lightData.xyz - vPositionW;\r\n\r\n\t\tattenuation = max(0., 1.0 - length(direction) / range);\r\n\t\tlightVectorW = normalize(direction);\r\n\t}\r\n\telse\r\n\t{\r\n\t\tlightVectorW = normalize(-lightData.xyz);\r\n\t}\r\n\r\n\t// diffuse\r\n\tfloat ndl = max(0., dot(vNormal, lightVectorW));\r\n\tresult.diffuse = ndl * diffuseColor * attenuation;\r\n\r\n\treturn result;\r\n}\r\n\r\nlightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, float range) {\r\n\tlightingInfo result;\r\n\r\n\tvec3 direction = lightData.xyz - vPositionW;\r\n\tvec3 lightVectorW = normalize(direction);\r\n\tfloat attenuation = max(0., 1.0 - length(direction) / range);\r\n\r\n\t// diffuse\r\n\tfloat cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));\r\n\tfloat spotAtten = 0.0;\r\n\r\n\tif (cosAngle >= lightDirection.w)\r\n\t{\r\n\t\tcosAngle = max(0., pow(cosAngle, lightData.w));\r\n\t\tspotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);\r\n\r\n\t\t// Diffuse\r\n\t\tfloat ndl = max(0., dot(vNormal, -lightDirection.xyz));\r\n\t\tresult.diffuse = ndl * spotAtten * diffuseColor * attenuation;\r\n\r\n\t\treturn result;\r\n\t}\r\n\r\n\tresult.diffuse = vec3(0.);\r\n\r\n\treturn result;\r\n}\r\n\r\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 groundColor) {\r\n\tlightingInfo result;\r\n\r\n\t// Diffuse\r\n\tfloat ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;\r\n\tresult.diffuse = mix(groundColor, diffuseColor, ndl);\r\n\r\n\treturn result;\r\n}\r\n\r\n\r\nfloat random( vec3 scale, float seed ){\r\n return fract( sin( dot( gl_FragCoord.xyz + seed, scale ) ) * 43758.5453 + seed ) ;\r\n}\r\n\r\n\r\nvoid main(void) {\r\n\t// Clip plane\r\n#ifdef CLIPPLANE\r\n\tif (fClipDistance > 0.0)\r\n\t\tdiscard;\r\n#endif\r\n\r\n\tvec3 viewDirectionW = normalize(vEyePosition - vPositionW);\r\n\r\n\t// Base color\r\n\tvec4 baseColor = vec4(1., 1., 1., 1.);\r\n\tvec3 diffuseColor = vDiffuseColor.rgb;\r\n\r\n\t// Alpha\r\n\tfloat alpha = vDiffuseColor.a;\r\n\r\n\r\n\r\n#ifdef DIFFUSE\r\n ////// MAGMA ///\r\n\r\n\tvec4 noiseTex = texture2D( noiseTexture, vDiffuseUV );\r\n\tvec2 T1 = vDiffuseUV + vec2( 1.5, -1.5 ) * time * 0.02;\r\n\tvec2 T2 = vDiffuseUV + vec2( -0.5, 2.0 ) * time * 0.01 * speed;\r\n\r\n\tT1.x += noiseTex.x * 2.0;\r\n\tT1.y += noiseTex.y * 2.0;\r\n\tT2.x -= noiseTex.y * 0.2;\r\n\tT2.y += noiseTex.z * 0.2;\r\n\r\n\tfloat p = texture2D( noiseTexture, T1 * 3.0 ).a;\r\n\r\n\tvec4 lavaColor = texture2D( diffuseSampler, T2 * 4.0);\r\n\tvec4 temp = lavaColor * ( vec4( p, p, p, p ) * 2.0 ) + ( lavaColor * lavaColor - 0.1 );\r\n\r\n\tbaseColor = temp;\r\n\r\n\tfloat fogDensity = 0.15;\r\n\tfloat depth = gl_FragCoord.z * 4.0;\r\n\tconst float LOG2 = 1.442695;\r\n float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );\r\n fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );\r\n\r\n baseColor = mix( baseColor, vec4( fogColor, baseColor.w ), fogFactor );\r\n\r\n ///// END MAGMA ////\r\n\r\n\r\n\r\n//\tbaseColor = texture2D(diffuseSampler, vDiffuseUV);\r\n\r\n#ifdef ALPHATEST\r\n\tif (baseColor.a < 0.4)\r\n\t\tdiscard;\r\n#endif\r\n\r\n\tbaseColor.rgb *= vDiffuseInfos.y;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\n\tbaseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n\t// Bump\r\n#ifdef NORMAL\r\n\tvec3 normalW = normalize(vNormalW);\r\n#else\r\n\tvec3 normalW = vec3(1.0, 1.0, 1.0);\r\n#endif\r\n\r\n\t// Lighting\r\n\tvec3 diffuseBase = vec3(0., 0., 0.);\r\n\tfloat shadow = 1.;\r\n\r\n#ifdef LIGHT0\r\n#ifdef SPOTLIGHT0\r\n\tlightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightDiffuse0.a);\r\n#endif\r\n#ifdef HEMILIGHT0\r\n\tlightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightGround0);\r\n#endif\r\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\r\n\tlightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightDiffuse0.a);\r\n#endif\r\n#ifdef SHADOW0\r\n#ifdef SHADOWVSM0\r\n\tshadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);\r\n#else\r\n#ifdef SHADOWPCF0\r\n\t#if defined(POINTLIGHT0)\r\n\tshadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);\r\n\t#else\r\n\tshadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\r\n\t#endif\r\n#else\r\n\t#if defined(POINTLIGHT0)\r\n\tshadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\r\n\t#else\r\n\tshadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\r\n\t#endif\r\n#endif\r\n#endif\r\n#else\r\n\tshadow = 1.;\r\n#endif\r\n\tdiffuseBase += info.diffuse * shadow;\r\n#endif\r\n\r\n#ifdef LIGHT1\r\n#ifdef SPOTLIGHT1\r\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightDiffuse1.a);\r\n#endif\r\n#ifdef HEMILIGHT1\r\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightGround1.a);\r\n#endif\r\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\r\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightDiffuse1.a);\r\n#endif\r\n#ifdef SHADOW1\r\n#ifdef SHADOWVSM1\r\n\tshadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);\r\n#else\r\n#ifdef SHADOWPCF1\r\n#if defined(POINTLIGHT1)\r\n\tshadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);\r\n#else\r\n\tshadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\r\n#endif\r\n#else\r\n\t#if defined(POINTLIGHT1)\r\n\tshadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\r\n\t#else\r\n\tshadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\r\n\t#endif\r\n#endif\r\n#endif\r\n#else\r\n\tshadow = 1.;\r\n#endif\r\n\tdiffuseBase += info.diffuse * shadow;\r\n#endif\r\n\r\n#ifdef LIGHT2\r\n#ifdef SPOTLIGHT2\r\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightDiffuse2.a);\r\n#endif\r\n#ifdef HEMILIGHT2\r\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightGround2);\r\n#endif\r\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\r\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightDiffuse2.a);\r\n#endif\r\n#ifdef SHADOW2\r\n#ifdef SHADOWVSM2\r\n\tshadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);\r\n#else\r\n#ifdef SHADOWPCF2\r\n#if defined(POINTLIGHT2)\r\n\tshadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);\r\n#else\r\n\tshadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\r\n#endif\r\n#else\r\n\t#if defined(POINTLIGHT2)\r\n\tshadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\r\n\t#else\r\n\tshadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\r\n\t#endif\r\n#endif\t\r\n#endif\t\r\n#else\r\n\tshadow = 1.;\r\n#endif\r\n\tdiffuseBase += info.diffuse * shadow;\r\n#endif\r\n\r\n#ifdef LIGHT3\r\n#ifdef SPOTLIGHT3\r\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightDiffuse3.a);\r\n#endif\r\n#ifdef HEMILIGHT3\r\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightGround3);\r\n#endif\r\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\r\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightDiffuse3.a);\r\n#endif\r\n#ifdef SHADOW3\r\n#ifdef SHADOWVSM3\r\n\t\tshadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);\r\n#else\r\n#ifdef SHADOWPCF3\r\n#if defined(POINTLIGHT3)\r\n\tshadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);\r\n#else\r\n\tshadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\r\n#endif\r\n#else\r\n\t#if defined(POINTLIGHT3)\r\n\tshadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\r\n\t#else\r\n\tshadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\r\n\t#endif\r\n#endif\t\r\n#endif\t\r\n#else\r\n\tshadow = 1.;\r\n#endif\r\n\tdiffuseBase += info.diffuse * shadow;\r\n#endif\r\n\r\n#ifdef VERTEXALPHA\r\n\talpha *= vColor.a;\r\n#endif\r\n\r\n\tvec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;\r\n\r\n\t// Composition\r\n\tvec4 color = vec4(finalDiffuse, alpha);\r\n\r\n#ifdef FOG\r\n\tfloat fog = CalcFogFactor();\r\n\tcolor.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;\r\n#endif\r\n\r\n\r\n\tgl_FragColor = color;\r\n}";