babylon.pbrMaterial.js 169 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  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.ALBEDO = false;
  10. this.AMBIENT = false;
  11. this.OPACITY = false;
  12. this.OPACITYRGB = false;
  13. this.REFLECTION = false;
  14. this.EMISSIVE = false;
  15. this.REFLECTIVITY = false;
  16. this.BUMP = false;
  17. this.SPECULAROVERALPHA = false;
  18. this.CLIPPLANE = false;
  19. this.ALPHATEST = false;
  20. this.ALPHAFROMALBEDO = 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.MICROSURFACEFROMREFLECTIVITYMAP = false;
  69. this.EMISSIVEASILLUMINATION = false;
  70. this.LINKEMISSIVEWITHALBEDO = 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.USESPHERICALFROMREFLECTIONMAP = false;
  88. this._keys = Object.keys(this);
  89. }
  90. return PBRMaterialDefines;
  91. })(BABYLON.MaterialDefines);
  92. var PBRMaterial = (function (_super) {
  93. __extends(PBRMaterial, _super);
  94. function PBRMaterial(name, scene) {
  95. var _this = this;
  96. _super.call(this, name, scene);
  97. this.directIntensity = 1.0;
  98. this.emissiveIntensity = 1.0;
  99. this.environmentIntensity = 1.0;
  100. this.specularIntensity = 1.0;
  101. this._lightingInfos = new BABYLON.Vector4(this.directIntensity, this.emissiveIntensity, this.environmentIntensity, this.specularIntensity);
  102. this.overloadedShadowIntensity = 1.0;
  103. this.overloadedShadeIntensity = 1.0;
  104. this._overloadedShadowInfos = new BABYLON.Vector4(this.overloadedShadowIntensity, this.overloadedShadeIntensity, 0.0, 0.0);
  105. this.cameraExposure = 1.0;
  106. this.cameraContrast = 1.0;
  107. this._cameraInfos = new BABYLON.Vector4(1.0, 1.0, 0.0, 0.0);
  108. this.overloadedAmbientIntensity = 0.0;
  109. this.overloadedAlbedoIntensity = 0.0;
  110. this.overloadedReflectivityIntensity = 0.0;
  111. this.overloadedEmissiveIntensity = 0.0;
  112. this._overloadedIntensity = new BABYLON.Vector4(this.overloadedAmbientIntensity, this.overloadedAlbedoIntensity, this.overloadedReflectivityIntensity, this.overloadedEmissiveIntensity);
  113. this.overloadedAmbient = BABYLON.Color3.White();
  114. this.overloadedAlbedo = BABYLON.Color3.White();
  115. this.overloadedReflectivity = BABYLON.Color3.White();
  116. this.overloadedEmissive = BABYLON.Color3.White();
  117. this.overloadedReflection = BABYLON.Color3.White();
  118. this.overloadedMicroSurface = 0.0;
  119. this.overloadedMicroSurfaceIntensity = 0.0;
  120. this.overloadedReflectionIntensity = 0.0;
  121. this._overloadedMicroSurface = new BABYLON.Vector3(this.overloadedMicroSurface, this.overloadedMicroSurfaceIntensity, this.overloadedReflectionIntensity);
  122. this.disableBumpMap = false;
  123. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  124. this.albedoColor = new BABYLON.Color3(1, 1, 1);
  125. this.reflectivityColor = new BABYLON.Color3(1, 1, 1);
  126. this.reflectionColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  127. this.microSurface = 0.5;
  128. this.emissiveColor = new BABYLON.Color3(0, 0, 0);
  129. this.useAlphaFromAlbedoTexture = false;
  130. this.useEmissiveAsIllumination = false;
  131. this.linkEmissiveWithAlbedo = false;
  132. this.useSpecularOverAlpha = true;
  133. this.disableLighting = false;
  134. this.useLightmapAsShadowmap = false;
  135. this.useMicroSurfaceFromReflectivityMapAlpha = false;
  136. this._renderTargets = new BABYLON.SmartArray(16);
  137. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  138. this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  139. this._tempColor = new BABYLON.Color3();
  140. this._defines = new PBRMaterialDefines();
  141. this._cachedDefines = new PBRMaterialDefines();
  142. this._myScene = null;
  143. this._myShadowGenerator = null;
  144. this._cachedDefines.BonesPerMesh = -1;
  145. this.getRenderTargetTextures = function () {
  146. _this._renderTargets.reset();
  147. if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
  148. _this._renderTargets.push(_this.reflectionTexture);
  149. }
  150. return _this._renderTargets;
  151. };
  152. }
  153. Object.defineProperty(PBRMaterial.prototype, "useLogarithmicDepth", {
  154. get: function () {
  155. return this._useLogarithmicDepth;
  156. },
  157. set: function (value) {
  158. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  159. },
  160. enumerable: true,
  161. configurable: true
  162. });
  163. PBRMaterial.prototype.needAlphaBlending = function () {
  164. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromAlbedoTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  165. };
  166. PBRMaterial.prototype.needAlphaTesting = function () {
  167. return this.albedoTexture != null && this.albedoTexture.hasAlpha;
  168. };
  169. PBRMaterial.prototype._shouldUseAlphaFromAlbedoTexture = function () {
  170. return this.albedoTexture != null && this.albedoTexture.hasAlpha && this.useAlphaFromAlbedoTexture;
  171. };
  172. PBRMaterial.prototype.getAlphaTestTexture = function () {
  173. return this.albedoTexture;
  174. };
  175. PBRMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  176. if (!mesh) {
  177. return true;
  178. }
  179. if (this._defines.INSTANCES !== useInstances) {
  180. return false;
  181. }
  182. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  183. return true;
  184. }
  185. return false;
  186. };
  187. PBRMaterial.PrepareDefinesForLights = function (scene, mesh, defines) {
  188. var lightIndex = 0;
  189. var needNormals = false;
  190. for (var index = 0; index < scene.lights.length; index++) {
  191. var light = scene.lights[index];
  192. if (!light.isEnabled()) {
  193. continue;
  194. }
  195. // Excluded check
  196. if (light._excludedMeshesIds.length > 0) {
  197. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  198. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  199. if (excludedMesh) {
  200. light.excludedMeshes.push(excludedMesh);
  201. }
  202. }
  203. light._excludedMeshesIds = [];
  204. }
  205. // Included check
  206. if (light._includedOnlyMeshesIds.length > 0) {
  207. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  208. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  209. if (includedOnlyMesh) {
  210. light.includedOnlyMeshes.push(includedOnlyMesh);
  211. }
  212. }
  213. light._includedOnlyMeshesIds = [];
  214. }
  215. if (!light.canAffectMesh(mesh)) {
  216. continue;
  217. }
  218. needNormals = true;
  219. defines["LIGHT" + lightIndex] = true;
  220. var type;
  221. if (light instanceof BABYLON.SpotLight) {
  222. type = "SPOTLIGHT" + lightIndex;
  223. }
  224. else if (light instanceof BABYLON.HemisphericLight) {
  225. type = "HEMILIGHT" + lightIndex;
  226. }
  227. else if (light instanceof BABYLON.PointLight) {
  228. type = "POINTLIGHT" + lightIndex;
  229. }
  230. else {
  231. type = "DIRLIGHT" + lightIndex;
  232. }
  233. defines[type] = true;
  234. // Specular
  235. if (!light.specular.equalsFloats(0, 0, 0)) {
  236. defines["SPECULARTERM"] = true;
  237. }
  238. // Shadows
  239. if (scene.shadowsEnabled) {
  240. var shadowGenerator = light.getShadowGenerator();
  241. if (mesh && mesh.receiveShadows && shadowGenerator) {
  242. defines["SHADOW" + lightIndex] = true;
  243. defines["SHADOWS"] = true;
  244. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  245. defines["SHADOWVSM" + lightIndex] = true;
  246. }
  247. if (shadowGenerator.usePoissonSampling) {
  248. defines["SHADOWPCF" + lightIndex] = true;
  249. }
  250. }
  251. }
  252. lightIndex++;
  253. if (lightIndex === maxSimultaneousLights)
  254. break;
  255. }
  256. return needNormals;
  257. };
  258. PBRMaterial.BindLights = function (scene, mesh, effect, defines) {
  259. var lightIndex = 0;
  260. var depthValuesAlreadySet = false;
  261. for (var index = 0; index < scene.lights.length; index++) {
  262. var light = scene.lights[index];
  263. if (!light.isEnabled()) {
  264. continue;
  265. }
  266. if (!light.canAffectMesh(mesh)) {
  267. continue;
  268. }
  269. if (light instanceof BABYLON.PointLight) {
  270. // Point Light
  271. light.transferToEffect(effect, "vLightData" + lightIndex);
  272. }
  273. else if (light instanceof BABYLON.DirectionalLight) {
  274. // Directional Light
  275. light.transferToEffect(effect, "vLightData" + lightIndex);
  276. }
  277. else if (light instanceof BABYLON.SpotLight) {
  278. // Spot Light
  279. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  280. }
  281. else if (light instanceof BABYLON.HemisphericLight) {
  282. // Hemispheric Light
  283. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  284. }
  285. // GAMMA CORRECTION.
  286. light.diffuse.toLinearSpaceToRef(PBRMaterial._scaledAlbedo);
  287. PBRMaterial._scaledAlbedo.scaleToRef(light.intensity, PBRMaterial._scaledAlbedo);
  288. light.diffuse.scaleToRef(light.intensity, PBRMaterial._scaledAlbedo);
  289. effect.setColor4("vLightDiffuse" + lightIndex, PBRMaterial._scaledAlbedo, light.range);
  290. if (defines["SPECULARTERM"]) {
  291. light.specular.toLinearSpaceToRef(PBRMaterial._scaledReflectivity);
  292. PBRMaterial._scaledReflectivity.scaleToRef(light.intensity, PBRMaterial._scaledReflectivity);
  293. effect.setColor3("vLightSpecular" + lightIndex, PBRMaterial._scaledReflectivity);
  294. }
  295. // Shadows
  296. if (scene.shadowsEnabled) {
  297. var shadowGenerator = light.getShadowGenerator();
  298. if (mesh.receiveShadows && shadowGenerator) {
  299. if (!light.needCube()) {
  300. effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  301. }
  302. else {
  303. if (!depthValuesAlreadySet) {
  304. depthValuesAlreadySet = true;
  305. effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
  306. }
  307. }
  308. effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  309. effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  310. }
  311. }
  312. lightIndex++;
  313. if (lightIndex === maxSimultaneousLights)
  314. break;
  315. }
  316. };
  317. PBRMaterial.prototype.isReady = function (mesh, useInstances) {
  318. if (this.checkReadyOnlyOnce) {
  319. if (this._wasPreviouslyReady) {
  320. return true;
  321. }
  322. }
  323. var scene = this.getScene();
  324. if (!this.checkReadyOnEveryCall) {
  325. if (this._renderId === scene.getRenderId()) {
  326. if (this._checkCache(scene, mesh, useInstances)) {
  327. return true;
  328. }
  329. }
  330. }
  331. var engine = scene.getEngine();
  332. var needNormals = false;
  333. var needUVs = false;
  334. this._defines.reset();
  335. // Textures
  336. if (scene.texturesEnabled) {
  337. if (this.albedoTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  338. if (!this.albedoTexture.isReady()) {
  339. return false;
  340. }
  341. else {
  342. needUVs = true;
  343. this._defines.ALBEDO = true;
  344. }
  345. }
  346. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  347. if (!this.ambientTexture.isReady()) {
  348. return false;
  349. }
  350. else {
  351. needUVs = true;
  352. this._defines.AMBIENT = true;
  353. }
  354. }
  355. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  356. if (!this.opacityTexture.isReady()) {
  357. return false;
  358. }
  359. else {
  360. needUVs = true;
  361. this._defines.OPACITY = true;
  362. if (this.opacityTexture.getAlphaFromRGB) {
  363. this._defines.OPACITYRGB = true;
  364. }
  365. }
  366. }
  367. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  368. if (!this.reflectionTexture.isReady()) {
  369. return false;
  370. }
  371. else {
  372. needNormals = true;
  373. this._defines.REFLECTION = true;
  374. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  375. this._defines.INVERTCUBICMAP = true;
  376. }
  377. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  378. switch (this.reflectionTexture.coordinatesMode) {
  379. case BABYLON.Texture.CUBIC_MODE:
  380. case BABYLON.Texture.INVCUBIC_MODE:
  381. this._defines.REFLECTIONMAP_CUBIC = true;
  382. break;
  383. case BABYLON.Texture.EXPLICIT_MODE:
  384. this._defines.REFLECTIONMAP_EXPLICIT = true;
  385. break;
  386. case BABYLON.Texture.PLANAR_MODE:
  387. this._defines.REFLECTIONMAP_PLANAR = true;
  388. break;
  389. case BABYLON.Texture.PROJECTION_MODE:
  390. this._defines.REFLECTIONMAP_PROJECTION = true;
  391. break;
  392. case BABYLON.Texture.SKYBOX_MODE:
  393. this._defines.REFLECTIONMAP_SKYBOX = true;
  394. break;
  395. case BABYLON.Texture.SPHERICAL_MODE:
  396. this._defines.REFLECTIONMAP_SPHERICAL = true;
  397. break;
  398. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  399. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  400. break;
  401. }
  402. if (this.reflectionTexture instanceof BABYLON.HDRCubeTexture) {
  403. this._defines.USESPHERICALFROMREFLECTIONMAP = true;
  404. needNormals = true;
  405. }
  406. }
  407. }
  408. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  409. if (!this.lightmapTexture.isReady()) {
  410. return false;
  411. }
  412. else {
  413. needUVs = true;
  414. this._defines.LIGHTMAP = true;
  415. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  416. }
  417. }
  418. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  419. if (!this.emissiveTexture.isReady()) {
  420. return false;
  421. }
  422. else {
  423. needUVs = true;
  424. this._defines.EMISSIVE = true;
  425. }
  426. }
  427. if (this.reflectivityTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  428. if (!this.reflectivityTexture.isReady()) {
  429. return false;
  430. }
  431. else {
  432. needUVs = true;
  433. this._defines.REFLECTIVITY = true;
  434. this._defines.MICROSURFACEFROMREFLECTIVITYMAP = this.useMicroSurfaceFromReflectivityMapAlpha;
  435. }
  436. }
  437. }
  438. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  439. if (!this.bumpTexture.isReady()) {
  440. return false;
  441. }
  442. else {
  443. needUVs = true;
  444. this._defines.BUMP = true;
  445. }
  446. }
  447. // Effect
  448. if (scene.clipPlane) {
  449. this._defines.CLIPPLANE = true;
  450. }
  451. if (engine.getAlphaTesting()) {
  452. this._defines.ALPHATEST = true;
  453. }
  454. if (this._shouldUseAlphaFromAlbedoTexture()) {
  455. this._defines.ALPHAFROMALBEDO = true;
  456. }
  457. if (this.useEmissiveAsIllumination) {
  458. this._defines.EMISSIVEASILLUMINATION = true;
  459. }
  460. if (this.linkEmissiveWithAlbedo) {
  461. this._defines.LINKEMISSIVEWITHALBEDO = true;
  462. }
  463. if (this.useLogarithmicDepth) {
  464. this._defines.LOGARITHMICDEPTH = true;
  465. }
  466. if (this.cameraContrast != 1) {
  467. this._defines.CAMERACONTRAST = true;
  468. }
  469. if (this.cameraExposure != 1) {
  470. this._defines.CAMERATONEMAP = true;
  471. }
  472. if (this.overloadedShadeIntensity != 1 ||
  473. this.overloadedShadowIntensity != 1) {
  474. this._defines.OVERLOADEDSHADOWVALUES = true;
  475. }
  476. if (this.overloadedMicroSurfaceIntensity > 0 ||
  477. this.overloadedEmissiveIntensity > 0 ||
  478. this.overloadedReflectivityIntensity > 0 ||
  479. this.overloadedAlbedoIntensity > 0 ||
  480. this.overloadedAmbientIntensity > 0 ||
  481. this.overloadedReflectionIntensity > 0) {
  482. this._defines.OVERLOADEDVALUES = true;
  483. }
  484. // Point size
  485. if (this.pointsCloud || scene.forcePointsCloud) {
  486. this._defines.POINTSIZE = true;
  487. }
  488. // Fog
  489. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  490. this._defines.FOG = true;
  491. }
  492. if (scene.lightsEnabled && !this.disableLighting) {
  493. needNormals = PBRMaterial.PrepareDefinesForLights(scene, mesh, this._defines) || needNormals;
  494. }
  495. if (BABYLON.StandardMaterial.FresnelEnabled) {
  496. // Fresnel
  497. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  498. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  499. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  500. this._defines.OPACITYFRESNEL = true;
  501. }
  502. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  503. this._defines.EMISSIVEFRESNEL = true;
  504. }
  505. needNormals = true;
  506. this._defines.FRESNEL = true;
  507. }
  508. }
  509. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  510. this._defines.SPECULAROVERALPHA = true;
  511. }
  512. // Attribs
  513. if (mesh) {
  514. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  515. this._defines.NORMAL = true;
  516. }
  517. if (needUVs) {
  518. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  519. this._defines.UV1 = true;
  520. }
  521. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  522. this._defines.UV2 = true;
  523. }
  524. }
  525. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  526. this._defines.VERTEXCOLOR = true;
  527. if (mesh.hasVertexAlpha) {
  528. this._defines.VERTEXALPHA = true;
  529. }
  530. }
  531. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  532. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  533. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  534. }
  535. // Instances
  536. if (useInstances) {
  537. this._defines.INSTANCES = true;
  538. }
  539. }
  540. // Get correct effect
  541. if (!this._defines.isEqual(this._cachedDefines)) {
  542. this._defines.cloneTo(this._cachedDefines);
  543. scene.resetCachedMaterial();
  544. // Fallbacks
  545. var fallbacks = new BABYLON.EffectFallbacks();
  546. if (this._defines.REFLECTION) {
  547. fallbacks.addFallback(0, "REFLECTION");
  548. }
  549. if (this._defines.REFLECTIVITY) {
  550. fallbacks.addFallback(0, "REFLECTIVITY");
  551. }
  552. if (this._defines.BUMP) {
  553. fallbacks.addFallback(0, "BUMP");
  554. }
  555. if (this._defines.SPECULAROVERALPHA) {
  556. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  557. }
  558. if (this._defines.FOG) {
  559. fallbacks.addFallback(1, "FOG");
  560. }
  561. if (this._defines.POINTSIZE) {
  562. fallbacks.addFallback(0, "POINTSIZE");
  563. }
  564. if (this._defines.LOGARITHMICDEPTH) {
  565. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  566. }
  567. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  568. if (!this._defines["LIGHT" + lightIndex]) {
  569. continue;
  570. }
  571. if (lightIndex > 0) {
  572. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  573. }
  574. if (this._defines["SHADOW" + lightIndex]) {
  575. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  576. }
  577. if (this._defines["SHADOWPCF" + lightIndex]) {
  578. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  579. }
  580. if (this._defines["SHADOWVSM" + lightIndex]) {
  581. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  582. }
  583. }
  584. if (this._defines.SPECULARTERM) {
  585. fallbacks.addFallback(0, "SPECULARTERM");
  586. }
  587. if (this._defines.OPACITYFRESNEL) {
  588. fallbacks.addFallback(1, "OPACITYFRESNEL");
  589. }
  590. if (this._defines.EMISSIVEFRESNEL) {
  591. fallbacks.addFallback(2, "EMISSIVEFRESNEL");
  592. }
  593. if (this._defines.FRESNEL) {
  594. fallbacks.addFallback(3, "FRESNEL");
  595. }
  596. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  597. fallbacks.addCPUSkinningFallback(0, mesh);
  598. }
  599. //Attributes
  600. var attribs = [BABYLON.VertexBuffer.PositionKind];
  601. if (this._defines.NORMAL) {
  602. attribs.push(BABYLON.VertexBuffer.NormalKind);
  603. }
  604. if (this._defines.UV1) {
  605. attribs.push(BABYLON.VertexBuffer.UVKind);
  606. }
  607. if (this._defines.UV2) {
  608. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  609. }
  610. if (this._defines.VERTEXCOLOR) {
  611. attribs.push(BABYLON.VertexBuffer.ColorKind);
  612. }
  613. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  614. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  615. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  616. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  617. attribs.push(BABYLON.VertexBuffer.MatricesIndicesExtraKind);
  618. attribs.push(BABYLON.VertexBuffer.MatricesWeightsExtraKind);
  619. }
  620. }
  621. if (this._defines.INSTANCES) {
  622. attribs.push("world0");
  623. attribs.push("world1");
  624. attribs.push("world2");
  625. attribs.push("world3");
  626. }
  627. // Legacy browser patch
  628. var shaderName = "pbr";
  629. if (!scene.getEngine().getCaps().standardDerivatives) {
  630. shaderName = "legacypbr";
  631. }
  632. var join = this._defines.toString();
  633. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vAlbedoColor", "vReflectivityColor", "vEmissiveColor", "vReflectionColor",
  634. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  635. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  636. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  637. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  638. "vFogInfos", "vFogColor", "pointSize",
  639. "vAlbedoInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vReflectivityInfos", "vBumpInfos", "vLightmapInfos",
  640. "mBones",
  641. "vClipPlane", "albedoMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "reflectivityMatrix", "bumpMatrix", "lightmapMatrix",
  642. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues",
  643. "opacityParts", "emissiveLeftColor", "emissiveRightColor",
  644. "vLightingIntensity", "vOverloadedShadowIntensity", "vOverloadedIntensity", "vCameraInfos", "vOverloadedAlbedo", "vOverloadedReflection", "vOverloadedReflectivity", "vOverloadedEmissive", "vOverloadedMicroSurface",
  645. "logarithmicDepthConstant",
  646. "vSphericalX", "vSphericalY", "vSphericalZ",
  647. "vSphericalXX", "vSphericalYY", "vSphericalZZ",
  648. "vSphericalXY", "vSphericalYZ", "vSphericalZX"
  649. ], ["albedoSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "reflectivitySampler", "bumpSampler", "lightmapSampler",
  650. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  651. ], join, fallbacks, this.onCompiled, this.onError);
  652. }
  653. if (!this._effect.isReady()) {
  654. return false;
  655. }
  656. this._renderId = scene.getRenderId();
  657. this._wasPreviouslyReady = true;
  658. if (mesh) {
  659. if (!mesh._materialDefines) {
  660. mesh._materialDefines = new PBRMaterialDefines();
  661. }
  662. this._defines.cloneTo(mesh._materialDefines);
  663. }
  664. return true;
  665. };
  666. PBRMaterial.prototype.unbind = function () {
  667. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  668. this._effect.setTexture("reflection2DSampler", null);
  669. }
  670. _super.prototype.unbind.call(this);
  671. };
  672. PBRMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  673. this._effect.setMatrix("world", world);
  674. };
  675. PBRMaterial.prototype.bind = function (world, mesh) {
  676. this._myScene = this.getScene();
  677. // Matrices
  678. this.bindOnlyWorldMatrix(world);
  679. this._effect.setMatrix("viewProjection", this._myScene.getTransformMatrix());
  680. // Bones
  681. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  682. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  683. }
  684. if (this._myScene.getCachedMaterial() !== this) {
  685. if (BABYLON.StandardMaterial.FresnelEnabled) {
  686. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  687. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  688. }
  689. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  690. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  691. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  692. }
  693. }
  694. // Textures
  695. if (this.albedoTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  696. this._effect.setTexture("albedoSampler", this.albedoTexture);
  697. this._effect.setFloat2("vAlbedoInfos", this.albedoTexture.coordinatesIndex, this.albedoTexture.level);
  698. this._effect.setMatrix("albedoMatrix", this.albedoTexture.getTextureMatrix());
  699. }
  700. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  701. this._effect.setTexture("ambientSampler", this.ambientTexture);
  702. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  703. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  704. }
  705. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  706. this._effect.setTexture("opacitySampler", this.opacityTexture);
  707. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  708. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  709. }
  710. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  711. if (this.reflectionTexture.isCube) {
  712. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  713. }
  714. else {
  715. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  716. }
  717. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  718. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, 0);
  719. if (this._defines.USESPHERICALFROMREFLECTIONMAP) {
  720. this._effect.setFloat3("vSphericalX", this.reflectionTexture.sphericalPolynomial.x.x, this.reflectionTexture.sphericalPolynomial.x.y, this.reflectionTexture.sphericalPolynomial.x.z);
  721. this._effect.setFloat3("vSphericalY", this.reflectionTexture.sphericalPolynomial.y.x, this.reflectionTexture.sphericalPolynomial.y.y, this.reflectionTexture.sphericalPolynomial.y.z);
  722. this._effect.setFloat3("vSphericalZ", this.reflectionTexture.sphericalPolynomial.z.x, this.reflectionTexture.sphericalPolynomial.z.y, this.reflectionTexture.sphericalPolynomial.z.z);
  723. this._effect.setFloat3("vSphericalXX", this.reflectionTexture.sphericalPolynomial.xx.x, this.reflectionTexture.sphericalPolynomial.xx.y, this.reflectionTexture.sphericalPolynomial.xx.z);
  724. this._effect.setFloat3("vSphericalYY", this.reflectionTexture.sphericalPolynomial.yy.x, this.reflectionTexture.sphericalPolynomial.yy.y, this.reflectionTexture.sphericalPolynomial.yy.z);
  725. this._effect.setFloat3("vSphericalZZ", this.reflectionTexture.sphericalPolynomial.zz.x, this.reflectionTexture.sphericalPolynomial.zz.y, this.reflectionTexture.sphericalPolynomial.zz.z);
  726. this._effect.setFloat3("vSphericalXY", this.reflectionTexture.sphericalPolynomial.xy.x, this.reflectionTexture.sphericalPolynomial.xy.y, this.reflectionTexture.sphericalPolynomial.xy.z);
  727. this._effect.setFloat3("vSphericalYZ", this.reflectionTexture.sphericalPolynomial.yz.x, this.reflectionTexture.sphericalPolynomial.yz.y, this.reflectionTexture.sphericalPolynomial.yz.z);
  728. this._effect.setFloat3("vSphericalZX", this.reflectionTexture.sphericalPolynomial.zx.x, this.reflectionTexture.sphericalPolynomial.zx.y, this.reflectionTexture.sphericalPolynomial.zx.z);
  729. }
  730. }
  731. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  732. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  733. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  734. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  735. }
  736. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  737. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  738. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  739. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  740. }
  741. if (this.reflectivityTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  742. this._effect.setTexture("reflectivitySampler", this.reflectivityTexture);
  743. this._effect.setFloat2("vReflectivityInfos", this.reflectivityTexture.coordinatesIndex, this.reflectivityTexture.level);
  744. this._effect.setMatrix("reflectivityMatrix", this.reflectivityTexture.getTextureMatrix());
  745. }
  746. if (this.bumpTexture && this._myScene.getEngine().getCaps().standardDerivatives && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  747. this._effect.setTexture("bumpSampler", this.bumpTexture);
  748. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level);
  749. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  750. }
  751. // Clip plane
  752. if (this._myScene.clipPlane) {
  753. this._effect.setFloat4("vClipPlane", this._myScene.clipPlane.normal.x, this._myScene.clipPlane.normal.y, this._myScene.clipPlane.normal.z, this._myScene.clipPlane.d);
  754. }
  755. // Point size
  756. if (this.pointsCloud) {
  757. this._effect.setFloat("pointSize", this.pointSize);
  758. }
  759. // Colors
  760. this._myScene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  761. // GAMMA CORRECTION.
  762. this.reflectivityColor.toLinearSpaceToRef(PBRMaterial._scaledReflectivity);
  763. this._effect.setVector3("vEyePosition", this._myScene._mirroredCameraPosition ? this._myScene._mirroredCameraPosition : this._myScene.activeCamera.position);
  764. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  765. this._effect.setColor4("vReflectivityColor", PBRMaterial._scaledReflectivity, this.microSurface);
  766. // GAMMA CORRECTION.
  767. this.emissiveColor.toLinearSpaceToRef(PBRMaterial._scaledEmissive);
  768. this._effect.setColor3("vEmissiveColor", PBRMaterial._scaledEmissive);
  769. // GAMMA CORRECTION.
  770. this.reflectionColor.toLinearSpaceToRef(PBRMaterial._scaledReflection);
  771. this._effect.setColor3("vReflectionColor", PBRMaterial._scaledReflection);
  772. }
  773. // GAMMA CORRECTION.
  774. this.albedoColor.toLinearSpaceToRef(PBRMaterial._scaledAlbedo);
  775. this._effect.setColor4("vAlbedoColor", PBRMaterial._scaledAlbedo, this.alpha * mesh.visibility);
  776. // Lights
  777. if (this._myScene.lightsEnabled && !this.disableLighting) {
  778. PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines);
  779. }
  780. // View
  781. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  782. this._effect.setMatrix("view", this._myScene.getViewMatrix());
  783. }
  784. // Fog
  785. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  786. this._effect.setFloat4("vFogInfos", this._myScene.fogMode, this._myScene.fogStart, this._myScene.fogEnd, this._myScene.fogDensity);
  787. this._effect.setColor3("vFogColor", this._myScene.fogColor);
  788. }
  789. this._lightingInfos.x = this.directIntensity;
  790. this._lightingInfos.y = this.emissiveIntensity;
  791. this._lightingInfos.z = this.environmentIntensity;
  792. this._lightingInfos.w = this.specularIntensity;
  793. this._effect.setVector4("vLightingIntensity", this._lightingInfos);
  794. this._overloadedShadowInfos.x = this.overloadedShadowIntensity;
  795. this._overloadedShadowInfos.y = this.overloadedShadeIntensity;
  796. this._effect.setVector4("vOverloadedShadowIntensity", this._overloadedShadowInfos);
  797. this._cameraInfos.x = this.cameraExposure;
  798. this._cameraInfos.y = this.cameraContrast;
  799. this._effect.setVector4("vCameraInfos", this._cameraInfos);
  800. this._overloadedIntensity.x = this.overloadedAmbientIntensity;
  801. this._overloadedIntensity.y = this.overloadedAlbedoIntensity;
  802. this._overloadedIntensity.z = this.overloadedReflectivityIntensity;
  803. this._overloadedIntensity.w = this.overloadedEmissiveIntensity;
  804. this._effect.setVector4("vOverloadedIntensity", this._overloadedIntensity);
  805. this.overloadedAmbient.toLinearSpaceToRef(this._tempColor);
  806. this._effect.setColor3("vOverloadedAmbient", this._tempColor);
  807. this.overloadedAlbedo.toLinearSpaceToRef(this._tempColor);
  808. this._effect.setColor3("vOverloadedAlbedo", this._tempColor);
  809. this.overloadedReflectivity.toLinearSpaceToRef(this._tempColor);
  810. this._effect.setColor3("vOverloadedReflectivity", this._tempColor);
  811. this.overloadedEmissive.toLinearSpaceToRef(this._tempColor);
  812. this._effect.setColor3("vOverloadedEmissive", this._tempColor);
  813. this.overloadedReflection.toLinearSpaceToRef(this._tempColor);
  814. this._effect.setColor3("vOverloadedReflection", this._tempColor);
  815. this._overloadedMicroSurface.x = this.overloadedMicroSurface;
  816. this._overloadedMicroSurface.y = this.overloadedMicroSurfaceIntensity;
  817. this._overloadedMicroSurface.z = this.overloadedReflectionIntensity;
  818. this._effect.setVector3("vOverloadedMicroSurface", this._overloadedMicroSurface);
  819. // Log. depth
  820. if (this._defines.LOGARITHMICDEPTH) {
  821. this._effect.setFloat("logarithmicDepthConstant", 2.0 / (Math.log(this._myScene.activeCamera.maxZ + 1.0) / Math.LN2));
  822. }
  823. _super.prototype.bind.call(this, world, mesh);
  824. this._myScene = null;
  825. };
  826. PBRMaterial.prototype.getAnimatables = function () {
  827. var results = [];
  828. if (this.albedoTexture && this.albedoTexture.animations && this.albedoTexture.animations.length > 0) {
  829. results.push(this.albedoTexture);
  830. }
  831. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  832. results.push(this.ambientTexture);
  833. }
  834. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  835. results.push(this.opacityTexture);
  836. }
  837. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  838. results.push(this.reflectionTexture);
  839. }
  840. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  841. results.push(this.emissiveTexture);
  842. }
  843. if (this.reflectivityTexture && this.reflectivityTexture.animations && this.reflectivityTexture.animations.length > 0) {
  844. results.push(this.reflectivityTexture);
  845. }
  846. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  847. results.push(this.bumpTexture);
  848. }
  849. return results;
  850. };
  851. PBRMaterial.prototype.dispose = function (forceDisposeEffect) {
  852. if (this.albedoTexture) {
  853. this.albedoTexture.dispose();
  854. }
  855. if (this.ambientTexture) {
  856. this.ambientTexture.dispose();
  857. }
  858. if (this.opacityTexture) {
  859. this.opacityTexture.dispose();
  860. }
  861. if (this.reflectionTexture) {
  862. this.reflectionTexture.dispose();
  863. }
  864. if (this.emissiveTexture) {
  865. this.emissiveTexture.dispose();
  866. }
  867. if (this.reflectivityTexture) {
  868. this.reflectivityTexture.dispose();
  869. }
  870. if (this.bumpTexture) {
  871. this.bumpTexture.dispose();
  872. }
  873. _super.prototype.dispose.call(this, forceDisposeEffect);
  874. };
  875. PBRMaterial.prototype.clone = function (name) {
  876. var newPBRMaterial = new PBRMaterial(name, this.getScene());
  877. // Base material
  878. this.copyTo(newPBRMaterial);
  879. newPBRMaterial.directIntensity = this.directIntensity;
  880. newPBRMaterial.emissiveIntensity = this.emissiveIntensity;
  881. newPBRMaterial.environmentIntensity = this.environmentIntensity;
  882. newPBRMaterial.specularIntensity = this.specularIntensity;
  883. newPBRMaterial.cameraExposure = this.cameraExposure;
  884. newPBRMaterial.cameraContrast = this.cameraContrast;
  885. newPBRMaterial.overloadedShadowIntensity = this.overloadedShadowIntensity;
  886. newPBRMaterial.overloadedShadeIntensity = this.overloadedShadeIntensity;
  887. newPBRMaterial.overloadedAmbientIntensity = this.overloadedAmbientIntensity;
  888. newPBRMaterial.overloadedAlbedoIntensity = this.overloadedAlbedoIntensity;
  889. newPBRMaterial.overloadedReflectivityIntensity = this.overloadedReflectivityIntensity;
  890. newPBRMaterial.overloadedEmissiveIntensity = this.overloadedEmissiveIntensity;
  891. newPBRMaterial.overloadedAmbient = this.overloadedAmbient;
  892. newPBRMaterial.overloadedAlbedo = this.overloadedAlbedo;
  893. newPBRMaterial.overloadedReflectivity = this.overloadedReflectivity;
  894. newPBRMaterial.overloadedEmissive = this.overloadedEmissive;
  895. newPBRMaterial.overloadedReflection = this.overloadedReflection;
  896. newPBRMaterial.overloadedMicroSurface = this.overloadedMicroSurface;
  897. newPBRMaterial.overloadedMicroSurfaceIntensity = this.overloadedMicroSurfaceIntensity;
  898. newPBRMaterial.overloadedReflectionIntensity = this.overloadedReflectionIntensity;
  899. newPBRMaterial.disableBumpMap = this.disableBumpMap;
  900. // Standard material
  901. if (this.albedoTexture && this.albedoTexture.clone) {
  902. newPBRMaterial.albedoTexture = this.albedoTexture.clone();
  903. }
  904. if (this.ambientTexture && this.ambientTexture.clone) {
  905. newPBRMaterial.ambientTexture = this.ambientTexture.clone();
  906. }
  907. if (this.opacityTexture && this.opacityTexture.clone) {
  908. newPBRMaterial.opacityTexture = this.opacityTexture.clone();
  909. }
  910. if (this.reflectionTexture && this.reflectionTexture.clone) {
  911. newPBRMaterial.reflectionTexture = this.reflectionTexture.clone();
  912. }
  913. if (this.emissiveTexture && this.emissiveTexture.clone) {
  914. newPBRMaterial.emissiveTexture = this.emissiveTexture.clone();
  915. }
  916. if (this.reflectivityTexture && this.reflectivityTexture.clone) {
  917. newPBRMaterial.reflectivityTexture = this.reflectivityTexture.clone();
  918. }
  919. if (this.bumpTexture && this.bumpTexture.clone) {
  920. newPBRMaterial.bumpTexture = this.bumpTexture.clone();
  921. }
  922. if (this.lightmapTexture && this.lightmapTexture.clone) {
  923. newPBRMaterial.lightmapTexture = this.lightmapTexture.clone();
  924. newPBRMaterial.useLightmapAsShadowmap = this.useLightmapAsShadowmap;
  925. }
  926. newPBRMaterial.ambientColor = this.ambientColor.clone();
  927. newPBRMaterial.albedoColor = this.albedoColor.clone();
  928. newPBRMaterial.reflectivityColor = this.reflectivityColor.clone();
  929. newPBRMaterial.reflectionColor = this.reflectionColor.clone();
  930. newPBRMaterial.microSurface = this.microSurface;
  931. newPBRMaterial.emissiveColor = this.emissiveColor.clone();
  932. newPBRMaterial.useAlphaFromAlbedoTexture = this.useAlphaFromAlbedoTexture;
  933. newPBRMaterial.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
  934. newPBRMaterial.useMicroSurfaceFromReflectivityMapAlpha = this.useMicroSurfaceFromReflectivityMapAlpha;
  935. newPBRMaterial.useSpecularOverAlpha = this.useSpecularOverAlpha;
  936. newPBRMaterial.emissiveFresnelParameters = this.emissiveFresnelParameters.clone();
  937. newPBRMaterial.opacityFresnelParameters = this.opacityFresnelParameters.clone();
  938. return newPBRMaterial;
  939. };
  940. PBRMaterial.prototype.serialize = function () {
  941. var serializationObject = _super.prototype.serialize.call(this);
  942. serializationObject.customType = "BABYLON.PBRMaterial";
  943. serializationObject.directIntensity = this.directIntensity;
  944. serializationObject.emissiveIntensity = this.emissiveIntensity;
  945. serializationObject.environmentIntensity = this.environmentIntensity;
  946. serializationObject.specularIntensity = this.specularIntensity;
  947. serializationObject.cameraExposure = this.cameraExposure;
  948. serializationObject.cameraContrast = this.cameraContrast;
  949. serializationObject.overloadedShadowIntensity = this.overloadedShadowIntensity;
  950. serializationObject.overloadedShadeIntensity = this.overloadedShadeIntensity;
  951. serializationObject.overloadedAmbientIntensity = this.overloadedAmbientIntensity;
  952. serializationObject.overloadedAlbedoIntensity = this.overloadedAlbedoIntensity;
  953. serializationObject.overloadedReflectivityIntensity = this.overloadedReflectivityIntensity;
  954. serializationObject.overloadedEmissiveIntensity = this.overloadedEmissiveIntensity;
  955. serializationObject.overloadedAmbient = this.overloadedAmbient.asArray();
  956. serializationObject.overloadedAlbedo = this.overloadedAlbedo.asArray();
  957. serializationObject.overloadedReflectivity = this.overloadedReflectivity.asArray();
  958. serializationObject.overloadedEmissive = this.overloadedEmissive.asArray();
  959. serializationObject.overloadedReflection = this.overloadedReflection.asArray();
  960. serializationObject.overloadedMicroSurface = this.overloadedMicroSurface;
  961. serializationObject.overloadedMicroSurfaceIntensity = this.overloadedMicroSurfaceIntensity;
  962. serializationObject.overloadedReflectionIntensity = this.overloadedReflectionIntensity;
  963. serializationObject.disableBumpMap = this.disableBumpMap;
  964. // Standard material
  965. if (this.albedoTexture) {
  966. serializationObject.albedoTexture = this.albedoTexture.serialize();
  967. }
  968. if (this.ambientTexture) {
  969. serializationObject.ambientTexture = this.ambientTexture.serialize();
  970. }
  971. if (this.opacityTexture) {
  972. serializationObject.opacityTexture = this.opacityTexture.serialize();
  973. }
  974. if (this.reflectionTexture) {
  975. serializationObject.reflectionTexture = this.reflectionTexture.serialize();
  976. }
  977. if (this.emissiveTexture) {
  978. serializationObject.emissiveTexture = this.emissiveTexture.serialize();
  979. }
  980. if (this.reflectivityTexture) {
  981. serializationObject.reflectivityTexture = this.reflectivityTexture.serialize();
  982. }
  983. if (this.bumpTexture) {
  984. serializationObject.bumpTexture = this.bumpTexture.serialize();
  985. }
  986. if (this.lightmapTexture) {
  987. serializationObject.lightmapTexture = this.lightmapTexture.serialize();
  988. serializationObject.useLightmapAsShadowmap = this.useLightmapAsShadowmap;
  989. }
  990. serializationObject.ambientColor = this.ambientColor.asArray();
  991. serializationObject.albedoColor = this.albedoColor.asArray();
  992. serializationObject.reflectivityColor = this.reflectivityColor.asArray();
  993. serializationObject.reflectionColor = this.reflectionColor.asArray();
  994. serializationObject.microSurface = this.microSurface;
  995. serializationObject.emissiveColor = this.emissiveColor.asArray();
  996. serializationObject.useAlphaFromAlbedoTexture = this.useAlphaFromAlbedoTexture;
  997. serializationObject.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
  998. serializationObject.useMicroSurfaceFromReflectivityMapAlpha = this.useMicroSurfaceFromReflectivityMapAlpha;
  999. serializationObject.useSpecularOverAlpha = this.useSpecularOverAlpha;
  1000. serializationObject.emissiveFresnelParameters = this.emissiveFresnelParameters.serialize();
  1001. serializationObject.opacityFresnelParameters = this.opacityFresnelParameters.serialize();
  1002. return serializationObject;
  1003. };
  1004. PBRMaterial.Parse = function (source, scene, rootUrl) {
  1005. var material = new PBRMaterial(source.name, scene);
  1006. material.alpha = source.alpha;
  1007. material.id = source.id;
  1008. if (source.disableDepthWrite) {
  1009. material.disableDepthWrite = source.disableDepthWrite;
  1010. }
  1011. if (source.checkReadyOnlyOnce) {
  1012. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  1013. }
  1014. BABYLON.Tags.AddTagsTo(material, source.tags);
  1015. material.backFaceCulling = source.backFaceCulling;
  1016. material.wireframe = source.wireframe;
  1017. material.directIntensity = source.directIntensity;
  1018. material.emissiveIntensity = source.emissiveIntensity;
  1019. material.environmentIntensity = source.environmentIntensity;
  1020. material.specularIntensity = source.specularIntensity;
  1021. material.cameraExposure = source.cameraExposure;
  1022. material.cameraContrast = source.cameraContrast;
  1023. material.overloadedShadowIntensity = source.overloadedShadowIntensity;
  1024. material.overloadedShadeIntensity = source.overloadedShadeIntensity;
  1025. material.overloadedAmbientIntensity = source.overloadedAmbientIntensity;
  1026. material.overloadedAlbedoIntensity = source.overloadedAlbedoIntensity;
  1027. material.overloadedReflectivityIntensity = source.overloadedReflectivityIntensity;
  1028. material.overloadedEmissiveIntensity = source.overloadedEmissiveIntensity;
  1029. material.overloadedAmbient = BABYLON.Color3.FromArray(source.overloadedAmbient);
  1030. material.overloadedAlbedo = BABYLON.Color3.FromArray(source.overloadedAlbedo);
  1031. material.overloadedReflectivity = BABYLON.Color3.FromArray(source.overloadedReflectivity);
  1032. material.overloadedEmissive = BABYLON.Color3.FromArray(source.overloadedEmissive);
  1033. material.overloadedReflection = BABYLON.Color3.FromArray(source.overloadedReflection);
  1034. material.overloadedMicroSurface = source.overloadedMicroSurface;
  1035. material.overloadedMicroSurfaceIntensity = source.overloadedMicroSurfaceIntensity;
  1036. material.overloadedReflectionIntensity = source.overloadedReflectionIntensity;
  1037. material.disableBumpMap = source.disableBumpMap;
  1038. // Standard material
  1039. if (source.albedoTexture) {
  1040. material.albedoTexture = BABYLON.Texture.Parse(source.albedoTexture, scene, rootUrl);
  1041. }
  1042. if (source.ambientTexture) {
  1043. material.ambientTexture = BABYLON.Texture.Parse(source.ambientTexture, scene, rootUrl);
  1044. }
  1045. if (source.opacityTexture) {
  1046. material.opacityTexture = BABYLON.Texture.Parse(source.opacityTexture, scene, rootUrl);
  1047. }
  1048. if (source.reflectionTexture) {
  1049. material.reflectionTexture = BABYLON.Texture.Parse(source.reflectionTexture, scene, rootUrl);
  1050. }
  1051. if (source.emissiveTexture) {
  1052. material.emissiveTexture = BABYLON.Texture.Parse(source.emissiveTexture, scene, rootUrl);
  1053. }
  1054. if (source.reflectivityTexture) {
  1055. material.reflectivityTexture = BABYLON.Texture.Parse(source.reflectivityTexture, scene, rootUrl);
  1056. }
  1057. if (source.bumpTexture) {
  1058. material.bumpTexture = BABYLON.Texture.Parse(source.bumpTexture, scene, rootUrl);
  1059. }
  1060. if (source.lightmapTexture) {
  1061. material.lightmapTexture = BABYLON.Texture.Parse(source.lightmapTexture, scene, rootUrl);
  1062. material.useLightmapAsShadowmap = source.useLightmapAsShadowmap;
  1063. }
  1064. material.ambientColor = BABYLON.Color3.FromArray(source.ambient);
  1065. material.albedoColor = BABYLON.Color3.FromArray(source.albedo);
  1066. material.reflectivityColor = BABYLON.Color3.FromArray(source.reflectivity);
  1067. material.reflectionColor = BABYLON.Color3.FromArray(source.reflectionColor);
  1068. material.microSurface = source.microSurface;
  1069. material.emissiveColor = BABYLON.Color3.FromArray(source.emissive);
  1070. material.useAlphaFromAlbedoTexture = source.useAlphaFromAlbedoTexture;
  1071. material.useEmissiveAsIllumination = source.useEmissiveAsIllumination;
  1072. material.useMicroSurfaceFromReflectivityMapAlpha = source.useMicroSurfaceFromReflectivityMapAlpha;
  1073. material.useSpecularOverAlpha = source.useSpecularOverAlpha;
  1074. material.emissiveFresnelParameters = BABYLON.FresnelParameters.Parse(source.emissiveFresnelParameters);
  1075. material.opacityFresnelParameters = BABYLON.FresnelParameters.Parse(source.opacityFresnelParameters);
  1076. return material;
  1077. };
  1078. PBRMaterial._scaledAlbedo = new BABYLON.Color3();
  1079. PBRMaterial._scaledReflectivity = new BABYLON.Color3();
  1080. PBRMaterial._scaledEmissive = new BABYLON.Color3();
  1081. PBRMaterial._scaledReflection = new BABYLON.Color3();
  1082. return PBRMaterial;
  1083. })(BABYLON.Material);
  1084. BABYLON.PBRMaterial = PBRMaterial;
  1085. })(BABYLON || (BABYLON = {}));
  1086. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  1087. var BABYLON;
  1088. (function (BABYLON) {
  1089. var SphericalHarmonics = (function () {
  1090. function SphericalHarmonics() {
  1091. this.L00 = BABYLON.Vector3.Zero();
  1092. this.L1_1 = BABYLON.Vector3.Zero();
  1093. this.L10 = BABYLON.Vector3.Zero();
  1094. this.L11 = BABYLON.Vector3.Zero();
  1095. this.L2_2 = BABYLON.Vector3.Zero();
  1096. this.L2_1 = BABYLON.Vector3.Zero();
  1097. this.L20 = BABYLON.Vector3.Zero();
  1098. this.L21 = BABYLON.Vector3.Zero();
  1099. this.L22 = BABYLON.Vector3.Zero();
  1100. }
  1101. SphericalHarmonics.prototype.addLight = function (direction, color, deltaSolidAngle) {
  1102. var colorVector = new BABYLON.Vector3(color.r, color.g, color.b);
  1103. var c = colorVector.scale(deltaSolidAngle);
  1104. this.L00 = this.L00.add(c.scale(0.282095));
  1105. this.L1_1 = this.L1_1.add(c.scale(0.488603 * direction.y));
  1106. this.L10 = this.L10.add(c.scale(0.488603 * direction.z));
  1107. this.L11 = this.L11.add(c.scale(0.488603 * direction.x));
  1108. this.L2_2 = this.L2_2.add(c.scale(1.092548 * direction.x * direction.y));
  1109. this.L2_1 = this.L2_1.add(c.scale(1.092548 * direction.y * direction.z));
  1110. this.L21 = this.L21.add(c.scale(1.092548 * direction.x * direction.z));
  1111. this.L20 = this.L20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
  1112. this.L22 = this.L22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
  1113. };
  1114. SphericalHarmonics.prototype.scale = function (scale) {
  1115. this.L00 = this.L00.scale(scale);
  1116. this.L1_1 = this.L1_1.scale(scale);
  1117. this.L10 = this.L10.scale(scale);
  1118. this.L11 = this.L11.scale(scale);
  1119. this.L2_2 = this.L2_2.scale(scale);
  1120. this.L2_1 = this.L2_1.scale(scale);
  1121. this.L20 = this.L20.scale(scale);
  1122. this.L21 = this.L21.scale(scale);
  1123. this.L22 = this.L22.scale(scale);
  1124. };
  1125. return SphericalHarmonics;
  1126. })();
  1127. BABYLON.SphericalHarmonics = SphericalHarmonics;
  1128. })(BABYLON || (BABYLON = {}));
  1129. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  1130. var BABYLON;
  1131. (function (BABYLON) {
  1132. var SphericalPolynomial = (function () {
  1133. function SphericalPolynomial() {
  1134. this.x = BABYLON.Vector3.Zero();
  1135. this.y = BABYLON.Vector3.Zero();
  1136. this.z = BABYLON.Vector3.Zero();
  1137. this.xx = BABYLON.Vector3.Zero();
  1138. this.yy = BABYLON.Vector3.Zero();
  1139. this.zz = BABYLON.Vector3.Zero();
  1140. this.xy = BABYLON.Vector3.Zero();
  1141. this.yz = BABYLON.Vector3.Zero();
  1142. this.zx = BABYLON.Vector3.Zero();
  1143. }
  1144. SphericalPolynomial.prototype.addAmbient = function (color) {
  1145. var colorVector = new BABYLON.Vector3(color.r, color.g, color.b);
  1146. this.xx = this.xx.add(colorVector);
  1147. this.yy = this.yy.add(colorVector);
  1148. this.zz = this.zz.add(colorVector);
  1149. };
  1150. SphericalPolynomial.getSphericalPolynomialFromHarmonics = function (harmonics) {
  1151. var result = new SphericalPolynomial();
  1152. result.x = harmonics.L11.scale(1.02333);
  1153. result.y = harmonics.L1_1.scale(1.02333);
  1154. result.z = harmonics.L10.scale(1.02333);
  1155. result.xx = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).add(harmonics.L22.scale(0.429043));
  1156. result.yy = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).subtract(harmonics.L22.scale(0.429043));
  1157. result.zz = harmonics.L00.scale(0.886277).add(harmonics.L20.scale(0.495417));
  1158. result.yz = harmonics.L2_1.scale(0.858086);
  1159. result.zx = harmonics.L21.scale(0.858086);
  1160. result.xy = harmonics.L2_2.scale(0.858086);
  1161. return result;
  1162. };
  1163. return SphericalPolynomial;
  1164. })();
  1165. BABYLON.SphericalPolynomial = SphericalPolynomial;
  1166. })(BABYLON || (BABYLON = {}));
  1167. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  1168. var BABYLON;
  1169. (function (BABYLON) {
  1170. var Internals;
  1171. (function (Internals) {
  1172. var PanoramaToCubeMapTools = (function () {
  1173. function PanoramaToCubeMapTools() {
  1174. }
  1175. PanoramaToCubeMapTools.ConvertPanoramaToCubemap = function (float32Array, inputWidth, inputHeight, size) {
  1176. if (!float32Array) {
  1177. throw "ConvertPanoramaToCubemap: input cannot be null";
  1178. }
  1179. if (float32Array.length != inputWidth * inputHeight * 3) {
  1180. throw "ConvertPanoramaToCubemap: input size is wrong";
  1181. }
  1182. var textureFront = this.CreateCubemapTexture(size, this.FACE_FRONT, float32Array, inputWidth, inputHeight);
  1183. var textureBack = this.CreateCubemapTexture(size, this.FACE_BACK, float32Array, inputWidth, inputHeight);
  1184. var textureLeft = this.CreateCubemapTexture(size, this.FACE_LEFT, float32Array, inputWidth, inputHeight);
  1185. var textureRight = this.CreateCubemapTexture(size, this.FACE_RIGHT, float32Array, inputWidth, inputHeight);
  1186. var textureUp = this.CreateCubemapTexture(size, this.FACE_UP, float32Array, inputWidth, inputHeight);
  1187. var textureDown = this.CreateCubemapTexture(size, this.FACE_DOWN, float32Array, inputWidth, inputHeight);
  1188. return {
  1189. front: textureFront,
  1190. back: textureBack,
  1191. left: textureLeft,
  1192. right: textureRight,
  1193. up: textureUp,
  1194. down: textureDown,
  1195. size: size
  1196. };
  1197. };
  1198. PanoramaToCubeMapTools.CreateCubemapTexture = function (texSize, faceData, float32Array, inputWidth, inputHeight) {
  1199. var buffer = new ArrayBuffer(texSize * texSize * 4 * 3);
  1200. var textureArray = new Float32Array(buffer);
  1201. var rotDX1 = faceData[1].subtract(faceData[0]).scale(1 / texSize);
  1202. var rotDX2 = faceData[3].subtract(faceData[2]).scale(1 / texSize);
  1203. var dy = 1 / texSize;
  1204. var fy = 0;
  1205. for (var y = 0; y < texSize; y++) {
  1206. var xv1 = faceData[0];
  1207. var xv2 = faceData[2];
  1208. for (var x = 0; x < texSize; x++) {
  1209. var v = xv2.subtract(xv1).scale(fy).add(xv1);
  1210. v.normalize();
  1211. var color = this.CalcProjectionSpherical(v, float32Array, inputWidth, inputHeight);
  1212. // 3 channels per pixels
  1213. textureArray[y * texSize * 3 + (x * 3) + 0] = color.r;
  1214. textureArray[y * texSize * 3 + (x * 3) + 1] = color.g;
  1215. textureArray[y * texSize * 3 + (x * 3) + 2] = color.b;
  1216. xv1 = xv1.add(rotDX1);
  1217. xv2 = xv2.add(rotDX2);
  1218. }
  1219. fy += dy;
  1220. }
  1221. return textureArray;
  1222. };
  1223. PanoramaToCubeMapTools.CalcProjectionSpherical = function (vDir, float32Array, inputWidth, inputHeight) {
  1224. var theta = Math.atan2(vDir.z, vDir.x);
  1225. var phi = Math.acos(vDir.y);
  1226. while (theta < -Math.PI)
  1227. theta += 2 * Math.PI;
  1228. while (theta > Math.PI)
  1229. theta -= 2 * Math.PI;
  1230. var dx = theta / Math.PI;
  1231. var dy = phi / Math.PI;
  1232. // recenter.
  1233. dx = dx * 0.5 + 0.5;
  1234. var px = Math.round(dx * inputWidth);
  1235. if (px < 0)
  1236. px = 0;
  1237. else if (px >= inputWidth)
  1238. px = inputWidth - 1;
  1239. var py = Math.round(dy * inputHeight);
  1240. if (py < 0)
  1241. py = 0;
  1242. else if (py >= inputHeight)
  1243. py = inputHeight - 1;
  1244. var inputY = (inputHeight - py - 1);
  1245. var r = float32Array[inputY * inputWidth * 3 + (px * 3) + 0];
  1246. var g = float32Array[inputY * inputWidth * 3 + (px * 3) + 1];
  1247. var b = float32Array[inputY * inputWidth * 3 + (px * 3) + 2];
  1248. return {
  1249. r: r,
  1250. g: g,
  1251. b: b
  1252. };
  1253. };
  1254. PanoramaToCubeMapTools.FACE_FRONT = [
  1255. new BABYLON.Vector3(-1.0, -1.0, -1.0),
  1256. new BABYLON.Vector3(1.0, -1.0, -1.0),
  1257. new BABYLON.Vector3(-1.0, 1.0, -1.0),
  1258. new BABYLON.Vector3(1.0, 1.0, -1.0)
  1259. ];
  1260. PanoramaToCubeMapTools.FACE_BACK = [
  1261. new BABYLON.Vector3(1.0, -1.0, 1.0),
  1262. new BABYLON.Vector3(-1.0, -1.0, 1.0),
  1263. new BABYLON.Vector3(1.0, 1.0, 1.0),
  1264. new BABYLON.Vector3(-1.0, 1.0, 1.0)
  1265. ];
  1266. PanoramaToCubeMapTools.FACE_LEFT = [
  1267. new BABYLON.Vector3(1.0, -1.0, -1.0),
  1268. new BABYLON.Vector3(1.0, -1.0, 1.0),
  1269. new BABYLON.Vector3(1.0, 1.0, -1.0),
  1270. new BABYLON.Vector3(1.0, 1.0, 1.0)
  1271. ];
  1272. PanoramaToCubeMapTools.FACE_RIGHT = [
  1273. new BABYLON.Vector3(-1.0, -1.0, 1.0),
  1274. new BABYLON.Vector3(-1.0, -1.0, -1.0),
  1275. new BABYLON.Vector3(-1.0, 1.0, 1.0),
  1276. new BABYLON.Vector3(-1.0, 1.0, -1.0)
  1277. ];
  1278. PanoramaToCubeMapTools.FACE_UP = [
  1279. new BABYLON.Vector3(-1.0, 1.0, -1.0),
  1280. new BABYLON.Vector3(1.0, 1.0, -1.0),
  1281. new BABYLON.Vector3(-1.0, 1.0, 1.0),
  1282. new BABYLON.Vector3(1.0, 1.0, 1.0)
  1283. ];
  1284. PanoramaToCubeMapTools.FACE_DOWN = [
  1285. new BABYLON.Vector3(-1.0, -1.0, 1.0),
  1286. new BABYLON.Vector3(1.0, -1.0, 1.0),
  1287. new BABYLON.Vector3(-1.0, -1.0, -1.0),
  1288. new BABYLON.Vector3(1.0, -1.0, -1.0)
  1289. ];
  1290. return PanoramaToCubeMapTools;
  1291. })();
  1292. Internals.PanoramaToCubeMapTools = PanoramaToCubeMapTools;
  1293. })(Internals = BABYLON.Internals || (BABYLON.Internals = {}));
  1294. })(BABYLON || (BABYLON = {}));
  1295. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  1296. var BABYLON;
  1297. (function (BABYLON) {
  1298. var Internals;
  1299. (function (Internals) {
  1300. var FileFaceOrientation = (function () {
  1301. function FileFaceOrientation(name, worldAxisForNormal, worldAxisForFileX, worldAxisForFileY) {
  1302. this.name = name;
  1303. this.worldAxisForNormal = worldAxisForNormal;
  1304. this.worldAxisForFileX = worldAxisForFileX;
  1305. this.worldAxisForFileY = worldAxisForFileY;
  1306. }
  1307. return FileFaceOrientation;
  1308. })();
  1309. ;
  1310. var CubeMapToSphericalPolynomialTools = (function () {
  1311. function CubeMapToSphericalPolynomialTools() {
  1312. }
  1313. CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial = function (cubeInfo) {
  1314. var sphericalHarmonics = new BABYLON.SphericalHarmonics();
  1315. var totalSolidAngle = 0.0;
  1316. // The (u,v) range is [-1,+1], so the distance between each texel is 2/Size.
  1317. var du = 2.0 / cubeInfo.size;
  1318. var dv = du;
  1319. // The (u,v) of the first texel is half a texel from the corner (-1,-1).
  1320. var minUV = du * 0.5 - 1.0;
  1321. for (var faceIndex = 0; faceIndex < 6; faceIndex++) {
  1322. var fileFace = this.FileFaces[faceIndex];
  1323. var dataArray = cubeInfo[fileFace.name];
  1324. var v = minUV;
  1325. // TODO: we could perform the summation directly into a SphericalPolynomial (SP), which is more efficient than SphericalHarmonic (SH).
  1326. // This is possible because during the summation we do not need the SH-specific properties, e.g. orthogonality.
  1327. // Because SP is still linear, so summation is fine in that basis.
  1328. for (var y = 0; y < cubeInfo.size; y++) {
  1329. var u = minUV;
  1330. for (var x = 0; x < cubeInfo.size; x++) {
  1331. // World direction (not normalised)
  1332. var worldDirection = fileFace.worldAxisForFileX.scale(u).add(fileFace.worldAxisForFileY.scale(v)).add(fileFace.worldAxisForNormal);
  1333. worldDirection.normalize();
  1334. var deltaSolidAngle = Math.pow(1.0 + u * u + v * v, -3.0 / 2.0);
  1335. if (1) {
  1336. var r = dataArray[(y * cubeInfo.size * 3) + (x * 3) + 0];
  1337. var g = dataArray[(y * cubeInfo.size * 3) + (x * 3) + 1];
  1338. var b = dataArray[(y * cubeInfo.size * 3) + (x * 3) + 2];
  1339. var color = new BABYLON.Color3(r, g, b);
  1340. sphericalHarmonics.addLight(worldDirection, color, deltaSolidAngle);
  1341. }
  1342. else {
  1343. if (faceIndex == 0) {
  1344. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 0] = 1;
  1345. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 1] = 0;
  1346. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 2] = 0;
  1347. }
  1348. else if (faceIndex == 1) {
  1349. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 0] = 0;
  1350. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 1] = 1;
  1351. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 2] = 0;
  1352. }
  1353. else if (faceIndex == 2) {
  1354. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 0] = 0;
  1355. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 1] = 0;
  1356. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 2] = 1;
  1357. }
  1358. else if (faceIndex == 3) {
  1359. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 0] = 1;
  1360. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 1] = 1;
  1361. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 2] = 0;
  1362. }
  1363. else if (faceIndex == 4) {
  1364. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 0] = 1;
  1365. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 1] = 0;
  1366. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 2] = 1;
  1367. }
  1368. else if (faceIndex == 5) {
  1369. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 0] = 0;
  1370. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 1] = 1;
  1371. dataArray[(y * cubeInfo.size * 3) + (x * 3) + 2] = 1;
  1372. }
  1373. var color = new BABYLON.Color3(dataArray[(y * cubeInfo.size * 3) + (x * 3) + 0], dataArray[(y * cubeInfo.size * 3) + (x * 3) + 1], dataArray[(y * cubeInfo.size * 3) + (x * 3) + 2]);
  1374. sphericalHarmonics.addLight(worldDirection, color, deltaSolidAngle);
  1375. }
  1376. totalSolidAngle += deltaSolidAngle;
  1377. u += du;
  1378. }
  1379. v += dv;
  1380. }
  1381. }
  1382. var correctSolidAngle = 4.0 * Math.PI; // Solid angle for entire sphere is 4*pi
  1383. var correction = correctSolidAngle / totalSolidAngle;
  1384. sphericalHarmonics.scale(correction);
  1385. // Additionally scale by pi -- audit needed
  1386. sphericalHarmonics.scale(1.0 / Math.PI);
  1387. return BABYLON.SphericalPolynomial.getSphericalPolynomialFromHarmonics(sphericalHarmonics);
  1388. };
  1389. CubeMapToSphericalPolynomialTools.FileFaces = [
  1390. new FileFaceOrientation("left", new BABYLON.Vector3(1, 0, 0), new BABYLON.Vector3(0, 0, -1), new BABYLON.Vector3(0, -1, 0)),
  1391. new FileFaceOrientation("right", new BABYLON.Vector3(-1, 0, 0), new BABYLON.Vector3(0, 0, 1), new BABYLON.Vector3(0, -1, 0)),
  1392. new FileFaceOrientation("down", new BABYLON.Vector3(0, 1, 0), new BABYLON.Vector3(1, 0, 0), new BABYLON.Vector3(0, 0, 1)),
  1393. new FileFaceOrientation("up", new BABYLON.Vector3(0, -1, 0), new BABYLON.Vector3(1, 0, 0), new BABYLON.Vector3(0, 0, -1)),
  1394. new FileFaceOrientation("front", new BABYLON.Vector3(0, 0, 1), new BABYLON.Vector3(1, 0, 0), new BABYLON.Vector3(0, -1, 0)),
  1395. new FileFaceOrientation("back", new BABYLON.Vector3(0, 0, -1), new BABYLON.Vector3(-1, 0, 0), new BABYLON.Vector3(0, -1, 0)) // -Z bottom
  1396. ];
  1397. return CubeMapToSphericalPolynomialTools;
  1398. })();
  1399. Internals.CubeMapToSphericalPolynomialTools = CubeMapToSphericalPolynomialTools;
  1400. })(Internals = BABYLON.Internals || (BABYLON.Internals = {}));
  1401. })(BABYLON || (BABYLON = {}));
  1402. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  1403. var BABYLON;
  1404. (function (BABYLON) {
  1405. var Internals;
  1406. (function (Internals) {
  1407. ;
  1408. var HDRTools = (function () {
  1409. function HDRTools() {
  1410. }
  1411. HDRTools.Ldexp = function (mantissa, exponent) {
  1412. if (exponent > 1023) {
  1413. return mantissa * Math.pow(2, 1023) * Math.pow(2, exponent - 1023);
  1414. }
  1415. if (exponent < -1074) {
  1416. return mantissa * Math.pow(2, -1074) * Math.pow(2, exponent + 1074);
  1417. }
  1418. return mantissa * Math.pow(2, exponent);
  1419. };
  1420. HDRTools.Rgbe2float = function (float32array, red, green, blue, exponent, index) {
  1421. if (exponent > 0) {
  1422. exponent = this.Ldexp(1.0, exponent - (128 + 8));
  1423. float32array[index + 0] = red * exponent;
  1424. float32array[index + 1] = green * exponent;
  1425. float32array[index + 2] = blue * exponent;
  1426. }
  1427. else {
  1428. float32array[index + 0] = 0;
  1429. float32array[index + 1] = 0;
  1430. float32array[index + 2] = 0;
  1431. }
  1432. };
  1433. HDRTools.readStringLine = function (uint8array, startIndex) {
  1434. var line = "";
  1435. var character = "";
  1436. for (var i = startIndex; i < uint8array.length - startIndex; i++) {
  1437. character = String.fromCharCode(uint8array[i]);
  1438. if (character == "\n") {
  1439. break;
  1440. }
  1441. line += character;
  1442. }
  1443. return line;
  1444. };
  1445. /* minimal header reading. modify if you want to parse more information */
  1446. HDRTools.RGBE_ReadHeader = function (uint8array) {
  1447. var height = 0;
  1448. var width = 0;
  1449. var line = this.readStringLine(uint8array, 0);
  1450. if (line[0] != '#' || line[1] != '?') {
  1451. throw "Bad HDR Format.";
  1452. }
  1453. var endOfHeader = false;
  1454. var findFormat = false;
  1455. var lineIndex = 0;
  1456. do {
  1457. lineIndex += (line.length + 1);
  1458. line = this.readStringLine(uint8array, lineIndex);
  1459. if (line == "FORMAT=32-bit_rle_rgbe") {
  1460. findFormat = true;
  1461. }
  1462. else if (line.length == 0) {
  1463. endOfHeader = true;
  1464. }
  1465. } while (!endOfHeader);
  1466. if (!findFormat) {
  1467. throw "HDR Bad header format, unsupported FORMAT";
  1468. }
  1469. lineIndex += (line.length + 1);
  1470. line = this.readStringLine(uint8array, lineIndex);
  1471. var sizeRegexp = /^\-Y (.*) \+X (.*)$/g;
  1472. var match = sizeRegexp.exec(line);
  1473. // TODO. Support +Y and -X if needed.
  1474. if (match.length < 3) {
  1475. throw "HDR Bad header format, no size";
  1476. }
  1477. width = parseInt(match[2]);
  1478. height = parseInt(match[1]);
  1479. if (width < 8 || width > 0x7fff) {
  1480. throw "HDR Bad header format, unsupported size";
  1481. }
  1482. lineIndex += (line.length + 1);
  1483. return {
  1484. height: height,
  1485. width: width,
  1486. dataPosition: lineIndex
  1487. };
  1488. };
  1489. HDRTools.GetCubeMapTextureData = function (buffer, size) {
  1490. var uint8array = new Uint8Array(buffer);
  1491. var hdrInfo = this.RGBE_ReadHeader(uint8array);
  1492. var data = this.RGBE_ReadPixels_RLE(uint8array, hdrInfo);
  1493. var cubeMapData = Internals.PanoramaToCubeMapTools.ConvertPanoramaToCubemap(data, hdrInfo.width, hdrInfo.height, size);
  1494. return cubeMapData;
  1495. };
  1496. HDRTools.RGBE_ReadPixels = function (uint8array, hdrInfo) {
  1497. // Keep for multi format supports.
  1498. return this.RGBE_ReadPixels_RLE(uint8array, hdrInfo);
  1499. };
  1500. HDRTools.RGBE_ReadPixels_RLE = function (uint8array, hdrInfo) {
  1501. var num_scanlines = hdrInfo.height;
  1502. var scanline_width = hdrInfo.width;
  1503. var a, b, c, d, count;
  1504. var dataIndex = hdrInfo.dataPosition;
  1505. var index = 0, endIndex = 0, i = 0;
  1506. var scanLineArrayBuffer = new ArrayBuffer(scanline_width * 4); // four channel R G B E
  1507. var scanLineArray = new Uint8Array(scanLineArrayBuffer);
  1508. // 3 channels of 4 bytes per pixel in float.
  1509. var resultBuffer = new ArrayBuffer(hdrInfo.width * hdrInfo.height * 4 * 3);
  1510. var resultArray = new Float32Array(resultBuffer);
  1511. // read in each successive scanline
  1512. while (num_scanlines > 0) {
  1513. a = uint8array[dataIndex++];
  1514. b = uint8array[dataIndex++];
  1515. c = uint8array[dataIndex++];
  1516. d = uint8array[dataIndex++];
  1517. if (a != 2 || b != 2 || (c & 0x80)) {
  1518. // this file is not run length encoded
  1519. throw "HDR Bad header format, not RLE";
  1520. }
  1521. if (((c << 8) | d) != scanline_width) {
  1522. throw "HDR Bad header format, wrong scan line width";
  1523. }
  1524. index = 0;
  1525. // read each of the four channels for the scanline into the buffer
  1526. for (i = 0; i < 4; i++) {
  1527. endIndex = (i + 1) * scanline_width;
  1528. while (index < endIndex) {
  1529. a = uint8array[dataIndex++];
  1530. b = uint8array[dataIndex++];
  1531. if (a > 128) {
  1532. // a run of the same value
  1533. count = a - 128;
  1534. if ((count == 0) || (count > endIndex - index)) {
  1535. throw "HDR Bad Format, bad scanline data (run)";
  1536. }
  1537. while (count-- > 0) {
  1538. scanLineArray[index++] = b;
  1539. }
  1540. }
  1541. else {
  1542. // a non-run
  1543. count = a;
  1544. if ((count == 0) || (count > endIndex - index)) {
  1545. throw "HDR Bad Format, bad scanline data (non-run)";
  1546. }
  1547. scanLineArray[index++] = b;
  1548. if (--count > 0) {
  1549. for (var j = 0; j < count; j++) {
  1550. scanLineArray[index++] = uint8array[dataIndex++];
  1551. }
  1552. }
  1553. }
  1554. }
  1555. }
  1556. // now convert data from buffer into floats
  1557. for (i = 0; i < scanline_width; i++) {
  1558. a = scanLineArray[i];
  1559. b = scanLineArray[i + scanline_width];
  1560. c = scanLineArray[i + 2 * scanline_width];
  1561. d = scanLineArray[i + 3 * scanline_width];
  1562. this.Rgbe2float(resultArray, a, b, c, d, (hdrInfo.height - num_scanlines) * scanline_width * 3 + i * 3);
  1563. }
  1564. num_scanlines--;
  1565. }
  1566. return resultArray;
  1567. };
  1568. return HDRTools;
  1569. })();
  1570. Internals.HDRTools = HDRTools;
  1571. })(Internals = BABYLON.Internals || (BABYLON.Internals = {}));
  1572. })(BABYLON || (BABYLON = {}));
  1573. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  1574. var BABYLON;
  1575. (function (BABYLON) {
  1576. var HDRCubeTexture = (function (_super) {
  1577. __extends(HDRCubeTexture, _super);
  1578. function HDRCubeTexture(url, scene, size, noMipmap) {
  1579. _super.call(this, scene);
  1580. this.coordinatesMode = BABYLON.Texture.CUBIC_MODE;
  1581. this.sphericalPolynomial = null;
  1582. this.name = url;
  1583. this.url = url;
  1584. this._noMipmap = noMipmap;
  1585. this.hasAlpha = false;
  1586. this._size = size;
  1587. if (!url) {
  1588. return;
  1589. }
  1590. this._texture = this._getFromCache(url, noMipmap);
  1591. if (!this._texture) {
  1592. if (!scene.useDelayedTextureLoading) {
  1593. this.loadTexture();
  1594. }
  1595. else {
  1596. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NOTLOADED;
  1597. }
  1598. }
  1599. this.isCube = true;
  1600. this._textureMatrix = BABYLON.Matrix.Identity();
  1601. }
  1602. HDRCubeTexture.prototype.loadTexture = function () {
  1603. var _this = this;
  1604. var callback = function (buffer) {
  1605. var data = BABYLON.Internals.HDRTools.GetCubeMapTextureData(buffer, _this._size);
  1606. _this.sphericalPolynomial = BABYLON.Internals.CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial(data);
  1607. var mapping = [
  1608. "left",
  1609. "down",
  1610. "front",
  1611. "right",
  1612. "up",
  1613. "back"
  1614. ];
  1615. var results = [];
  1616. for (var j = 0; j < 6; j++) {
  1617. var dataFace = data[mapping[j]];
  1618. // TODO. Support Int Textures...
  1619. // // 3 channels of 1 bytes per pixel in bytes.
  1620. // var byteBuffer = new ArrayBuffer(this._size * this._size * 3);
  1621. // var byteArray = new Uint8Array(byteBuffer);
  1622. //
  1623. // /* now convert data from buffer into bytes */
  1624. // for(var i = 0; i < this._size * this._size; i++) {
  1625. // byteArray[(i * 3) + 0] = dataFace[(i * 3) + 0] * 255;
  1626. // byteArray[(i * 3) + 1] = dataFace[(i * 3) + 1] * 255;
  1627. // byteArray[(i * 3) + 2] = dataFace[(i * 3) + 2] * 255;
  1628. // }
  1629. results.push(dataFace);
  1630. }
  1631. return results;
  1632. };
  1633. this._texture = this.getScene().getEngine().createRawCubeTexture(this.url, this.getScene(), this._size, BABYLON.Engine.TEXTUREFORMAT_RGB, BABYLON.Engine.TEXTURETYPE_FLOAT, this._noMipmap, callback);
  1634. };
  1635. HDRCubeTexture.prototype.clone = function () {
  1636. var newTexture = new HDRCubeTexture(this.url, this.getScene(), this._size, this._noMipmap);
  1637. // Base texture
  1638. newTexture.level = this.level;
  1639. newTexture.wrapU = this.wrapU;
  1640. newTexture.wrapV = this.wrapV;
  1641. newTexture.coordinatesIndex = this.coordinatesIndex;
  1642. newTexture.coordinatesMode = this.coordinatesMode;
  1643. return newTexture;
  1644. };
  1645. // Methods
  1646. HDRCubeTexture.prototype.delayLoad = function () {
  1647. if (this.delayLoadState !== BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  1648. return;
  1649. }
  1650. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  1651. this._texture = this._getFromCache(this.url, this._noMipmap);
  1652. if (!this._texture) {
  1653. this.loadTexture();
  1654. }
  1655. };
  1656. HDRCubeTexture.prototype.getReflectionTextureMatrix = function () {
  1657. return this._textureMatrix;
  1658. };
  1659. HDRCubeTexture.Parse = function (parsedTexture, scene, rootUrl) {
  1660. var texture = null;
  1661. if (parsedTexture.name && !parsedTexture.isRenderTarget) {
  1662. texture = new BABYLON.HDRCubeTexture(rootUrl + parsedTexture.name, scene, parsedTexture.size);
  1663. texture.name = parsedTexture.name;
  1664. texture.hasAlpha = parsedTexture.hasAlpha;
  1665. texture.level = parsedTexture.level;
  1666. texture.coordinatesMode = parsedTexture.coordinatesMode;
  1667. }
  1668. return texture;
  1669. };
  1670. HDRCubeTexture.prototype.serialize = function () {
  1671. if (!this.name) {
  1672. return null;
  1673. }
  1674. var serializationObject = {};
  1675. serializationObject.name = this.name;
  1676. serializationObject.hasAlpha = this.hasAlpha;
  1677. serializationObject.isCube = true;
  1678. serializationObject.level = this.level;
  1679. serializationObject.size = this._size;
  1680. serializationObject.coordinatesMode = this.coordinatesMode;
  1681. return serializationObject;
  1682. };
  1683. return HDRCubeTexture;
  1684. })(BABYLON.BaseTexture);
  1685. BABYLON.HDRCubeTexture = HDRCubeTexture;
  1686. })(BABYLON || (BABYLON = {}));
  1687. 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 ALBEDO\nvarying vec2 vAlbedoUV;\nuniform mat4 albedoMatrix;\nuniform vec2 vAlbedoInfos;\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(REFLECTIVITY)\nvarying vec2 vReflectivityUV;\nuniform vec2 vReflectivityInfos;\nuniform mat4 reflectivityMatrix;\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 ALBEDO\n if (vAlbedoInfos.x == 0.)\n {\n vAlbedoUV = vec2(albedoMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vAlbedoUV = vec2(albedoMatrix * 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(REFLECTIVITY)\n if (vReflectivityInfos.x == 0.)\n {\n vReflectivityUV = vec2(reflectivityMatrix * vec4(uv, 1.0, 0.0));\n }\n else\n {\n vReflectivityUV = vec2(reflectivityMatrix * 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}";
  1688. 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 vAlbedoColor;\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 vOverloadedAlbedo;\n uniform vec3 vOverloadedReflectivity;\n uniform vec3 vOverloadedEmissive;\n uniform vec3 vOverloadedReflection;\n uniform vec3 vOverloadedMicroSurface;\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\n uniform vec4 vOverloadedShadowIntensity;\n#endif\n\n#ifdef USESPHERICALFROMREFLECTIONMAP\n uniform vec3 vSphericalX;\n uniform vec3 vSphericalY;\n uniform vec3 vSphericalZ;\n uniform vec3 vSphericalXX;\n uniform vec3 vSphericalYY;\n uniform vec3 vSphericalZZ;\n uniform vec3 vSphericalXY;\n uniform vec3 vSphericalYZ;\n uniform vec3 vSphericalZX;\n\n vec3 EnvironmentIrradiance(vec3 normal)\n {\n // Note: 'normal' is assumed to be normalised (or near normalised)\n // This isn't as critical as it is with other calculations (e.g. specular highlight), but the result will be incorrect nonetheless.\n\n // TODO: switch to optimal implementation\n vec3 result =\n vSphericalX * normal.x +\n vSphericalY * normal.y +\n vSphericalZ * normal.z +\n vSphericalXX * normal.x * normal.x +\n vSphericalYY * normal.y * normal.y +\n vSphericalZZ * normal.z * normal.z +\n vSphericalYZ * normal.y * normal.z +\n vSphericalZX * normal.z * normal.x +\n vSphericalXY * normal.x * normal.y;\n\n return result.rgb;\n }\n#endif\n\n// PBR CUSTOM CONSTANTS\nconst float kPi = 3.1415926535897932384626433832795;\n\n#ifdef PoissonSamplingEnvironment\n const int poissonSphereSamplersCount = 32;\n vec3 poissonSphereSamplers[poissonSphereSamplersCount];\n\n void initSamplers()\n {\n poissonSphereSamplers[0] = vec3( -0.552198926093, 0.801049753814, -0.0322487480415 );\n poissonSphereSamplers[1] = vec3( 0.344874796559, -0.650989584719, 0.283038477033 ); \n poissonSphereSamplers[2] = vec3( -0.0710183703467, 0.163770497767, -0.95022416734 ); \n poissonSphereSamplers[3] = vec3( 0.422221832073, 0.576613638193, 0.519157625948 ); \n poissonSphereSamplers[4] = vec3( -0.561872200916, -0.665581249881, -0.131630473211 ); \n poissonSphereSamplers[5] = vec3( -0.409905973809, 0.0250731510778, 0.674676954809 ); \n poissonSphereSamplers[6] = vec3( 0.206829570551, -0.190199352704, 0.919073906156 ); \n poissonSphereSamplers[7] = vec3( -0.857514664463, 0.0274425010091, -0.475068738967 ); \n poissonSphereSamplers[8] = vec3( -0.816275009951, -0.0432916479141, 0.40394579291 ); \n poissonSphereSamplers[9] = vec3( 0.397976181928, -0.633227519667, -0.617794410447 ); \n poissonSphereSamplers[10] = vec3( -0.181484199014, 0.0155418272003, -0.34675720703 ); \n poissonSphereSamplers[11] = vec3( 0.591734926919, 0.489930882201, -0.51675303188 ); \n poissonSphereSamplers[12] = vec3( -0.264514973057, 0.834248662136, 0.464624235985 ); \n poissonSphereSamplers[13] = vec3( -0.125845223505, 0.812029586099, -0.46213797731 ); \n poissonSphereSamplers[14] = vec3( 0.0345715424639, 0.349983742938, 0.855109899027 ); \n poissonSphereSamplers[15] = vec3( 0.694340492749, -0.281052190209, -0.379600605543 ); \n poissonSphereSamplers[16] = vec3( -0.241055518078, -0.580199280578, 0.435381168431 );\n poissonSphereSamplers[17] = vec3( 0.126313722289, 0.715113642744, 0.124385788055 ); \n poissonSphereSamplers[18] = vec3( 0.752862552387, 0.277075021888, 0.275059597549 );\n poissonSphereSamplers[19] = vec3( -0.400896300918, -0.309374534321, -0.74285782627 ); \n poissonSphereSamplers[20] = vec3( 0.121843331941, -0.00381197918195, 0.322441835258 ); \n poissonSphereSamplers[21] = vec3( 0.741656771351, -0.472083016745, 0.14589173819 ); \n poissonSphereSamplers[22] = vec3( -0.120347565985, -0.397252703556, -0.00153836114051 ); \n poissonSphereSamplers[23] = vec3( -0.846258835203, -0.433763808754, 0.168732209784 ); \n poissonSphereSamplers[24] = vec3( 0.257765618362, -0.546470581239, -0.242234375624 ); \n poissonSphereSamplers[25] = vec3( -0.640343473361, 0.51920903395, 0.549310644325 ); \n poissonSphereSamplers[26] = vec3( -0.894309984621, 0.297394061018, 0.0884583225292 ); \n poissonSphereSamplers[27] = vec3( -0.126241933628, -0.535151016335, -0.440093659672 ); \n poissonSphereSamplers[28] = vec3( -0.158176440297, -0.393125021578, 0.890727226039 ); \n poissonSphereSamplers[29] = vec3( 0.896024272938, 0.203068725821, -0.11198597748 ); \n poissonSphereSamplers[30] = vec3( 0.568671758933, -0.314144243629, 0.509070768816 ); \n poissonSphereSamplers[31] = vec3( 0.289665332178, 0.104356977462, -0.348379247171 );\n }\n\n vec3 environmentSampler(samplerCube cubeMapSampler, vec3 centralDirection, float microsurfaceAverageSlope)\n {\n vec3 result = vec3(0., 0., 0.);\n for(int i = 0; i < poissonSphereSamplersCount; i++)\n {\n vec3 offset = poissonSphereSamplers[i];\n vec3 direction = centralDirection + microsurfaceAverageSlope * offset;\n result += textureCube(cubeMapSampler, direction, 0.).rgb;\n }\n\n result /= 32.0;\n return result;\n }\n\n#endif\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 computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)\n{\n float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;\n\n float reflectivityLuminance = getLuminance(reflectivityColor);\n float reflectivityLuma = sqrt(reflectivityLuminance);\n microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;\n\n return microSurface;\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 uniform vec4 vReflectivityColor;\n uniform 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 ALBEDO\nvarying vec2 vAlbedoUV;\nuniform sampler2D albedoSampler;\nuniform vec2 vAlbedoInfos;\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(REFLECTIVITY)\nvarying vec2 vReflectivityUV;\nuniform vec2 vReflectivityInfos;\nuniform sampler2D reflectivitySampler;\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)\nuniform vec2 depthValues;\n\nfloat computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)\n{\n\tvec3 directionToLight = vPositionW - lightPosition;\n\tfloat depth = length(directionToLight);\n\tdepth = clamp(depth, 0., 1.0);\n\n\tdirectionToLight = normalize(directionToLight);\n\tdirectionToLight.y = - directionToLight.y;\n\n\tfloat 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\tvec3 directionToLight = vPositionW - lightPosition;\n\tfloat depth = length(directionToLight);\n\n\tdepth = clamp(depth, 0., 1.0);\n\tfloat diskScale = 2.0 / mapSize;\n\n\tdirectionToLight = normalize(directionToLight);\n\tdirectionToLight.y = -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 #ifdef PoissonSamplingEnvironment\n initSamplers();\n #endif\n\n // Clip plane\n #ifdef CLIPPLANE\n if (fClipDistance > 0.0)\n discard;\n #endif\n\n vec3 viewDirectionW = normalize(vEyePosition - vPositionW);\n\n // Albedo\n vec4 surfaceAlbedo = vec4(1., 1., 1., 1.);\n vec3 surfaceAlbedoContribution = vAlbedoColor.rgb;\n \n // Alpha\n float alpha = vAlbedoColor.a;\n\n #ifdef ALBEDO\n surfaceAlbedo = texture2D(albedoSampler, vAlbedoUV);\n surfaceAlbedo = vec4(toLinearSpace(surfaceAlbedo.rgb), surfaceAlbedo.a);\n\n #ifdef ALPHATEST\n if (surfaceAlbedo.a < 0.4)\n discard;\n #endif\n\n #ifdef ALPHAFROMALBEDO\n alpha *= surfaceAlbedo.a;\n #endif\n\n surfaceAlbedo.rgb *= vAlbedoInfos.y;\n #endif\n\n #ifdef VERTEXCOLOR\n surfaceAlbedo.rgb *= vColor.rgb;\n #endif\n\n #ifdef OVERLOADEDVALUES\n surfaceAlbedo.rgb = mix(surfaceAlbedo.rgb, vOverloadedAlbedo, 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 ambientColor = vec3(1., 1., 1.);\n\n #ifdef AMBIENT\n ambientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;\n \n #ifdef OVERLOADEDVALUES\n ambientColor.rgb = mix(ambientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);\n #endif\n #endif\n\n // Specular map\n float microSurface = vReflectivityColor.a;\n vec3 surfaceReflectivityColor = vReflectivityColor.rgb;\n \n #ifdef OVERLOADEDVALUES\n surfaceReflectivityColor.rgb = mix(surfaceReflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);\n #endif\n\n #ifdef REFLECTIVITY\n surfaceReflectivityColor = texture2D(reflectivitySampler, vReflectivityUV).rgb;\n surfaceReflectivityColor = toLinearSpace(surfaceReflectivityColor);\n\n #ifdef OVERLOADEDVALUES\n surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);\n #endif\n\n #ifdef MICROSURFACEFROMREFLECTIVITYMAP\n microSurface = reflectivityMapColor.a;\n #else\n microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);\n #endif\n #endif\n\n #ifdef OVERLOADEDVALUES\n microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);\n #endif\n\n // Apply Energy Conservation taking in account the environment level only if the environment is present.\n float reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);\n surfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;\n\n // Compute Specular Fresnel + Reflectance.\n float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));\n\n // Adapt microSurface.\n microSurface = clamp(microSurface, 0., 1.) * 0.98;\n\n // Call rough to not conflict with previous one.\n float rough = clamp(1. - microSurface, 0.000001, 1.0);\n\n // Lighting\n vec3 lightDiffuseContribution = vec3(0., 0., 0.);\n \n#ifdef OVERLOADEDSHADOWVALUES\n vec3 shadowedOnlyLightDiffuseContribution = vec3(1., 1., 1.);\n#endif\n\n#ifdef SPECULARTERM\n vec3 lightSpecularContribution= vec3(0., 0., 0.);\n#endif\n float notShadowLevel = 1.; // 1 - shadowLevel\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 notShadowLevel = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);\n#else\n#ifdef SHADOWPCF0\n#if defined(POINTLIGHT0)\n notShadowLevel = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\n#else\n notShadowLevel = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\n#endif\n#else\n#if defined(POINTLIGHT0)\n notShadowLevel = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\n#else\n notShadowLevel = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\n#endif\n#endif\n#endif\n#else\n notShadowLevel = 1.;\n#endif\n lightDiffuseContribution += info.diffuse * notShadowLevel;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution *= notShadowLevel;\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution += info.specular * notShadowLevel;\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 notShadowLevel = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);\n#else\n#ifdef SHADOWPCF1\n#if defined(POINTLIGHT1)\n notShadowLevel = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\n#else\n notShadowLevel = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\n#endif\n#else\n#if defined(POINTLIGHT1)\n notShadowLevel = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\n#else\n notShadowLevel = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\n#endif\n#endif\n#endif\n#else\n notShadowLevel = 1.;\n#endif\n\n lightDiffuseContribution += info.diffuse * notShadowLevel;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution *= notShadowLevel;\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution += info.specular * notShadowLevel;\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 notShadowLevel = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);\n#else\n#ifdef SHADOWPCF2\n#if defined(POINTLIGHT2)\n notShadowLevel = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\n#else\n notShadowLevel = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\n#endif\n#else\n#if defined(POINTLIGHT2)\n notShadowLevel = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\n#else\n notShadowLevel = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\n#endif\n#endif\t\n#endif\t\n#else\n notShadowLevel = 1.;\n#endif\n\n lightDiffuseContribution += info.diffuse * notShadowLevel;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution *= notShadowLevel;\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution += info.specular * notShadowLevel;\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 notShadowLevel = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);\n#else\n#ifdef SHADOWPCF3\n#if defined(POINTLIGHT3)\n notShadowLevel = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\n#else\n notShadowLevel = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\n#endif\n#else\n#if defined(POINTLIGHT3)\n notShadowLevel = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\n#else\n notShadowLevel = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\n#endif\n#endif\t\n#endif\t\n#else\n notShadowLevel = 1.;\n#endif\n\n lightDiffuseContribution += info.diffuse * notShadowLevel;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution *= notShadowLevel;\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution += info.specular * notShadowLevel;\n#endif\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution *= vLightingIntensity.w;\n#endif\n\n// Reflection\nvec3 environmentRadiance = vReflectionColor.rgb;\nvec3 environmentIrradiance = vReflectionColor.rgb;\n\n#ifdef REFLECTION\n vec3 vReflectionUVW = computeReflectionCoords(vec4(vPositionW, 1.0), normalW);\n\n #ifdef REFLECTIONMAP_3D\n // Go mat -> blurry reflexion according to microSurface\n float bias = 20. * (1.0 - microSurface);\n\n environmentRadiance = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;\n \n #ifdef PoissonSamplingEnvironment\n float alphaG = convertRoughnessToAverageSlope(rough);\n environmentRadiance = environmentSampler(reflectionCubeSampler, vReflectionUVW, alphaG) * vReflectionInfos.x;\n #endif\n\n #ifdef USESPHERICALFROMREFLECTIONMAP\n vec3 normalEnvironmentSpace = (reflectionMatrix * vec4(normalW, 1)).xyz;\n environmentIrradiance = EnvironmentIrradiance(normalEnvironmentSpace);\n #else\n environmentRadiance = toLinearSpace(environmentRadiance.rgb);\n \n environmentIrradiance = textureCube(reflectionCubeSampler, normalW, 20.).rgb * vReflectionInfos.x;\n environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);\n environmentIrradiance *= 0.2; // Hack in case of no hdr cube map use for environment.\n #endif\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 environmentRadiance = texture2D(reflection2DSampler, coords).rgb * vReflectionInfos.x;\n environmentRadiance = toLinearSpace(environmentRadiance.rgb);\n\n environmentIrradiance = texture2D(reflection2DSampler, coords, 20.).rgb * vReflectionInfos.x;\n environmentIrradiance = toLinearSpace(environmentIrradiance.rgb);\n #endif\n#endif\n\n#ifdef OVERLOADEDVALUES\n environmentIrradiance = mix(environmentIrradiance, vOverloadedReflection, vOverloadedMicroSurface.z);\n environmentRadiance = mix(environmentRadiance, vOverloadedReflection, vOverloadedMicroSurface.z);\n#endif\n\nenvironmentRadiance *= vLightingIntensity.z;\nenvironmentIrradiance *= vLightingIntensity.z;\n\n// Compute reflection specular fresnel\nvec3 specularEnvironmentR0 = surfaceReflectivityColor.rgb;\nvec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0);\nvec3 specularEnvironmentReflectance = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), specularEnvironmentR0, specularEnvironmentR90, sqrt(microSurface));\nenvironmentRadiance *= specularEnvironmentReflectance;\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 surfaceEmissiveColor = vEmissiveColor;\n#ifdef EMISSIVE\n vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;\n surfaceEmissiveColor = toLinearSpace(emissiveColorTex.rgb) * surfaceEmissiveColor * vEmissiveInfos.y;\n#endif\n\n#ifdef OVERLOADEDVALUES\n surfaceEmissiveColor = mix(surfaceEmissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);\n#endif\n\n#ifdef EMISSIVEFRESNEL\n float emissiveFresnelTerm = computeFresnelTerm(viewDirectionW, normalW, emissiveRightColor.a, emissiveLeftColor.a);\n\n surfaceEmissiveColor *= emissiveLeftColor.rgb * (1.0 - emissiveFresnelTerm) + emissiveFresnelTerm * emissiveRightColor.rgb;\n#endif\n\n// Composition\n#ifdef EMISSIVEASILLUMINATION\n vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;\n \n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;\n #endif\n#else\n #ifdef LINKEMISSIVEWITHALBEDO\n vec3 finalDiffuse = max((lightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;\n\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution = max((shadowedOnlyLightDiffuseContribution + surfaceEmissiveColor) * surfaceAlbedoContribution + vAmbientColor, 0.0) * surfaceAlbedo.rgb;\n #endif\n #else\n vec3 finalDiffuse = max(lightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;\n\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution = max(shadowedOnlyLightDiffuseContribution * surfaceAlbedoContribution + surfaceEmissiveColor + vAmbientColor, 0.0) * surfaceAlbedo.rgb;\n #endif\n #endif\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\n finalDiffuse = mix(finalDiffuse, shadowedOnlyLightDiffuseContribution, (1.0 - vOverloadedShadowIntensity.y));\n#endif\n\n#ifdef SPECULARTERM\n vec3 finalSpecular = lightSpecularContribution * surfaceReflectivityColor;\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 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y, alpha);\n#else\n vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance, alpha);\n#endif\n\n#ifdef LIGHTMAP\n vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV).rgb * vLightmapInfos.y;\n\n #ifdef USELIGHTMAPASSHADOWMAP\n finalColor.rgb *= lightmapColor;\n #else\n finalColor.rgb += lightmapColor;\n #endif\n#endif\n\n#ifdef FOG\n float fog = CalcFogFactor();\n finalColor.rgb = fog * finalColor.rgb + (1.0 - fog) * vFogColor;\n#endif\n\n finalColor = max(finalColor, 0.0);\n\n#ifdef CAMERATONEMAP\n finalColor.rgb = toneMaps(finalColor.rgb);\n#endif\n\n finalColor.rgb = toGammaSpace(finalColor.rgb);\n\n#ifdef CAMERACONTRAST\n finalColor = contrasts(finalColor);\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(surfaceAlbedo.rgb, 1.0);\n\n // Specular color.\n // gl_FragColor = vec4(surfaceReflectivityColor.rgb, 1.0);\n\n // MicroSurface color.\n // gl_FragColor = vec4(microSurface, microSurface, microSurface, 1.0);\n\n // Specular Map\n // gl_FragColor = vec4(reflectivityMapColor.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 = finalColor;\n}";
  1689. 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 ALBEDO\nvarying vec2 vAlbedoUV;\nuniform mat4 albedoMatrix;\nuniform vec2 vAlbedoInfos;\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(REFLECTIVITY)\nvarying vec2 vReflectivityUV;\nuniform vec2 vReflectivityInfos;\nuniform mat4 reflectivityMatrix;\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 ALBEDO\n\tif (vAlbedoInfos.x == 0.)\n\t{\n\t\tvAlbedoUV = vec2(albedoMatrix * vec4(uv, 1.0, 0.0));\n\t}\n\telse\n\t{\n\t\tvAlbedoUV = vec2(albedoMatrix * 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(REFLECTIVITY)\n\tif (vReflectivityInfos.x == 0.)\n\t{\n\t\tvReflectivityUV = vec2(reflectivityMatrix * vec4(uv, 1.0, 0.0));\n\t}\n\telse\n\t{\n\t\tvReflectivityUV = vec2(reflectivityMatrix * 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}";
  1690. 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 vAlbedoColor;\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 vOverloadedAlbedo;\nuniform vec3 vOverloadedReflectivity;\nuniform vec3 vOverloadedEmissive;\nuniform vec3 vOverloadedReflection;\nuniform vec3 vOverloadedMicroSurface;\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 computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)\n{\n if (microSurface == 0.)\n {\n float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;\n\n float reflectivityLuminance = getLuminance(reflectivityColor);\n float reflectivityLuma = sqrt(reflectivityLuminance);\n microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;\n }\n return microSurface;\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\nuniform vec4 vReflectivityColor;\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 ALBEDO\nvarying vec2 vAlbedoUV;\nuniform sampler2D albedoSampler;\nuniform vec2 vAlbedoInfos;\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(REFLECTIVITY)\nvarying vec2 vReflectivityUV;\nuniform vec2 vReflectivityInfos;\nuniform sampler2D reflectivitySampler;\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 = vAlbedoColor.rgb;\n \n // Alpha\n float alpha = vAlbedoColor.a;\n\n#ifdef ALBEDO\n baseColor = texture2D(diffuseSampler, vAlbedoUV);\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 ALPHAFROMALBEDO\n alpha *= baseColor.a;\n#endif\n\n baseColor.rgb *= vAlbedoInfos.y;\n#endif\n\n#ifdef VERTEXCOLOR\n baseColor.rgb *= vColor.rgb;\n#endif\n\n#ifdef OVERLOADEDVALUES\n baseColor.rgb = mix(baseColor.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);\n albedoColor.rgb = mix(albedoColor.rgb, vOverloadedAlbedo, 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 // Reflectivity map\n float microSurface = vReflectivityColor.a;\n vec3 reflectivityColor = vReflectivityColor.rgb;\n\n #ifdef OVERLOADEDVALUES\n reflectivityColor.rgb = mix(reflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);\n #endif\n\n #ifdef REFLECTIVITY\n vec4 reflectivityMapColor = texture2D(reflectivitySampler, vReflectivityUV);\n reflectivityColor = toLinearSpace(reflectivityMapColor.rgb);\n\n #ifdef OVERLOADEDVALUES\n reflectivityColor.rgb = mix(reflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);\n #endif\n\n #ifdef MICROSURFACEFROMREFLECTIVITYMAP\n microSurface = reflectivityMapColor.a;\n #else\n microSurface = computeDefaultMicroSurface(microSurface, reflectivityColor);\n #endif\n #endif\n\n #ifdef OVERLOADEDVALUES\n microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);\n #endif\n\n // Apply Energy Conservation taking in account the environment level only if the environment is present.\n float reflectance = max(max(reflectivityColor.r, reflectivityColor.g), reflectivityColor.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 microSurface.\n microSurface = clamp(microSurface, 0., 1.) * 0.98;\n\n // Call rough to not conflict with previous one.\n float rough = clamp(1. - microSurface, 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 reflectivity fresnel\nvec3 reflectivityEnvironmentR0 = reflectivityColor.rgb;\nvec3 reflectivityEnvironmentR90 = vec3(1.0, 1.0, 1.0);\nvec3 reflectivityEnvironmentReflectanceViewer = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), reflectivityEnvironmentR0, reflectivityEnvironmentR90, sqrt(microSurface));\nreflectionColor *= reflectivityEnvironmentReflectanceViewer;\n\n#ifdef OVERLOADEDVALUES\n ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedMicroSurface.z);\n reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedMicroSurface.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 * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;\n\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n#else\n #ifdef LINKEMISSIVEWITHALBEDO\n vec3 finalDiffuse = max((diffuseBase + emissiveColor) * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n #else\n vec3 finalDiffuse = max(diffuseBase * albedoColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * albedoColor + 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 * reflectivityColor * vLightingIntensity.w;\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}";