babylon.pbrMaterial.js 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var maxSimultaneousLights = 4;
  5. var PBRMaterialDefines = (function (_super) {
  6. __extends(PBRMaterialDefines, _super);
  7. function PBRMaterialDefines() {
  8. _super.call(this);
  9. this.DIFFUSE = false;
  10. this.AMBIENT = false;
  11. this.OPACITY = false;
  12. this.OPACITYRGB = false;
  13. this.REFLECTION = false;
  14. this.EMISSIVE = false;
  15. this.SPECULAR = false;
  16. this.BUMP = false;
  17. this.SPECULAROVERALPHA = false;
  18. this.CLIPPLANE = false;
  19. this.ALPHATEST = false;
  20. this.ALPHAFROMDIFFUSE = 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.POINTLIGHT0 = false;
  36. this.POINTLIGHT1 = false;
  37. this.POINTLIGHT2 = false;
  38. this.POINTLIGHT3 = false;
  39. this.DIRLIGHT0 = false;
  40. this.DIRLIGHT1 = false;
  41. this.DIRLIGHT2 = false;
  42. this.DIRLIGHT3 = false;
  43. this.SPECULARTERM = false;
  44. this.SHADOW0 = false;
  45. this.SHADOW1 = false;
  46. this.SHADOW2 = false;
  47. this.SHADOW3 = false;
  48. this.SHADOWS = false;
  49. this.SHADOWVSM0 = false;
  50. this.SHADOWVSM1 = false;
  51. this.SHADOWVSM2 = false;
  52. this.SHADOWVSM3 = false;
  53. this.SHADOWPCF0 = false;
  54. this.SHADOWPCF1 = false;
  55. this.SHADOWPCF2 = false;
  56. this.SHADOWPCF3 = false;
  57. this.OPACITYFRESNEL = false;
  58. this.EMISSIVEFRESNEL = false;
  59. this.FRESNEL = false;
  60. this.NORMAL = false;
  61. this.UV1 = false;
  62. this.UV2 = false;
  63. this.VERTEXCOLOR = false;
  64. this.VERTEXALPHA = false;
  65. this.NUM_BONE_INFLUENCERS = 0;
  66. this.BonesPerMesh = 0;
  67. this.INSTANCES = false;
  68. this.GLOSSINESSFROMSPECULARMAP = false;
  69. this.EMISSIVEASILLUMINATION = false;
  70. this.LINKEMISSIVEWITHDIFFUSE = false;
  71. this.LIGHTMAP = false;
  72. this.USELIGHTMAPASSHADOWMAP = false;
  73. this.REFLECTIONMAP_3D = false;
  74. this.REFLECTIONMAP_SPHERICAL = false;
  75. this.REFLECTIONMAP_PLANAR = false;
  76. this.REFLECTIONMAP_CUBIC = false;
  77. this.REFLECTIONMAP_PROJECTION = false;
  78. this.REFLECTIONMAP_SKYBOX = false;
  79. this.REFLECTIONMAP_EXPLICIT = false;
  80. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  81. this.INVERTCUBICMAP = false;
  82. this.LOGARITHMICDEPTH = false;
  83. this.CAMERATONEMAP = false;
  84. this.CAMERACONTRAST = false;
  85. this.OVERLOADEDVALUES = false;
  86. this.OVERLOADEDSHADOWVALUES = false;
  87. this._keys = Object.keys(this);
  88. }
  89. return PBRMaterialDefines;
  90. })(BABYLON.MaterialDefines);
  91. var PBRMaterial = (function (_super) {
  92. __extends(PBRMaterial, _super);
  93. function PBRMaterial(name, scene) {
  94. var _this = this;
  95. _super.call(this, name, scene);
  96. this.directIntensity = 1.0;
  97. this.emissiveIntensity = 1.0;
  98. this.environmentIntensity = 1.0;
  99. this._lightingInfos = new BABYLON.Vector4(this.directIntensity, this.emissiveIntensity, this.environmentIntensity, 0.0);
  100. this.overloadedShadowIntensity = 1.0;
  101. this.overloadedShadeIntensity = 1.0;
  102. this._overloadedShadowInfos = new BABYLON.Vector4(this.overloadedShadowIntensity, this.overloadedShadeIntensity, 0.0, 0.0);
  103. this.cameraExposure = 1.0;
  104. this.cameraContrast = 1.0;
  105. this._cameraInfos = new BABYLON.Vector4(1.0, 1.0, 0.0, 0.0);
  106. this.overloadedAmbientIntensity = 0.0;
  107. this.overloadedDiffuseIntensity = 0.0;
  108. this.overloadedSpecularIntensity = 0.0;
  109. this.overloadedEmissiveIntensity = 0.0;
  110. this._overloadedIntensity = new BABYLON.Vector4(this.overloadedAmbientIntensity, this.overloadedDiffuseIntensity, this.overloadedSpecularIntensity, this.overloadedEmissiveIntensity);
  111. this.overloadedAmbient = BABYLON.Color3.White();
  112. this.overloadedDiffuse = BABYLON.Color3.White();
  113. this.overloadedSpecular = BABYLON.Color3.White();
  114. this.overloadedEmissive = BABYLON.Color3.White();
  115. this.overloadedReflection = BABYLON.Color3.White();
  116. this.overloadedGlossiness = 0.0;
  117. this.overloadedGlossinessIntensity = 0.0;
  118. this.overloadedReflectionIntensity = 0.0;
  119. this._overloadedGlossiness = new BABYLON.Vector3(this.overloadedGlossiness, this.overloadedGlossinessIntensity, this.overloadedReflectionIntensity);
  120. this.disableBumpMap = false;
  121. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  122. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  123. this.specularColor = new BABYLON.Color3(1, 1, 1);
  124. this.reflectionColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  125. this.glossiness = 0.5;
  126. this.emissiveColor = new BABYLON.Color3(0, 0, 0);
  127. this.useAlphaFromDiffuseTexture = false;
  128. this.useEmissiveAsIllumination = false;
  129. this.linkEmissiveWithDiffuse = false;
  130. this.useSpecularOverAlpha = true;
  131. this.disableLighting = false;
  132. this.useLightmapAsShadowmap = false;
  133. this.useGlossinessFromSpecularMapAlpha = false;
  134. this._renderTargets = new BABYLON.SmartArray(16);
  135. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  136. this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  137. this._tempColor = new BABYLON.Color3();
  138. this._defines = new PBRMaterialDefines();
  139. this._cachedDefines = new PBRMaterialDefines();
  140. this._myScene = null;
  141. this._myShadowGenerator = null;
  142. this._cachedDefines.BonesPerMesh = -1;
  143. this.getRenderTargetTextures = function () {
  144. _this._renderTargets.reset();
  145. if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
  146. _this._renderTargets.push(_this.reflectionTexture);
  147. }
  148. return _this._renderTargets;
  149. };
  150. }
  151. Object.defineProperty(PBRMaterial.prototype, "useLogarithmicDepth", {
  152. get: function () {
  153. return this._useLogarithmicDepth;
  154. },
  155. set: function (value) {
  156. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  157. },
  158. enumerable: true,
  159. configurable: true
  160. });
  161. PBRMaterial.prototype.needAlphaBlending = function () {
  162. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  163. };
  164. PBRMaterial.prototype.needAlphaTesting = function () {
  165. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha;
  166. };
  167. PBRMaterial.prototype._shouldUseAlphaFromDiffuseTexture = function () {
  168. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && this.useAlphaFromDiffuseTexture;
  169. };
  170. PBRMaterial.prototype.getAlphaTestTexture = function () {
  171. return this.diffuseTexture;
  172. };
  173. PBRMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  174. if (!mesh) {
  175. return true;
  176. }
  177. if (this._defines.INSTANCES !== useInstances) {
  178. return false;
  179. }
  180. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  181. return true;
  182. }
  183. return false;
  184. };
  185. PBRMaterial.PrepareDefinesForLights = function (scene, mesh, defines) {
  186. var lightIndex = 0;
  187. var needNormals = false;
  188. for (var index = 0; index < scene.lights.length; index++) {
  189. var light = scene.lights[index];
  190. if (!light.isEnabled()) {
  191. continue;
  192. }
  193. // Excluded check
  194. if (light._excludedMeshesIds.length > 0) {
  195. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  196. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  197. if (excludedMesh) {
  198. light.excludedMeshes.push(excludedMesh);
  199. }
  200. }
  201. light._excludedMeshesIds = [];
  202. }
  203. // Included check
  204. if (light._includedOnlyMeshesIds.length > 0) {
  205. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  206. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  207. if (includedOnlyMesh) {
  208. light.includedOnlyMeshes.push(includedOnlyMesh);
  209. }
  210. }
  211. light._includedOnlyMeshesIds = [];
  212. }
  213. if (!light.canAffectMesh(mesh)) {
  214. continue;
  215. }
  216. needNormals = true;
  217. defines["LIGHT" + lightIndex] = true;
  218. var type;
  219. if (light instanceof BABYLON.SpotLight) {
  220. type = "SPOTLIGHT" + lightIndex;
  221. }
  222. else if (light instanceof BABYLON.HemisphericLight) {
  223. type = "HEMILIGHT" + lightIndex;
  224. }
  225. else if (light instanceof BABYLON.PointLight) {
  226. type = "POINTLIGHT" + lightIndex;
  227. }
  228. else {
  229. type = "DIRLIGHT" + lightIndex;
  230. }
  231. defines[type] = true;
  232. // Specular
  233. if (!light.specular.equalsFloats(0, 0, 0)) {
  234. defines["SPECULARTERM"] = true;
  235. }
  236. // Shadows
  237. if (scene.shadowsEnabled) {
  238. var shadowGenerator = light.getShadowGenerator();
  239. if (mesh && mesh.receiveShadows && shadowGenerator) {
  240. defines["SHADOW" + lightIndex] = true;
  241. defines["SHADOWS"] = true;
  242. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  243. defines["SHADOWVSM" + lightIndex] = true;
  244. }
  245. if (shadowGenerator.usePoissonSampling) {
  246. defines["SHADOWPCF" + lightIndex] = true;
  247. }
  248. }
  249. }
  250. lightIndex++;
  251. if (lightIndex === maxSimultaneousLights)
  252. break;
  253. }
  254. return needNormals;
  255. };
  256. PBRMaterial.BindLights = function (scene, mesh, effect, defines) {
  257. var lightIndex = 0;
  258. for (var index = 0; index < scene.lights.length; index++) {
  259. var light = scene.lights[index];
  260. if (!light.isEnabled()) {
  261. continue;
  262. }
  263. if (!light.canAffectMesh(mesh)) {
  264. continue;
  265. }
  266. if (light instanceof BABYLON.PointLight) {
  267. // Point Light
  268. light.transferToEffect(effect, "vLightData" + lightIndex);
  269. }
  270. else if (light instanceof BABYLON.DirectionalLight) {
  271. // Directional Light
  272. light.transferToEffect(effect, "vLightData" + lightIndex);
  273. }
  274. else if (light instanceof BABYLON.SpotLight) {
  275. // Spot Light
  276. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  277. }
  278. else if (light instanceof BABYLON.HemisphericLight) {
  279. // Hemispheric Light
  280. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  281. }
  282. // GAMMA CORRECTION.
  283. light.diffuse.toLinearSpaceToRef(PBRMaterial._scaledDiffuse);
  284. PBRMaterial._scaledDiffuse.scaleToRef(light.intensity, PBRMaterial._scaledDiffuse);
  285. light.diffuse.scaleToRef(light.intensity, PBRMaterial._scaledDiffuse);
  286. effect.setColor4("vLightDiffuse" + lightIndex, PBRMaterial._scaledDiffuse, light.range);
  287. if (defines["SPECULARTERM"]) {
  288. light.specular.toLinearSpaceToRef(PBRMaterial._scaledSpecular);
  289. PBRMaterial._scaledSpecular.scaleToRef(light.intensity, PBRMaterial._scaledSpecular);
  290. effect.setColor3("vLightSpecular" + lightIndex, PBRMaterial._scaledSpecular);
  291. }
  292. // Shadows
  293. if (scene.shadowsEnabled) {
  294. var shadowGenerator = light.getShadowGenerator();
  295. if (mesh.receiveShadows && shadowGenerator) {
  296. if (!light.needCube()) {
  297. effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  298. }
  299. effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  300. effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  301. }
  302. }
  303. lightIndex++;
  304. if (lightIndex === maxSimultaneousLights)
  305. break;
  306. }
  307. };
  308. PBRMaterial.prototype.isReady = function (mesh, useInstances) {
  309. if (this.checkReadyOnlyOnce) {
  310. if (this._wasPreviouslyReady) {
  311. return true;
  312. }
  313. }
  314. var scene = this.getScene();
  315. if (!this.checkReadyOnEveryCall) {
  316. if (this._renderId === scene.getRenderId()) {
  317. if (this._checkCache(scene, mesh, useInstances)) {
  318. return true;
  319. }
  320. }
  321. }
  322. var engine = scene.getEngine();
  323. var needNormals = false;
  324. var needUVs = false;
  325. this._defines.reset();
  326. // Textures
  327. if (scene.texturesEnabled) {
  328. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  329. if (!this.diffuseTexture.isReady()) {
  330. return false;
  331. }
  332. else {
  333. needUVs = true;
  334. this._defines.DIFFUSE = true;
  335. }
  336. }
  337. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  338. if (!this.ambientTexture.isReady()) {
  339. return false;
  340. }
  341. else {
  342. needUVs = true;
  343. this._defines.AMBIENT = true;
  344. }
  345. }
  346. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  347. if (!this.opacityTexture.isReady()) {
  348. return false;
  349. }
  350. else {
  351. needUVs = true;
  352. this._defines.OPACITY = true;
  353. if (this.opacityTexture.getAlphaFromRGB) {
  354. this._defines.OPACITYRGB = true;
  355. }
  356. }
  357. }
  358. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  359. if (!this.reflectionTexture.isReady()) {
  360. return false;
  361. }
  362. else {
  363. needNormals = true;
  364. this._defines.REFLECTION = true;
  365. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  366. this._defines.INVERTCUBICMAP = true;
  367. }
  368. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  369. switch (this.reflectionTexture.coordinatesMode) {
  370. case BABYLON.Texture.CUBIC_MODE:
  371. case BABYLON.Texture.INVCUBIC_MODE:
  372. this._defines.REFLECTIONMAP_CUBIC = true;
  373. break;
  374. case BABYLON.Texture.EXPLICIT_MODE:
  375. this._defines.REFLECTIONMAP_EXPLICIT = true;
  376. break;
  377. case BABYLON.Texture.PLANAR_MODE:
  378. this._defines.REFLECTIONMAP_PLANAR = true;
  379. break;
  380. case BABYLON.Texture.PROJECTION_MODE:
  381. this._defines.REFLECTIONMAP_PROJECTION = true;
  382. break;
  383. case BABYLON.Texture.SKYBOX_MODE:
  384. this._defines.REFLECTIONMAP_SKYBOX = true;
  385. break;
  386. case BABYLON.Texture.SPHERICAL_MODE:
  387. this._defines.REFLECTIONMAP_SPHERICAL = true;
  388. break;
  389. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  390. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  391. break;
  392. }
  393. }
  394. }
  395. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapEnabled) {
  396. if (!this.lightmapTexture.isReady()) {
  397. return false;
  398. }
  399. else {
  400. needUVs = true;
  401. this._defines.LIGHTMAP = true;
  402. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  403. }
  404. }
  405. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  406. if (!this.emissiveTexture.isReady()) {
  407. return false;
  408. }
  409. else {
  410. needUVs = true;
  411. this._defines.EMISSIVE = true;
  412. }
  413. }
  414. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  415. if (!this.specularTexture.isReady()) {
  416. return false;
  417. }
  418. else {
  419. needUVs = true;
  420. this._defines.SPECULAR = true;
  421. this._defines.GLOSSINESSFROMSPECULARMAP = this.useGlossinessFromSpecularMapAlpha;
  422. }
  423. }
  424. }
  425. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  426. if (!this.bumpTexture.isReady()) {
  427. return false;
  428. }
  429. else {
  430. needUVs = true;
  431. this._defines.BUMP = true;
  432. }
  433. }
  434. // Effect
  435. if (scene.clipPlane) {
  436. this._defines.CLIPPLANE = true;
  437. }
  438. if (engine.getAlphaTesting()) {
  439. this._defines.ALPHATEST = true;
  440. }
  441. if (this._shouldUseAlphaFromDiffuseTexture()) {
  442. this._defines.ALPHAFROMDIFFUSE = true;
  443. }
  444. if (this.useEmissiveAsIllumination) {
  445. this._defines.EMISSIVEASILLUMINATION = true;
  446. }
  447. if (this.linkEmissiveWithDiffuse) {
  448. this._defines.LINKEMISSIVEWITHDIFFUSE = true;
  449. }
  450. if (this.useLogarithmicDepth) {
  451. this._defines.LOGARITHMICDEPTH = true;
  452. }
  453. if (this.cameraContrast != 1) {
  454. this._defines.CAMERACONTRAST = true;
  455. }
  456. if (this.cameraExposure != 1) {
  457. this._defines.CAMERATONEMAP = true;
  458. }
  459. if (this.overloadedShadeIntensity != 1 ||
  460. this.overloadedShadowIntensity != 1) {
  461. this._defines.OVERLOADEDSHADOWVALUES = true;
  462. }
  463. if (this.overloadedGlossinessIntensity > 0 ||
  464. this.overloadedEmissiveIntensity > 0 ||
  465. this.overloadedSpecularIntensity > 0 ||
  466. this.overloadedDiffuseIntensity > 0 ||
  467. this.overloadedAmbientIntensity > 0 ||
  468. this.overloadedReflectionIntensity > 0) {
  469. this._defines.OVERLOADEDVALUES = true;
  470. }
  471. // Point size
  472. if (this.pointsCloud || scene.forcePointsCloud) {
  473. this._defines.POINTSIZE = true;
  474. }
  475. // Fog
  476. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  477. this._defines.FOG = true;
  478. }
  479. if (scene.lightsEnabled && !this.disableLighting) {
  480. needNormals = PBRMaterial.PrepareDefinesForLights(scene, mesh, this._defines);
  481. }
  482. if (BABYLON.StandardMaterial.FresnelEnabled) {
  483. // Fresnel
  484. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  485. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  486. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  487. this._defines.OPACITYFRESNEL = true;
  488. }
  489. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  490. this._defines.EMISSIVEFRESNEL = true;
  491. }
  492. needNormals = true;
  493. this._defines.FRESNEL = true;
  494. }
  495. }
  496. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  497. this._defines.SPECULAROVERALPHA = true;
  498. }
  499. // Attribs
  500. if (mesh) {
  501. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  502. this._defines.NORMAL = true;
  503. }
  504. if (needUVs) {
  505. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  506. this._defines.UV1 = true;
  507. }
  508. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  509. this._defines.UV2 = true;
  510. }
  511. }
  512. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  513. this._defines.VERTEXCOLOR = true;
  514. if (mesh.hasVertexAlpha) {
  515. this._defines.VERTEXALPHA = true;
  516. }
  517. }
  518. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  519. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  520. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  521. }
  522. // Instances
  523. if (useInstances) {
  524. this._defines.INSTANCES = true;
  525. }
  526. }
  527. // Get correct effect
  528. if (!this._defines.isEqual(this._cachedDefines)) {
  529. this._defines.cloneTo(this._cachedDefines);
  530. scene.resetCachedMaterial();
  531. // Fallbacks
  532. var fallbacks = new BABYLON.EffectFallbacks();
  533. if (this._defines.REFLECTION) {
  534. fallbacks.addFallback(0, "REFLECTION");
  535. }
  536. if (this._defines.SPECULAR) {
  537. fallbacks.addFallback(0, "SPECULAR");
  538. }
  539. if (this._defines.BUMP) {
  540. fallbacks.addFallback(0, "BUMP");
  541. }
  542. if (this._defines.SPECULAROVERALPHA) {
  543. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  544. }
  545. if (this._defines.FOG) {
  546. fallbacks.addFallback(1, "FOG");
  547. }
  548. if (this._defines.POINTSIZE) {
  549. fallbacks.addFallback(0, "POINTSIZE");
  550. }
  551. if (this._defines.LOGARITHMICDEPTH) {
  552. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  553. }
  554. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  555. if (!this._defines["LIGHT" + lightIndex]) {
  556. continue;
  557. }
  558. if (lightIndex > 0) {
  559. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  560. }
  561. if (this._defines["SHADOW" + lightIndex]) {
  562. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  563. }
  564. if (this._defines["SHADOWPCF" + lightIndex]) {
  565. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  566. }
  567. if (this._defines["SHADOWVSM" + lightIndex]) {
  568. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  569. }
  570. }
  571. if (this._defines.SPECULARTERM) {
  572. fallbacks.addFallback(0, "SPECULARTERM");
  573. }
  574. if (this._defines.OPACITYFRESNEL) {
  575. fallbacks.addFallback(1, "OPACITYFRESNEL");
  576. }
  577. if (this._defines.EMISSIVEFRESNEL) {
  578. fallbacks.addFallback(2, "EMISSIVEFRESNEL");
  579. }
  580. if (this._defines.FRESNEL) {
  581. fallbacks.addFallback(3, "FRESNEL");
  582. }
  583. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  584. fallbacks.addCPUSkinningFallback(0, mesh);
  585. }
  586. //Attributes
  587. var attribs = [BABYLON.VertexBuffer.PositionKind];
  588. if (this._defines.NORMAL) {
  589. attribs.push(BABYLON.VertexBuffer.NormalKind);
  590. }
  591. if (this._defines.UV1) {
  592. attribs.push(BABYLON.VertexBuffer.UVKind);
  593. }
  594. if (this._defines.UV2) {
  595. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  596. }
  597. if (this._defines.VERTEXCOLOR) {
  598. attribs.push(BABYLON.VertexBuffer.ColorKind);
  599. }
  600. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  601. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  602. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  603. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  604. attribs.push(BABYLON.VertexBuffer.MatricesIndicesExtraKind);
  605. attribs.push(BABYLON.VertexBuffer.MatricesWeightsExtraKind);
  606. }
  607. }
  608. if (this._defines.INSTANCES) {
  609. attribs.push("world0");
  610. attribs.push("world1");
  611. attribs.push("world2");
  612. attribs.push("world3");
  613. }
  614. // Legacy browser patch
  615. var shaderName = "pbr";
  616. if (!scene.getEngine().getCaps().standardDerivatives) {
  617. shaderName = "legacypbr";
  618. }
  619. var join = this._defines.toString();
  620. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor", "vReflectionColor",
  621. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  622. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  623. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  624. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  625. "vFogInfos", "vFogColor", "pointSize",
  626. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos",
  627. "mBones",
  628. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "lightmapMatrix",
  629. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  630. "opacityParts", "emissiveLeftColor", "emissiveRightColor",
  631. "vLightingIntensity", "vOverloadedShadowIntensity", "vOverloadedIntensity", "vCameraInfos", "vOverloadedDiffuse", "vOverloadedReflection", "vOverloadedSpecular", "vOverloadedEmissive", "vOverloadedGlossiness",
  632. "logarithmicDepthConstant"
  633. ], ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler",
  634. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  635. ], join, fallbacks, this.onCompiled, this.onError);
  636. }
  637. if (!this._effect.isReady()) {
  638. return false;
  639. }
  640. this._renderId = scene.getRenderId();
  641. this._wasPreviouslyReady = true;
  642. if (mesh) {
  643. if (!mesh._materialDefines) {
  644. mesh._materialDefines = new PBRMaterialDefines();
  645. }
  646. this._defines.cloneTo(mesh._materialDefines);
  647. }
  648. return true;
  649. };
  650. PBRMaterial.prototype.unbind = function () {
  651. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  652. this._effect.setTexture("reflection2DSampler", null);
  653. }
  654. _super.prototype.unbind.call(this);
  655. };
  656. PBRMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  657. this._effect.setMatrix("world", world);
  658. };
  659. PBRMaterial.prototype.bind = function (world, mesh) {
  660. this._myScene = this.getScene();
  661. // Matrices
  662. this.bindOnlyWorldMatrix(world);
  663. this._effect.setMatrix("viewProjection", this._myScene.getTransformMatrix());
  664. // Bones
  665. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  666. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  667. }
  668. if (this._myScene.getCachedMaterial() !== this) {
  669. if (BABYLON.StandardMaterial.FresnelEnabled) {
  670. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  671. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  672. }
  673. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  674. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  675. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  676. }
  677. }
  678. // Textures
  679. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  680. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  681. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  682. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  683. }
  684. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  685. this._effect.setTexture("ambientSampler", this.ambientTexture);
  686. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  687. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  688. }
  689. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  690. this._effect.setTexture("opacitySampler", this.opacityTexture);
  691. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  692. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  693. }
  694. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  695. if (this.reflectionTexture.isCube) {
  696. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  697. }
  698. else {
  699. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  700. }
  701. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  702. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, 0);
  703. }
  704. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  705. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  706. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  707. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  708. }
  709. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapEnabled) {
  710. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  711. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  712. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  713. }
  714. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  715. this._effect.setTexture("specularSampler", this.specularTexture);
  716. this._effect.setFloat2("vSpecularInfos", this.specularTexture.coordinatesIndex, this.specularTexture.level);
  717. this._effect.setMatrix("specularMatrix", this.specularTexture.getTextureMatrix());
  718. }
  719. if (this.bumpTexture && this._myScene.getEngine().getCaps().standardDerivatives && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  720. this._effect.setTexture("bumpSampler", this.bumpTexture);
  721. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level);
  722. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  723. }
  724. // Clip plane
  725. if (this._myScene.clipPlane) {
  726. this._effect.setFloat4("vClipPlane", this._myScene.clipPlane.normal.x, this._myScene.clipPlane.normal.y, this._myScene.clipPlane.normal.z, this._myScene.clipPlane.d);
  727. }
  728. // Point size
  729. if (this.pointsCloud) {
  730. this._effect.setFloat("pointSize", this.pointSize);
  731. }
  732. // Colors
  733. this._myScene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  734. // GAMMA CORRECTION.
  735. this.specularColor.toLinearSpaceToRef(PBRMaterial._scaledSpecular);
  736. this._effect.setVector3("vEyePosition", this._myScene._mirroredCameraPosition ? this._myScene._mirroredCameraPosition : this._myScene.activeCamera.position);
  737. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  738. if (this._defines.SPECULARTERM) {
  739. this._effect.setColor4("vSpecularColor", PBRMaterial._scaledSpecular, this.glossiness);
  740. }
  741. // GAMMA CORRECTION.
  742. this.emissiveColor.toLinearSpaceToRef(PBRMaterial._scaledEmissive);
  743. this._effect.setColor3("vEmissiveColor", PBRMaterial._scaledEmissive);
  744. // GAMMA CORRECTION.
  745. this.reflectionColor.toLinearSpaceToRef(PBRMaterial._scaledReflection);
  746. this._effect.setColor3("vReflectionColor", PBRMaterial._scaledReflection);
  747. }
  748. // GAMMA CORRECTION.
  749. this.diffuseColor.toLinearSpaceToRef(PBRMaterial._scaledDiffuse);
  750. this._effect.setColor4("vDiffuseColor", PBRMaterial._scaledDiffuse, this.alpha * mesh.visibility);
  751. // Lights
  752. if (this._myScene.lightsEnabled && !this.disableLighting) {
  753. PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines);
  754. }
  755. // View
  756. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  757. this._effect.setMatrix("view", this._myScene.getViewMatrix());
  758. }
  759. // Fog
  760. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  761. this._effect.setFloat4("vFogInfos", this._myScene.fogMode, this._myScene.fogStart, this._myScene.fogEnd, this._myScene.fogDensity);
  762. this._effect.setColor3("vFogColor", this._myScene.fogColor);
  763. }
  764. this._lightingInfos.x = this.directIntensity;
  765. this._lightingInfos.y = this.emissiveIntensity;
  766. this._lightingInfos.z = this.environmentIntensity;
  767. this._effect.setVector4("vLightingIntensity", this._lightingInfos);
  768. this._overloadedShadowInfos.x = this.overloadedShadowIntensity;
  769. this._overloadedShadowInfos.y = this.overloadedShadeIntensity;
  770. this._effect.setVector4("vOverloadedShadowIntensity", this._overloadedShadowInfos);
  771. this._cameraInfos.x = this.cameraExposure;
  772. this._cameraInfos.y = this.cameraContrast;
  773. this._effect.setVector4("vCameraInfos", this._cameraInfos);
  774. this._overloadedIntensity.x = this.overloadedAmbientIntensity;
  775. this._overloadedIntensity.y = this.overloadedDiffuseIntensity;
  776. this._overloadedIntensity.z = this.overloadedSpecularIntensity;
  777. this._overloadedIntensity.w = this.overloadedEmissiveIntensity;
  778. this._effect.setVector4("vOverloadedIntensity", this._overloadedIntensity);
  779. this.overloadedAmbient.toLinearSpaceToRef(this._tempColor);
  780. this._effect.setColor3("vOverloadedAmbient", this._tempColor);
  781. this.overloadedDiffuse.toLinearSpaceToRef(this._tempColor);
  782. this._effect.setColor3("vOverloadedDiffuse", this._tempColor);
  783. this.overloadedSpecular.toLinearSpaceToRef(this._tempColor);
  784. this._effect.setColor3("vOverloadedSpecular", this._tempColor);
  785. this.overloadedEmissive.toLinearSpaceToRef(this._tempColor);
  786. this._effect.setColor3("vOverloadedEmissive", this._tempColor);
  787. this.overloadedReflection.toLinearSpaceToRef(this._tempColor);
  788. this._effect.setColor3("vOverloadedReflection", this._tempColor);
  789. this._overloadedGlossiness.x = this.overloadedGlossiness;
  790. this._overloadedGlossiness.y = this.overloadedGlossinessIntensity;
  791. this._overloadedGlossiness.z = this.overloadedReflectionIntensity;
  792. this._effect.setVector3("vOverloadedGlossiness", this._overloadedGlossiness);
  793. // Log. depth
  794. if (this._defines.LOGARITHMICDEPTH) {
  795. this._effect.setFloat("logarithmicDepthConstant", 2.0 / (Math.log(this._myScene.activeCamera.maxZ + 1.0) / Math.LN2));
  796. }
  797. _super.prototype.bind.call(this, world, mesh);
  798. this._myScene = null;
  799. };
  800. PBRMaterial.prototype.getAnimatables = function () {
  801. var results = [];
  802. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  803. results.push(this.diffuseTexture);
  804. }
  805. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  806. results.push(this.ambientTexture);
  807. }
  808. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  809. results.push(this.opacityTexture);
  810. }
  811. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  812. results.push(this.reflectionTexture);
  813. }
  814. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  815. results.push(this.emissiveTexture);
  816. }
  817. if (this.specularTexture && this.specularTexture.animations && this.specularTexture.animations.length > 0) {
  818. results.push(this.specularTexture);
  819. }
  820. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  821. results.push(this.bumpTexture);
  822. }
  823. return results;
  824. };
  825. PBRMaterial.prototype.dispose = function (forceDisposeEffect) {
  826. if (this.diffuseTexture) {
  827. this.diffuseTexture.dispose();
  828. }
  829. if (this.ambientTexture) {
  830. this.ambientTexture.dispose();
  831. }
  832. if (this.opacityTexture) {
  833. this.opacityTexture.dispose();
  834. }
  835. if (this.reflectionTexture) {
  836. this.reflectionTexture.dispose();
  837. }
  838. if (this.emissiveTexture) {
  839. this.emissiveTexture.dispose();
  840. }
  841. if (this.specularTexture) {
  842. this.specularTexture.dispose();
  843. }
  844. if (this.bumpTexture) {
  845. this.bumpTexture.dispose();
  846. }
  847. _super.prototype.dispose.call(this, forceDisposeEffect);
  848. };
  849. PBRMaterial.prototype.clone = function (name) {
  850. var newPBRMaterial = new PBRMaterial(name, this.getScene());
  851. // Base material
  852. this.copyTo(newPBRMaterial);
  853. newPBRMaterial.directIntensity = this.directIntensity;
  854. newPBRMaterial.emissiveIntensity = this.emissiveIntensity;
  855. newPBRMaterial.environmentIntensity = this.environmentIntensity;
  856. newPBRMaterial.cameraExposure = this.cameraExposure;
  857. newPBRMaterial.cameraContrast = this.cameraContrast;
  858. newPBRMaterial.overloadedShadowIntensity = this.overloadedShadowIntensity;
  859. newPBRMaterial.overloadedShadeIntensity = this.overloadedShadeIntensity;
  860. newPBRMaterial.overloadedAmbientIntensity = this.overloadedAmbientIntensity;
  861. newPBRMaterial.overloadedDiffuseIntensity = this.overloadedDiffuseIntensity;
  862. newPBRMaterial.overloadedSpecularIntensity = this.overloadedSpecularIntensity;
  863. newPBRMaterial.overloadedEmissiveIntensity = this.overloadedEmissiveIntensity;
  864. newPBRMaterial.overloadedAmbient = this.overloadedAmbient;
  865. newPBRMaterial.overloadedDiffuse = this.overloadedDiffuse;
  866. newPBRMaterial.overloadedSpecular = this.overloadedSpecular;
  867. newPBRMaterial.overloadedEmissive = this.overloadedEmissive;
  868. newPBRMaterial.overloadedReflection = this.overloadedReflection;
  869. newPBRMaterial.overloadedGlossiness = this.overloadedGlossiness;
  870. newPBRMaterial.overloadedGlossinessIntensity = this.overloadedGlossinessIntensity;
  871. newPBRMaterial.overloadedReflectionIntensity = this.overloadedReflectionIntensity;
  872. newPBRMaterial.disableBumpMap = this.disableBumpMap;
  873. // Standard material
  874. if (this.diffuseTexture && this.diffuseTexture.clone) {
  875. newPBRMaterial.diffuseTexture = this.diffuseTexture.clone();
  876. }
  877. if (this.ambientTexture && this.ambientTexture.clone) {
  878. newPBRMaterial.ambientTexture = this.ambientTexture.clone();
  879. }
  880. if (this.opacityTexture && this.opacityTexture.clone) {
  881. newPBRMaterial.opacityTexture = this.opacityTexture.clone();
  882. }
  883. if (this.reflectionTexture && this.reflectionTexture.clone) {
  884. newPBRMaterial.reflectionTexture = this.reflectionTexture.clone();
  885. }
  886. if (this.emissiveTexture && this.emissiveTexture.clone) {
  887. newPBRMaterial.emissiveTexture = this.emissiveTexture.clone();
  888. }
  889. if (this.specularTexture && this.specularTexture.clone) {
  890. newPBRMaterial.specularTexture = this.specularTexture.clone();
  891. }
  892. if (this.bumpTexture && this.bumpTexture.clone) {
  893. newPBRMaterial.bumpTexture = this.bumpTexture.clone();
  894. }
  895. if (this.lightmapTexture && this.lightmapTexture.clone) {
  896. newPBRMaterial.lightmapTexture = this.lightmapTexture.clone();
  897. newPBRMaterial.useLightmapAsShadowmap = this.useLightmapAsShadowmap;
  898. }
  899. newPBRMaterial.ambientColor = this.ambientColor.clone();
  900. newPBRMaterial.diffuseColor = this.diffuseColor.clone();
  901. newPBRMaterial.specularColor = this.specularColor.clone();
  902. newPBRMaterial.reflectionColor = this.reflectionColor.clone();
  903. newPBRMaterial.glossiness = this.glossiness;
  904. newPBRMaterial.emissiveColor = this.emissiveColor.clone();
  905. newPBRMaterial.useAlphaFromDiffuseTexture = this.useAlphaFromDiffuseTexture;
  906. newPBRMaterial.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
  907. newPBRMaterial.useGlossinessFromSpecularMapAlpha = this.useGlossinessFromSpecularMapAlpha;
  908. newPBRMaterial.useSpecularOverAlpha = this.useSpecularOverAlpha;
  909. newPBRMaterial.emissiveFresnelParameters = this.emissiveFresnelParameters.clone();
  910. newPBRMaterial.opacityFresnelParameters = this.opacityFresnelParameters.clone();
  911. return newPBRMaterial;
  912. };
  913. PBRMaterial._scaledDiffuse = new BABYLON.Color3();
  914. PBRMaterial._scaledSpecular = new BABYLON.Color3();
  915. PBRMaterial._scaledEmissive = new BABYLON.Color3();
  916. PBRMaterial._scaledReflection = new BABYLON.Color3();
  917. return PBRMaterial;
  918. })(BABYLON.Material);
  919. BABYLON.PBRMaterial = PBRMaterial;
  920. })(BABYLON || (BABYLON = {}));
  921. BABYLON.Effect.ShadersStore['pbrVertexShader'] = "precision highp float;\n\n// Attributes\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#if NUM_BONE_INFLUENCERS > 0\nuniform mat4 mBones[BonesPerMesh];\n\nattribute vec4 matricesIndices;\nattribute vec4 matricesWeights;\n#if NUM_BONE_INFLUENCERS > 4\nattribute vec4 matricesIndicesExtra;\nattribute vec4 matricesWeightsExtra;\n#endif\n#endif\n\n// Uniforms\n\n#ifdef INSTANCES\nattribute vec4 world0;\nattribute vec4 world1;\nattribute vec4 world2;\nattribute vec4 world3;\n#else\nuniform mat4 world;\n#endif\n\nuniform mat4 view;\nuniform mat4 viewProjection;\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform mat4 diffuseMatrix;\nuniform vec2 vDiffuseInfos;\n#endif\n\n#ifdef AMBIENT\nvarying vec2 vAmbientUV;\nuniform mat4 ambientMatrix;\nuniform vec2 vAmbientInfos;\n#endif\n\n#ifdef OPACITY\nvarying vec2 vOpacityUV;\nuniform mat4 opacityMatrix;\nuniform vec2 vOpacityInfos;\n#endif\n\n#ifdef EMISSIVE\nvarying vec2 vEmissiveUV;\nuniform vec2 vEmissiveInfos;\nuniform mat4 emissiveMatrix;\n#endif\n\n#ifdef LIGHTMAP\nvarying vec2 vLightmapUV;\nuniform vec2 vLightmapInfos;\nuniform mat4 lightmapMatrix;\n#endif\n\n#if defined(SPECULAR) && defined(SPECULARTERM)\nvarying vec2 vSpecularUV;\nuniform vec2 vSpecularInfos;\nuniform mat4 specularMatrix;\n#endif\n\n#ifdef BUMP\nvarying vec2 vBumpUV;\nuniform vec2 vBumpInfos;\nuniform mat4 bumpMatrix;\n#endif\n\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\n// Output\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#ifdef CLIPPLANE\nuniform vec4 vClipPlane;\nvarying float fClipDistance;\n#endif\n\n#ifdef FOG\nvarying float fFogDistance;\n#endif\n\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\n\n#ifdef REFLECTIONMAP_SKYBOX\nvarying vec3 vPositionUVW;\n#endif\n\n#ifdef REFLECTIONMAP_EQUIRECTANGULAR\nvarying vec3 vDirectionW;\n#endif\n\n#ifdef LOGARITHMICDEPTH\nuniform float logarithmicDepthConstant;\nvarying float vFragmentDepth;\n#endif\n\nvoid main(void) {\n\n#ifdef REFLECTIONMAP_SKYBOX\n vPositionUVW = position;\n#endif \n\n#ifdef INSTANCES\n mat4 finalWorld = mat4(world0, world1, world2, world3);\n#else\n mat4 finalWorld = world;\n#endif\n\n#if NUM_BONE_INFLUENCERS > 0\n mat4 influence;\n influence = mBones[int(matricesIndices[0])] * matricesWeights[0];\n\n#if NUM_BONE_INFLUENCERS > 1\n influence += mBones[int(matricesIndices[1])] * matricesWeights[1];\n#endif \n#if NUM_BONE_INFLUENCERS > 2\n influence += mBones[int(matricesIndices[2])] * matricesWeights[2];\n#endif\t\n#if NUM_BONE_INFLUENCERS > 3\n influence += mBones[int(matricesIndices[3])] * matricesWeights[3];\n#endif\t\n\n#if NUM_BONE_INFLUENCERS > 4\n influence += mBones[int(matricesIndicesExtra[0])] * matricesWeightsExtra[0];\n#endif\n#if NUM_BONE_INFLUENCERS > 5\n influence += mBones[int(matricesIndicesExtra[1])] * matricesWeightsExtra[1];\n#endif\t\n#if NUM_BONE_INFLUENCERS > 6\n influence += mBones[int(matricesIndicesExtra[2])] * matricesWeightsExtra[2];\n#endif\t\n#if NUM_BONE_INFLUENCERS > 7\n influence += mBones[int(matricesIndicesExtra[3])] * matricesWeightsExtra[3];\n#endif\t\n\n finalWorld = finalWorld * influence;\n#endif\n\n gl_Position = viewProjection * finalWorld * vec4(position, 1.0);\n\n vec4 worldPos = finalWorld * vec4(position, 1.0);\n vPositionW = vec3(worldPos);\n\n#ifdef NORMAL\n vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\n#endif\n\n#ifdef REFLECTIONMAP_EQUIRECTANGULAR\n vDirectionW = normalize(vec3(finalWorld * vec4(position, 0.0)));\n#endif\n\n // Texture coordinates\n#ifndef UV1\n vec2 uv = vec2(0., 0.);\n#endif\n#ifndef UV2\n vec2 uv2 = vec2(0., 0.);\n#endif\n\n#ifdef DIFFUSE\n if (vDiffuseInfos.x == 0.)\n {\n vDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));\n }\n#endif\n\n#ifdef AMBIENT\n if (vAmbientInfos.x == 0.)\n {\n vAmbientUV = vec2(ambientMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vAmbientUV = vec2(ambientMatrix * vec4(uv2, 1.0, 0.0));\n }\n#endif\n\n#ifdef OPACITY\n if (vOpacityInfos.x == 0.)\n {\n vOpacityUV = vec2(opacityMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vOpacityUV = vec2(opacityMatrix * vec4(uv2, 1.0, 0.0));\n }\n#endif\n\n#ifdef EMISSIVE\n if (vEmissiveInfos.x == 0.)\n {\n vEmissiveUV = vec2(emissiveMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vEmissiveUV = vec2(emissiveMatrix * vec4(uv2, 1.0, 0.0));\n }\n#endif\n\n#ifdef LIGHTMAP\n if (vLightmapInfos.x == 0.)\n {\n vLightmapUV = vec2(lightmapMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vLightmapUV = vec2(lightmapMatrix * vec4(uv2, 1.0, 0.0));\n }\n#endif\n\n#if defined(SPECULAR) && defined(SPECULARTERM)\n if (vSpecularInfos.x == 0.)\n {\n vSpecularUV = vec2(specularMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vSpecularUV = vec2(specularMatrix * vec4(uv2, 1.0, 0.0));\n }\n#endif\n\n#ifdef BUMP\n if (vBumpInfos.x == 0.)\n {\n vBumpUV = vec2(bumpMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vBumpUV = vec2(bumpMatrix * vec4(uv2, 1.0, 0.0));\n }\n#endif\n\n // Clip plane\n#ifdef CLIPPLANE\n fClipDistance = dot(worldPos, vClipPlane);\n#endif\n\n // Fog\n#ifdef FOG\n fFogDistance = (view * worldPos).z;\n#endif\n\n // Shadows\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 // Vertex color\n#ifdef VERTEXCOLOR\n vColor = color;\n#endif\n\n // Point size\n#ifdef POINTSIZE\n gl_PointSize = pointSize;\n#endif\n\n // Log. depth\n#ifdef LOGARITHMICDEPTH\n vFragmentDepth = 1.0 + gl_Position.w;\n gl_Position.z = log2(max(0.000001, vFragmentDepth)) * logarithmicDepthConstant;\n#endif\n}";
  922. BABYLON.Effect.ShadersStore['pbrPixelShader'] = "#ifdef BUMP\n#extension GL_OES_standard_derivatives : enable\n#endif\n\n#ifdef LOGARITHMICDEPTH\n#extension GL_EXT_frag_depth : enable\n#endif\n\nprecision highp float;\n\n// Constants\n#define RECIPROCAL_PI2 0.15915494\n#define FRESNEL_MAXIMUM_ON_ROUGH 0.25\n\nuniform vec3 vEyePosition;\nuniform vec3 vAmbientColor;\nuniform vec3 vReflectionColor;\nuniform vec4 vDiffuseColor;\n\n// CUSTOM CONTROLS\nuniform vec4 vLightingIntensity;\nuniform vec4 vCameraInfos;\n\n#ifdef OVERLOADEDVALUES\n uniform vec4 vOverloadedIntensity;\n uniform vec3 vOverloadedAmbient;\n uniform vec3 vOverloadedDiffuse;\n uniform vec3 vOverloadedSpecular;\n uniform vec3 vOverloadedEmissive;\n uniform vec3 vOverloadedReflection;\n uniform vec3 vOverloadedGlossiness;\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\n uniform vec4 vOverloadedShadowIntensity;\n#endif\n\n// PBR CUSTOM CONSTANTS\nconst float kPi = 3.1415926535897932384626433832795;\n\n// PBR HELPER METHODS\nfloat Square(float value)\n{\n return value * value;\n}\n\nfloat getLuminance(vec3 color)\n{\n return clamp(dot(color, vec3(0.2126, 0.7152, 0.0722)), 0., 1.);\n}\n\nfloat convertRoughnessToAverageSlope(float roughness)\n{\n // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues\n const float kMinimumVariance = 0.0005;\n float alphaG = Square(roughness) + kMinimumVariance;\n return alphaG;\n}\n\n// From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007\nfloat smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)\n{\n float tanSquared = (1.0 - dot * dot) / (dot * dot);\n return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));\n}\n\nfloat smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)\n{\n return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);\n}\n\n// Trowbridge-Reitz (GGX)\n// Generalised Trowbridge-Reitz with gamma power=2.0\nfloat normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)\n{\n // Note: alphaG is average slope (gradient) of the normals in slope-space.\n // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have\n // a tangent (gradient) closer to the macrosurface than this slope.\n float a2 = Square(alphaG);\n float d = NdotH * NdotH * (a2 - 1.0) + 1.0;\n return a2 / (kPi * d * d);\n}\n\nvec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)\n{\n return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0);\n}\n\nvec3 FresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)\n{\n // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle\n float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);\n return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);\n}\n\n// Cook Torance Specular computation.\nvec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor)\n{\n float alphaG = convertRoughnessToAverageSlope(roughness);\n float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);\n float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG);\n visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes.\n\n vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.));\n\n float specTerm = max(0., visibility * distribution) * NdotL;\n return fresnel * specTerm * kPi; // TODO: audit pi constants\n}\n\nfloat computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness)\n{\n // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of\n // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.\n float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);\n float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);\n float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;\n float diffuseFresnelTerm =\n (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *\n (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);\n\n\n return diffuseFresnelTerm * NdotL;\n // PI Test\n // diffuseFresnelTerm /= kPi;\n}\n\nfloat computeDefaultGlossiness(float glossiness, vec3 specularColor)\n{\n float kSpecularNoAlphaWorkflow_SmoothnessMax = 0.95;\n\n float specularLuminance = getLuminance(specularColor);\n float specularLuma = sqrt(specularLuminance);\n glossiness = specularLuma * kSpecularNoAlphaWorkflow_SmoothnessMax;\n\n return glossiness;\n}\n\nvec3 toLinearSpace(vec3 color)\n{\n return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));\n}\n\nvec3 toGammaSpace(vec3 color)\n{\n return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));\n}\n\n#ifdef CAMERATONEMAP\n vec3 toneMaps(vec3 color)\n {\n color = max(color, 0.0);\n\n // TONE MAPPING / EXPOSURE\n color.rgb = color.rgb * vCameraInfos.x;\n\n float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately\n // PI Test\n // tuning *= kPi;\n vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper\n color.rgb = mix(color.rgb, tonemapped, 1.0);\n return color;\n }\n#endif\n\n#ifdef CAMERACONTRAST\n vec4 contrasts(vec4 color)\n {\n color = clamp(color, 0.0, 1.0);\n\n vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);\n float contrast = vCameraInfos.y;\n if (contrast < 1.0)\n {\n // Decrease contrast: interpolate towards zero-contrast image (flat grey)\n color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);\n }\n else\n {\n // Increase contrast: apply simple shoulder-toe high contrast curve\n color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);\n }\n\n return color;\n }\n#endif\n// END PBR HELPER METHODS\n\n#ifdef SPECULARTERM\nuniform vec4 vSpecularColor;\n#endif\nuniform vec3 vEmissiveColor;\n\n// Input\nvarying vec3 vPositionW;\n\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n// Lights\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\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\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\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// Samplers\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n\n#ifdef AMBIENT\nvarying vec2 vAmbientUV;\nuniform sampler2D ambientSampler;\nuniform vec2 vAmbientInfos;\n#endif\n\n#ifdef OPACITY\t\nvarying vec2 vOpacityUV;\nuniform sampler2D opacitySampler;\nuniform vec2 vOpacityInfos;\n#endif\n\n#ifdef EMISSIVE\nvarying vec2 vEmissiveUV;\nuniform vec2 vEmissiveInfos;\nuniform sampler2D emissiveSampler;\n#endif\n\n#ifdef LIGHTMAP\nvarying vec2 vLightmapUV;\nuniform vec2 vLightmapInfos;\nuniform sampler2D lightmapSampler;\n#endif\n\n#if defined(SPECULAR) && defined(SPECULARTERM)\nvarying vec2 vSpecularUV;\nuniform vec2 vSpecularInfos;\nuniform sampler2D specularSampler;\n#endif\n\n// Fresnel\n#ifdef FRESNEL\nfloat computeFresnelTerm(vec3 viewDirection, vec3 worldNormal, float bias, float power)\n{\n float fresnelTerm = pow(bias + abs(dot(viewDirection, worldNormal)), power);\n return clamp(fresnelTerm, 0., 1.);\n}\n#endif\n\n#ifdef OPACITYFRESNEL\nuniform vec4 opacityParts;\n#endif\n\n#ifdef EMISSIVEFRESNEL\nuniform vec4 emissiveLeftColor;\nuniform vec4 emissiveRightColor;\n#endif\n\n// Reflection\n#ifdef REFLECTION\nuniform vec2 vReflectionInfos;\n\n#ifdef REFLECTIONMAP_3D\nuniform samplerCube reflectionCubeSampler;\n#else\nuniform sampler2D reflection2DSampler;\n#endif\n\n#ifdef REFLECTIONMAP_SKYBOX\nvarying vec3 vPositionUVW;\n#else\n#ifdef REFLECTIONMAP_EQUIRECTANGULAR\nvarying vec3 vDirectionW;\n#endif\n\n#if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)\nuniform mat4 reflectionMatrix;\n#endif\n#if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION)\nuniform mat4 view;\n#endif\n#endif\n\nvec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)\n{\n#ifdef REFLECTIONMAP_EQUIRECTANGULAR\n vec3 direction = normalize(vDirectionW);\n\n float t = clamp(direction.y * -0.5 + 0.5, 0., 1.0);\n float s = atan(direction.z, direction.x) * RECIPROCAL_PI2 + 0.5;\n\n return vec3(s, t, 0);\n#endif\n\n#ifdef REFLECTIONMAP_SPHERICAL\n vec3 viewDir = normalize(vec3(view * worldPos));\n vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));\n\n vec3 r = reflect(viewDir, viewNormal);\n r.z = r.z - 1.0;\n\n float m = 2.0 * length(r);\n\n return vec3(r.x / m + 0.5, 1.0 - r.y / m - 0.5, 0);\n#endif\n\n#ifdef REFLECTIONMAP_PLANAR\n vec3 viewDir = worldPos.xyz - vEyePosition;\n vec3 coords = normalize(reflect(viewDir, worldNormal));\n\n return vec3(reflectionMatrix * vec4(coords, 1));\n#endif\n\n#ifdef REFLECTIONMAP_CUBIC\n vec3 viewDir = worldPos.xyz - vEyePosition;\n vec3 coords = reflect(viewDir, worldNormal);\n#ifdef INVERTCUBICMAP\n coords.y = 1.0 - coords.y;\n#endif\n return vec3(reflectionMatrix * vec4(coords, 0));\n#endif\n\n#ifdef REFLECTIONMAP_PROJECTION\n return vec3(reflectionMatrix * (view * worldPos));\n#endif\n\n#ifdef REFLECTIONMAP_SKYBOX\n return vPositionUVW;\n#endif\n\n#ifdef REFLECTIONMAP_EXPLICIT\n return vec3(0, 0, 0);\n#endif\n}\n\n#endif\n\n// Shadows\n#ifdef SHADOWS\n\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\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\n depth = clamp(depth, 0., 1.);\n\n directionToLight.y = 1.0 - directionToLight.y;\n\n float shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;\n\n if (depth > shadow)\n {\n#ifdef OVERLOADEDSHADOWVALUES\n return mix(1.0, darkness, vOverloadedShadowIntensity.x);\n#else\n return darkness;\n#endif\n }\n return 1.0;\n}\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 float diskScale = (1.0 - (1.0 + depth * 3.0)) / mapSize;\n\n depth = clamp(depth, 0., 1.);\n\n directionToLight.y = 1.0 - directionToLight.y;\n\n float visibility = 1.;\n\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 // Poisson Sampling\n float biasedDepth = depth - bias;\n\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\n#ifdef OVERLOADEDSHADOWVALUES\n return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));\n#else\n return min(1.0, visibility + darkness);\n#endif\n}\n#endif\n\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\n if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\n {\n return 1.0;\n }\n\n float shadow = unpack(texture2D(shadowSampler, uv)) + bias;\n\n if (depth.z > shadow)\n {\n#ifdef OVERLOADEDSHADOWVALUES\n return mix(1.0, darkness, vOverloadedShadowIntensity.x);\n#else\n return darkness;\n#endif\n }\n return 1.;\n}\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\n if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\n {\n return 1.0;\n }\n\n float visibility = 1.;\n\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 // Poisson Sampling\n float biasedDepth = depth.z - bias;\n\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\n#ifdef OVERLOADEDSHADOWVALUES\n return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));\n#else\n return min(1.0, visibility + darkness);\n#endif\n}\n\n// Thanks to http://devmaster.net/\nfloat unpackHalf(vec2 color)\n{\n return color.x + (color.y / 255.0);\n}\n\nfloat linstep(float low, float high, float v) {\n return clamp((v - low) / (high - low), 0.0, 1.0);\n}\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\n return clamp(max(p, p_max), 0.0, 1.0);\n}\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\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\n vec4 texel = texture2D(shadowSampler, uv);\n\n vec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));\n#ifdef OVERLOADEDSHADOWVALUES\n return min(1.0, mix(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness, vOverloadedShadowIntensity.x));\n#else\n return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);\n#endif\n}\n#endif\n\n#endif\n\n// Bump\n#ifdef BUMP\nvarying vec2 vBumpUV;\nuniform vec2 vBumpInfos;\nuniform sampler2D bumpSampler;\n\n// Thanks to http://www.thetenthplanet.de/archives/1180\nmat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)\n{\n // get edge vectors of the pixel triangle\n vec3 dp1 = dFdx(p);\n vec3 dp2 = dFdy(p);\n vec2 duv1 = dFdx(uv);\n vec2 duv2 = dFdy(uv);\n\n // solve the linear system\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 // construct a scale-invariant frame \n float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));\n return mat3(tangent * invmax, binormal * invmax, normal);\n}\n\nvec3 perturbNormal(vec3 viewDir)\n{\n vec3 map = texture2D(bumpSampler, vBumpUV).xyz;\n map = map * 255. / 127. - 128. / 127.;\n mat3 TBN = cotangent_frame(vNormalW * vBumpInfos.y, -viewDir, vBumpUV);\n return normalize(TBN * map);\n}\n#endif\n\n#ifdef CLIPPLANE\nvarying float fClipDistance;\n#endif\n\n#ifdef LOGARITHMICDEPTH\nuniform float logarithmicDepthConstant;\nvarying float vFragmentDepth;\n#endif\n\n// Fog\n#ifdef FOG\n\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\n\nuniform vec4 vFogInfos;\nuniform vec3 vFogColor;\nvarying float fFogDistance;\n\nfloat CalcFogFactor()\n{\n float fogCoeff = 1.0;\n float fogStart = vFogInfos.y;\n float fogEnd = vFogInfos.z;\n float fogDensity = vFogInfos.w;\n\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\n return clamp(fogCoeff, 0.0, 1.0);\n}\n#endif\n\n// Light Computing\nstruct lightingInfo\n{\n vec3 diffuse;\n#ifdef SPECULARTERM\n vec3 specular;\n#endif\n};\n\nlightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 lightVectorW;\n float attenuation = 1.0;\n if (lightData.w == 0.)\n {\n vec3 direction = lightData.xyz - vPositionW;\n\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 // diffuse\n vec3 H = normalize(viewDirectionW + lightVectorW);\n float NdotL = max(0.00000000001, dot(vNormal, lightVectorW));\n float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));\n\n float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);\n result.diffuse = diffuseTerm * diffuseColor * attenuation;\n\n#ifdef SPECULARTERM\n // Specular\n float NdotH = max(0.00000000001, dot(vNormal, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * attenuation;\n#endif\n\n return result;\n}\n\nlightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 direction = lightData.xyz - vPositionW;\n vec3 lightVectorW = normalize(direction);\n float attenuation = max(0., 1.0 - length(direction) / range);\n\n // diffuse\n float cosAngle = max(0.0000001, dot(-lightDirection.xyz, lightVectorW));\n float spotAtten = 0.0;\n\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 // Diffuse\n vec3 H = normalize(viewDirectionW - lightDirection.xyz);\n float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));\n float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);\n\n float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);\n result.diffuse = diffuseTerm * diffuseColor * attenuation * spotAtten;\n\n#ifdef SPECULARTERM\n // Specular\n float NdotH = max(0.00000000001, dot(vNormal, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * attenuation * spotAtten;\n#endif\n\n return result;\n }\n\n result.diffuse = vec3(0.);\n#ifdef SPECULARTERM\n result.specular = vec3(0.);\n#endif\n\n return result;\n}\n\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 lightVectorW = normalize(lightData.xyz);\n\n // Diffuse\n float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;\n result.diffuse = mix(groundColor, diffuseColor, ndl);\n\n#ifdef SPECULARTERM\n // Specular\n vec3 H = normalize(viewDirectionW + lightVectorW);\n float NdotH = max(0.00000000001, dot(vNormal, H));\n float NdotL = max(0.00000000001, ndl);\n float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm;\n#endif\n\n return result;\n}\n\nvoid main(void) {\n // Clip plane\n#ifdef CLIPPLANE\n if (fClipDistance > 0.0)\n discard;\n#endif\n\n vec3 viewDirectionW = normalize(vEyePosition - vPositionW);\n\n // Base color\n vec4 baseColor = vec4(1., 1., 1., 1.);\n vec3 diffuseColor = vDiffuseColor.rgb;\n \n // Alpha\n float alpha = vDiffuseColor.a;\n\n#ifdef DIFFUSE\n baseColor = texture2D(diffuseSampler, vDiffuseUV);\n baseColor = vec4(toLinearSpace(baseColor.rgb), baseColor.a);\n\n#ifdef ALPHATEST\n if (baseColor.a < 0.4)\n discard;\n#endif\n\n#ifdef ALPHAFROMDIFFUSE\n alpha *= baseColor.a;\n#endif\n\n baseColor.rgb *= vDiffuseInfos.y;\n#endif\n\n#ifdef VERTEXCOLOR\n baseColor.rgb *= vColor.rgb;\n#endif\n\n#ifdef OVERLOADEDVALUES\n baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);\n diffuseColor.rgb = mix(diffuseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);\n#endif\n\n // Bump\n#ifdef NORMAL\n vec3 normalW = normalize(vNormalW);\n#else\n vec3 normalW = vec3(1.0, 1.0, 1.0);\n#endif\n\n\n#ifdef BUMP\n normalW = perturbNormal(viewDirectionW);\n#endif\n\n // Ambient color\n vec3 baseAmbientColor = vec3(1., 1., 1.);\n\n#ifdef AMBIENT\n baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;\n \n #ifdef OVERLOADEDVALUES\n baseAmbientColor.rgb = mix(baseAmbientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);\n #endif\n#endif\n\n // Specular map\n#ifdef SPECULARTERM\n float glossiness = vSpecularColor.a;\n vec3 specularColor = vSpecularColor.rgb;\n \n #ifdef OVERLOADEDVALUES\n specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);\n #endif\n\n #ifdef SPECULAR\n vec4 specularMapColor = texture2D(specularSampler, vSpecularUV);\n specularColor = toLinearSpace(specularMapColor.rgb);\n\n #ifdef OVERLOADEDVALUES\n specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);\n #endif\n\n #ifdef GLOSSINESSFROMSPECULARMAP\n glossiness = specularMapColor.a;\n #else\n glossiness = computeDefaultGlossiness(glossiness, specularColor);\n #endif\n #endif\n\n #ifdef OVERLOADEDVALUES\n glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);\n #endif\n#else\n float glossiness = 0.;\n #ifdef OVERLOADEDVALUES\n glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);\n #endif\n \n vec3 specularColor = vec3(0., 0., 0);\n #ifdef OVERLOADEDVALUES\n specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);\n #endif\n#endif\n\n // Apply Energy Conservation taking in account the environment level only if the environment is present.\n float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\n baseColor.rgb = (1. - reflectance) * baseColor.rgb;\n\n // Compute Specular Fresnel + Reflectance.\n float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));\n\n // Adapt glossiness.\n glossiness = clamp(glossiness, 0., 1.) * 0.98;\n\n // Call rough to not conflict with previous one.\n float rough = clamp(1. - glossiness, 0.000001, 1.0);\n\n // Lighting\n vec3 diffuseBase = vec3(0., 0., 0.);\n \n#ifdef OVERLOADEDSHADOWVALUES\n vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);\n#endif\n\n#ifdef SPECULARTERM\n vec3 specularBase = vec3(0., 0., 0.);\n#endif\n float shadow = 1.;\n\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, rough, NdotV);\n#endif\n#ifdef HEMILIGHT0\n lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, rough, NdotV);\n#endif\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\n lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);\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 OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\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, rough, NdotV);\n#endif\n#ifdef HEMILIGHT1\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, rough, NdotV);\n#endif\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\n info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);\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\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\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, rough, NdotV);\n#endif\n#ifdef HEMILIGHT2\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, rough, NdotV);\n#endif\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\n info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);\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\t\n#endif\t\n#else\n shadow = 1.;\n#endif\n\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\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, rough, NdotV);\n#endif\n#ifdef HEMILIGHT3\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, rough, NdotV);\n#endif\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\n info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);\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\t\n#endif\t\n#else\n shadow = 1.;\n#endif\n\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\n// Reflection\nvec3 reflectionColor = vReflectionColor.rgb;\nvec3 ambientReflectionColor = vReflectionColor.rgb;\n\n#ifdef REFLECTION\n vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);\n\n #ifdef REFLECTIONMAP_3D\n float bias = 0.;\n\n #ifdef SPECULARTERM\n // Go mat -> blurry reflexion according to glossiness\n bias = 20. * (1.0 - glossiness);\n #endif\n\n reflectionColor = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;\n reflectionColor = toLinearSpace(reflectionColor.rgb);\n\n ambientReflectionColor = textureCube(reflectionCubeSampler, normalW, 20.).rgb * vReflectionInfos.x;\n ambientReflectionColor = toLinearSpace(ambientReflectionColor.rgb);\n #else\n vec2 coords = vReflectionUVW.xy;\n\n #ifdef REFLECTIONMAP_PROJECTION\n coords /= vReflectionUVW.z;\n #endif\n\n coords.y = 1.0 - coords.y;\n\n reflectionColor = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.x;\n reflectionColor = toLinearSpace(reflectionColor.rgb);\n\n ambientReflectionColor = texture2D(reflection2DSampler, coords, 20.).rgb * vReflectionInfos.x;\n ambientReflectionColor = toLinearSpace(ambientReflectionColor.rgb);\n #endif\n#endif\n\n#ifdef OVERLOADEDVALUES\n ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);\n reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);\n#endif\n\nreflectionColor *= vLightingIntensity.z;\nambientReflectionColor *= vLightingIntensity.z;\n\n// Compute reflection specular fresnel\nvec3 specularEnvironmentR0 = specularColor.rgb;\nvec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);\nvec3 specularEnvironmentReflectanceViewer = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(glossiness));\nreflectionColor *= specularEnvironmentReflectanceViewer;\n\n#ifdef OPACITY\n vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);\n\n #ifdef OPACITYRGB\n opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);\n alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;\n #else\n alpha *= opacityMap.a * vOpacityInfos.y;\n #endif\n\n#endif\n\n#ifdef VERTEXALPHA\n alpha *= vColor.a;\n#endif\n\n#ifdef OPACITYFRESNEL\n float opacityFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, opacityParts.z, opacityParts.w);\n\n alpha += opacityParts.x * (1.0 - opacityFresnelTerm) + opacityFresnelTerm * opacityParts.y;\n#endif\n\n // Emissive\n vec3 emissiveColor = vEmissiveColor;\n#ifdef EMISSIVE\n vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;\n emissiveColor = toLinearSpace(emissiveColorTex.rgb) * emissiveColor * vEmissiveInfos.y;\n#endif\n\n#ifdef OVERLOADEDVALUES\n emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);\n#endif\n\n#ifdef EMISSIVEFRESNEL\n float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);\n\n emissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;\n#endif\n\n // Composition\n#ifdef EMISSIVEASILLUMINATION\n vec3 finalDiffuse = max(diffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\n \n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n#else\n #ifdef LINKEMISSIVEWITHDIFFUSE\n vec3 finalDiffuse = max((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\n\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n #else\n vec3 finalDiffuse = max(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\n\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n #endif\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\n finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));\n#endif\n\n// diffuse lighting from environment 0.2 replaces Harmonic...\n// Ambient Reflection already includes the environment intensity.\nfinalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;\n\n#ifdef SPECULARTERM\n vec3 finalSpecular = specularBase * specularColor;\n#else\n vec3 finalSpecular = vec3(0.0);\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\n finalSpecular = mix(finalSpecular, vec3(0.0), (1.0 - vOverloadedShadowIntensity.y));\n#endif\n\n#ifdef SPECULAROVERALPHA\n alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);\n#endif\n\n// Composition\n// Reflection already includes the environment intensity.\n#ifdef EMISSIVEASILLUMINATION\n vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor + emissiveColor * vLightingIntensity.y, alpha);\n#else\n vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor, alpha);\n#endif\n\n#ifdef LIGHTMAP\n vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;\n\n #ifdef USELIGHTMAPASSHADOWMAP\n color.rgb *= lightmapColor;\n #else\n color.rgb += lightmapColor;\n #endif\n#endif\n\n#ifdef FOG\n float fog = CalcFogFactor();\n color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;\n#endif\n\n color = max(color, 0.0);\n\n#ifdef CAMERATONEMAP\n color.rgb = toneMaps(color.rgb);\n#endif\n\n color.rgb = toGammaSpace(color.rgb);\n\n#ifdef CAMERACONTRAST\n color = contrasts(color);\n#endif\n\n // Normal Display.\n // gl_FragColor = vec4(normalW * 0.5 + 0.5, 1.0);\n\n // Ambient reflection color.\n // gl_FragColor = vec4(ambientReflectionColor, 1.0);\n\n // Reflection color.\n // gl_FragColor = vec4(reflectionColor, 1.0);\n\n // Base color.\n // gl_FragColor = vec4(baseColor.rgb, 1.0);\n\n // Specular color.\n // gl_FragColor = vec4(specularColor.rgb, 1.0);\n\n // Glossiness color.\n // gl_FragColor = vec4(glossiness, glossiness, glossiness, 1.0);\n\n // Specular Map\n // gl_FragColor = vec4(specularMapColor.rgb, 1.0);\n\n //// Emissive Color\n //vec2 test = vEmissiveUV * 0.5 + 0.5;\n //gl_FragColor = vec4(test.x, test.y, 1.0, 1.0);\n\n gl_FragColor = color;\n}";
  923. BABYLON.Effect.ShadersStore['legacypbrVertexShader'] = "precision mediump float;\n\n// Attributes\nattribute vec3 position;\nattribute vec3 normal;\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\n#if NUM_BONE_INFLUENCERS > 0\nuniform mat4 mBones[BonesPerMesh];\n\nattribute vec4 matricesIndices;\nattribute vec4 matricesWeights;\n#if NUM_BONE_INFLUENCERS > 4\nattribute vec4 matricesIndicesExtra;\nattribute vec4 matricesWeightsExtra;\n#endif\n#endif\n\n// Uniforms\nuniform mat4 world;\nuniform mat4 view;\nuniform mat4 viewProjection;\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform mat4 diffuseMatrix;\nuniform vec2 vDiffuseInfos;\n#endif\n\n#ifdef AMBIENT\nvarying vec2 vAmbientUV;\nuniform mat4 ambientMatrix;\nuniform vec2 vAmbientInfos;\n#endif\n\n#ifdef OPACITY\nvarying vec2 vOpacityUV;\nuniform mat4 opacityMatrix;\nuniform vec2 vOpacityInfos;\n#endif\n\n#ifdef EMISSIVE\nvarying vec2 vEmissiveUV;\nuniform vec2 vEmissiveInfos;\nuniform mat4 emissiveMatrix;\n#endif\n\n#if defined(SPECULAR) && defined(SPECULARTERM)\nvarying vec2 vSpecularUV;\nuniform vec2 vSpecularInfos;\nuniform mat4 specularMatrix;\n#endif\n\n// Output\nvarying vec3 vPositionW;\nvarying vec3 vNormalW;\n\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#ifdef CLIPPLANE\nuniform vec4 vClipPlane;\nvarying float fClipDistance;\n#endif\n\nvoid main(void) {\n mat4 finalWorld = world;\n\n#if NUM_BONE_INFLUENCERS > 0\n mat4 influence;\n influence = mBones[int(matricesIndices[0])] * matricesWeights[0];\n\n#if NUM_BONE_INFLUENCERS > 1\n influence += mBones[int(matricesIndices[1])] * matricesWeights[1];\n#endif \n#if NUM_BONE_INFLUENCERS > 2\n influence += mBones[int(matricesIndices[2])] * matricesWeights[2];\n#endif\t\n#if NUM_BONE_INFLUENCERS > 3\n influence += mBones[int(matricesIndices[3])] * matricesWeights[3];\n#endif\t\n\n#if NUM_BONE_INFLUENCERS > 4\n influence += mBones[int(matricesIndicesExtra[0])] * matricesWeightsExtra[0];\n#endif\n#if NUM_BONE_INFLUENCERS > 5\n influence += mBones[int(matricesIndicesExtra[1])] * matricesWeightsExtra[1];\n#endif\t\n#if NUM_BONE_INFLUENCERS > 6\n influence += mBones[int(matricesIndicesExtra[2])] * matricesWeightsExtra[2];\n#endif\t\n#if NUM_BONE_INFLUENCERS > 7\n influence += mBones[int(matricesIndicesExtra[3])] * matricesWeightsExtra[3];\n#endif\t\n\n finalWorld = finalWorld * influence;\n#endif\n\n\tgl_Position = viewProjection * finalWorld * vec4(position, 1.0);\n\n\tvec4 worldPos = finalWorld * vec4(position, 1.0);\n\tvPositionW = vec3(worldPos);\n\tvNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\n\n\t// Texture coordinates\n#ifndef UV1\n\tvec2 uv = vec2(0., 0.);\n#endif\n#ifndef UV2\n\tvec2 uv2 = vec2(0., 0.);\n#endif\n\n#ifdef DIFFUSE\n\tif (vDiffuseInfos.x == 0.)\n\t{\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));\n\t}\n\telse\n\t{\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));\n\t}\n#endif\n\n#ifdef AMBIENT\n\tif (vAmbientInfos.x == 0.)\n\t{\n\t\tvAmbientUV = vec2(ambientMatrix * vec4(uv, 1.0, 0.0));\n\t}\n\telse\n\t{\n\t\tvAmbientUV = vec2(ambientMatrix * vec4(uv2, 1.0, 0.0));\n\t}\n#endif\n\n#ifdef OPACITY\n\tif (vOpacityInfos.x == 0.)\n\t{\n\t\tvOpacityUV = vec2(opacityMatrix * vec4(uv, 1.0, 0.0));\n\t}\n\telse\n\t{\n\t\tvOpacityUV = vec2(opacityMatrix * vec4(uv2, 1.0, 0.0));\n\t}\n#endif\n\n#ifdef EMISSIVE\n\tif (vEmissiveInfos.x == 0.)\n\t{\n\t\tvEmissiveUV = vec2(emissiveMatrix * vec4(uv, 1.0, 0.0));\n\t}\n\telse\n\t{\n\t\tvEmissiveUV = vec2(emissiveMatrix * vec4(uv2, 1.0, 0.0));\n\t}\n#endif\n\n#if defined(SPECULAR) && defined(SPECULARTERM)\n\tif (vSpecularInfos.x == 0.)\n\t{\n\t\tvSpecularUV = vec2(specularMatrix * vec4(uv, 1.0, 0.0));\n\t}\n\telse\n\t{\n\t\tvSpecularUV = vec2(specularMatrix * vec4(uv2, 1.0, 0.0));\n\t}\n#endif\n\n\t// Clip plane\n#ifdef CLIPPLANE\n\tfClipDistance = dot(worldPos, vClipPlane);\n#endif\n\n\t// Vertex color\n#ifdef VERTEXCOLOR\n\tvColor = color;\n#endif\n}";
  924. BABYLON.Effect.ShadersStore['legacypbrPixelShader'] = "precision mediump float;\n\n// Constants\n#define RECIPROCAL_PI2 0.15915494\n#define FRESNEL_MAXIMUM_ON_ROUGH 0.25\n\nuniform vec3 vEyePosition;\nuniform vec3 vAmbientColor;\nuniform vec4 vDiffuseColor;\nuniform vec3 vReflectionColor;\n\n// CUSTOM CONTROLS\nuniform vec4 vLightingIntensity;\nuniform vec4 vCameraInfos;\n\n#ifdef OVERLOADEDVALUES\nuniform vec4 vOverloadedIntensity;\nuniform vec3 vOverloadedAmbient;\nuniform vec3 vOverloadedDiffuse;\nuniform vec3 vOverloadedSpecular;\nuniform vec3 vOverloadedEmissive;\nuniform vec3 vOverloadedReflection;\nuniform vec3 vOverloadedGlossiness;\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\nuniform vec4 vOverloadedShadowIntensity;\n#endif\n\n// PBR CUSTOM CONSTANTS\nconst float kPi = 3.1415926535897932384626433832795;\n\n// PBR HELPER METHODS\nfloat Square(float value)\n{\n return value * value;\n}\n\nfloat getLuminance(vec3 color)\n{\n return clamp(dot(color, vec3(0.2126, 0.7152, 0.0722)), 0., 1.);\n}\n\nfloat convertRoughnessToAverageSlope(float roughness)\n{\n // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues\n const float kMinimumVariance = 0.0005;\n float alphaG = Square(roughness) + kMinimumVariance;\n return alphaG;\n}\n\n// From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007\nfloat smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)\n{\n float tanSquared = (1.0 - dot * dot) / (dot * dot);\n return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));\n}\n\nfloat smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)\n{\n return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);\n}\n\n// Trowbridge-Reitz (GGX)\n// Generalised Trowbridge-Reitz with gamma power=2.0\nfloat normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)\n{\n // Note: alphaG is average slope (gradient) of the normals in slope-space.\n // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have\n // a tangent (gradient) closer to the macrosurface than this slope.\n float a2 = Square(alphaG);\n float d = NdotH * NdotH * (a2 - 1.0) + 1.0;\n return a2 / (kPi * d * d);\n}\n\nvec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)\n{\n return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0);\n}\n\nvec3 FresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)\n{\n // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle\n float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);\n return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);\n}\n\n// Cook Torance Specular computation.\nvec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor)\n{\n float alphaG = convertRoughnessToAverageSlope(roughness);\n float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);\n float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG);\n visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes.\n\n vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.));\n\n float specTerm = max(0., visibility * distribution) * NdotL;\n return fresnel * specTerm;\n}\n\nfloat computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness)\n{\n // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of\n // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.\n float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);\n float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);\n float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;\n float diffuseFresnelTerm =\n (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *\n (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);\n\n return diffuseFresnelTerm * NdotL;\n}\n\nfloat computeDefaultGlossiness(float glossiness, vec3 specularColor)\n{\n if (glossiness == 0.)\n {\n float kSpecularNoAlphaWorkflow_SmoothnessMax = 0.95;\n\n float specularLuminance = getLuminance(specularColor);\n float specularLuma = sqrt(specularLuminance);\n glossiness = specularLuma * kSpecularNoAlphaWorkflow_SmoothnessMax;\n }\n return glossiness;\n}\n\nvec3 toLinearSpace(vec3 color)\n{\n return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));\n}\n\nvec3 toGammaSpace(vec3 color)\n{\n return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));\n}\n\n#ifdef CAMERATONEMAP\n vec3 toneMaps(vec3 color)\n {\n color = max(color, 0.0);\n\n // TONE MAPPING / EXPOSURE\n color.rgb = color.rgb * vCameraInfos.x;\n\n float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately\n vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper\n color.rgb = mix(color.rgb, tonemapped, 1.0);\n return color;\n }\n#endif\n\n#ifdef CAMERACONTRAST\n vec4 contrasts(vec4 color)\n {\n color = clamp(color, 0.0, 1.0);\n\n vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);\n float contrast = vCameraInfos.y;\n if (contrast < 1.0)\n {\n // Decrease contrast: interpolate towards zero-contrast image (flat grey)\n color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);\n }\n else\n {\n // Increase contrast: apply simple shoulder-toe high contrast curve\n color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);\n }\n\n return color;\n }\n#endif\n// END PBR HELPER METHODS\n\n#ifdef SPECULARTERM\nuniform vec4 vSpecularColor;\n#endif\nuniform vec3 vEmissiveColor;\n\n// Input\nvarying vec3 vPositionW;\n\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n// Lights\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\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\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\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// Samplers\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n\n#ifdef AMBIENT\nvarying vec2 vAmbientUV;\nuniform sampler2D ambientSampler;\nuniform vec2 vAmbientInfos;\n#endif\n\n#ifdef OPACITY\t\nvarying vec2 vOpacityUV;\nuniform sampler2D opacitySampler;\nuniform vec2 vOpacityInfos;\n#endif\n\n#ifdef EMISSIVE\nvarying vec2 vEmissiveUV;\nuniform vec2 vEmissiveInfos;\nuniform sampler2D emissiveSampler;\n#endif\n\n#ifdef LIGHTMAP\nvarying vec2 vLightmapUV;\nuniform vec2 vLightmapInfos;\nuniform sampler2D lightmapSampler;\n#endif\n\n#if defined(SPECULAR) && defined(SPECULARTERM)\nvarying vec2 vSpecularUV;\nuniform vec2 vSpecularInfos;\nuniform sampler2D specularSampler;\n#endif\n\n#ifdef CLIPPLANE\nvarying float fClipDistance;\n#endif\n\n// Light Computing\nstruct lightingInfo\n{\n vec3 diffuse;\n#ifdef SPECULARTERM\n vec3 specular;\n#endif\n};\n\nlightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 lightVectorW;\n float attenuation = 1.0;\n if (lightData.w == 0.)\n {\n vec3 direction = lightData.xyz - vPositionW;\n\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 // diffuse\n vec3 H = normalize(viewDirectionW + lightVectorW);\n float NdotL = max(0.00000000001, dot(vNormal, lightVectorW));\n float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));\n\n float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);\n result.diffuse = diffuseTerm * diffuseColor * attenuation;\n\n#ifdef SPECULARTERM\n // Specular\n float NdotH = max(0.00000000001, dot(vNormal, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * specularColor * attenuation;\n#endif\n\n return result;\n}\n\nlightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 direction = lightData.xyz - vPositionW;\n vec3 lightVectorW = normalize(direction);\n float attenuation = max(0., 1.0 - length(direction) / range);\n\n // diffuse\n float cosAngle = max(0.0000001, dot(-lightDirection.xyz, lightVectorW));\n float spotAtten = 0.0;\n\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 // Diffuse\n vec3 H = normalize(viewDirectionW - lightDirection.xyz);\n float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));\n float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);\n\n float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);\n result.diffuse = diffuseTerm * diffuseColor * attenuation * spotAtten;\n\n#ifdef SPECULARTERM\n // Specular\n float NdotH = max(0.00000000001, dot(vNormal, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * specularColor * attenuation * spotAtten;\n#endif\n\n return result;\n }\n\n result.diffuse = vec3(0.);\n#ifdef SPECULARTERM\n result.specular = vec3(0.);\n#endif\n\n return result;\n}\n\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 lightVectorW = normalize(lightData.xyz);\n\n // Diffuse\n float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;\n result.diffuse = mix(groundColor, diffuseColor, ndl);\n\n#ifdef SPECULARTERM\n // Specular\n vec3 H = normalize(viewDirectionW + lightVectorW);\n float NdotH = max(0.00000000001, dot(vNormal, H));\n float NdotL = max(0.00000000001, ndl);\n float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * specularColor;\n#endif\n\n return result;\n}\n\nvoid main(void) {\n // Clip plane\n#ifdef CLIPPLANE\n if (fClipDistance > 0.0)\n discard;\n#endif\n\n vec3 viewDirectionW = normalize(vEyePosition - vPositionW);\n\n // Base color\n vec4 baseColor = vec4(1., 1., 1., 1.);\n vec3 diffuseColor = vDiffuseColor.rgb;\n \n // Alpha\n float alpha = vDiffuseColor.a;\n\n#ifdef DIFFUSE\n baseColor = texture2D(diffuseSampler, vDiffuseUV);\n baseColor = vec4(toLinearSpace(baseColor.rgb), baseColor.a);\n\n#ifdef ALPHATEST\n if (baseColor.a < 0.4)\n discard;\n#endif\n\n#ifdef ALPHAFROMDIFFUSE\n alpha *= baseColor.a;\n#endif\n\n baseColor.rgb *= vDiffuseInfos.y;\n#endif\n\n#ifdef VERTEXCOLOR\n baseColor.rgb *= vColor.rgb;\n#endif\n\n#ifdef OVERLOADEDVALUES\n baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);\n diffuseColor.rgb = mix(diffuseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);\n#endif\n\n // Bump\n#ifdef NORMAL\n vec3 normalW = normalize(vNormalW);\n#else\n vec3 normalW = vec3(1.0, 1.0, 1.0);\n#endif\n\n // Ambient color\n vec3 baseAmbientColor = vec3(1., 1., 1.);\n\n#ifdef AMBIENT\n baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;\n #ifdef OVERLOADEDVALUES\n baseAmbientColor.rgb = mix(baseAmbientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);\n #endif\n#endif\n\n // Specular map\n#ifdef SPECULARTERM\n float glossiness = vSpecularColor.a;\n vec3 specularColor = vSpecularColor.rgb;\n\n #ifdef OVERLOADEDVALUES\n specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);\n #endif\n\n #ifdef SPECULAR\n vec4 specularMapColor = texture2D(specularSampler, vSpecularUV);\n specularColor = toLinearSpace(specularMapColor.rgb);\n\n #ifdef OVERLOADEDVALUES\n specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);\n #endif\n\n #ifdef GLOSSINESSFROMSPECULARMAP\n glossiness = specularMapColor.a;\n #else\n glossiness = computeDefaultGlossiness(glossiness, specularColor);\n #endif\n #endif\n\n #ifdef OVERLOADEDVALUES\n glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);\n #endif\n#else\n float glossiness = 0.;\n #ifdef OVERLOADEDVALUES\n glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.y);\n #endif\n\n vec3 specularColor = vec3(0., 0., 0);\n #ifdef OVERLOADEDVALUES\n specularColor.rgb = mix(specularColor.rgb, vOverloadedSpecular, vOverloadedIntensity.z);\n #endif\n#endif\n\n // Apply Energy Conservation taking in account the environment level only if the environment is present.\n float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);\n baseColor.rgb = (1. - reflectance) * baseColor.rgb;\n\n // Compute Specular Fresnel + Reflectance.\n float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));\n\n // Adapt glossiness.\n glossiness = clamp(glossiness, 0., 1.) * 0.98;\n\n // Call rough to not conflict with previous one.\n float rough = clamp(1. - glossiness, 0.000001, 1.0);\n\n // Lighting\n vec3 diffuseBase = vec3(0., 0., 0.);\n\n#ifdef OVERLOADEDSHADOWVALUES\n vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);\n#endif\n\n#ifdef SPECULARTERM\n vec3 specularBase = vec3(0., 0., 0.);\n#endif\n float shadow = 1.;\n\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, rough, NdotV);\n#endif\n#ifdef HEMILIGHT0\n lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, rough, NdotV);\n#endif\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\n lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);\n#endif\n\n shadow = 1.;\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\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, rough, NdotV);\n#endif\n#ifdef HEMILIGHT1\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, rough, NdotV);\n#endif\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\n info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);\n#endif\n\n shadow = 1.;\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\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, rough, NdotV);\n#endif\n#ifdef HEMILIGHT2\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, rough, NdotV);\n#endif\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\n info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);\n#endif\n\n shadow = 1.;\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\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, rough, NdotV);\n#endif\n#ifdef HEMILIGHT3\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, rough, NdotV);\n#endif\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\n info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);\n#endif\n\n shadow = 1.;\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\n// Reflection\nvec3 reflectionColor = vReflectionColor.rgb;\nvec3 ambientReflectionColor = vReflectionColor.rgb;\n\nreflectionColor *= vLightingIntensity.z;\nambientReflectionColor *= vLightingIntensity.z;\n\n// Compute reflection specular fresnel\nvec3 specularEnvironmentR0 = specularColor.rgb;\nvec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);\nvec3 specularEnvironmentReflectanceViewer = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(glossiness));\nreflectionColor *= specularEnvironmentReflectanceViewer;\n\n#ifdef OVERLOADEDVALUES\n ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);\n reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);\n#endif\n\n#ifdef OPACITY\n vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);\n\n#ifdef OPACITYRGB\n opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);\n alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;\n#else\n alpha *= opacityMap.a * vOpacityInfos.y;\n#endif\n\n#endif\n\n#ifdef VERTEXALPHA\n alpha *= vColor.a;\n#endif\n\n // Emissive\n vec3 emissiveColor = vEmissiveColor;\n#ifdef EMISSIVE\n vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;\n emissiveColor = toLinearSpace(emissiveColorTex.rgb) * emissiveColor * vEmissiveInfos.y;\n#endif\n\n#ifdef OVERLOADEDVALUES\n emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);\n#endif\n\n // Composition\n#ifdef EMISSIVEASILLUMINATION\n vec3 finalDiffuse = max(diffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\n\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n#else\n #ifdef LINKEMISSIVEWITHDIFFUSE\n vec3 finalDiffuse = max((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n #else\n vec3 finalDiffuse = max(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n #endif\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\n finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));\n#endif\n\n// diffuse lighting from environment 0.2 replaces Harmonic...\n// Ambient Reflection already includes the environment intensity.\nfinalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;\n\n#ifdef SPECULARTERM\n vec3 finalSpecular = specularBase * specularColor;\n#else\n vec3 finalSpecular = vec3(0.0);\n#endif\n\n#ifdef SPECULAROVERALPHA\n alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);\n#endif\n\n// Composition\n// Reflection already includes the environment intensity.\n#ifdef EMISSIVEASILLUMINATION\n vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor + emissiveColor * vLightingIntensity.y, alpha);\n#else\n vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor, alpha);\n#endif\n\n color = max(color, 0.0);\n\n#ifdef CAMERATONEMAP\n color.rgb = toneMaps(color.rgb);\n#endif\n\n color.rgb = toGammaSpace(color.rgb);\n\n#ifdef CAMERACONTRAST\n color = contrasts(color);\n#endif\n\n gl_FragColor = color;\n}";