babylon.terrainMaterial.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
  4. switch (arguments.length) {
  5. case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
  6. case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
  7. case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
  8. }
  9. };
  10. var BABYLON;
  11. (function (BABYLON) {
  12. var maxSimultaneousLights = 4;
  13. var TerrainMaterialDefines = (function (_super) {
  14. __extends(TerrainMaterialDefines, _super);
  15. function TerrainMaterialDefines() {
  16. _super.call(this);
  17. this.DIFFUSE = false;
  18. this.BUMP = false;
  19. this.CLIPPLANE = false;
  20. this.ALPHATEST = false;
  21. this.POINTSIZE = false;
  22. this.FOG = false;
  23. this.LIGHT0 = false;
  24. this.LIGHT1 = false;
  25. this.LIGHT2 = false;
  26. this.LIGHT3 = false;
  27. this.SPOTLIGHT0 = false;
  28. this.SPOTLIGHT1 = false;
  29. this.SPOTLIGHT2 = false;
  30. this.SPOTLIGHT3 = false;
  31. this.HEMILIGHT0 = false;
  32. this.HEMILIGHT1 = false;
  33. this.HEMILIGHT2 = false;
  34. this.HEMILIGHT3 = false;
  35. this.DIRLIGHT0 = false;
  36. this.DIRLIGHT1 = false;
  37. this.DIRLIGHT2 = false;
  38. this.DIRLIGHT3 = false;
  39. this.POINTLIGHT0 = false;
  40. this.POINTLIGHT1 = false;
  41. this.POINTLIGHT2 = false;
  42. this.POINTLIGHT3 = false;
  43. this.SHADOW0 = false;
  44. this.SHADOW1 = false;
  45. this.SHADOW2 = false;
  46. this.SHADOW3 = false;
  47. this.SHADOWS = false;
  48. this.SHADOWVSM0 = false;
  49. this.SHADOWVSM1 = false;
  50. this.SHADOWVSM2 = false;
  51. this.SHADOWVSM3 = false;
  52. this.SHADOWPCF0 = false;
  53. this.SHADOWPCF1 = false;
  54. this.SHADOWPCF2 = false;
  55. this.SHADOWPCF3 = false;
  56. this.SPECULARTERM = false;
  57. this.NORMAL = false;
  58. this.UV1 = false;
  59. this.UV2 = false;
  60. this.VERTEXCOLOR = false;
  61. this.VERTEXALPHA = false;
  62. this.BONES = false;
  63. this.BONES4 = false;
  64. this.BonesPerMesh = 0;
  65. this.INSTANCES = false;
  66. this._keys = Object.keys(this);
  67. }
  68. return TerrainMaterialDefines;
  69. })(BABYLON.MaterialDefines);
  70. var TerrainMaterial = (function (_super) {
  71. __extends(TerrainMaterial, _super);
  72. function TerrainMaterial(name, scene) {
  73. _super.call(this, name, scene);
  74. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  75. this.specularColor = new BABYLON.Color3(0, 0, 0);
  76. this.specularPower = 64;
  77. this.disableLighting = false;
  78. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  79. this._scaledDiffuse = new BABYLON.Color3();
  80. this._scaledSpecular = new BABYLON.Color3();
  81. this._defines = new TerrainMaterialDefines();
  82. this._cachedDefines = new TerrainMaterialDefines();
  83. this._cachedDefines.BonesPerMesh = -1;
  84. }
  85. TerrainMaterial.prototype.needAlphaBlending = function () {
  86. return (this.alpha < 1.0);
  87. };
  88. TerrainMaterial.prototype.needAlphaTesting = function () {
  89. return false;
  90. };
  91. TerrainMaterial.prototype.getAlphaTestTexture = function () {
  92. return null;
  93. };
  94. // Methods
  95. TerrainMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  96. if (!mesh) {
  97. return true;
  98. }
  99. if (this._defines.INSTANCES !== useInstances) {
  100. return false;
  101. }
  102. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  103. return true;
  104. }
  105. return false;
  106. };
  107. TerrainMaterial.prototype.isReady = function (mesh, useInstances) {
  108. if (this.checkReadyOnlyOnce) {
  109. if (this._wasPreviouslyReady) {
  110. return true;
  111. }
  112. }
  113. var scene = this.getScene();
  114. if (!this.checkReadyOnEveryCall) {
  115. if (this._renderId === scene.getRenderId()) {
  116. if (this._checkCache(scene, mesh, useInstances)) {
  117. return true;
  118. }
  119. }
  120. }
  121. var engine = scene.getEngine();
  122. var needNormals = false;
  123. var needUVs = false;
  124. this._defines.reset();
  125. // Textures
  126. if (scene.texturesEnabled) {
  127. if (this.mixTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  128. if (!this.mixTexture.isReady()) {
  129. return false;
  130. }
  131. else {
  132. needUVs = true;
  133. this._defines.DIFFUSE = true;
  134. }
  135. }
  136. if ((this.bumpTexture1 || this.bumpTexture2 || this.bumpTexture3) && BABYLON.StandardMaterial.BumpTextureEnabled) {
  137. needUVs = true;
  138. needNormals = true;
  139. this._defines.BUMP = true;
  140. }
  141. }
  142. // Effect
  143. if (scene.clipPlane) {
  144. this._defines.CLIPPLANE = true;
  145. }
  146. if (engine.getAlphaTesting()) {
  147. this._defines.ALPHATEST = true;
  148. }
  149. // Point size
  150. if (this.pointsCloud || scene.forcePointsCloud) {
  151. this._defines.POINTSIZE = true;
  152. }
  153. // Fog
  154. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  155. this._defines.FOG = true;
  156. }
  157. var lightIndex = 0;
  158. if (scene.lightsEnabled && !this.disableLighting) {
  159. for (var index = 0; index < scene.lights.length; index++) {
  160. var light = scene.lights[index];
  161. if (!light.isEnabled()) {
  162. continue;
  163. }
  164. // Excluded check
  165. if (light._excludedMeshesIds.length > 0) {
  166. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  167. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  168. if (excludedMesh) {
  169. light.excludedMeshes.push(excludedMesh);
  170. }
  171. }
  172. light._excludedMeshesIds = [];
  173. }
  174. // Included check
  175. if (light._includedOnlyMeshesIds.length > 0) {
  176. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  177. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  178. if (includedOnlyMesh) {
  179. light.includedOnlyMeshes.push(includedOnlyMesh);
  180. }
  181. }
  182. light._includedOnlyMeshesIds = [];
  183. }
  184. if (!light.canAffectMesh(mesh)) {
  185. continue;
  186. }
  187. needNormals = true;
  188. this._defines["LIGHT" + lightIndex] = true;
  189. var type;
  190. if (light instanceof BABYLON.SpotLight) {
  191. type = "SPOTLIGHT" + lightIndex;
  192. }
  193. else if (light instanceof BABYLON.HemisphericLight) {
  194. type = "HEMILIGHT" + lightIndex;
  195. }
  196. else if (light instanceof BABYLON.PointLight) {
  197. type = "POINTLIGHT" + lightIndex;
  198. }
  199. else {
  200. type = "DIRLIGHT" + lightIndex;
  201. }
  202. this._defines[type] = true;
  203. // Specular
  204. if (!light.specular.equalsFloats(0, 0, 0)) {
  205. this._defines.SPECULARTERM = true;
  206. }
  207. // Shadows
  208. if (scene.shadowsEnabled) {
  209. var shadowGenerator = light.getShadowGenerator();
  210. if (mesh && mesh.receiveShadows && shadowGenerator) {
  211. this._defines["SHADOW" + lightIndex] = true;
  212. this._defines.SHADOWS = true;
  213. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  214. this._defines["SHADOWVSM" + lightIndex] = true;
  215. }
  216. if (shadowGenerator.usePoissonSampling) {
  217. this._defines["SHADOWPCF" + lightIndex] = true;
  218. }
  219. }
  220. }
  221. lightIndex++;
  222. if (lightIndex === maxSimultaneousLights)
  223. break;
  224. }
  225. }
  226. // Attribs
  227. if (mesh) {
  228. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  229. this._defines.NORMAL = true;
  230. }
  231. if (needUVs) {
  232. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  233. this._defines.UV1 = true;
  234. }
  235. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  236. this._defines.UV2 = true;
  237. }
  238. }
  239. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  240. this._defines.VERTEXCOLOR = true;
  241. if (mesh.hasVertexAlpha) {
  242. this._defines.VERTEXALPHA = true;
  243. }
  244. }
  245. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  246. this._defines.BONES = true;
  247. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  248. this._defines.BONES4 = true;
  249. }
  250. // Instances
  251. if (useInstances) {
  252. this._defines.INSTANCES = true;
  253. }
  254. }
  255. // Get correct effect
  256. if (!this._defines.isEqual(this._cachedDefines)) {
  257. this._defines.cloneTo(this._cachedDefines);
  258. scene.resetCachedMaterial();
  259. // Fallbacks
  260. var fallbacks = new BABYLON.EffectFallbacks();
  261. if (this._defines.FOG) {
  262. fallbacks.addFallback(1, "FOG");
  263. }
  264. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  265. if (!this._defines["LIGHT" + lightIndex]) {
  266. continue;
  267. }
  268. if (lightIndex > 0) {
  269. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  270. }
  271. if (this._defines["SHADOW" + lightIndex]) {
  272. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  273. }
  274. if (this._defines["SHADOWPCF" + lightIndex]) {
  275. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  276. }
  277. if (this._defines["SHADOWVSM" + lightIndex]) {
  278. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  279. }
  280. }
  281. if (this._defines.BONES4) {
  282. fallbacks.addFallback(0, "BONES4");
  283. }
  284. //Attributes
  285. var attribs = [BABYLON.VertexBuffer.PositionKind];
  286. if (this._defines.NORMAL) {
  287. attribs.push(BABYLON.VertexBuffer.NormalKind);
  288. }
  289. if (this._defines.UV1) {
  290. attribs.push(BABYLON.VertexBuffer.UVKind);
  291. }
  292. if (this._defines.UV2) {
  293. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  294. }
  295. if (this._defines.VERTEXCOLOR) {
  296. attribs.push(BABYLON.VertexBuffer.ColorKind);
  297. }
  298. if (this._defines.BONES) {
  299. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  300. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  301. }
  302. if (this._defines.INSTANCES) {
  303. attribs.push("world0");
  304. attribs.push("world1");
  305. attribs.push("world2");
  306. attribs.push("world3");
  307. }
  308. // Legacy browser patch
  309. var shaderName = "terrain";
  310. var join = this._defines.toString();
  311. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  312. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  313. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  314. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  315. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  316. "vFogInfos", "vFogColor", "pointSize",
  317. "vTextureInfos",
  318. "mBones",
  319. "vClipPlane", "textureMatrix",
  320. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  321. "diffuse1Infos", "diffuse2Infos", "diffuse3Infos"
  322. ], ["textureSampler", "diffuse1Sampler", "diffuse2Sampler", "diffuse3Sampler",
  323. "bump1Sampler", "bump2Sampler", "bump3Sampler",
  324. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  325. ], join, fallbacks, this.onCompiled, this.onError);
  326. }
  327. if (!this._effect.isReady()) {
  328. return false;
  329. }
  330. this._renderId = scene.getRenderId();
  331. this._wasPreviouslyReady = true;
  332. if (mesh) {
  333. if (!mesh._materialDefines) {
  334. mesh._materialDefines = new TerrainMaterialDefines();
  335. }
  336. this._defines.cloneTo(mesh._materialDefines);
  337. }
  338. return true;
  339. };
  340. TerrainMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  341. this._effect.setMatrix("world", world);
  342. };
  343. TerrainMaterial.prototype.bind = function (world, mesh) {
  344. var scene = this.getScene();
  345. // Matrices
  346. this.bindOnlyWorldMatrix(world);
  347. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  348. // Bones
  349. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  350. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  351. }
  352. if (scene.getCachedMaterial() !== this) {
  353. // Textures
  354. if (this.mixTexture) {
  355. this._effect.setTexture("textureSampler", this.mixTexture);
  356. this._effect.setFloat2("vTextureInfos", this.mixTexture.coordinatesIndex, this.mixTexture.level);
  357. this._effect.setMatrix("textureMatrix", this.mixTexture.getTextureMatrix());
  358. if (BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  359. if (this.diffuseTexture1) {
  360. this._effect.setTexture("diffuse1Sampler", this.diffuseTexture1);
  361. this._effect.setFloat2("diffuse1Infos", this.diffuseTexture1.uScale, this.diffuseTexture1.vScale);
  362. }
  363. if (this.diffuseTexture2) {
  364. this._effect.setTexture("diffuse2Sampler", this.diffuseTexture2);
  365. this._effect.setFloat2("diffuse2Infos", this.diffuseTexture2.uScale, this.diffuseTexture2.vScale);
  366. }
  367. if (this.diffuseTexture3) {
  368. this._effect.setTexture("diffuse3Sampler", this.diffuseTexture3);
  369. this._effect.setFloat2("diffuse3Infos", this.diffuseTexture3.uScale, this.diffuseTexture3.vScale);
  370. }
  371. }
  372. if (BABYLON.StandardMaterial.BumpTextureEnabled && scene.getEngine().getCaps().standardDerivatives) {
  373. if (this.bumpTexture1) {
  374. this._effect.setTexture("bump1Sampler", this.bumpTexture1);
  375. }
  376. if (this.bumpTexture2) {
  377. this._effect.setTexture("bump2Sampler", this.bumpTexture2);
  378. }
  379. if (this.bumpTexture3) {
  380. this._effect.setTexture("bump3Sampler", this.bumpTexture3);
  381. }
  382. }
  383. }
  384. // Clip plane
  385. if (scene.clipPlane) {
  386. var clipPlane = scene.clipPlane;
  387. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  388. }
  389. // Point size
  390. if (this.pointsCloud) {
  391. this._effect.setFloat("pointSize", this.pointSize);
  392. }
  393. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  394. }
  395. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  396. if (this._defines.SPECULARTERM) {
  397. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  398. }
  399. if (scene.lightsEnabled && !this.disableLighting) {
  400. var lightIndex = 0;
  401. for (var index = 0; index < scene.lights.length; index++) {
  402. var light = scene.lights[index];
  403. if (!light.isEnabled()) {
  404. continue;
  405. }
  406. if (!light.canAffectMesh(mesh)) {
  407. continue;
  408. }
  409. if (light instanceof BABYLON.PointLight) {
  410. // Point Light
  411. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  412. }
  413. else if (light instanceof BABYLON.DirectionalLight) {
  414. // Directional Light
  415. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  416. }
  417. else if (light instanceof BABYLON.SpotLight) {
  418. // Spot Light
  419. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  420. }
  421. else if (light instanceof BABYLON.HemisphericLight) {
  422. // Hemispheric Light
  423. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  424. }
  425. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  426. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  427. if (this._defines.SPECULARTERM) {
  428. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  429. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  430. }
  431. // Shadows
  432. if (scene.shadowsEnabled) {
  433. var shadowGenerator = light.getShadowGenerator();
  434. if (mesh.receiveShadows && shadowGenerator) {
  435. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  436. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  437. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  438. }
  439. }
  440. lightIndex++;
  441. if (lightIndex === maxSimultaneousLights)
  442. break;
  443. }
  444. }
  445. // View
  446. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  447. this._effect.setMatrix("view", scene.getViewMatrix());
  448. }
  449. // Fog
  450. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  451. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  452. this._effect.setColor3("vFogColor", scene.fogColor);
  453. }
  454. _super.prototype.bind.call(this, world, mesh);
  455. };
  456. TerrainMaterial.prototype.getAnimatables = function () {
  457. var results = [];
  458. if (this.mixTexture && this.mixTexture.animations && this.mixTexture.animations.length > 0) {
  459. results.push(this.mixTexture);
  460. }
  461. return results;
  462. };
  463. TerrainMaterial.prototype.dispose = function (forceDisposeEffect) {
  464. if (this.mixTexture) {
  465. this.mixTexture.dispose();
  466. }
  467. _super.prototype.dispose.call(this, forceDisposeEffect);
  468. };
  469. TerrainMaterial.prototype.clone = function (name) {
  470. var _this = this;
  471. return BABYLON.SerializationHelper.Clone(function () { return new TerrainMaterial(name, _this.getScene()); }, this);
  472. };
  473. TerrainMaterial.prototype.serialize = function () {
  474. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  475. serializationObject.customType = "BABYLON.TerrainMaterial";
  476. return serializationObject;
  477. };
  478. // Statics
  479. TerrainMaterial.Parse = function (source, scene, rootUrl) {
  480. return BABYLON.SerializationHelper.Parse(function () { return new TerrainMaterial(source.name, scene); }, source, scene, rootUrl);
  481. };
  482. __decorate([
  483. BABYLON.serializeAsTexture()
  484. ], TerrainMaterial.prototype, "mixTexture");
  485. __decorate([
  486. BABYLON.serializeAsTexture()
  487. ], TerrainMaterial.prototype, "diffuseTexture1");
  488. __decorate([
  489. BABYLON.serializeAsTexture()
  490. ], TerrainMaterial.prototype, "diffuseTexture2");
  491. __decorate([
  492. BABYLON.serializeAsTexture()
  493. ], TerrainMaterial.prototype, "diffuseTexture3");
  494. __decorate([
  495. BABYLON.serializeAsTexture()
  496. ], TerrainMaterial.prototype, "bumpTexture1");
  497. __decorate([
  498. BABYLON.serializeAsTexture()
  499. ], TerrainMaterial.prototype, "bumpTexture2");
  500. __decorate([
  501. BABYLON.serializeAsTexture()
  502. ], TerrainMaterial.prototype, "bumpTexture3");
  503. __decorate([
  504. BABYLON.serializeAsColor3()
  505. ], TerrainMaterial.prototype, "diffuseColor");
  506. __decorate([
  507. BABYLON.serializeAsColor3()
  508. ], TerrainMaterial.prototype, "specularColor");
  509. __decorate([
  510. BABYLON.serialize()
  511. ], TerrainMaterial.prototype, "specularPower");
  512. __decorate([
  513. BABYLON.serialize()
  514. ], TerrainMaterial.prototype, "disableLighting");
  515. return TerrainMaterial;
  516. })(BABYLON.Material);
  517. BABYLON.TerrainMaterial = TerrainMaterial;
  518. })(BABYLON || (BABYLON = {}));
  519. BABYLON.Effect.ShadersStore['terrainVertexShader'] = "precision highp float;\n\nattribute vec3 position;\n#ifdef NORMAL\nattribute vec3 normal;\n#endif\n#ifdef UV1\nattribute vec2 uv;\n#endif\n#ifdef UV2\nattribute vec2 uv2;\n#endif\n#ifdef VERTEXCOLOR\nattribute vec4 color;\n#endif\n#ifdef BONES\nattribute vec4 matricesIndices;\nattribute vec4 matricesWeights;\n#endif\n\n#ifdef INSTANCES\nattribute vec4 world0;\nattribute vec4 world1;\nattribute vec4 world2;\nattribute vec4 world3;\n#else\nuniform mat4 world;\n#endif\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef DIFFUSE\nvarying vec2 vTextureUV;\nuniform mat4 textureMatrix;\nuniform vec2 vTextureInfos;\n#endif\n#ifdef BONES\nuniform mat4 mBones[BonesPerMesh];\n#endif\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#ifdef CLIPPLANE\nuniform vec4 vClipPlane;\nvarying float fClipDistance;\n#endif\n#ifdef FOG\nvarying float fFogDistance;\n#endif\n#ifdef SHADOWS\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\nuniform mat4 lightMatrix0;\nvarying vec4 vPositionFromLight0;\n#endif\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\nuniform mat4 lightMatrix1;\nvarying vec4 vPositionFromLight1;\n#endif\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\nuniform mat4 lightMatrix2;\nvarying vec4 vPositionFromLight2;\n#endif\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\nuniform mat4 lightMatrix3;\nvarying vec4 vPositionFromLight3;\n#endif\n#endif\nvoid main(void) {\n mat4 finalWorld;\n#ifdef INSTANCES\n finalWorld = mat4(world0, world1, world2, world3);\n#else\n finalWorld = world;\n#endif\n#ifdef BONES\n mat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;\n mat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;\n mat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;\n#ifdef BONES4\n mat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;\n finalWorld = finalWorld * (m0 + m1 + m2 + m3);\n#else\n finalWorld = finalWorld * (m0 + m1 + m2);\n#endif \n#endif\n gl_Position = viewProjection * finalWorld * vec4(position, 1.0);\n vec4 worldPos = finalWorld * vec4(position, 1.0);\n vPositionW = vec3(worldPos);\n#ifdef NORMAL\n vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\n#endif\n \n#ifndef UV1\n vec2 uv = vec2(0., 0.);\n#endif\n#ifndef UV2\n vec2 uv2 = vec2(0., 0.);\n#endif\n#ifdef DIFFUSE\n if (vTextureInfos.x == 0.)\n {\n vTextureUV = vec2(textureMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vTextureUV = vec2(textureMatrix * vec4(uv2, 1.0, 0.0));\n }\n#endif\n \n#ifdef CLIPPLANE\n fClipDistance = dot(worldPos, vClipPlane);\n#endif\n \n#ifdef FOG\n fFogDistance = (view * worldPos).z;\n#endif\n \n#ifdef SHADOWS\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\n vPositionFromLight0 = lightMatrix0 * worldPos;\n#endif\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\n vPositionFromLight1 = lightMatrix1 * worldPos;\n#endif\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\n vPositionFromLight2 = lightMatrix2 * worldPos;\n#endif\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\n vPositionFromLight3 = lightMatrix3 * worldPos;\n#endif\n#endif\n \n#ifdef VERTEXCOLOR\n vColor = color;\n#endif\n \n#ifdef POINTSIZE\n gl_PointSize = pointSize;\n#endif\n}\n";
  520. BABYLON.Effect.ShadersStore['terrainPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n#ifdef SPECULARTERM\nuniform vec4 vSpecularColor;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#ifdef LIGHT0\nuniform vec4 vLightData0;\nuniform vec4 vLightDiffuse0;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular0;\n#endif\n#ifdef SHADOW0\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\nvarying vec4 vPositionFromLight0;\nuniform sampler2D shadowSampler0;\n#else\nuniform samplerCube shadowSampler0;\n#endif\nuniform vec3 shadowsInfo0;\n#endif\n#ifdef SPOTLIGHT0\nuniform vec4 vLightDirection0;\n#endif\n#ifdef HEMILIGHT0\nuniform vec3 vLightGround0;\n#endif\n#endif\n#ifdef LIGHT1\nuniform vec4 vLightData1;\nuniform vec4 vLightDiffuse1;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular1;\n#endif\n#ifdef SHADOW1\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\nvarying vec4 vPositionFromLight1;\nuniform sampler2D shadowSampler1;\n#else\nuniform samplerCube shadowSampler1;\n#endif\nuniform vec3 shadowsInfo1;\n#endif\n#ifdef SPOTLIGHT1\nuniform vec4 vLightDirection1;\n#endif\n#ifdef HEMILIGHT1\nuniform vec3 vLightGround1;\n#endif\n#endif\n#ifdef LIGHT2\nuniform vec4 vLightData2;\nuniform vec4 vLightDiffuse2;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular2;\n#endif\n#ifdef SHADOW2\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\nvarying vec4 vPositionFromLight2;\nuniform sampler2D shadowSampler2;\n#else\nuniform samplerCube shadowSampler2;\n#endif\nuniform vec3 shadowsInfo2;\n#endif\n#ifdef SPOTLIGHT2\nuniform vec4 vLightDirection2;\n#endif\n#ifdef HEMILIGHT2\nuniform vec3 vLightGround2;\n#endif\n#endif\n#ifdef LIGHT3\nuniform vec4 vLightData3;\nuniform vec4 vLightDiffuse3;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular3;\n#endif\n#ifdef SHADOW3\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\nvarying vec4 vPositionFromLight3;\nuniform sampler2D shadowSampler3;\n#else\nuniform samplerCube shadowSampler3;\n#endif\nuniform vec3 shadowsInfo3;\n#endif\n#ifdef SPOTLIGHT3\nuniform vec4 vLightDirection3;\n#endif\n#ifdef HEMILIGHT3\nuniform vec3 vLightGround3;\n#endif\n#endif\n\n#ifdef DIFFUSE\nvarying vec2 vTextureUV;\nuniform sampler2D textureSampler;\nuniform vec2 vTextureInfos;\nuniform sampler2D diffuse1Sampler;\nuniform sampler2D diffuse2Sampler;\nuniform sampler2D diffuse3Sampler;\nuniform vec2 diffuse1Infos;\nuniform vec2 diffuse2Infos;\nuniform vec2 diffuse3Infos;\n#endif\n#ifdef BUMP\nuniform sampler2D bump1Sampler;\nuniform sampler2D bump2Sampler;\nuniform sampler2D bump3Sampler;\n#endif\n\n#ifdef SHADOWS\nfloat unpack(vec4 color)\n{\n const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);\n return dot(color, bit_shift);\n}\n#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)\nfloat computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)\n{\n vec3 directionToLight = vPositionW - lightPosition;\n float depth = length(directionToLight);\n depth = clamp(depth, 0., 1.0);\n directionToLight = normalize(directionToLight);\n directionToLight.y = - directionToLight.y;\n float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;\n if (depth > shadow)\n {\n return darkness;\n }\n return 1.0;\n}\nfloat computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)\n{\n vec3 directionToLight = vPositionW - lightPosition;\n float depth = length(directionToLight);\n depth = clamp(depth, 0., 1.0);\n float diskScale = 2.0 / mapSize;\n directionToLight = normalize(directionToLight);\n directionToLight.y = -directionToLight.y;\n float visibility = 1.;\n vec3 poissonDisk[4];\n poissonDisk[0] = vec3(-1.0, 1.0, -1.0);\n poissonDisk[1] = vec3(1.0, -1.0, -1.0);\n poissonDisk[2] = vec3(-1.0, -1.0, -1.0);\n poissonDisk[3] = vec3(1.0, -1.0, 1.0);\n \n float biasedDepth = depth - bias;\n if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[0] * diskScale)) < biasedDepth) visibility -= 0.25;\n if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * diskScale)) < biasedDepth) visibility -= 0.25;\n if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * diskScale)) < biasedDepth) visibility -= 0.25;\n if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[3] * diskScale)) < biasedDepth) visibility -= 0.25;\n return min(1.0, visibility + darkness);\n}\n#endif\n#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)\nfloat computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)\n{\n vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\n depth = 0.5 * depth + vec3(0.5);\n vec2 uv = depth.xy;\n if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\n {\n return 1.0;\n }\n float shadow = unpack(texture2D(shadowSampler, uv)) + bias;\n if (depth.z > shadow)\n {\n return darkness;\n }\n return 1.;\n}\nfloat computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)\n{\n vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\n depth = 0.5 * depth + vec3(0.5);\n vec2 uv = depth.xy;\n if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\n {\n return 1.0;\n }\n float visibility = 1.;\n vec2 poissonDisk[4];\n poissonDisk[0] = vec2(-0.94201624, -0.39906216);\n poissonDisk[1] = vec2(0.94558609, -0.76890725);\n poissonDisk[2] = vec2(-0.094184101, -0.92938870);\n poissonDisk[3] = vec2(0.34495938, 0.29387760);\n \n float biasedDepth = depth.z - bias;\n if (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;\n if (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;\n if (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;\n if (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;\n return min(1.0, visibility + darkness);\n}\n\nfloat unpackHalf(vec2 color)\n{\n return color.x + (color.y / 255.0);\n}\nfloat linstep(float low, float high, float v) {\n return clamp((v - low) / (high - low), 0.0, 1.0);\n}\nfloat ChebychevInequality(vec2 moments, float compare, float bias)\n{\n float p = smoothstep(compare - bias, compare, moments.x);\n float variance = max(moments.y - moments.x * moments.x, 0.02);\n float d = compare - moments.x;\n float p_max = linstep(0.2, 1.0, variance / (variance + d * d));\n return clamp(max(p, p_max), 0.0, 1.0);\n}\nfloat computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)\n{\n vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\n depth = 0.5 * depth + vec3(0.5);\n vec2 uv = depth.xy;\n if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)\n {\n return 1.0;\n }\n vec4 texel = texture2D(shadowSampler, uv);\n vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));\n return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);\n}\n#endif\n#endif\n#ifdef CLIPPLANE\nvarying float fClipDistance;\n#endif\n\n#ifdef FOG\n#define FOGMODE_NONE 0.\n#define FOGMODE_EXP 1.\n#define FOGMODE_EXP2 2.\n#define FOGMODE_LINEAR 3.\n#define E 2.71828\nuniform vec4 vFogInfos;\nuniform vec3 vFogColor;\nvarying float fFogDistance;\nfloat CalcFogFactor()\n{\n float fogCoeff = 1.0;\n float fogStart = vFogInfos.y;\n float fogEnd = vFogInfos.z;\n float fogDensity = vFogInfos.w;\n if (FOGMODE_LINEAR == vFogInfos.x)\n {\n fogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);\n }\n else if (FOGMODE_EXP == vFogInfos.x)\n {\n fogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);\n }\n else if (FOGMODE_EXP2 == vFogInfos.x)\n {\n fogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);\n }\n return clamp(fogCoeff, 0.0, 1.0);\n}\n#endif\n\n#ifdef BUMP\n#extension GL_OES_standard_derivatives : enable\n\nmat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)\n{\n \n vec3 dp1 = dFdx(p);\n vec3 dp2 = dFdy(p);\n vec2 duv1 = dFdx(uv);\n vec2 duv2 = dFdy(uv);\n \n vec3 dp2perp = cross(dp2, normal);\n vec3 dp1perp = cross(normal, dp1);\n vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;\n vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;\n \n float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));\n return mat3(tangent * invmax, binormal * invmax, normal);\n}\nvec3 perturbNormal(vec3 viewDir, vec3 mixColor)\n{ \n vec3 bump1Color = texture2D(bump1Sampler, vTextureUV * diffuse1Infos).xyz;\n vec3 bump2Color = texture2D(bump2Sampler, vTextureUV * diffuse2Infos).xyz;\n vec3 bump3Color = texture2D(bump3Sampler, vTextureUV * diffuse3Infos).xyz;\n bump1Color.rgb *= mixColor.r;\n bump2Color.rgb = mix(bump1Color.rgb, bump2Color.rgb, mixColor.g);\n vec3 map = mix(bump2Color.rgb, bump3Color.rgb, mixColor.b);\n map = map * 255. / 127. - 128. / 127.;\n mat3 TBN = cotangent_frame(vNormalW * vTextureInfos.y, -viewDir, vTextureUV);\n return normalize(TBN * map);\n}\n#endif\n\nstruct lightingInfo\n{\n vec3 diffuse;\n#ifdef SPECULARTERM\n vec3 specular;\n#endif\n};\nlightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {\n lightingInfo result;\n vec3 lightVectorW;\n float attenuation = 1.0;\n if (lightData.w == 0.)\n {\n vec3 direction = lightData.xyz - vPositionW;\n attenuation = max(0., 1.0 - length(direction) / range);\n lightVectorW = normalize(direction);\n }\n else\n {\n lightVectorW = normalize(-lightData.xyz);\n }\n \n float ndl = max(0., dot(vNormal, lightVectorW));\n result.diffuse = ndl * diffuseColor * attenuation;\n#ifdef SPECULARTERM\n \n vec3 angleW = normalize(viewDirectionW + lightVectorW);\n float specComp = max(0., dot(vNormal, angleW));\n specComp = pow(specComp, max(1., glossiness));\n result.specular = specComp * specularColor * attenuation;\n#endif\n return result;\n}\nlightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float glossiness) {\n lightingInfo result;\n vec3 direction = lightData.xyz - vPositionW;\n vec3 lightVectorW = normalize(direction);\n float attenuation = max(0., 1.0 - length(direction) / range);\n \n float cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));\n float spotAtten = 0.0;\n if (cosAngle >= lightDirection.w)\n {\n cosAngle = max(0., pow(cosAngle, lightData.w));\n spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);\n \n float ndl = max(0., dot(vNormal, -lightDirection.xyz));\n result.diffuse = ndl * spotAtten * diffuseColor * attenuation;\n#ifdef SPECULARTERM\n \n vec3 angleW = normalize(viewDirectionW - lightDirection.xyz);\n float specComp = max(0., dot(vNormal, angleW));\n specComp = pow(specComp, max(1., glossiness));\n result.specular = specComp * specularColor * spotAtten * attenuation;\n#endif\n return result;\n }\n result.diffuse = vec3(0.);\n#ifdef SPECULARTERM\n result.specular = vec3(0.);\n#endif\n return result;\n}\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float glossiness) {\n lightingInfo result;\n \n float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;\n result.diffuse = mix(groundColor, diffuseColor, ndl);\n#ifdef SPECULARTERM\n \n vec3 angleW = normalize(viewDirectionW + lightData.xyz);\n float specComp = max(0., dot(vNormal, angleW));\n specComp = pow(specComp, max(1., glossiness));\n result.specular = specComp * specularColor;\n#endif\n return result;\n}\nvoid main(void) {\n \n#ifdef CLIPPLANE\n if (fClipDistance > 0.0)\n discard;\n#endif\n vec3 viewDirectionW = normalize(vEyePosition - vPositionW);\n \n vec4 baseColor = vec4(1., 1., 1., 1.);\n vec3 diffuseColor = vDiffuseColor.rgb;\n#ifdef SPECULARTERM\n float glossiness = vSpecularColor.a;\n vec3 specularColor = vSpecularColor.rgb;\n#else\n float glossiness = 0.;\n#endif\n \n float alpha = vDiffuseColor.a;\n \n#ifdef NORMAL\n vec3 normalW = normalize(vNormalW);\n#else\n vec3 normalW = vec3(1.0, 1.0, 1.0);\n#endif\n#ifdef DIFFUSE\n baseColor = texture2D(textureSampler, vTextureUV);\n#if defined(BUMP) && defined(DIFFUSE)\n normalW = perturbNormal(viewDirectionW, baseColor.rgb);\n#endif\n#ifdef ALPHATEST\n if (baseColor.a < 0.4)\n discard;\n#endif\n baseColor.rgb *= vTextureInfos.y;\n vec4 diffuse1Color = texture2D(diffuse1Sampler, vTextureUV * diffuse1Infos);\n vec4 diffuse2Color = texture2D(diffuse2Sampler, vTextureUV * diffuse2Infos);\n vec4 diffuse3Color = texture2D(diffuse3Sampler, vTextureUV * diffuse3Infos);\n diffuse1Color.rgb *= baseColor.r;\n diffuse2Color.rgb = mix(diffuse1Color.rgb, diffuse2Color.rgb, baseColor.g);\n baseColor.rgb = mix(diffuse2Color.rgb, diffuse3Color.rgb, baseColor.b);\n#endif\n#ifdef VERTEXCOLOR\n baseColor.rgb *= vColor.rgb;\n#endif\n \n vec3 diffuseBase = vec3(0., 0., 0.);\n#ifdef SPECULARTERM\n vec3 specularBase = vec3(0., 0., 0.);\n#endif\n float shadow = 1.;\n#ifdef LIGHT0\n#ifndef SPECULARTERM\n vec3 vLightSpecular0 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT0\n lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);\n#endif\n#ifdef HEMILIGHT0\n lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, glossiness);\n#endif\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\n lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, glossiness);\n#endif\n#ifdef SHADOW0\n#ifdef SHADOWVSM0\n shadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);\n#else\n#ifdef SHADOWPCF0\n#if defined(POINTLIGHT0)\n shadow = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\n#else\n shadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\n#endif\n#else\n#if defined(POINTLIGHT0)\n shadow = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\n#else\n shadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\n#endif\n#endif\n#endif\n#else\n shadow = 1.;\n#endif\n diffuseBase += info.diffuse * shadow;\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n#ifdef LIGHT1\n#ifndef SPECULARTERM\n vec3 vLightSpecular1 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT1\n info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);\n#endif\n#ifdef HEMILIGHT1\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, glossiness);\n#endif\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\n info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, glossiness);\n#endif\n#ifdef SHADOW1\n#ifdef SHADOWVSM1\n shadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);\n#else\n#ifdef SHADOWPCF1\n#if defined(POINTLIGHT1)\n shadow = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\n#else\n shadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\n#endif\n#else\n#if defined(POINTLIGHT1)\n shadow = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\n#else\n shadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\n#endif\n#endif\n#endif\n#else\n shadow = 1.;\n#endif\n diffuseBase += info.diffuse * shadow;\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n#ifdef LIGHT2\n#ifndef SPECULARTERM\n vec3 vLightSpecular2 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT2\n info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);\n#endif\n#ifdef HEMILIGHT2\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, glossiness);\n#endif\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\n info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, glossiness);\n#endif\n#ifdef SHADOW2\n#ifdef SHADOWVSM2\n shadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);\n#else\n#ifdef SHADOWPCF2\n#if defined(POINTLIGHT2)\n shadow = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\n#else\n shadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\n#endif\n#else\n#if defined(POINTLIGHT2)\n shadow = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\n#else\n shadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\n#endif\n#endif \n#endif \n#else\n shadow = 1.;\n#endif\n diffuseBase += info.diffuse * shadow;\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n#ifdef LIGHT3\n#ifndef SPECULARTERM\n vec3 vLightSpecular3 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT3\n info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);\n#endif\n#ifdef HEMILIGHT3\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, glossiness);\n#endif\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\n info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, glossiness);\n#endif\n#ifdef SHADOW3\n#ifdef SHADOWVSM3\n shadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);\n#else\n#ifdef SHADOWPCF3\n#if defined(POINTLIGHT3)\n shadow = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\n#else\n shadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\n#endif\n#else\n#if defined(POINTLIGHT3)\n shadow = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\n#else\n shadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\n#endif\n#endif \n#endif \n#else\n shadow = 1.;\n#endif\n diffuseBase += info.diffuse * shadow;\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n#ifdef VERTEXALPHA\n alpha *= vColor.a;\n#endif\n#ifdef SPECULARTERM\n vec3 finalSpecular = specularBase * specularColor;\n#else\n vec3 finalSpecular = vec3(0.0);\n#endif\n vec3 finalDiffuse = clamp(diffuseBase * diffuseColor * baseColor.rgb, 0.0, 1.0);\n \n vec4 color = vec4(finalDiffuse + finalSpecular, alpha);\n#ifdef FOG\n float fog = CalcFogFactor();\n color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;\n#endif\n gl_FragColor = color;\n}\n";