babylon.pbrMaterial.js 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
  4. switch (arguments.length) {
  5. case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
  6. case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
  7. case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
  8. }
  9. };
  10. var BABYLON;
  11. (function (BABYLON) {
  12. var maxSimultaneousLights = 4;
  13. var PBRMaterialDefines = (function (_super) {
  14. __extends(PBRMaterialDefines, _super);
  15. function PBRMaterialDefines() {
  16. _super.call(this);
  17. this.ALBEDO = false;
  18. this.AMBIENT = false;
  19. this.OPACITY = false;
  20. this.OPACITYRGB = false;
  21. this.REFLECTION = false;
  22. this.EMISSIVE = false;
  23. this.REFLECTIVITY = false;
  24. this.BUMP = false;
  25. this.SPECULAROVERALPHA = false;
  26. this.CLIPPLANE = false;
  27. this.ALPHATEST = false;
  28. this.ALPHAFROMALBEDO = false;
  29. this.POINTSIZE = false;
  30. this.FOG = false;
  31. this.LIGHT0 = false;
  32. this.LIGHT1 = false;
  33. this.LIGHT2 = false;
  34. this.LIGHT3 = false;
  35. this.SPOTLIGHT0 = false;
  36. this.SPOTLIGHT1 = false;
  37. this.SPOTLIGHT2 = false;
  38. this.SPOTLIGHT3 = false;
  39. this.HEMILIGHT0 = false;
  40. this.HEMILIGHT1 = false;
  41. this.HEMILIGHT2 = false;
  42. this.HEMILIGHT3 = false;
  43. this.POINTLIGHT0 = false;
  44. this.POINTLIGHT1 = false;
  45. this.POINTLIGHT2 = false;
  46. this.POINTLIGHT3 = false;
  47. this.DIRLIGHT0 = false;
  48. this.DIRLIGHT1 = false;
  49. this.DIRLIGHT2 = false;
  50. this.DIRLIGHT3 = false;
  51. this.SPECULARTERM = false;
  52. this.SHADOW0 = false;
  53. this.SHADOW1 = false;
  54. this.SHADOW2 = false;
  55. this.SHADOW3 = false;
  56. this.SHADOWS = false;
  57. this.SHADOWVSM0 = false;
  58. this.SHADOWVSM1 = false;
  59. this.SHADOWVSM2 = false;
  60. this.SHADOWVSM3 = false;
  61. this.SHADOWPCF0 = false;
  62. this.SHADOWPCF1 = false;
  63. this.SHADOWPCF2 = false;
  64. this.SHADOWPCF3 = false;
  65. this.OPACITYFRESNEL = false;
  66. this.EMISSIVEFRESNEL = false;
  67. this.FRESNEL = false;
  68. this.NORMAL = false;
  69. this.UV1 = false;
  70. this.UV2 = false;
  71. this.VERTEXCOLOR = false;
  72. this.VERTEXALPHA = false;
  73. this.NUM_BONE_INFLUENCERS = 0;
  74. this.BonesPerMesh = 0;
  75. this.INSTANCES = false;
  76. this.MICROSURFACEFROMREFLECTIVITYMAP = false;
  77. this.MICROSURFACEAUTOMATIC = false;
  78. this.EMISSIVEASILLUMINATION = false;
  79. this.LINKEMISSIVEWITHALBEDO = false;
  80. this.LIGHTMAP = false;
  81. this.USELIGHTMAPASSHADOWMAP = false;
  82. this.REFLECTIONMAP_3D = false;
  83. this.REFLECTIONMAP_SPHERICAL = false;
  84. this.REFLECTIONMAP_PLANAR = false;
  85. this.REFLECTIONMAP_CUBIC = false;
  86. this.REFLECTIONMAP_PROJECTION = false;
  87. this.REFLECTIONMAP_SKYBOX = false;
  88. this.REFLECTIONMAP_EXPLICIT = false;
  89. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  90. this.INVERTCUBICMAP = false;
  91. this.LOGARITHMICDEPTH = false;
  92. this.CAMERATONEMAP = false;
  93. this.CAMERACONTRAST = false;
  94. this.OVERLOADEDVALUES = false;
  95. this.OVERLOADEDSHADOWVALUES = false;
  96. this.USESPHERICALFROMREFLECTIONMAP = false;
  97. this.REFRACTION = false;
  98. this.REFRACTIONMAP_3D = false;
  99. this.LINKREFRACTIONTOTRANSPARENCY = false;
  100. this.REFRACTIONMAPINLINEARSPACE = false;
  101. this.LODBASEDMICROSFURACE = false;
  102. this.USEPHYSICALLIGHTFALLOFF = false;
  103. this.RADIANCEOVERALPHA = false;
  104. this._keys = Object.keys(this);
  105. }
  106. return PBRMaterialDefines;
  107. })(BABYLON.MaterialDefines);
  108. var PBRMaterial = (function (_super) {
  109. __extends(PBRMaterial, _super);
  110. function PBRMaterial(name, scene) {
  111. var _this = this;
  112. _super.call(this, name, scene);
  113. this.directIntensity = 1.0;
  114. this.emissiveIntensity = 1.0;
  115. this.environmentIntensity = 1.0;
  116. this.specularIntensity = 1.0;
  117. this._lightingInfos = new BABYLON.Vector4(this.directIntensity, this.emissiveIntensity, this.environmentIntensity, this.specularIntensity);
  118. this.overloadedShadowIntensity = 1.0;
  119. this.overloadedShadeIntensity = 1.0;
  120. this._overloadedShadowInfos = new BABYLON.Vector4(this.overloadedShadowIntensity, this.overloadedShadeIntensity, 0.0, 0.0);
  121. this.cameraExposure = 1.0;
  122. this.cameraContrast = 1.0;
  123. this._cameraInfos = new BABYLON.Vector4(1.0, 1.0, 0.0, 0.0);
  124. this._microsurfaceTextureLods = new BABYLON.Vector2(0.0, 0.0);
  125. this.overloadedAmbientIntensity = 0.0;
  126. this.overloadedAlbedoIntensity = 0.0;
  127. this.overloadedReflectivityIntensity = 0.0;
  128. this.overloadedEmissiveIntensity = 0.0;
  129. this._overloadedIntensity = new BABYLON.Vector4(this.overloadedAmbientIntensity, this.overloadedAlbedoIntensity, this.overloadedReflectivityIntensity, this.overloadedEmissiveIntensity);
  130. this.overloadedAmbient = BABYLON.Color3.White();
  131. this.overloadedAlbedo = BABYLON.Color3.White();
  132. this.overloadedReflectivity = BABYLON.Color3.White();
  133. this.overloadedEmissive = BABYLON.Color3.White();
  134. this.overloadedReflection = BABYLON.Color3.White();
  135. this.overloadedMicroSurface = 0.0;
  136. this.overloadedMicroSurfaceIntensity = 0.0;
  137. this.overloadedReflectionIntensity = 0.0;
  138. this._overloadedMicroSurface = new BABYLON.Vector3(this.overloadedMicroSurface, this.overloadedMicroSurfaceIntensity, this.overloadedReflectionIntensity);
  139. this.disableBumpMap = false;
  140. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  141. this.albedoColor = new BABYLON.Color3(1, 1, 1);
  142. this.reflectivityColor = new BABYLON.Color3(1, 1, 1);
  143. this.reflectionColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  144. this.emissiveColor = new BABYLON.Color3(0, 0, 0);
  145. this.microSurface = 0.5;
  146. this.indexOfRefraction = 0.66;
  147. this.invertRefractionY = false;
  148. this.linkRefractionWithTransparency = false;
  149. this.linkEmissiveWithAlbedo = false;
  150. this.useLightmapAsShadowmap = false;
  151. this.useEmissiveAsIllumination = false;
  152. this.useAlphaFromAlbedoTexture = false;
  153. this.useSpecularOverAlpha = true;
  154. this.useMicroSurfaceFromReflectivityMapAlpha = false;
  155. this.useAutoMicroSurfaceFromReflectivityMap = false;
  156. this.useScalarInLinearSpace = false;
  157. this.usePhysicalLightFalloff = true;
  158. this.useRadianceOverAlpha = true;
  159. this.disableLighting = false;
  160. this._renderTargets = new BABYLON.SmartArray(16);
  161. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  162. this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  163. this._tempColor = new BABYLON.Color3();
  164. this._defines = new PBRMaterialDefines();
  165. this._cachedDefines = new PBRMaterialDefines();
  166. this._myScene = null;
  167. this._myShadowGenerator = null;
  168. this._cachedDefines.BonesPerMesh = -1;
  169. this.getRenderTargetTextures = function () {
  170. _this._renderTargets.reset();
  171. if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
  172. _this._renderTargets.push(_this.reflectionTexture);
  173. }
  174. if (_this.refractionTexture && _this.refractionTexture.isRenderTarget) {
  175. _this._renderTargets.push(_this.refractionTexture);
  176. }
  177. return _this._renderTargets;
  178. };
  179. }
  180. Object.defineProperty(PBRMaterial.prototype, "useLogarithmicDepth", {
  181. get: function () {
  182. return this._useLogarithmicDepth;
  183. },
  184. set: function (value) {
  185. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  186. },
  187. enumerable: true,
  188. configurable: true
  189. });
  190. PBRMaterial.prototype.needAlphaBlending = function () {
  191. if (this.linkRefractionWithTransparency) {
  192. return false;
  193. }
  194. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromAlbedoTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  195. };
  196. PBRMaterial.prototype.needAlphaTesting = function () {
  197. if (this.linkRefractionWithTransparency) {
  198. return false;
  199. }
  200. return this.albedoTexture != null && this.albedoTexture.hasAlpha;
  201. };
  202. PBRMaterial.prototype._shouldUseAlphaFromAlbedoTexture = function () {
  203. return this.albedoTexture != null && this.albedoTexture.hasAlpha && this.useAlphaFromAlbedoTexture;
  204. };
  205. PBRMaterial.prototype.getAlphaTestTexture = function () {
  206. return this.albedoTexture;
  207. };
  208. PBRMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  209. if (!mesh) {
  210. return true;
  211. }
  212. if (this._defines.INSTANCES !== useInstances) {
  213. return false;
  214. }
  215. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  216. return true;
  217. }
  218. return false;
  219. };
  220. PBRMaterial.prototype.convertColorToLinearSpaceToRef = function (color, ref) {
  221. PBRMaterial.convertColorToLinearSpaceToRef(color, ref, this.useScalarInLinearSpace);
  222. };
  223. PBRMaterial.convertColorToLinearSpaceToRef = function (color, ref, useScalarInLinear) {
  224. if (!useScalarInLinear) {
  225. color.toLinearSpaceToRef(ref);
  226. }
  227. else {
  228. ref.r = color.r;
  229. ref.g = color.g;
  230. ref.b = color.b;
  231. }
  232. };
  233. PBRMaterial.BindLights = function (scene, mesh, effect, defines, useScalarInLinearSpace) {
  234. var lightIndex = 0;
  235. var depthValuesAlreadySet = false;
  236. for (var index = 0; index < scene.lights.length; index++) {
  237. var light = scene.lights[index];
  238. if (!light.isEnabled()) {
  239. continue;
  240. }
  241. if (!light.canAffectMesh(mesh)) {
  242. continue;
  243. }
  244. this._lightRadiuses[lightIndex] = light.radius;
  245. BABYLON.MaterialHelper.BindLightProperties(light, effect, lightIndex);
  246. // GAMMA CORRECTION.
  247. this.convertColorToLinearSpaceToRef(light.diffuse, PBRMaterial._scaledAlbedo, useScalarInLinearSpace);
  248. PBRMaterial._scaledAlbedo.scaleToRef(light.intensity, PBRMaterial._scaledAlbedo);
  249. effect.setColor4("vLightDiffuse" + lightIndex, PBRMaterial._scaledAlbedo, light.range);
  250. if (defines["SPECULARTERM"]) {
  251. this.convertColorToLinearSpaceToRef(light.specular, PBRMaterial._scaledReflectivity, useScalarInLinearSpace);
  252. PBRMaterial._scaledReflectivity.scaleToRef(light.intensity, PBRMaterial._scaledReflectivity);
  253. effect.setColor3("vLightSpecular" + lightIndex, PBRMaterial._scaledReflectivity);
  254. }
  255. // Shadows
  256. if (scene.shadowsEnabled) {
  257. depthValuesAlreadySet = BABYLON.MaterialHelper.BindLightShadow(light, scene, mesh, lightIndex, effect, depthValuesAlreadySet);
  258. }
  259. lightIndex++;
  260. if (lightIndex === maxSimultaneousLights)
  261. break;
  262. }
  263. effect.setFloat4("vLightRadiuses", this._lightRadiuses[0], this._lightRadiuses[1], this._lightRadiuses[2], this._lightRadiuses[3]);
  264. };
  265. PBRMaterial.prototype.isReady = function (mesh, useInstances) {
  266. if (this.checkReadyOnlyOnce) {
  267. if (this._wasPreviouslyReady) {
  268. return true;
  269. }
  270. }
  271. var scene = this.getScene();
  272. if (!this.checkReadyOnEveryCall) {
  273. if (this._renderId === scene.getRenderId()) {
  274. if (this._checkCache(scene, mesh, useInstances)) {
  275. return true;
  276. }
  277. }
  278. }
  279. var engine = scene.getEngine();
  280. var needNormals = false;
  281. var needUVs = false;
  282. this._defines.reset();
  283. if (scene.texturesEnabled) {
  284. // Textures
  285. if (scene.texturesEnabled) {
  286. if (scene.getEngine().getCaps().textureLOD) {
  287. this._defines.LODBASEDMICROSFURACE = true;
  288. }
  289. if (this.albedoTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  290. if (!this.albedoTexture.isReady()) {
  291. return false;
  292. }
  293. else {
  294. needUVs = true;
  295. this._defines.ALBEDO = true;
  296. }
  297. }
  298. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  299. if (!this.ambientTexture.isReady()) {
  300. return false;
  301. }
  302. else {
  303. needUVs = true;
  304. this._defines.AMBIENT = true;
  305. }
  306. }
  307. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  308. if (!this.opacityTexture.isReady()) {
  309. return false;
  310. }
  311. else {
  312. needUVs = true;
  313. this._defines.OPACITY = true;
  314. if (this.opacityTexture.getAlphaFromRGB) {
  315. this._defines.OPACITYRGB = true;
  316. }
  317. }
  318. }
  319. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  320. if (!this.reflectionTexture.isReady()) {
  321. return false;
  322. }
  323. else {
  324. needNormals = true;
  325. this._defines.REFLECTION = true;
  326. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  327. this._defines.INVERTCUBICMAP = true;
  328. }
  329. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  330. switch (this.reflectionTexture.coordinatesMode) {
  331. case BABYLON.Texture.CUBIC_MODE:
  332. case BABYLON.Texture.INVCUBIC_MODE:
  333. this._defines.REFLECTIONMAP_CUBIC = true;
  334. break;
  335. case BABYLON.Texture.EXPLICIT_MODE:
  336. this._defines.REFLECTIONMAP_EXPLICIT = true;
  337. break;
  338. case BABYLON.Texture.PLANAR_MODE:
  339. this._defines.REFLECTIONMAP_PLANAR = true;
  340. break;
  341. case BABYLON.Texture.PROJECTION_MODE:
  342. this._defines.REFLECTIONMAP_PROJECTION = true;
  343. break;
  344. case BABYLON.Texture.SKYBOX_MODE:
  345. this._defines.REFLECTIONMAP_SKYBOX = true;
  346. break;
  347. case BABYLON.Texture.SPHERICAL_MODE:
  348. this._defines.REFLECTIONMAP_SPHERICAL = true;
  349. break;
  350. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  351. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  352. break;
  353. }
  354. if (this.reflectionTexture instanceof BABYLON.HDRCubeTexture && this.reflectionTexture) {
  355. this._defines.USESPHERICALFROMREFLECTIONMAP = true;
  356. needNormals = true;
  357. }
  358. }
  359. }
  360. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  361. if (!this.lightmapTexture.isReady()) {
  362. return false;
  363. }
  364. else {
  365. needUVs = true;
  366. this._defines.LIGHTMAP = true;
  367. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  368. }
  369. }
  370. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  371. if (!this.emissiveTexture.isReady()) {
  372. return false;
  373. }
  374. else {
  375. needUVs = true;
  376. this._defines.EMISSIVE = true;
  377. }
  378. }
  379. if (this.reflectivityTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  380. if (!this.reflectivityTexture.isReady()) {
  381. return false;
  382. }
  383. else {
  384. needUVs = true;
  385. this._defines.REFLECTIVITY = true;
  386. this._defines.MICROSURFACEFROMREFLECTIVITYMAP = this.useMicroSurfaceFromReflectivityMapAlpha;
  387. this._defines.MICROSURFACEAUTOMATIC = this.useAutoMicroSurfaceFromReflectivityMap;
  388. }
  389. }
  390. }
  391. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  392. if (!this.bumpTexture.isReady()) {
  393. return false;
  394. }
  395. else {
  396. needUVs = true;
  397. this._defines.BUMP = true;
  398. }
  399. }
  400. if (this.refractionTexture && BABYLON.StandardMaterial.RefractionTextureEnabled) {
  401. if (!this.refractionTexture.isReady()) {
  402. return false;
  403. }
  404. else {
  405. needUVs = true;
  406. this._defines.REFRACTION = true;
  407. this._defines.REFRACTIONMAP_3D = this.refractionTexture.isCube;
  408. if (this.linkRefractionWithTransparency) {
  409. this._defines.LINKREFRACTIONTOTRANSPARENCY = true;
  410. }
  411. if (this.refractionTexture instanceof BABYLON.HDRCubeTexture) {
  412. this._defines.REFRACTIONMAPINLINEARSPACE = true;
  413. }
  414. }
  415. }
  416. }
  417. // Effect
  418. if (scene.clipPlane) {
  419. this._defines.CLIPPLANE = true;
  420. }
  421. if (engine.getAlphaTesting()) {
  422. this._defines.ALPHATEST = true;
  423. }
  424. if (this._shouldUseAlphaFromAlbedoTexture()) {
  425. this._defines.ALPHAFROMALBEDO = true;
  426. }
  427. if (this.useEmissiveAsIllumination) {
  428. this._defines.EMISSIVEASILLUMINATION = true;
  429. }
  430. if (this.linkEmissiveWithAlbedo) {
  431. this._defines.LINKEMISSIVEWITHALBEDO = true;
  432. }
  433. if (this.useLogarithmicDepth) {
  434. this._defines.LOGARITHMICDEPTH = true;
  435. }
  436. if (this.cameraContrast != 1) {
  437. this._defines.CAMERACONTRAST = true;
  438. }
  439. if (this.cameraExposure != 1) {
  440. this._defines.CAMERATONEMAP = true;
  441. }
  442. if (this.overloadedShadeIntensity != 1 ||
  443. this.overloadedShadowIntensity != 1) {
  444. this._defines.OVERLOADEDSHADOWVALUES = true;
  445. }
  446. if (this.overloadedMicroSurfaceIntensity > 0 ||
  447. this.overloadedEmissiveIntensity > 0 ||
  448. this.overloadedReflectivityIntensity > 0 ||
  449. this.overloadedAlbedoIntensity > 0 ||
  450. this.overloadedAmbientIntensity > 0 ||
  451. this.overloadedReflectionIntensity > 0) {
  452. this._defines.OVERLOADEDVALUES = true;
  453. }
  454. // Point size
  455. if (this.pointsCloud || scene.forcePointsCloud) {
  456. this._defines.POINTSIZE = true;
  457. }
  458. // Fog
  459. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  460. this._defines.FOG = true;
  461. }
  462. if (scene.lightsEnabled && !this.disableLighting) {
  463. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines) || needNormals;
  464. }
  465. if (BABYLON.StandardMaterial.FresnelEnabled) {
  466. // Fresnel
  467. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  468. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  469. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  470. this._defines.OPACITYFRESNEL = true;
  471. }
  472. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  473. this._defines.EMISSIVEFRESNEL = true;
  474. }
  475. needNormals = true;
  476. this._defines.FRESNEL = true;
  477. }
  478. }
  479. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  480. this._defines.SPECULAROVERALPHA = true;
  481. }
  482. if (this.usePhysicalLightFalloff) {
  483. this._defines.USEPHYSICALLIGHTFALLOFF = true;
  484. }
  485. if (this.useRadianceOverAlpha) {
  486. this._defines.RADIANCEOVERALPHA = true;
  487. }
  488. // Attribs
  489. if (mesh) {
  490. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  491. this._defines.NORMAL = true;
  492. }
  493. if (needUVs) {
  494. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  495. this._defines.UV1 = true;
  496. }
  497. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  498. this._defines.UV2 = true;
  499. }
  500. }
  501. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  502. this._defines.VERTEXCOLOR = true;
  503. if (mesh.hasVertexAlpha) {
  504. this._defines.VERTEXALPHA = true;
  505. }
  506. }
  507. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  508. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  509. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  510. }
  511. // Instances
  512. if (useInstances) {
  513. this._defines.INSTANCES = true;
  514. }
  515. }
  516. // Get correct effect
  517. if (!this._defines.isEqual(this._cachedDefines)) {
  518. this._defines.cloneTo(this._cachedDefines);
  519. scene.resetCachedMaterial();
  520. // Fallbacks
  521. var fallbacks = new BABYLON.EffectFallbacks();
  522. if (this._defines.REFLECTION) {
  523. fallbacks.addFallback(0, "REFLECTION");
  524. }
  525. if (this._defines.REFLECTIVITY) {
  526. fallbacks.addFallback(0, "REFLECTIVITY");
  527. }
  528. if (this._defines.BUMP) {
  529. fallbacks.addFallback(0, "BUMP");
  530. }
  531. if (this._defines.SPECULAROVERALPHA) {
  532. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  533. }
  534. if (this._defines.FOG) {
  535. fallbacks.addFallback(1, "FOG");
  536. }
  537. if (this._defines.POINTSIZE) {
  538. fallbacks.addFallback(0, "POINTSIZE");
  539. }
  540. if (this._defines.LOGARITHMICDEPTH) {
  541. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  542. }
  543. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  544. if (this._defines.SPECULARTERM) {
  545. fallbacks.addFallback(0, "SPECULARTERM");
  546. }
  547. if (this._defines.OPACITYFRESNEL) {
  548. fallbacks.addFallback(1, "OPACITYFRESNEL");
  549. }
  550. if (this._defines.EMISSIVEFRESNEL) {
  551. fallbacks.addFallback(2, "EMISSIVEFRESNEL");
  552. }
  553. if (this._defines.FRESNEL) {
  554. fallbacks.addFallback(3, "FRESNEL");
  555. }
  556. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  557. fallbacks.addCPUSkinningFallback(0, mesh);
  558. }
  559. //Attributes
  560. var attribs = [BABYLON.VertexBuffer.PositionKind];
  561. if (this._defines.NORMAL) {
  562. attribs.push(BABYLON.VertexBuffer.NormalKind);
  563. }
  564. if (this._defines.UV1) {
  565. attribs.push(BABYLON.VertexBuffer.UVKind);
  566. }
  567. if (this._defines.UV2) {
  568. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  569. }
  570. if (this._defines.VERTEXCOLOR) {
  571. attribs.push(BABYLON.VertexBuffer.ColorKind);
  572. }
  573. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  574. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  575. // Legacy browser patch
  576. var shaderName = "pbr";
  577. if (!scene.getEngine().getCaps().standardDerivatives) {
  578. shaderName = "legacypbr";
  579. }
  580. var join = this._defines.toString();
  581. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vAlbedoColor", "vReflectivityColor", "vEmissiveColor", "vReflectionColor",
  582. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  583. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  584. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  585. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  586. "vFogInfos", "vFogColor", "pointSize",
  587. "vAlbedoInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vReflectivityInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  588. "mBones",
  589. "vClipPlane", "albedoMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "reflectivityMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  590. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues",
  591. "opacityParts", "emissiveLeftColor", "emissiveRightColor",
  592. "vLightingIntensity", "vOverloadedShadowIntensity", "vOverloadedIntensity", "vCameraInfos", "vOverloadedAlbedo", "vOverloadedReflection", "vOverloadedReflectivity", "vOverloadedEmissive", "vOverloadedMicroSurface",
  593. "logarithmicDepthConstant",
  594. "vSphericalX", "vSphericalY", "vSphericalZ",
  595. "vSphericalXX", "vSphericalYY", "vSphericalZZ",
  596. "vSphericalXY", "vSphericalYZ", "vSphericalZX",
  597. "vMicrosurfaceTextureLods", "vLightRadiuses"
  598. ], ["albedoSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "reflectivitySampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler",
  599. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  600. ], join, fallbacks, this.onCompiled, this.onError);
  601. }
  602. if (!this._effect.isReady()) {
  603. return false;
  604. }
  605. this._renderId = scene.getRenderId();
  606. this._wasPreviouslyReady = true;
  607. if (mesh) {
  608. if (!mesh._materialDefines) {
  609. mesh._materialDefines = new PBRMaterialDefines();
  610. }
  611. this._defines.cloneTo(mesh._materialDefines);
  612. }
  613. return true;
  614. };
  615. PBRMaterial.prototype.unbind = function () {
  616. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  617. this._effect.setTexture("reflection2DSampler", null);
  618. }
  619. if (this.refractionTexture && this.refractionTexture.isRenderTarget) {
  620. this._effect.setTexture("refraction2DSampler", null);
  621. }
  622. _super.prototype.unbind.call(this);
  623. };
  624. PBRMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  625. this._effect.setMatrix("world", world);
  626. };
  627. PBRMaterial.prototype.bind = function (world, mesh) {
  628. this._myScene = this.getScene();
  629. // Matrices
  630. this.bindOnlyWorldMatrix(world);
  631. // Bones
  632. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  633. if (this._myScene.getCachedMaterial() !== this) {
  634. this._effect.setMatrix("viewProjection", this._myScene.getTransformMatrix());
  635. if (BABYLON.StandardMaterial.FresnelEnabled) {
  636. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  637. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  638. }
  639. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  640. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  641. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  642. }
  643. }
  644. // Textures
  645. if (this._myScene.texturesEnabled) {
  646. if (this.albedoTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  647. this._effect.setTexture("albedoSampler", this.albedoTexture);
  648. this._effect.setFloat2("vAlbedoInfos", this.albedoTexture.coordinatesIndex, this.albedoTexture.level);
  649. this._effect.setMatrix("albedoMatrix", this.albedoTexture.getTextureMatrix());
  650. }
  651. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  652. this._effect.setTexture("ambientSampler", this.ambientTexture);
  653. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  654. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  655. }
  656. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  657. this._effect.setTexture("opacitySampler", this.opacityTexture);
  658. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  659. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  660. }
  661. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  662. this._microsurfaceTextureLods.x = Math.log(this.reflectionTexture.getSize().width) * Math.LOG2E;
  663. if (this.reflectionTexture.isCube) {
  664. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  665. }
  666. else {
  667. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  668. }
  669. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  670. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, 0);
  671. if (this._defines.USESPHERICALFROMREFLECTIONMAP) {
  672. this._effect.setFloat3("vSphericalX", this.reflectionTexture.sphericalPolynomial.x.x, this.reflectionTexture.sphericalPolynomial.x.y, this.reflectionTexture.sphericalPolynomial.x.z);
  673. this._effect.setFloat3("vSphericalY", this.reflectionTexture.sphericalPolynomial.y.x, this.reflectionTexture.sphericalPolynomial.y.y, this.reflectionTexture.sphericalPolynomial.y.z);
  674. this._effect.setFloat3("vSphericalZ", this.reflectionTexture.sphericalPolynomial.z.x, this.reflectionTexture.sphericalPolynomial.z.y, this.reflectionTexture.sphericalPolynomial.z.z);
  675. this._effect.setFloat3("vSphericalXX", this.reflectionTexture.sphericalPolynomial.xx.x, this.reflectionTexture.sphericalPolynomial.xx.y, this.reflectionTexture.sphericalPolynomial.xx.z);
  676. this._effect.setFloat3("vSphericalYY", this.reflectionTexture.sphericalPolynomial.yy.x, this.reflectionTexture.sphericalPolynomial.yy.y, this.reflectionTexture.sphericalPolynomial.yy.z);
  677. this._effect.setFloat3("vSphericalZZ", this.reflectionTexture.sphericalPolynomial.zz.x, this.reflectionTexture.sphericalPolynomial.zz.y, this.reflectionTexture.sphericalPolynomial.zz.z);
  678. this._effect.setFloat3("vSphericalXY", this.reflectionTexture.sphericalPolynomial.xy.x, this.reflectionTexture.sphericalPolynomial.xy.y, this.reflectionTexture.sphericalPolynomial.xy.z);
  679. this._effect.setFloat3("vSphericalYZ", this.reflectionTexture.sphericalPolynomial.yz.x, this.reflectionTexture.sphericalPolynomial.yz.y, this.reflectionTexture.sphericalPolynomial.yz.z);
  680. this._effect.setFloat3("vSphericalZX", this.reflectionTexture.sphericalPolynomial.zx.x, this.reflectionTexture.sphericalPolynomial.zx.y, this.reflectionTexture.sphericalPolynomial.zx.z);
  681. }
  682. }
  683. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  684. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  685. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  686. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  687. }
  688. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  689. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  690. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  691. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  692. }
  693. if (this.reflectivityTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  694. this._effect.setTexture("reflectivitySampler", this.reflectivityTexture);
  695. this._effect.setFloat2("vReflectivityInfos", this.reflectivityTexture.coordinatesIndex, this.reflectivityTexture.level);
  696. this._effect.setMatrix("reflectivityMatrix", this.reflectivityTexture.getTextureMatrix());
  697. }
  698. if (this.bumpTexture && this._myScene.getEngine().getCaps().standardDerivatives && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  699. this._effect.setTexture("bumpSampler", this.bumpTexture);
  700. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level);
  701. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  702. }
  703. if (this.refractionTexture && BABYLON.StandardMaterial.RefractionTextureEnabled) {
  704. this._microsurfaceTextureLods.y = Math.log(this.refractionTexture.getSize().width) * Math.LOG2E;
  705. var depth = 1.0;
  706. if (this.refractionTexture.isCube) {
  707. this._effect.setTexture("refractionCubeSampler", this.refractionTexture);
  708. }
  709. else {
  710. this._effect.setTexture("refraction2DSampler", this.refractionTexture);
  711. this._effect.setMatrix("refractionMatrix", this.refractionTexture.getReflectionTextureMatrix());
  712. if (this.refractionTexture.depth) {
  713. depth = this.refractionTexture.depth;
  714. }
  715. }
  716. this._effect.setFloat4("vRefractionInfos", this.refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  717. }
  718. if ((this.reflectionTexture || this.refractionTexture) && this._myScene.getEngine().getCaps().textureLOD) {
  719. this._effect.setFloat2("vMicrosurfaceTextureLods", this._microsurfaceTextureLods.x, this._microsurfaceTextureLods.y);
  720. }
  721. }
  722. // Clip plane
  723. BABYLON.MaterialHelper.BindClipPlane(this._effect, this._myScene);
  724. // Point size
  725. if (this.pointsCloud) {
  726. this._effect.setFloat("pointSize", this.pointSize);
  727. }
  728. // Colors
  729. this._myScene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  730. // GAMMA CORRECTION.
  731. this.convertColorToLinearSpaceToRef(this.reflectivityColor, PBRMaterial._scaledReflectivity);
  732. this._effect.setVector3("vEyePosition", this._myScene._mirroredCameraPosition ? this._myScene._mirroredCameraPosition : this._myScene.activeCamera.position);
  733. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  734. this._effect.setColor4("vReflectivityColor", PBRMaterial._scaledReflectivity, this.microSurface);
  735. // GAMMA CORRECTION.
  736. this.convertColorToLinearSpaceToRef(this.emissiveColor, PBRMaterial._scaledEmissive);
  737. this._effect.setColor3("vEmissiveColor", PBRMaterial._scaledEmissive);
  738. // GAMMA CORRECTION.
  739. this.convertColorToLinearSpaceToRef(this.reflectionColor, PBRMaterial._scaledReflection);
  740. this._effect.setColor3("vReflectionColor", PBRMaterial._scaledReflection);
  741. }
  742. if (this._myScene.getCachedMaterial() !== this || !this.isFrozen) {
  743. // GAMMA CORRECTION.
  744. this.convertColorToLinearSpaceToRef(this.albedoColor, PBRMaterial._scaledAlbedo);
  745. this._effect.setColor4("vAlbedoColor", PBRMaterial._scaledAlbedo, this.alpha * mesh.visibility);
  746. // Lights
  747. if (this._myScene.lightsEnabled && !this.disableLighting) {
  748. PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines, this.useScalarInLinearSpace);
  749. }
  750. // View
  751. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  752. this._effect.setMatrix("view", this._myScene.getViewMatrix());
  753. }
  754. // Fog
  755. BABYLON.MaterialHelper.BindFogParameters(this._myScene, mesh, this._effect);
  756. this._lightingInfos.x = this.directIntensity;
  757. this._lightingInfos.y = this.emissiveIntensity;
  758. this._lightingInfos.z = this.environmentIntensity;
  759. this._lightingInfos.w = this.specularIntensity;
  760. this._effect.setVector4("vLightingIntensity", this._lightingInfos);
  761. this._overloadedShadowInfos.x = this.overloadedShadowIntensity;
  762. this._overloadedShadowInfos.y = this.overloadedShadeIntensity;
  763. this._effect.setVector4("vOverloadedShadowIntensity", this._overloadedShadowInfos);
  764. this._cameraInfos.x = this.cameraExposure;
  765. this._cameraInfos.y = this.cameraContrast;
  766. this._effect.setVector4("vCameraInfos", this._cameraInfos);
  767. this._overloadedIntensity.x = this.overloadedAmbientIntensity;
  768. this._overloadedIntensity.y = this.overloadedAlbedoIntensity;
  769. this._overloadedIntensity.z = this.overloadedReflectivityIntensity;
  770. this._overloadedIntensity.w = this.overloadedEmissiveIntensity;
  771. this._effect.setVector4("vOverloadedIntensity", this._overloadedIntensity);
  772. this.convertColorToLinearSpaceToRef(this.overloadedAmbient, this._tempColor);
  773. this._effect.setColor3("vOverloadedAmbient", this._tempColor);
  774. this.convertColorToLinearSpaceToRef(this.overloadedAlbedo, this._tempColor);
  775. this._effect.setColor3("vOverloadedAlbedo", this._tempColor);
  776. this.convertColorToLinearSpaceToRef(this.overloadedReflectivity, this._tempColor);
  777. this._effect.setColor3("vOverloadedReflectivity", this._tempColor);
  778. this.convertColorToLinearSpaceToRef(this.overloadedEmissive, this._tempColor);
  779. this._effect.setColor3("vOverloadedEmissive", this._tempColor);
  780. this.convertColorToLinearSpaceToRef(this.overloadedReflection, this._tempColor);
  781. this._effect.setColor3("vOverloadedReflection", this._tempColor);
  782. this._overloadedMicroSurface.x = this.overloadedMicroSurface;
  783. this._overloadedMicroSurface.y = this.overloadedMicroSurfaceIntensity;
  784. this._overloadedMicroSurface.z = this.overloadedReflectionIntensity;
  785. this._effect.setVector3("vOverloadedMicroSurface", this._overloadedMicroSurface);
  786. // Log. depth
  787. BABYLON.MaterialHelper.BindLogDepth(this._defines, this._effect, this._myScene);
  788. }
  789. _super.prototype.bind.call(this, world, mesh);
  790. this._myScene = null;
  791. };
  792. PBRMaterial.prototype.getAnimatables = function () {
  793. var results = [];
  794. if (this.albedoTexture && this.albedoTexture.animations && this.albedoTexture.animations.length > 0) {
  795. results.push(this.albedoTexture);
  796. }
  797. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  798. results.push(this.ambientTexture);
  799. }
  800. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  801. results.push(this.opacityTexture);
  802. }
  803. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  804. results.push(this.reflectionTexture);
  805. }
  806. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  807. results.push(this.emissiveTexture);
  808. }
  809. if (this.reflectivityTexture && this.reflectivityTexture.animations && this.reflectivityTexture.animations.length > 0) {
  810. results.push(this.reflectivityTexture);
  811. }
  812. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  813. results.push(this.bumpTexture);
  814. }
  815. if (this.lightmapTexture && this.lightmapTexture.animations && this.lightmapTexture.animations.length > 0) {
  816. results.push(this.lightmapTexture);
  817. }
  818. if (this.refractionTexture && this.refractionTexture.animations && this.refractionTexture.animations.length > 0) {
  819. results.push(this.refractionTexture);
  820. }
  821. return results;
  822. };
  823. PBRMaterial.prototype.dispose = function (forceDisposeEffect) {
  824. if (this.albedoTexture) {
  825. this.albedoTexture.dispose();
  826. }
  827. if (this.ambientTexture) {
  828. this.ambientTexture.dispose();
  829. }
  830. if (this.opacityTexture) {
  831. this.opacityTexture.dispose();
  832. }
  833. if (this.reflectionTexture) {
  834. this.reflectionTexture.dispose();
  835. }
  836. if (this.emissiveTexture) {
  837. this.emissiveTexture.dispose();
  838. }
  839. if (this.reflectivityTexture) {
  840. this.reflectivityTexture.dispose();
  841. }
  842. if (this.bumpTexture) {
  843. this.bumpTexture.dispose();
  844. }
  845. if (this.lightmapTexture) {
  846. this.lightmapTexture.dispose();
  847. }
  848. if (this.refractionTexture) {
  849. this.refractionTexture.dispose();
  850. }
  851. _super.prototype.dispose.call(this, forceDisposeEffect);
  852. };
  853. PBRMaterial.prototype.clone = function (name) {
  854. var _this = this;
  855. return BABYLON.SerializationHelper.Clone(function () { return new PBRMaterial(name, _this.getScene()); }, this);
  856. };
  857. PBRMaterial.prototype.serialize = function () {
  858. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  859. serializationObject.customType = "BABYLON.PBRMaterial";
  860. return serializationObject;
  861. };
  862. // Statics
  863. PBRMaterial.Parse = function (source, scene, rootUrl) {
  864. return BABYLON.SerializationHelper.Parse(function () { return new PBRMaterial(source.name, scene); }, source, scene, rootUrl);
  865. };
  866. PBRMaterial._scaledAlbedo = new BABYLON.Color3();
  867. PBRMaterial._scaledReflectivity = new BABYLON.Color3();
  868. PBRMaterial._scaledEmissive = new BABYLON.Color3();
  869. PBRMaterial._scaledReflection = new BABYLON.Color3();
  870. PBRMaterial._lightRadiuses = [1, 1, 1, 1];
  871. __decorate([
  872. BABYLON.serialize()
  873. ], PBRMaterial.prototype, "directIntensity");
  874. __decorate([
  875. BABYLON.serialize()
  876. ], PBRMaterial.prototype, "emissiveIntensity");
  877. __decorate([
  878. BABYLON.serialize()
  879. ], PBRMaterial.prototype, "environmentIntensity");
  880. __decorate([
  881. BABYLON.serialize()
  882. ], PBRMaterial.prototype, "specularIntensity");
  883. __decorate([
  884. BABYLON.serialize()
  885. ], PBRMaterial.prototype, "overloadedShadowIntensity");
  886. __decorate([
  887. BABYLON.serialize()
  888. ], PBRMaterial.prototype, "overloadedShadeIntensity");
  889. __decorate([
  890. BABYLON.serialize()
  891. ], PBRMaterial.prototype, "cameraExposure");
  892. __decorate([
  893. BABYLON.serialize()
  894. ], PBRMaterial.prototype, "cameraContrast");
  895. __decorate([
  896. BABYLON.serialize()
  897. ], PBRMaterial.prototype, "overloadedAmbientIntensity");
  898. __decorate([
  899. BABYLON.serialize()
  900. ], PBRMaterial.prototype, "overloadedAlbedoIntensity");
  901. __decorate([
  902. BABYLON.serialize()
  903. ], PBRMaterial.prototype, "overloadedReflectivityIntensity");
  904. __decorate([
  905. BABYLON.serialize()
  906. ], PBRMaterial.prototype, "overloadedEmissiveIntensity");
  907. __decorate([
  908. BABYLON.serializeAsColor3()
  909. ], PBRMaterial.prototype, "overloadedAmbient");
  910. __decorate([
  911. BABYLON.serializeAsColor3()
  912. ], PBRMaterial.prototype, "overloadedAlbedo");
  913. __decorate([
  914. BABYLON.serializeAsColor3()
  915. ], PBRMaterial.prototype, "overloadedReflectivity");
  916. __decorate([
  917. BABYLON.serializeAsColor3()
  918. ], PBRMaterial.prototype, "overloadedEmissive");
  919. __decorate([
  920. BABYLON.serializeAsColor3()
  921. ], PBRMaterial.prototype, "overloadedReflection");
  922. __decorate([
  923. BABYLON.serialize()
  924. ], PBRMaterial.prototype, "overloadedMicroSurface");
  925. __decorate([
  926. BABYLON.serialize()
  927. ], PBRMaterial.prototype, "overloadedMicroSurfaceIntensity");
  928. __decorate([
  929. BABYLON.serialize()
  930. ], PBRMaterial.prototype, "overloadedReflectionIntensity");
  931. __decorate([
  932. BABYLON.serialize()
  933. ], PBRMaterial.prototype, "disableBumpMap");
  934. __decorate([
  935. BABYLON.serializeAsTexture()
  936. ], PBRMaterial.prototype, "albedoTexture");
  937. __decorate([
  938. BABYLON.serializeAsTexture()
  939. ], PBRMaterial.prototype, "ambientTexture");
  940. __decorate([
  941. BABYLON.serializeAsTexture()
  942. ], PBRMaterial.prototype, "opacityTexture");
  943. __decorate([
  944. BABYLON.serializeAsTexture()
  945. ], PBRMaterial.prototype, "reflectionTexture");
  946. __decorate([
  947. BABYLON.serializeAsTexture()
  948. ], PBRMaterial.prototype, "emissiveTexture");
  949. __decorate([
  950. BABYLON.serializeAsTexture()
  951. ], PBRMaterial.prototype, "reflectivityTexture");
  952. __decorate([
  953. BABYLON.serializeAsTexture()
  954. ], PBRMaterial.prototype, "bumpTexture");
  955. __decorate([
  956. BABYLON.serializeAsTexture()
  957. ], PBRMaterial.prototype, "lightmapTexture");
  958. __decorate([
  959. BABYLON.serializeAsTexture()
  960. ], PBRMaterial.prototype, "refractionTexture");
  961. __decorate([
  962. BABYLON.serializeAsColor3("ambient")
  963. ], PBRMaterial.prototype, "ambientColor");
  964. __decorate([
  965. BABYLON.serializeAsColor3("albedo")
  966. ], PBRMaterial.prototype, "albedoColor");
  967. __decorate([
  968. BABYLON.serializeAsColor3("reflectivity")
  969. ], PBRMaterial.prototype, "reflectivityColor");
  970. __decorate([
  971. BABYLON.serializeAsColor3("reflection")
  972. ], PBRMaterial.prototype, "reflectionColor");
  973. __decorate([
  974. BABYLON.serializeAsColor3("emissivie")
  975. ], PBRMaterial.prototype, "emissiveColor");
  976. __decorate([
  977. BABYLON.serialize()
  978. ], PBRMaterial.prototype, "microSurface");
  979. __decorate([
  980. BABYLON.serialize()
  981. ], PBRMaterial.prototype, "indexOfRefraction");
  982. __decorate([
  983. BABYLON.serialize()
  984. ], PBRMaterial.prototype, "invertRefractionY");
  985. __decorate([
  986. BABYLON.serializeAsFresnelParameters()
  987. ], PBRMaterial.prototype, "opacityFresnelParameters");
  988. __decorate([
  989. BABYLON.serializeAsFresnelParameters()
  990. ], PBRMaterial.prototype, "emissiveFresnelParameters");
  991. __decorate([
  992. BABYLON.serialize()
  993. ], PBRMaterial.prototype, "linkRefractionWithTransparency");
  994. __decorate([
  995. BABYLON.serialize()
  996. ], PBRMaterial.prototype, "linkEmissiveWithAlbedo");
  997. __decorate([
  998. BABYLON.serialize()
  999. ], PBRMaterial.prototype, "useLightmapAsShadowmap");
  1000. __decorate([
  1001. BABYLON.serialize()
  1002. ], PBRMaterial.prototype, "useEmissiveAsIllumination");
  1003. __decorate([
  1004. BABYLON.serialize()
  1005. ], PBRMaterial.prototype, "useAlphaFromAlbedoTexture");
  1006. __decorate([
  1007. BABYLON.serialize()
  1008. ], PBRMaterial.prototype, "useSpecularOverAlpha");
  1009. __decorate([
  1010. BABYLON.serialize()
  1011. ], PBRMaterial.prototype, "useMicroSurfaceFromReflectivityMapAlpha");
  1012. __decorate([
  1013. BABYLON.serialize()
  1014. ], PBRMaterial.prototype, "useAutoMicroSurfaceFromReflectivityMap");
  1015. __decorate([
  1016. BABYLON.serialize()
  1017. ], PBRMaterial.prototype, "useScalarInLinearSpace");
  1018. __decorate([
  1019. BABYLON.serialize()
  1020. ], PBRMaterial.prototype, "usePhysicalLightFalloff");
  1021. __decorate([
  1022. BABYLON.serialize()
  1023. ], PBRMaterial.prototype, "useRadianceOverAlpha");
  1024. __decorate([
  1025. BABYLON.serialize()
  1026. ], PBRMaterial.prototype, "disableLighting");
  1027. Object.defineProperty(PBRMaterial.prototype, "useLogarithmicDepth",
  1028. __decorate([
  1029. BABYLON.serialize()
  1030. ], PBRMaterial.prototype, "useLogarithmicDepth", Object.getOwnPropertyDescriptor(PBRMaterial.prototype, "useLogarithmicDepth")));
  1031. return PBRMaterial;
  1032. })(BABYLON.Material);
  1033. BABYLON.PBRMaterial = PBRMaterial;
  1034. })(BABYLON || (BABYLON = {}));
  1035. 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\n#include<bonesDeclaration>\n\n// Uniforms\n#include<instancesDeclaration>\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\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<shadowsVertexDeclaration>\n\n#ifdef REFLECTIONMAP_SKYBOX\nvarying vec3 vPositionUVW;\n#endif\n\n#ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED\nvarying vec3 vDirectionW;\n#endif\n\n#include<logDepthDeclaration>\n\nvoid main(void) {\n#ifdef REFLECTIONMAP_SKYBOX\n vPositionUVW = position;\n#endif \n\n#include<instancesVertex>\n#include<bonesVertex>\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_FIXED\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#include<clipPlaneVertex>\n\n // Fog\n#include<fogVertex>\n\n // Shadows\n#include<shadowsVertex>\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#include<logDepthVertex>\n}";
  1036. BABYLON.Effect.ShadersStore['pbrPixelShader'] = "#ifdef BUMP\n#extension GL_OES_standard_derivatives : enable\n#endif\n\n#ifdef LODBASEDMICROSFURACE\n#extension GL_EXT_shader_texture_lod : 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;\nuniform vec4 vLightRadiuses;\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#ifdef LODBASEDMICROSFURACE\n uniform vec2 vMicrosurfaceTextureLods;\n#endif\n\n// PBR CUSTOM CONSTANTS\nconst float kPi = 3.1415926535897932384626433832795;\nconst float kRougnhessToAlphaScale = 0.1;\nconst float kRougnhessToAlphaOffset = 0.29248125;\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// Based on Beckamm roughness to Blinn exponent + http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html \nfloat getMipMapIndexFromAverageSlope(float maxMipLevel, float alpha)\n{\n // do not take in account lower mips hence -1... and wait from proper preprocess.\n // formula comes from approximation of the mathematical solution.\n //float mip = maxMipLevel + kRougnhessToAlphaOffset + 0.5 * log2(alpha);\n \n // In the mean time \n // Always [0..1] goes from max mip to min mip in a log2 way. \n // Change 5 to nummip below.\n // http://www.wolframalpha.com/input/?i=x+in+0..1+plot+(+5+%2B+0.3+%2B+0.1+*+5+*+log2(+(1+-+x)+*+(1+-+x)+%2B+0.0005))\n float mip = kRougnhessToAlphaOffset + maxMipLevel + (maxMipLevel * kRougnhessToAlphaScale * log2(alpha));\n \n return clamp(mip, 0., maxMipLevel);\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 adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance)\n{\n // At small angle this approximation works. \n float lightRoughness = lightRadius / lightDistance;\n // Distribution can sum.\n float totalRoughness = clamp(lightRoughness + roughness, 0., 1.);\n return totalRoughness;\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\nfloat computeLightFalloff(vec3 lightOffset, float lightDistanceSquared, float range)\n{\n #ifdef USEPHYSICALLIGHTFALLOFF\n float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001));\n return lightDistanceFalloff;\n #else\n float lightFalloff = max(0., 1.0 - length(lightOffset) / range);\n return lightFalloff;\n #endif\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#include<light0FragmentDeclaration>\n#include<light1FragmentDeclaration>\n#include<light2FragmentDeclaration>\n#include<light3FragmentDeclaration>\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#include<fresnelFunction>\n\n#ifdef OPACITYFRESNEL\nuniform vec4 opacityParts;\n#endif\n\n#ifdef EMISSIVEFRESNEL\nuniform vec4 emissiveLeftColor;\nuniform vec4 emissiveRightColor;\n#endif\n\n// Refraction Reflection\n#if defined(REFLECTIONMAP_SPHERICAL) || defined(REFLECTIONMAP_PROJECTION) || defined(REFRACTION)\n uniform mat4 view;\n#endif\n\n// Refraction\n#ifdef REFRACTION\n uniform vec4 vRefractionInfos;\n\n #ifdef REFRACTIONMAP_3D\n uniform samplerCube refractionCubeSampler;\n #else\n uniform sampler2D refraction2DSampler;\n uniform mat4 refractionMatrix;\n #endif\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_FIXED\n varying vec3 vDirectionW;\n #endif\n\n #if defined(REFLECTIONMAP_PLANAR) || defined(REFLECTIONMAP_CUBIC) || defined(REFLECTIONMAP_PROJECTION)\n uniform mat4 reflectionMatrix;\n #endif\n#endif\n\n#include<reflectionFunction>\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 vec3 directionToLight = vPositionW - lightPosition;\n float depth = length(directionToLight);\n\n depth = (depth - depthValues.x) / (depthValues.y - depthValues.x);\n depth = clamp(depth, 0., 1.0);\n\n directionToLight = normalize(directionToLight);\n directionToLight.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] * mapSize)) < biasedDepth) visibility -= 0.25;\n if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[1] * mapSize)) < biasedDepth) visibility -= 0.25;\n if (unpack(textureCube(shadowSampler, directionToLight + poissonDisk[2] * mapSize)) < biasedDepth) visibility -= 0.25;\n if (unpack(textureCube(shadowSampler, directionToLight + 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#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#include<bumpFragmentFunctions>\n#include<clipPlaneFragmentDeclaration>\n#include<logDepthDeclaration>\n\n// Fog\n#include<fogFragmentDeclaration>\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, float lightRadius) {\n lightingInfo result;\n\n vec3 lightDirection;\n float attenuation = 1.0;\n float lightDistance;\n \n // Point\n if (lightData.w == 0.)\n {\n vec3 lightOffset = lightData.xyz - vPositionW;\n float lightDistanceSquared = dot(lightOffset, lightOffset);\n attenuation = computeLightFalloff(lightOffset, lightDistanceSquared, range);\n \n lightDistance = sqrt(lightDistanceSquared);\n lightDirection = normalize(lightOffset);\n }\n // Directional\n else\n {\n lightDistance = length(-lightData.xyz);\n lightDirection = normalize(-lightData.xyz);\n }\n \n // Roughness\n roughness = adjustRoughnessFromLightProperties(roughness, lightRadius, lightDistance);\n \n // diffuse\n vec3 H = normalize(viewDirectionW + lightDirection);\n float NdotL = max(0.00000000001, dot(vNormal, lightDirection));\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, float lightRadius) {\n lightingInfo result;\n\n vec3 lightOffset = lightData.xyz - vPositionW;\n vec3 lightVectorW = normalize(lightOffset);\n\n // diffuse\n float cosAngle = max(0.000000000000001, dot(-lightDirection.xyz, lightVectorW));\n \n if (cosAngle >= lightDirection.w)\n {\n cosAngle = max(0., pow(cosAngle, lightData.w));\n \n // Inverse squared falloff.\n float lightDistanceSquared = dot(lightOffset, lightOffset);\n float attenuation = computeLightFalloff(lightOffset, lightDistanceSquared, range);\n \n // Directional falloff.\n attenuation *= cosAngle;\n \n // Roughness.\n float lightDistance = sqrt(lightDistanceSquared);\n roughness = adjustRoughnessFromLightProperties(roughness, lightRadius, lightDistance);\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;\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\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, float lightRadius) {\n lightingInfo result;\n\n // Roughness\n // Do not touch roughness on hemispheric.\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 lightVectorW = normalize(lightData.xyz);\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#include<clipPlaneFragment>\n\n #ifdef PoissonSamplingEnvironment\n initSamplers();\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 #ifndef LINKREFRACTIONTOTRANSPARENCY\n #ifdef ALPHATEST\n if (surfaceAlbedo.a < 0.4)\n discard;\n #endif\n #endif\n\n #ifdef ALPHAFROMALBEDO\n alpha *= surfaceAlbedo.a;\n #endif\n\n surfaceAlbedo.rgb *= vAlbedoInfos.y;\n #else\n // No Albedo texture.\n surfaceAlbedo.rgb = surfaceAlbedoContribution;\n surfaceAlbedoContribution = vec3(1., 1., 1.);\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 vec4 surfaceReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV);\n surfaceReflectivityColor = surfaceReflectivityColorMap.rgb;\n surfaceReflectivityColor = toLinearSpace(surfaceReflectivityColor);\n\n #ifdef OVERLOADEDVALUES\n surfaceReflectivityColor = mix(surfaceReflectivityColor, vOverloadedReflectivity, vOverloadedIntensity.z);\n #endif\n\n #ifdef MICROSURFACEFROMREFLECTIVITYMAP\n microSurface = surfaceReflectivityColorMap.a;\n #else\n #ifdef MICROSURFACEAUTOMATIC\n microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);\n #endif\n #endif\n #endif\n\n #ifdef OVERLOADEDVALUES\n microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);\n #endif\n\n // Compute N dot V.\n float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));\n\n // Adapt microSurface.\n microSurface = clamp(microSurface, 0., 1.) * 0.98;\n\n // Compute roughness.\n float roughness = 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, roughness, NdotV, vLightRadiuses[0]);\n#endif\n#ifdef HEMILIGHT0\n lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, roughness, NdotV, vLightRadiuses[0]);\n#endif\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\n lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, roughness, NdotV, vLightRadiuses[0]);\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, roughness, NdotV, vLightRadiuses[1]);\n#endif\n#ifdef HEMILIGHT1\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, roughness, NdotV, vLightRadiuses[1]);\n#endif\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\n info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, roughness, NdotV, vLightRadiuses[1]);\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, roughness, NdotV, vLightRadiuses[2]);\n#endif\n#ifdef HEMILIGHT2\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, roughness, NdotV, vLightRadiuses[2]);\n#endif\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\n info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, roughness, NdotV, vLightRadiuses[2]);\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, roughness, NdotV, vLightRadiuses[3]);\n#endif\n#ifdef HEMILIGHT3\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, roughness, NdotV, vLightRadiuses[3]);\n#endif\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\n info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, roughness, NdotV, vLightRadiuses[3]);\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#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// Refraction\nvec3 surfaceRefractionColor = vec3(0., 0., 0.);\n\n// Go mat -> blurry reflexion according to microSurface\n#ifdef LODBASEDMICROSFURACE\n float alphaG = convertRoughnessToAverageSlope(roughness);\n#else\n float bias = 20. * (1.0 - microSurface);\n#endif\n \n#ifdef REFRACTION\n\tvec3 refractionVector = normalize(refract(-viewDirectionW, normalW, vRefractionInfos.y));\n \n #ifdef LODBASEDMICROSFURACE\n float lodRefraction = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.y, alphaG);\n #endif\n \n #ifdef REFRACTIONMAP_3D\n refractionVector.y = refractionVector.y * vRefractionInfos.w;\n\n if (dot(refractionVector, viewDirectionW) < 1.0)\n {\n #ifdef LODBASEDMICROSFURACE\n surfaceRefractionColor = textureCubeLodEXT(refractionCubeSampler, refractionVector, lodRefraction).rgb * vRefractionInfos.x;\n #else\n surfaceRefractionColor = textureCube(refractionCubeSampler, refractionVector, bias).rgb * vRefractionInfos.x;\n #endif\n }\n \n #ifndef REFRACTIONMAPINLINEARSPACE\n surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb); \n #endif\n #else\n vec3 vRefractionUVW = vec3(refractionMatrix * (view * vec4(vPositionW + refractionVector * vRefractionInfos.z, 1.0)));\n\n vec2 refractionCoords = vRefractionUVW.xy / vRefractionUVW.z;\n\n refractionCoords.y = 1.0 - refractionCoords.y;\n\n #ifdef LODBASEDMICROSFURACE\n surfaceRefractionColor = texture2DLodEXT(refraction2DSampler, refractionCoords, lodRefraction).rgb * vRefractionInfos.x;\n #else\n surfaceRefractionColor = texture2D(refraction2DSampler, refractionCoords, bias).rgb * vRefractionInfos.x;\n #endif \n \n surfaceRefractionColor = toLinearSpace(surfaceRefractionColor.rgb); \n #endif\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 LODBASEDMICROSFURACE\n float lodReflection = getMipMapIndexFromAverageSlope(vMicrosurfaceTextureLods.x, alphaG);\n #endif\n \n #ifdef REFLECTIONMAP_3D\n \n #ifdef LODBASEDMICROSFURACE\n environmentRadiance = textureCubeLodEXT(reflectionCubeSampler, vReflectionUVW, lodReflection).rgb * vReflectionInfos.x;\n #else\n environmentRadiance = textureCube(reflectionCubeSampler, vReflectionUVW, bias).rgb * vReflectionInfos.x;\n #endif\n \n #ifdef PoissonSamplingEnvironment\n environmentRadiance = environmentSampler(reflectionCubeSampler, vReflectionUVW, alphaG) * vReflectionInfos.x;\n #endif\n\n #ifdef USESPHERICALFROMREFLECTIONMAP\n #ifndef REFLECTIONMAP_SKYBOX\n vec3 normalEnvironmentSpace = (reflectionMatrix * vec4(normalW, 1)).xyz;\n environmentIrradiance = EnvironmentIrradiance(normalEnvironmentSpace);\n #endif\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 #ifdef LODBASEDMICROSFURACE\n environmentRadiance = texture2DLodEXT(reflection2DSampler, coords, lodReflection).rgb * vReflectionInfos.x;\n #else\n environmentRadiance = texture2D(reflection2DSampler, coords, bias).rgb * vReflectionInfos.x;\n #endif\n \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));\n\n// Compute refractance\nvec3 refractance = vec3(0.0 , 0.0, 0.0);\n#ifdef REFRACTION\n vec3 transmission = vec3(1.0 , 1.0, 1.0);\n #ifdef LINKREFRACTIONTOTRANSPARENCY\n // Transmission based on alpha.\n transmission *= (1.0 - alpha);\n \n // Tint the material with albedo.\n // TODO. PBR Tinting.\n vec3 mixedAlbedo = surfaceAlbedoContribution.rgb * surfaceAlbedo.rgb;\n float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);\n vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);\n \n // Decrease Albedo Contribution\n surfaceAlbedoContribution *= alpha;\n \n // Decrease irradiance Contribution\n environmentIrradiance *= alpha;\n \n // Tint reflectance\n surfaceRefractionColor *= tint;\n \n // Put alpha back to 1;\n alpha = 1.0;\n #endif\n \n // Add Multiple internal bounces.\n vec3 bounceSpecularEnvironmentReflectance = (2.0 * specularEnvironmentReflectance) / (1.0 + specularEnvironmentReflectance);\n specularEnvironmentReflectance = mix(bounceSpecularEnvironmentReflectance, specularEnvironmentReflectance, alpha);\n \n // In theory T = 1 - R.\n transmission *= 1.0 - specularEnvironmentReflectance;\n \n // Should baked in diffuse.\n refractance = surfaceRefractionColor * transmission;\n#endif\n\n// Apply Energy Conservation taking in account the environment level only if the environment is present.\nfloat reflectance = max(max(surfaceReflectivityColor.r, surfaceReflectivityColor.g), surfaceReflectivityColor.b);\nsurfaceAlbedo.rgb = (1. - reflectance) * surfaceAlbedo.rgb;\n\nrefractance *= vLightingIntensity.z;\nenvironmentRadiance *= specularEnvironmentReflectance;\n\n// Emissive\nvec3 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 SPECULAROVERALPHA\n alpha = clamp(alpha + getLuminance(finalSpecular), 0., 1.);\n#endif\n\n#ifdef RADIANCEOVERALPHA\n alpha = clamp(alpha + getLuminance(environmentRadiance), 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 + refractance, alpha);\n#else\n vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + refractance, 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 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 // Refractance\n // gl_FragColor = vec4(refractance.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#include<logDepthFragment>\n#include<fogFragment>(color, finalColor)\n\n gl_FragColor = finalColor;\n}";
  1037. 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#include<bonesDeclaration>\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#include<clipPlaneVertexDeclaration>\n\nvoid main(void) {\n mat4 finalWorld = world;\n\n#include<bonesVertex>\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#include<clipPlaneVertex>\n\n\t// Vertex color\n#ifdef VERTEXCOLOR\n\tvColor = color;\n#endif\n}";
  1038. 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#include<light0FragmentDeclaration>\n#include<light1FragmentDeclaration>\n#include<light2FragmentDeclaration>\n#include<light3FragmentDeclaration>\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#include<clipPlaneFragmentDeclaration>\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#include<clipPlaneFragment>\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}";