babylon.pbrMaterial.js 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var maxSimultaneousLights = 4;
  5. var PBRMaterialDefines = (function (_super) {
  6. __extends(PBRMaterialDefines, _super);
  7. function PBRMaterialDefines() {
  8. _super.call(this);
  9. this.ALBEDO = false;
  10. this.AMBIENT = false;
  11. this.OPACITY = false;
  12. this.OPACITYRGB = false;
  13. this.REFLECTION = false;
  14. this.EMISSIVE = false;
  15. this.REFLECTIVITY = false;
  16. this.BUMP = false;
  17. this.SPECULAROVERALPHA = false;
  18. this.CLIPPLANE = false;
  19. this.ALPHATEST = false;
  20. this.ALPHAFROMALBEDO = false;
  21. this.POINTSIZE = false;
  22. this.FOG = false;
  23. this.LIGHT0 = false;
  24. this.LIGHT1 = false;
  25. this.LIGHT2 = false;
  26. this.LIGHT3 = false;
  27. this.SPOTLIGHT0 = false;
  28. this.SPOTLIGHT1 = false;
  29. this.SPOTLIGHT2 = false;
  30. this.SPOTLIGHT3 = false;
  31. this.HEMILIGHT0 = false;
  32. this.HEMILIGHT1 = false;
  33. this.HEMILIGHT2 = false;
  34. this.HEMILIGHT3 = false;
  35. this.POINTLIGHT0 = false;
  36. this.POINTLIGHT1 = false;
  37. this.POINTLIGHT2 = false;
  38. this.POINTLIGHT3 = false;
  39. this.DIRLIGHT0 = false;
  40. this.DIRLIGHT1 = false;
  41. this.DIRLIGHT2 = false;
  42. this.DIRLIGHT3 = false;
  43. this.SPECULARTERM = false;
  44. this.SHADOW0 = false;
  45. this.SHADOW1 = false;
  46. this.SHADOW2 = false;
  47. this.SHADOW3 = false;
  48. this.SHADOWS = false;
  49. this.SHADOWVSM0 = false;
  50. this.SHADOWVSM1 = false;
  51. this.SHADOWVSM2 = false;
  52. this.SHADOWVSM3 = false;
  53. this.SHADOWPCF0 = false;
  54. this.SHADOWPCF1 = false;
  55. this.SHADOWPCF2 = false;
  56. this.SHADOWPCF3 = false;
  57. this.OPACITYFRESNEL = false;
  58. this.EMISSIVEFRESNEL = false;
  59. this.FRESNEL = false;
  60. this.NORMAL = false;
  61. this.UV1 = false;
  62. this.UV2 = false;
  63. this.VERTEXCOLOR = false;
  64. this.VERTEXALPHA = false;
  65. this.NUM_BONE_INFLUENCERS = 0;
  66. this.BonesPerMesh = 0;
  67. this.INSTANCES = false;
  68. this.MICROSURFACEFROMREFLECTIVITYMAP = false;
  69. this.EMISSIVEASILLUMINATION = false;
  70. this.LINKEMISSIVEWITHALBEDO = false;
  71. this.LIGHTMAP = false;
  72. this.USELIGHTMAPASSHADOWMAP = false;
  73. this.REFLECTIONMAP_3D = false;
  74. this.REFLECTIONMAP_SPHERICAL = false;
  75. this.REFLECTIONMAP_PLANAR = false;
  76. this.REFLECTIONMAP_CUBIC = false;
  77. this.REFLECTIONMAP_PROJECTION = false;
  78. this.REFLECTIONMAP_SKYBOX = false;
  79. this.REFLECTIONMAP_EXPLICIT = false;
  80. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  81. this.INVERTCUBICMAP = false;
  82. this.LOGARITHMICDEPTH = false;
  83. this.CAMERATONEMAP = false;
  84. this.CAMERACONTRAST = false;
  85. this.OVERLOADEDVALUES = false;
  86. this.OVERLOADEDSHADOWVALUES = false;
  87. this.USESPHERICALFROMREFLECTIONMAP = false;
  88. this.REFRACTION = false;
  89. this.REFRACTIONMAP_3D = false;
  90. this.LINKREFRACTIONTOTRANSPARENCY = false;
  91. this.REFRACTIONMAPINLINEARSPACE = false;
  92. this.LODBASEDMICROSFURACE = false;
  93. this._keys = Object.keys(this);
  94. }
  95. return PBRMaterialDefines;
  96. })(BABYLON.MaterialDefines);
  97. var PBRMaterial = (function (_super) {
  98. __extends(PBRMaterial, _super);
  99. function PBRMaterial(name, scene) {
  100. var _this = this;
  101. _super.call(this, name, scene);
  102. this.directIntensity = 1.0;
  103. this.emissiveIntensity = 1.0;
  104. this.environmentIntensity = 1.0;
  105. this.specularIntensity = 1.0;
  106. this._lightingInfos = new BABYLON.Vector4(this.directIntensity, this.emissiveIntensity, this.environmentIntensity, this.specularIntensity);
  107. this.overloadedShadowIntensity = 1.0;
  108. this.overloadedShadeIntensity = 1.0;
  109. this._overloadedShadowInfos = new BABYLON.Vector4(this.overloadedShadowIntensity, this.overloadedShadeIntensity, 0.0, 0.0);
  110. this.cameraExposure = 1.0;
  111. this.cameraContrast = 1.0;
  112. this._cameraInfos = new BABYLON.Vector4(1.0, 1.0, 0.0, 0.0);
  113. this._microsurfaceTextureLods = new BABYLON.Vector2(0.0, 0.0);
  114. this.overloadedAmbientIntensity = 0.0;
  115. this.overloadedAlbedoIntensity = 0.0;
  116. this.overloadedReflectivityIntensity = 0.0;
  117. this.overloadedEmissiveIntensity = 0.0;
  118. this._overloadedIntensity = new BABYLON.Vector4(this.overloadedAmbientIntensity, this.overloadedAlbedoIntensity, this.overloadedReflectivityIntensity, this.overloadedEmissiveIntensity);
  119. this.overloadedAmbient = BABYLON.Color3.White();
  120. this.overloadedAlbedo = BABYLON.Color3.White();
  121. this.overloadedReflectivity = BABYLON.Color3.White();
  122. this.overloadedEmissive = BABYLON.Color3.White();
  123. this.overloadedReflection = BABYLON.Color3.White();
  124. this.overloadedMicroSurface = 0.0;
  125. this.overloadedMicroSurfaceIntensity = 0.0;
  126. this.overloadedReflectionIntensity = 0.0;
  127. this._overloadedMicroSurface = new BABYLON.Vector3(this.overloadedMicroSurface, this.overloadedMicroSurfaceIntensity, this.overloadedReflectionIntensity);
  128. this.disableBumpMap = false;
  129. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  130. this.albedoColor = new BABYLON.Color3(1, 1, 1);
  131. this.reflectivityColor = new BABYLON.Color3(1, 1, 1);
  132. this.reflectionColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  133. this.microSurface = 0.5;
  134. this.emissiveColor = new BABYLON.Color3(0, 0, 0);
  135. this.useAlphaFromAlbedoTexture = false;
  136. this.useEmissiveAsIllumination = false;
  137. this.linkEmissiveWithAlbedo = false;
  138. this.useSpecularOverAlpha = true;
  139. this.disableLighting = false;
  140. this.indexOfRefraction = 0.66;
  141. this.invertRefractionY = false;
  142. this.linkRefractionWithTransparency = false;
  143. this.useLightmapAsShadowmap = false;
  144. this.useMicroSurfaceFromReflectivityMapAlpha = false;
  145. this._renderTargets = new BABYLON.SmartArray(16);
  146. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  147. this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  148. this._tempColor = new BABYLON.Color3();
  149. this._defines = new PBRMaterialDefines();
  150. this._cachedDefines = new PBRMaterialDefines();
  151. this._myScene = null;
  152. this._myShadowGenerator = null;
  153. this._cachedDefines.BonesPerMesh = -1;
  154. this.getRenderTargetTextures = function () {
  155. _this._renderTargets.reset();
  156. if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
  157. _this._renderTargets.push(_this.reflectionTexture);
  158. }
  159. if (_this.refractionTexture && _this.refractionTexture.isRenderTarget) {
  160. _this._renderTargets.push(_this.refractionTexture);
  161. }
  162. return _this._renderTargets;
  163. };
  164. }
  165. Object.defineProperty(PBRMaterial.prototype, "useLogarithmicDepth", {
  166. get: function () {
  167. return this._useLogarithmicDepth;
  168. },
  169. set: function (value) {
  170. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  171. },
  172. enumerable: true,
  173. configurable: true
  174. });
  175. PBRMaterial.prototype.needAlphaBlending = function () {
  176. if (this.linkRefractionWithTransparency) {
  177. return false;
  178. }
  179. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromAlbedoTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  180. };
  181. PBRMaterial.prototype.needAlphaTesting = function () {
  182. if (this.linkRefractionWithTransparency) {
  183. return false;
  184. }
  185. return this.albedoTexture != null && this.albedoTexture.hasAlpha;
  186. };
  187. PBRMaterial.prototype._shouldUseAlphaFromAlbedoTexture = function () {
  188. return this.albedoTexture != null && this.albedoTexture.hasAlpha && this.useAlphaFromAlbedoTexture;
  189. };
  190. PBRMaterial.prototype.getAlphaTestTexture = function () {
  191. return this.albedoTexture;
  192. };
  193. PBRMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  194. if (!mesh) {
  195. return true;
  196. }
  197. if (this._defines.INSTANCES !== useInstances) {
  198. return false;
  199. }
  200. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  201. return true;
  202. }
  203. return false;
  204. };
  205. PBRMaterial.BindLights = function (scene, mesh, effect, defines) {
  206. var lightIndex = 0;
  207. var depthValuesAlreadySet = false;
  208. for (var index = 0; index < scene.lights.length; index++) {
  209. var light = scene.lights[index];
  210. if (!light.isEnabled()) {
  211. continue;
  212. }
  213. if (!light.canAffectMesh(mesh)) {
  214. continue;
  215. }
  216. if (light instanceof BABYLON.PointLight) {
  217. // Point Light
  218. light.transferToEffect(effect, "vLightData" + lightIndex);
  219. }
  220. else if (light instanceof BABYLON.DirectionalLight) {
  221. // Directional Light
  222. light.transferToEffect(effect, "vLightData" + lightIndex);
  223. }
  224. else if (light instanceof BABYLON.SpotLight) {
  225. // Spot Light
  226. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  227. }
  228. else if (light instanceof BABYLON.HemisphericLight) {
  229. // Hemispheric Light
  230. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  231. }
  232. // GAMMA CORRECTION.
  233. light.diffuse.toLinearSpaceToRef(PBRMaterial._scaledAlbedo);
  234. PBRMaterial._scaledAlbedo.scaleToRef(light.intensity, PBRMaterial._scaledAlbedo);
  235. light.diffuse.scaleToRef(light.intensity, PBRMaterial._scaledAlbedo);
  236. effect.setColor4("vLightDiffuse" + lightIndex, PBRMaterial._scaledAlbedo, light.range);
  237. if (defines["SPECULARTERM"]) {
  238. light.specular.toLinearSpaceToRef(PBRMaterial._scaledReflectivity);
  239. PBRMaterial._scaledReflectivity.scaleToRef(light.intensity, PBRMaterial._scaledReflectivity);
  240. effect.setColor3("vLightSpecular" + lightIndex, PBRMaterial._scaledReflectivity);
  241. }
  242. // Shadows
  243. if (scene.shadowsEnabled) {
  244. depthValuesAlreadySet = BABYLON.StandardMaterial.BindLightShadow(light, scene, mesh, lightIndex, effect, depthValuesAlreadySet);
  245. }
  246. lightIndex++;
  247. if (lightIndex === maxSimultaneousLights)
  248. break;
  249. }
  250. };
  251. PBRMaterial.prototype.isReady = function (mesh, useInstances) {
  252. if (this.checkReadyOnlyOnce) {
  253. if (this._wasPreviouslyReady) {
  254. return true;
  255. }
  256. }
  257. var scene = this.getScene();
  258. if (!this.checkReadyOnEveryCall) {
  259. if (this._renderId === scene.getRenderId()) {
  260. if (this._checkCache(scene, mesh, useInstances)) {
  261. return true;
  262. }
  263. }
  264. }
  265. var engine = scene.getEngine();
  266. var needNormals = false;
  267. var needUVs = false;
  268. this._defines.reset();
  269. if (scene.texturesEnabled) {
  270. // Textures
  271. if (scene.texturesEnabled) {
  272. if (scene.getEngine().getCaps().textureLOD) {
  273. this._defines.LODBASEDMICROSFURACE = true;
  274. }
  275. if (this.albedoTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  276. if (!this.albedoTexture.isReady()) {
  277. return false;
  278. }
  279. else {
  280. needUVs = true;
  281. this._defines.ALBEDO = true;
  282. }
  283. }
  284. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  285. if (!this.ambientTexture.isReady()) {
  286. return false;
  287. }
  288. else {
  289. needUVs = true;
  290. this._defines.AMBIENT = true;
  291. }
  292. }
  293. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  294. if (!this.opacityTexture.isReady()) {
  295. return false;
  296. }
  297. else {
  298. needUVs = true;
  299. this._defines.OPACITY = true;
  300. if (this.opacityTexture.getAlphaFromRGB) {
  301. this._defines.OPACITYRGB = true;
  302. }
  303. }
  304. }
  305. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  306. if (!this.reflectionTexture.isReady()) {
  307. return false;
  308. }
  309. else {
  310. needNormals = true;
  311. this._defines.REFLECTION = true;
  312. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  313. this._defines.INVERTCUBICMAP = true;
  314. }
  315. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  316. switch (this.reflectionTexture.coordinatesMode) {
  317. case BABYLON.Texture.CUBIC_MODE:
  318. case BABYLON.Texture.INVCUBIC_MODE:
  319. this._defines.REFLECTIONMAP_CUBIC = true;
  320. break;
  321. case BABYLON.Texture.EXPLICIT_MODE:
  322. this._defines.REFLECTIONMAP_EXPLICIT = true;
  323. break;
  324. case BABYLON.Texture.PLANAR_MODE:
  325. this._defines.REFLECTIONMAP_PLANAR = true;
  326. break;
  327. case BABYLON.Texture.PROJECTION_MODE:
  328. this._defines.REFLECTIONMAP_PROJECTION = true;
  329. break;
  330. case BABYLON.Texture.SKYBOX_MODE:
  331. this._defines.REFLECTIONMAP_SKYBOX = true;
  332. break;
  333. case BABYLON.Texture.SPHERICAL_MODE:
  334. this._defines.REFLECTIONMAP_SPHERICAL = true;
  335. break;
  336. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  337. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  338. break;
  339. }
  340. if (this.reflectionTexture instanceof BABYLON.HDRCubeTexture) {
  341. this._defines.USESPHERICALFROMREFLECTIONMAP = true;
  342. needNormals = true;
  343. }
  344. }
  345. }
  346. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  347. if (!this.lightmapTexture.isReady()) {
  348. return false;
  349. }
  350. else {
  351. needUVs = true;
  352. this._defines.LIGHTMAP = true;
  353. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  354. }
  355. }
  356. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  357. if (!this.emissiveTexture.isReady()) {
  358. return false;
  359. }
  360. else {
  361. needUVs = true;
  362. this._defines.EMISSIVE = true;
  363. }
  364. }
  365. if (this.reflectivityTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  366. if (!this.reflectivityTexture.isReady()) {
  367. return false;
  368. }
  369. else {
  370. needUVs = true;
  371. this._defines.REFLECTIVITY = true;
  372. this._defines.MICROSURFACEFROMREFLECTIVITYMAP = this.useMicroSurfaceFromReflectivityMapAlpha;
  373. }
  374. }
  375. }
  376. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  377. if (!this.bumpTexture.isReady()) {
  378. return false;
  379. }
  380. else {
  381. needUVs = true;
  382. this._defines.BUMP = true;
  383. }
  384. }
  385. if (this.refractionTexture && BABYLON.StandardMaterial.RefractionTextureEnabled) {
  386. if (!this.refractionTexture.isReady()) {
  387. return false;
  388. }
  389. else {
  390. needUVs = true;
  391. this._defines.REFRACTION = true;
  392. this._defines.REFRACTIONMAP_3D = this.refractionTexture.isCube;
  393. if (this.linkRefractionWithTransparency) {
  394. this._defines.LINKREFRACTIONTOTRANSPARENCY = true;
  395. }
  396. if (this.refractionTexture instanceof BABYLON.HDRCubeTexture) {
  397. this._defines.REFRACTIONMAPINLINEARSPACE = true;
  398. }
  399. }
  400. }
  401. }
  402. // Effect
  403. if (scene.clipPlane) {
  404. this._defines.CLIPPLANE = true;
  405. }
  406. if (engine.getAlphaTesting()) {
  407. this._defines.ALPHATEST = true;
  408. }
  409. if (this._shouldUseAlphaFromAlbedoTexture()) {
  410. this._defines.ALPHAFROMALBEDO = true;
  411. }
  412. if (this.useEmissiveAsIllumination) {
  413. this._defines.EMISSIVEASILLUMINATION = true;
  414. }
  415. if (this.linkEmissiveWithAlbedo) {
  416. this._defines.LINKEMISSIVEWITHALBEDO = true;
  417. }
  418. if (this.useLogarithmicDepth) {
  419. this._defines.LOGARITHMICDEPTH = true;
  420. }
  421. if (this.cameraContrast != 1) {
  422. this._defines.CAMERACONTRAST = true;
  423. }
  424. if (this.cameraExposure != 1) {
  425. this._defines.CAMERATONEMAP = true;
  426. }
  427. if (this.overloadedShadeIntensity != 1 ||
  428. this.overloadedShadowIntensity != 1) {
  429. this._defines.OVERLOADEDSHADOWVALUES = true;
  430. }
  431. if (this.overloadedMicroSurfaceIntensity > 0 ||
  432. this.overloadedEmissiveIntensity > 0 ||
  433. this.overloadedReflectivityIntensity > 0 ||
  434. this.overloadedAlbedoIntensity > 0 ||
  435. this.overloadedAmbientIntensity > 0 ||
  436. this.overloadedReflectionIntensity > 0) {
  437. this._defines.OVERLOADEDVALUES = true;
  438. }
  439. // Point size
  440. if (this.pointsCloud || scene.forcePointsCloud) {
  441. this._defines.POINTSIZE = true;
  442. }
  443. // Fog
  444. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  445. this._defines.FOG = true;
  446. }
  447. if (scene.lightsEnabled && !this.disableLighting) {
  448. needNormals = BABYLON.StandardMaterial.PrepareDefinesForLights(scene, mesh, this._defines) || needNormals;
  449. }
  450. if (BABYLON.StandardMaterial.FresnelEnabled) {
  451. // Fresnel
  452. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  453. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  454. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  455. this._defines.OPACITYFRESNEL = true;
  456. }
  457. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  458. this._defines.EMISSIVEFRESNEL = true;
  459. }
  460. needNormals = true;
  461. this._defines.FRESNEL = true;
  462. }
  463. }
  464. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  465. this._defines.SPECULAROVERALPHA = true;
  466. }
  467. // Attribs
  468. if (mesh) {
  469. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  470. this._defines.NORMAL = true;
  471. }
  472. if (needUVs) {
  473. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  474. this._defines.UV1 = true;
  475. }
  476. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  477. this._defines.UV2 = true;
  478. }
  479. }
  480. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  481. this._defines.VERTEXCOLOR = true;
  482. if (mesh.hasVertexAlpha) {
  483. this._defines.VERTEXALPHA = true;
  484. }
  485. }
  486. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  487. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  488. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  489. }
  490. // Instances
  491. if (useInstances) {
  492. this._defines.INSTANCES = true;
  493. }
  494. }
  495. // Get correct effect
  496. if (!this._defines.isEqual(this._cachedDefines)) {
  497. this._defines.cloneTo(this._cachedDefines);
  498. scene.resetCachedMaterial();
  499. // Fallbacks
  500. var fallbacks = new BABYLON.EffectFallbacks();
  501. if (this._defines.REFLECTION) {
  502. fallbacks.addFallback(0, "REFLECTION");
  503. }
  504. if (this._defines.REFLECTIVITY) {
  505. fallbacks.addFallback(0, "REFLECTIVITY");
  506. }
  507. if (this._defines.BUMP) {
  508. fallbacks.addFallback(0, "BUMP");
  509. }
  510. if (this._defines.SPECULAROVERALPHA) {
  511. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  512. }
  513. if (this._defines.FOG) {
  514. fallbacks.addFallback(1, "FOG");
  515. }
  516. if (this._defines.POINTSIZE) {
  517. fallbacks.addFallback(0, "POINTSIZE");
  518. }
  519. if (this._defines.LOGARITHMICDEPTH) {
  520. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  521. }
  522. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  523. if (!this._defines["LIGHT" + lightIndex]) {
  524. continue;
  525. }
  526. if (lightIndex > 0) {
  527. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  528. }
  529. if (this._defines["SHADOW" + lightIndex]) {
  530. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  531. }
  532. if (this._defines["SHADOWPCF" + lightIndex]) {
  533. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  534. }
  535. if (this._defines["SHADOWVSM" + lightIndex]) {
  536. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  537. }
  538. }
  539. if (this._defines.SPECULARTERM) {
  540. fallbacks.addFallback(0, "SPECULARTERM");
  541. }
  542. if (this._defines.OPACITYFRESNEL) {
  543. fallbacks.addFallback(1, "OPACITYFRESNEL");
  544. }
  545. if (this._defines.EMISSIVEFRESNEL) {
  546. fallbacks.addFallback(2, "EMISSIVEFRESNEL");
  547. }
  548. if (this._defines.FRESNEL) {
  549. fallbacks.addFallback(3, "FRESNEL");
  550. }
  551. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  552. fallbacks.addCPUSkinningFallback(0, mesh);
  553. }
  554. //Attributes
  555. var attribs = [BABYLON.VertexBuffer.PositionKind];
  556. if (this._defines.NORMAL) {
  557. attribs.push(BABYLON.VertexBuffer.NormalKind);
  558. }
  559. if (this._defines.UV1) {
  560. attribs.push(BABYLON.VertexBuffer.UVKind);
  561. }
  562. if (this._defines.UV2) {
  563. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  564. }
  565. if (this._defines.VERTEXCOLOR) {
  566. attribs.push(BABYLON.VertexBuffer.ColorKind);
  567. }
  568. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  569. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  570. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  571. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  572. attribs.push(BABYLON.VertexBuffer.MatricesIndicesExtraKind);
  573. attribs.push(BABYLON.VertexBuffer.MatricesWeightsExtraKind);
  574. }
  575. }
  576. if (this._defines.INSTANCES) {
  577. attribs.push("world0");
  578. attribs.push("world1");
  579. attribs.push("world2");
  580. attribs.push("world3");
  581. }
  582. // Legacy browser patch
  583. var shaderName = "pbr";
  584. if (!scene.getEngine().getCaps().standardDerivatives) {
  585. shaderName = "legacypbr";
  586. }
  587. var join = this._defines.toString();
  588. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vAlbedoColor", "vReflectivityColor", "vEmissiveColor", "vReflectionColor",
  589. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  590. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  591. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  592. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  593. "vFogInfos", "vFogColor", "pointSize",
  594. "vAlbedoInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vReflectivityInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  595. "mBones",
  596. "vClipPlane", "albedoMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "reflectivityMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  597. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues",
  598. "opacityParts", "emissiveLeftColor", "emissiveRightColor",
  599. "vLightingIntensity", "vOverloadedShadowIntensity", "vOverloadedIntensity", "vCameraInfos", "vOverloadedAlbedo", "vOverloadedReflection", "vOverloadedReflectivity", "vOverloadedEmissive", "vOverloadedMicroSurface",
  600. "logarithmicDepthConstant",
  601. "vSphericalX", "vSphericalY", "vSphericalZ",
  602. "vSphericalXX", "vSphericalYY", "vSphericalZZ",
  603. "vSphericalXY", "vSphericalYZ", "vSphericalZX",
  604. "vMicrosurfaceTextureLods"
  605. ], ["albedoSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "reflectivitySampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler",
  606. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  607. ], join, fallbacks, this.onCompiled, this.onError);
  608. }
  609. if (!this._effect.isReady()) {
  610. return false;
  611. }
  612. this._renderId = scene.getRenderId();
  613. this._wasPreviouslyReady = true;
  614. if (mesh) {
  615. if (!mesh._materialDefines) {
  616. mesh._materialDefines = new PBRMaterialDefines();
  617. }
  618. this._defines.cloneTo(mesh._materialDefines);
  619. }
  620. return true;
  621. };
  622. PBRMaterial.prototype.unbind = function () {
  623. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  624. this._effect.setTexture("reflection2DSampler", null);
  625. }
  626. if (this.refractionTexture && this.refractionTexture.isRenderTarget) {
  627. this._effect.setTexture("refraction2DSampler", null);
  628. }
  629. _super.prototype.unbind.call(this);
  630. };
  631. PBRMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  632. this._effect.setMatrix("world", world);
  633. };
  634. PBRMaterial.prototype.bind = function (world, mesh) {
  635. this._myScene = this.getScene();
  636. // Matrices
  637. this.bindOnlyWorldMatrix(world);
  638. // Bones
  639. BABYLON.StandardMaterial.ApplyBonesParameters(mesh, this._effect);
  640. if (this._myScene.getCachedMaterial() !== this) {
  641. this._effect.setMatrix("viewProjection", this._myScene.getTransformMatrix());
  642. if (BABYLON.StandardMaterial.FresnelEnabled) {
  643. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  644. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  645. }
  646. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  647. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  648. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  649. }
  650. }
  651. // Textures
  652. if (this._myScene.texturesEnabled) {
  653. if (this.albedoTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  654. this._effect.setTexture("albedoSampler", this.albedoTexture);
  655. this._effect.setFloat2("vAlbedoInfos", this.albedoTexture.coordinatesIndex, this.albedoTexture.level);
  656. this._effect.setMatrix("albedoMatrix", this.albedoTexture.getTextureMatrix());
  657. }
  658. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  659. this._effect.setTexture("ambientSampler", this.ambientTexture);
  660. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  661. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  662. }
  663. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  664. this._effect.setTexture("opacitySampler", this.opacityTexture);
  665. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  666. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  667. }
  668. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  669. this._microsurfaceTextureLods.x = Math.log(this.reflectionTexture.getSize().width) * Math.LOG2E;
  670. if (this.reflectionTexture.isCube) {
  671. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  672. }
  673. else {
  674. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  675. }
  676. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  677. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, 0);
  678. if (this._defines.USESPHERICALFROMREFLECTIONMAP) {
  679. this._effect.setFloat3("vSphericalX", this.reflectionTexture.sphericalPolynomial.x.x, this.reflectionTexture.sphericalPolynomial.x.y, this.reflectionTexture.sphericalPolynomial.x.z);
  680. this._effect.setFloat3("vSphericalY", this.reflectionTexture.sphericalPolynomial.y.x, this.reflectionTexture.sphericalPolynomial.y.y, this.reflectionTexture.sphericalPolynomial.y.z);
  681. this._effect.setFloat3("vSphericalZ", this.reflectionTexture.sphericalPolynomial.z.x, this.reflectionTexture.sphericalPolynomial.z.y, this.reflectionTexture.sphericalPolynomial.z.z);
  682. this._effect.setFloat3("vSphericalXX", this.reflectionTexture.sphericalPolynomial.xx.x, this.reflectionTexture.sphericalPolynomial.xx.y, this.reflectionTexture.sphericalPolynomial.xx.z);
  683. this._effect.setFloat3("vSphericalYY", this.reflectionTexture.sphericalPolynomial.yy.x, this.reflectionTexture.sphericalPolynomial.yy.y, this.reflectionTexture.sphericalPolynomial.yy.z);
  684. this._effect.setFloat3("vSphericalZZ", this.reflectionTexture.sphericalPolynomial.zz.x, this.reflectionTexture.sphericalPolynomial.zz.y, this.reflectionTexture.sphericalPolynomial.zz.z);
  685. this._effect.setFloat3("vSphericalXY", this.reflectionTexture.sphericalPolynomial.xy.x, this.reflectionTexture.sphericalPolynomial.xy.y, this.reflectionTexture.sphericalPolynomial.xy.z);
  686. this._effect.setFloat3("vSphericalYZ", this.reflectionTexture.sphericalPolynomial.yz.x, this.reflectionTexture.sphericalPolynomial.yz.y, this.reflectionTexture.sphericalPolynomial.yz.z);
  687. this._effect.setFloat3("vSphericalZX", this.reflectionTexture.sphericalPolynomial.zx.x, this.reflectionTexture.sphericalPolynomial.zx.y, this.reflectionTexture.sphericalPolynomial.zx.z);
  688. }
  689. }
  690. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  691. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  692. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  693. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  694. }
  695. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  696. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  697. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  698. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  699. }
  700. if (this.reflectivityTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  701. this._effect.setTexture("reflectivitySampler", this.reflectivityTexture);
  702. this._effect.setFloat2("vReflectivityInfos", this.reflectivityTexture.coordinatesIndex, this.reflectivityTexture.level);
  703. this._effect.setMatrix("reflectivityMatrix", this.reflectivityTexture.getTextureMatrix());
  704. }
  705. if (this.bumpTexture && this._myScene.getEngine().getCaps().standardDerivatives && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  706. this._effect.setTexture("bumpSampler", this.bumpTexture);
  707. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level);
  708. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  709. }
  710. if (this.refractionTexture && BABYLON.StandardMaterial.RefractionTextureEnabled) {
  711. this._microsurfaceTextureLods.y = Math.log(this.refractionTexture.getSize().width) * Math.LOG2E;
  712. var depth = 1.0;
  713. if (this.refractionTexture.isCube) {
  714. this._effect.setTexture("refractionCubeSampler", this.refractionTexture);
  715. }
  716. else {
  717. this._effect.setTexture("refraction2DSampler", this.refractionTexture);
  718. this._effect.setMatrix("refractionMatrix", this.refractionTexture.getReflectionTextureMatrix());
  719. if (this.refractionTexture.depth) {
  720. depth = this.refractionTexture.depth;
  721. }
  722. }
  723. this._effect.setFloat4("vRefractionInfos", this.refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  724. }
  725. if ((this.reflectionTexture || this.refractionTexture) && this._myScene.getEngine().getCaps().textureLOD) {
  726. this._effect.setFloat2("vMicrosurfaceTextureLods", this._microsurfaceTextureLods.x, this._microsurfaceTextureLods.y);
  727. }
  728. }
  729. // Clip plane
  730. if (this._myScene.clipPlane) {
  731. this._effect.setFloat4("vClipPlane", this._myScene.clipPlane.normal.x, this._myScene.clipPlane.normal.y, this._myScene.clipPlane.normal.z, this._myScene.clipPlane.d);
  732. }
  733. // Point size
  734. if (this.pointsCloud) {
  735. this._effect.setFloat("pointSize", this.pointSize);
  736. }
  737. // Colors
  738. this._myScene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  739. // GAMMA CORRECTION.
  740. this.reflectivityColor.toLinearSpaceToRef(PBRMaterial._scaledReflectivity);
  741. this._effect.setVector3("vEyePosition", this._myScene._mirroredCameraPosition ? this._myScene._mirroredCameraPosition : this._myScene.activeCamera.position);
  742. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  743. this._effect.setColor4("vReflectivityColor", PBRMaterial._scaledReflectivity, this.microSurface);
  744. // GAMMA CORRECTION.
  745. this.emissiveColor.toLinearSpaceToRef(PBRMaterial._scaledEmissive);
  746. this._effect.setColor3("vEmissiveColor", PBRMaterial._scaledEmissive);
  747. // GAMMA CORRECTION.
  748. this.reflectionColor.toLinearSpaceToRef(PBRMaterial._scaledReflection);
  749. this._effect.setColor3("vReflectionColor", PBRMaterial._scaledReflection);
  750. }
  751. if (this._myScene.getCachedMaterial() !== this || !this.isFrozen) {
  752. // GAMMA CORRECTION.
  753. this.albedoColor.toLinearSpaceToRef(PBRMaterial._scaledAlbedo);
  754. this._effect.setColor4("vAlbedoColor", PBRMaterial._scaledAlbedo, this.alpha * mesh.visibility);
  755. // Lights
  756. if (this._myScene.lightsEnabled && !this.disableLighting) {
  757. PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines);
  758. }
  759. // View
  760. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  761. this._effect.setMatrix("view", this._myScene.getViewMatrix());
  762. }
  763. // Fog
  764. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  765. this._effect.setFloat4("vFogInfos", this._myScene.fogMode, this._myScene.fogStart, this._myScene.fogEnd, this._myScene.fogDensity);
  766. this._effect.setColor3("vFogColor", this._myScene.fogColor);
  767. }
  768. this._lightingInfos.x = this.directIntensity;
  769. this._lightingInfos.y = this.emissiveIntensity;
  770. this._lightingInfos.z = this.environmentIntensity;
  771. this._lightingInfos.w = this.specularIntensity;
  772. this._effect.setVector4("vLightingIntensity", this._lightingInfos);
  773. this._overloadedShadowInfos.x = this.overloadedShadowIntensity;
  774. this._overloadedShadowInfos.y = this.overloadedShadeIntensity;
  775. this._effect.setVector4("vOverloadedShadowIntensity", this._overloadedShadowInfos);
  776. this._cameraInfos.x = this.cameraExposure;
  777. this._cameraInfos.y = this.cameraContrast;
  778. this._effect.setVector4("vCameraInfos", this._cameraInfos);
  779. this._overloadedIntensity.x = this.overloadedAmbientIntensity;
  780. this._overloadedIntensity.y = this.overloadedAlbedoIntensity;
  781. this._overloadedIntensity.z = this.overloadedReflectivityIntensity;
  782. this._overloadedIntensity.w = this.overloadedEmissiveIntensity;
  783. this._effect.setVector4("vOverloadedIntensity", this._overloadedIntensity);
  784. this.overloadedAmbient.toLinearSpaceToRef(this._tempColor);
  785. this._effect.setColor3("vOverloadedAmbient", this._tempColor);
  786. this.overloadedAlbedo.toLinearSpaceToRef(this._tempColor);
  787. this._effect.setColor3("vOverloadedAlbedo", this._tempColor);
  788. this.overloadedReflectivity.toLinearSpaceToRef(this._tempColor);
  789. this._effect.setColor3("vOverloadedReflectivity", this._tempColor);
  790. this.overloadedEmissive.toLinearSpaceToRef(this._tempColor);
  791. this._effect.setColor3("vOverloadedEmissive", this._tempColor);
  792. this.overloadedReflection.toLinearSpaceToRef(this._tempColor);
  793. this._effect.setColor3("vOverloadedReflection", this._tempColor);
  794. this._overloadedMicroSurface.x = this.overloadedMicroSurface;
  795. this._overloadedMicroSurface.y = this.overloadedMicroSurfaceIntensity;
  796. this._overloadedMicroSurface.z = this.overloadedReflectionIntensity;
  797. this._effect.setVector3("vOverloadedMicroSurface", this._overloadedMicroSurface);
  798. // Log. depth
  799. if (this._defines.LOGARITHMICDEPTH) {
  800. this._effect.setFloat("logarithmicDepthConstant", 2.0 / (Math.log(this._myScene.activeCamera.maxZ + 1.0) / Math.LN2));
  801. }
  802. }
  803. _super.prototype.bind.call(this, world, mesh);
  804. this._myScene = null;
  805. };
  806. PBRMaterial.prototype.getAnimatables = function () {
  807. var results = [];
  808. if (this.albedoTexture && this.albedoTexture.animations && this.albedoTexture.animations.length > 0) {
  809. results.push(this.albedoTexture);
  810. }
  811. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  812. results.push(this.ambientTexture);
  813. }
  814. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  815. results.push(this.opacityTexture);
  816. }
  817. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  818. results.push(this.reflectionTexture);
  819. }
  820. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  821. results.push(this.emissiveTexture);
  822. }
  823. if (this.reflectivityTexture && this.reflectivityTexture.animations && this.reflectivityTexture.animations.length > 0) {
  824. results.push(this.reflectivityTexture);
  825. }
  826. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  827. results.push(this.bumpTexture);
  828. }
  829. if (this.lightmapTexture && this.lightmapTexture.animations && this.lightmapTexture.animations.length > 0) {
  830. results.push(this.lightmapTexture);
  831. }
  832. if (this.refractionTexture && this.refractionTexture.animations && this.refractionTexture.animations.length > 0) {
  833. results.push(this.refractionTexture);
  834. }
  835. return results;
  836. };
  837. PBRMaterial.prototype.dispose = function (forceDisposeEffect) {
  838. if (this.albedoTexture) {
  839. this.albedoTexture.dispose();
  840. }
  841. if (this.ambientTexture) {
  842. this.ambientTexture.dispose();
  843. }
  844. if (this.opacityTexture) {
  845. this.opacityTexture.dispose();
  846. }
  847. if (this.reflectionTexture) {
  848. this.reflectionTexture.dispose();
  849. }
  850. if (this.emissiveTexture) {
  851. this.emissiveTexture.dispose();
  852. }
  853. if (this.reflectivityTexture) {
  854. this.reflectivityTexture.dispose();
  855. }
  856. if (this.bumpTexture) {
  857. this.bumpTexture.dispose();
  858. }
  859. if (this.lightmapTexture) {
  860. this.lightmapTexture.dispose();
  861. }
  862. if (this.refractionTexture) {
  863. this.refractionTexture.dispose();
  864. }
  865. _super.prototype.dispose.call(this, forceDisposeEffect);
  866. };
  867. PBRMaterial.prototype.clone = function (name) {
  868. var newPBRMaterial = new PBRMaterial(name, this.getScene());
  869. // Base material
  870. this.copyTo(newPBRMaterial);
  871. newPBRMaterial.directIntensity = this.directIntensity;
  872. newPBRMaterial.emissiveIntensity = this.emissiveIntensity;
  873. newPBRMaterial.environmentIntensity = this.environmentIntensity;
  874. newPBRMaterial.specularIntensity = this.specularIntensity;
  875. newPBRMaterial.cameraExposure = this.cameraExposure;
  876. newPBRMaterial.cameraContrast = this.cameraContrast;
  877. newPBRMaterial.overloadedShadowIntensity = this.overloadedShadowIntensity;
  878. newPBRMaterial.overloadedShadeIntensity = this.overloadedShadeIntensity;
  879. newPBRMaterial.overloadedAmbientIntensity = this.overloadedAmbientIntensity;
  880. newPBRMaterial.overloadedAlbedoIntensity = this.overloadedAlbedoIntensity;
  881. newPBRMaterial.overloadedReflectivityIntensity = this.overloadedReflectivityIntensity;
  882. newPBRMaterial.overloadedEmissiveIntensity = this.overloadedEmissiveIntensity;
  883. newPBRMaterial.overloadedAmbient = this.overloadedAmbient;
  884. newPBRMaterial.overloadedAlbedo = this.overloadedAlbedo;
  885. newPBRMaterial.overloadedReflectivity = this.overloadedReflectivity;
  886. newPBRMaterial.overloadedEmissive = this.overloadedEmissive;
  887. newPBRMaterial.overloadedReflection = this.overloadedReflection;
  888. newPBRMaterial.overloadedMicroSurface = this.overloadedMicroSurface;
  889. newPBRMaterial.overloadedMicroSurfaceIntensity = this.overloadedMicroSurfaceIntensity;
  890. newPBRMaterial.overloadedReflectionIntensity = this.overloadedReflectionIntensity;
  891. newPBRMaterial.disableBumpMap = this.disableBumpMap;
  892. // Standard material
  893. if (this.albedoTexture && this.albedoTexture.clone) {
  894. newPBRMaterial.albedoTexture = this.albedoTexture.clone();
  895. }
  896. if (this.ambientTexture && this.ambientTexture.clone) {
  897. newPBRMaterial.ambientTexture = this.ambientTexture.clone();
  898. }
  899. if (this.opacityTexture && this.opacityTexture.clone) {
  900. newPBRMaterial.opacityTexture = this.opacityTexture.clone();
  901. }
  902. if (this.reflectionTexture && this.reflectionTexture.clone) {
  903. newPBRMaterial.reflectionTexture = this.reflectionTexture.clone();
  904. }
  905. if (this.emissiveTexture && this.emissiveTexture.clone) {
  906. newPBRMaterial.emissiveTexture = this.emissiveTexture.clone();
  907. }
  908. if (this.reflectivityTexture && this.reflectivityTexture.clone) {
  909. newPBRMaterial.reflectivityTexture = this.reflectivityTexture.clone();
  910. }
  911. if (this.bumpTexture && this.bumpTexture.clone) {
  912. newPBRMaterial.bumpTexture = this.bumpTexture.clone();
  913. }
  914. if (this.lightmapTexture && this.lightmapTexture.clone) {
  915. newPBRMaterial.lightmapTexture = this.lightmapTexture.clone();
  916. newPBRMaterial.useLightmapAsShadowmap = this.useLightmapAsShadowmap;
  917. }
  918. if (this.refractionTexture && this.refractionTexture.clone) {
  919. newPBRMaterial.refractionTexture = this.refractionTexture.clone();
  920. newPBRMaterial.linkRefractionWithTransparency = this.linkRefractionWithTransparency;
  921. }
  922. newPBRMaterial.ambientColor = this.ambientColor.clone();
  923. newPBRMaterial.albedoColor = this.albedoColor.clone();
  924. newPBRMaterial.reflectivityColor = this.reflectivityColor.clone();
  925. newPBRMaterial.reflectionColor = this.reflectionColor.clone();
  926. newPBRMaterial.microSurface = this.microSurface;
  927. newPBRMaterial.emissiveColor = this.emissiveColor.clone();
  928. newPBRMaterial.useAlphaFromAlbedoTexture = this.useAlphaFromAlbedoTexture;
  929. newPBRMaterial.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
  930. newPBRMaterial.useMicroSurfaceFromReflectivityMapAlpha = this.useMicroSurfaceFromReflectivityMapAlpha;
  931. newPBRMaterial.useSpecularOverAlpha = this.useSpecularOverAlpha;
  932. newPBRMaterial.indexOfRefraction = this.indexOfRefraction;
  933. newPBRMaterial.invertRefractionY = this.invertRefractionY;
  934. newPBRMaterial.emissiveFresnelParameters = this.emissiveFresnelParameters.clone();
  935. newPBRMaterial.opacityFresnelParameters = this.opacityFresnelParameters.clone();
  936. return newPBRMaterial;
  937. };
  938. PBRMaterial.prototype.serialize = function () {
  939. var serializationObject = _super.prototype.serialize.call(this);
  940. serializationObject.customType = "BABYLON.PBRMaterial";
  941. serializationObject.directIntensity = this.directIntensity;
  942. serializationObject.emissiveIntensity = this.emissiveIntensity;
  943. serializationObject.environmentIntensity = this.environmentIntensity;
  944. serializationObject.specularIntensity = this.specularIntensity;
  945. serializationObject.cameraExposure = this.cameraExposure;
  946. serializationObject.cameraContrast = this.cameraContrast;
  947. serializationObject.overloadedShadowIntensity = this.overloadedShadowIntensity;
  948. serializationObject.overloadedShadeIntensity = this.overloadedShadeIntensity;
  949. serializationObject.overloadedAmbientIntensity = this.overloadedAmbientIntensity;
  950. serializationObject.overloadedAlbedoIntensity = this.overloadedAlbedoIntensity;
  951. serializationObject.overloadedReflectivityIntensity = this.overloadedReflectivityIntensity;
  952. serializationObject.overloadedEmissiveIntensity = this.overloadedEmissiveIntensity;
  953. serializationObject.overloadedAmbient = this.overloadedAmbient.asArray();
  954. serializationObject.overloadedAlbedo = this.overloadedAlbedo.asArray();
  955. serializationObject.overloadedReflectivity = this.overloadedReflectivity.asArray();
  956. serializationObject.overloadedEmissive = this.overloadedEmissive.asArray();
  957. serializationObject.overloadedReflection = this.overloadedReflection.asArray();
  958. serializationObject.overloadedMicroSurface = this.overloadedMicroSurface;
  959. serializationObject.overloadedMicroSurfaceIntensity = this.overloadedMicroSurfaceIntensity;
  960. serializationObject.overloadedReflectionIntensity = this.overloadedReflectionIntensity;
  961. serializationObject.disableBumpMap = this.disableBumpMap;
  962. // Standard material
  963. if (this.albedoTexture) {
  964. serializationObject.albedoTexture = this.albedoTexture.serialize();
  965. }
  966. if (this.ambientTexture) {
  967. serializationObject.ambientTexture = this.ambientTexture.serialize();
  968. }
  969. if (this.opacityTexture) {
  970. serializationObject.opacityTexture = this.opacityTexture.serialize();
  971. }
  972. if (this.reflectionTexture) {
  973. serializationObject.reflectionTexture = this.reflectionTexture.serialize();
  974. }
  975. if (this.emissiveTexture) {
  976. serializationObject.emissiveTexture = this.emissiveTexture.serialize();
  977. }
  978. if (this.reflectivityTexture) {
  979. serializationObject.reflectivityTexture = this.reflectivityTexture.serialize();
  980. }
  981. if (this.bumpTexture) {
  982. serializationObject.bumpTexture = this.bumpTexture.serialize();
  983. }
  984. if (this.lightmapTexture) {
  985. serializationObject.lightmapTexture = this.lightmapTexture.serialize();
  986. serializationObject.useLightmapAsShadowmap = this.useLightmapAsShadowmap;
  987. }
  988. if (this.refractionTexture) {
  989. serializationObject.refractionTexture = this.refractionTexture;
  990. serializationObject.linkRefractionWithTransparency = this.linkRefractionWithTransparency;
  991. }
  992. serializationObject.ambientColor = this.ambientColor.asArray();
  993. serializationObject.albedoColor = this.albedoColor.asArray();
  994. serializationObject.reflectivityColor = this.reflectivityColor.asArray();
  995. serializationObject.reflectionColor = this.reflectionColor.asArray();
  996. serializationObject.microSurface = this.microSurface;
  997. serializationObject.emissiveColor = this.emissiveColor.asArray();
  998. serializationObject.useAlphaFromAlbedoTexture = this.useAlphaFromAlbedoTexture;
  999. serializationObject.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
  1000. serializationObject.useMicroSurfaceFromReflectivityMapAlpha = this.useMicroSurfaceFromReflectivityMapAlpha;
  1001. serializationObject.useSpecularOverAlpha = this.useSpecularOverAlpha;
  1002. serializationObject.indexOfRefraction = this.indexOfRefraction;
  1003. serializationObject.invertRefractionY = this.invertRefractionY;
  1004. serializationObject.emissiveFresnelParameters = this.emissiveFresnelParameters.serialize();
  1005. serializationObject.opacityFresnelParameters = this.opacityFresnelParameters.serialize();
  1006. return serializationObject;
  1007. };
  1008. PBRMaterial.Parse = function (source, scene, rootUrl) {
  1009. var material = new PBRMaterial(source.name, scene);
  1010. material.alpha = source.alpha;
  1011. material.id = source.id;
  1012. if (source.disableDepthWrite) {
  1013. material.disableDepthWrite = source.disableDepthWrite;
  1014. }
  1015. if (source.checkReadyOnlyOnce) {
  1016. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  1017. }
  1018. BABYLON.Tags.AddTagsTo(material, source.tags);
  1019. material.backFaceCulling = source.backFaceCulling;
  1020. material.wireframe = source.wireframe;
  1021. material.directIntensity = source.directIntensity;
  1022. material.emissiveIntensity = source.emissiveIntensity;
  1023. material.environmentIntensity = source.environmentIntensity;
  1024. material.specularIntensity = source.specularIntensity;
  1025. material.cameraExposure = source.cameraExposure;
  1026. material.cameraContrast = source.cameraContrast;
  1027. material.overloadedShadowIntensity = source.overloadedShadowIntensity;
  1028. material.overloadedShadeIntensity = source.overloadedShadeIntensity;
  1029. material.overloadedAmbientIntensity = source.overloadedAmbientIntensity;
  1030. material.overloadedAlbedoIntensity = source.overloadedAlbedoIntensity;
  1031. material.overloadedReflectivityIntensity = source.overloadedReflectivityIntensity;
  1032. material.overloadedEmissiveIntensity = source.overloadedEmissiveIntensity;
  1033. material.overloadedAmbient = BABYLON.Color3.FromArray(source.overloadedAmbient);
  1034. material.overloadedAlbedo = BABYLON.Color3.FromArray(source.overloadedAlbedo);
  1035. material.overloadedReflectivity = BABYLON.Color3.FromArray(source.overloadedReflectivity);
  1036. material.overloadedEmissive = BABYLON.Color3.FromArray(source.overloadedEmissive);
  1037. material.overloadedReflection = BABYLON.Color3.FromArray(source.overloadedReflection);
  1038. material.overloadedMicroSurface = source.overloadedMicroSurface;
  1039. material.overloadedMicroSurfaceIntensity = source.overloadedMicroSurfaceIntensity;
  1040. material.overloadedReflectionIntensity = source.overloadedReflectionIntensity;
  1041. material.disableBumpMap = source.disableBumpMap;
  1042. // Standard material
  1043. if (source.albedoTexture) {
  1044. material.albedoTexture = BABYLON.Texture.Parse(source.albedoTexture, scene, rootUrl);
  1045. }
  1046. if (source.ambientTexture) {
  1047. material.ambientTexture = BABYLON.Texture.Parse(source.ambientTexture, scene, rootUrl);
  1048. }
  1049. if (source.opacityTexture) {
  1050. material.opacityTexture = BABYLON.Texture.Parse(source.opacityTexture, scene, rootUrl);
  1051. }
  1052. if (source.reflectionTexture) {
  1053. material.reflectionTexture = BABYLON.Texture.Parse(source.reflectionTexture, scene, rootUrl);
  1054. }
  1055. if (source.emissiveTexture) {
  1056. material.emissiveTexture = BABYLON.Texture.Parse(source.emissiveTexture, scene, rootUrl);
  1057. }
  1058. if (source.reflectivityTexture) {
  1059. material.reflectivityTexture = BABYLON.Texture.Parse(source.reflectivityTexture, scene, rootUrl);
  1060. }
  1061. if (source.bumpTexture) {
  1062. material.bumpTexture = BABYLON.Texture.Parse(source.bumpTexture, scene, rootUrl);
  1063. }
  1064. if (source.lightmapTexture) {
  1065. material.lightmapTexture = BABYLON.Texture.Parse(source.lightmapTexture, scene, rootUrl);
  1066. material.useLightmapAsShadowmap = source.useLightmapAsShadowmap;
  1067. }
  1068. if (source.refractionTexture) {
  1069. material.refractionTexture = BABYLON.Texture.Parse(source.refractionTexture, scene, rootUrl);
  1070. material.linkRefractionWithTransparency = source.linkRefractionWithTransparency;
  1071. }
  1072. material.ambientColor = BABYLON.Color3.FromArray(source.ambient);
  1073. material.albedoColor = BABYLON.Color3.FromArray(source.albedo);
  1074. material.reflectivityColor = BABYLON.Color3.FromArray(source.reflectivity);
  1075. material.reflectionColor = BABYLON.Color3.FromArray(source.reflectionColor);
  1076. material.microSurface = source.microSurface;
  1077. material.emissiveColor = BABYLON.Color3.FromArray(source.emissive);
  1078. material.useAlphaFromAlbedoTexture = source.useAlphaFromAlbedoTexture;
  1079. material.useEmissiveAsIllumination = source.useEmissiveAsIllumination;
  1080. material.useMicroSurfaceFromReflectivityMapAlpha = source.useMicroSurfaceFromReflectivityMapAlpha;
  1081. material.useSpecularOverAlpha = source.useSpecularOverAlpha;
  1082. material.indexOfRefraction = source.indexOfRefraction;
  1083. material.invertRefractionY = source.invertRefractionY;
  1084. material.emissiveFresnelParameters = BABYLON.FresnelParameters.Parse(source.emissiveFresnelParameters);
  1085. material.opacityFresnelParameters = BABYLON.FresnelParameters.Parse(source.opacityFresnelParameters);
  1086. return material;
  1087. };
  1088. PBRMaterial._scaledAlbedo = new BABYLON.Color3();
  1089. PBRMaterial._scaledReflectivity = new BABYLON.Color3();
  1090. PBRMaterial._scaledEmissive = new BABYLON.Color3();
  1091. PBRMaterial._scaledReflection = new BABYLON.Color3();
  1092. return PBRMaterial;
  1093. })(BABYLON.Material);
  1094. BABYLON.PBRMaterial = PBRMaterial;
  1095. })(BABYLON || (BABYLON = {}));
  1096. 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#ifdef CLIPPLANE\nuniform vec4 vClipPlane;\nvarying float fClipDistance;\n#endif\n\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#ifdef LOGARITHMICDEPTH\nuniform float logarithmicDepthConstant;\nvarying float vFragmentDepth;\n#endif\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#ifdef CLIPPLANE\n fClipDistance = dot(worldPos, vClipPlane);\n#endif\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#ifdef LOGARITHMICDEPTH\n vFragmentDepth = 1.0 + gl_Position.w;\n gl_Position.z = log2(max(0.000001, vFragmentDepth)) * logarithmicDepthConstant;\n#endif\n}";
  1097. 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;\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 computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)\n{\n float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;\n\n float reflectivityLuminance = getLuminance(reflectivityColor);\n float reflectivityLuma = sqrt(reflectivityLuminance);\n microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;\n\n return microSurface;\n}\n\nvec3 toLinearSpace(vec3 color)\n{\n return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));\n}\n\nvec3 toGammaSpace(vec3 color)\n{\n return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));\n}\n\n#ifdef CAMERATONEMAP\n vec3 toneMaps(vec3 color)\n {\n color = max(color, 0.0);\n\n // TONE MAPPING / EXPOSURE\n color.rgb = color.rgb * vCameraInfos.x;\n\n float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately\n // PI Test\n // tuning *= kPi;\n vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper\n color.rgb = mix(color.rgb, tonemapped, 1.0);\n return color;\n }\n#endif\n\n#ifdef CAMERACONTRAST\n vec4 contrasts(vec4 color)\n {\n color = clamp(color, 0.0, 1.0);\n\n vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);\n float contrast = vCameraInfos.y;\n if (contrast < 1.0)\n {\n // Decrease contrast: interpolate towards zero-contrast image (flat grey)\n color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);\n }\n else\n {\n // Increase contrast: apply simple shoulder-toe high contrast curve\n color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);\n }\n\n return color;\n }\n#endif\n// END PBR HELPER METHODS\n\n uniform vec4 vReflectivityColor;\n uniform vec3 vEmissiveColor;\n\n// Input\nvarying vec3 vPositionW;\n\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n// Lights\n#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#ifdef FRESNEL\nfloat computeFresnelTerm(vec3 viewDirection, vec3 worldNormal, float bias, float power)\n{\n float fresnelTerm = pow(bias + abs(dot(viewDirection, worldNormal)), power);\n return clamp(fresnelTerm, 0., 1.);\n}\n#endif\n\n#ifdef OPACITYFRESNEL\nuniform vec4 opacityParts;\n#endif\n\n#ifdef EMISSIVEFRESNEL\nuniform vec4 emissiveLeftColor;\nuniform vec4 emissiveRightColor;\n#endif\n\n// 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\nvec3 computeReflectionCoords(vec4 worldPos, vec3 worldNormal)\n{\n#ifdef REFLECTIONMAP_EQUIRECTANGULAR_FIXED\n vec3 direction = normalize(vDirectionW);\n\n float t = clamp(direction.y * -0.5 + 0.5, 0., 1.0);\n float s = atan(direction.z, direction.x) * RECIPROCAL_PI2 + 0.5;\n\n return vec3(s, t, 0);\n#endif\n\n#ifdef REFLECTIONMAP_EQUIRECTANGULAR\n\n\tvec3 cameraToVertex = normalize(worldPos.xyz - vEyePosition);\n\tvec3 r = reflect(cameraToVertex, worldNormal);\n\tfloat t = clamp(r.y * -0.5 + 0.5, 0., 1.0);\n\tfloat s = atan(r.z, r.x) * RECIPROCAL_PI2 + 0.5;\n\n\treturn vec3(s, t, 0);\n#endif\n\n#ifdef REFLECTIONMAP_SPHERICAL\n vec3 viewDir = normalize(vec3(view * worldPos));\n vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));\n\n vec3 r = reflect(viewDir, viewNormal);\n r.z = r.z - 1.0;\n\n float m = 2.0 * length(r);\n\n return vec3(r.x / m + 0.5, 1.0 - r.y / m - 0.5, 0);\n#endif\n\n#ifdef REFLECTIONMAP_PLANAR\n vec3 viewDir = worldPos.xyz - vEyePosition;\n vec3 coords = normalize(reflect(viewDir, worldNormal));\n\n return vec3(reflectionMatrix * vec4(coords, 1));\n#endif\n\n#ifdef REFLECTIONMAP_CUBIC\n vec3 viewDir = worldPos.xyz - vEyePosition;\n vec3 coords = reflect(viewDir, worldNormal);\n#ifdef INVERTCUBICMAP\n coords.y = 1.0 - coords.y;\n#endif\n return vec3(reflectionMatrix * vec4(coords, 0));\n#endif\n\n#ifdef REFLECTIONMAP_PROJECTION\n return vec3(reflectionMatrix * (view * worldPos));\n#endif\n\n#ifdef REFLECTIONMAP_SKYBOX\n return vPositionUVW;\n#endif\n\n#ifdef REFLECTIONMAP_EXPLICIT\n return vec3(0, 0, 0);\n#endif\n}\n\n#endif\n\n// Shadows\n#ifdef SHADOWS\n\nfloat unpack(vec4 color)\n{\n const vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);\n return dot(color, bit_shift);\n}\n\n#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)\nuniform vec2 depthValues;\n\nfloat computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, float bias)\n{\n\tvec3 directionToLight = vPositionW - lightPosition;\n\tfloat depth = length(directionToLight);\n\tdepth = clamp(depth, 0., 1.0);\n\n\tdirectionToLight = normalize(directionToLight);\n\tdirectionToLight.y = - directionToLight.y;\n\n\tfloat shadow = unpack(textureCube(shadowSampler, directionToLight)) + bias;\n\n if (depth > shadow)\n {\n#ifdef OVERLOADEDSHADOWVALUES\n return mix(1.0, darkness, vOverloadedShadowIntensity.x);\n#else\n return darkness;\n#endif\n }\n return 1.0;\n}\n\nfloat computeShadowWithPCFCube(vec3 lightPosition, samplerCube shadowSampler, float mapSize, float bias, float darkness)\n{\n 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// Bump\n#ifdef BUMP\nvarying vec2 vBumpUV;\nuniform vec2 vBumpInfos;\nuniform sampler2D bumpSampler;\n\n// Thanks to http://www.thetenthplanet.de/archives/1180\nmat3 cotangent_frame(vec3 normal, vec3 p, vec2 uv)\n{\n // get edge vectors of the pixel triangle\n vec3 dp1 = dFdx(p);\n vec3 dp2 = dFdy(p);\n vec2 duv1 = dFdx(uv);\n vec2 duv2 = dFdy(uv);\n\n // solve the linear system\n vec3 dp2perp = cross(dp2, normal);\n vec3 dp1perp = cross(normal, dp1);\n vec3 tangent = dp2perp * duv1.x + dp1perp * duv2.x;\n vec3 binormal = dp2perp * duv1.y + dp1perp * duv2.y;\n\n // construct a scale-invariant frame \n float invmax = inversesqrt(max(dot(tangent, tangent), dot(binormal, binormal)));\n return mat3(tangent * invmax, binormal * invmax, normal);\n}\n\nvec3 perturbNormal(vec3 viewDir)\n{\n vec3 map = texture2D(bumpSampler, vBumpUV).xyz;\n map = map * 255. / 127. - 128. / 127.;\n mat3 TBN = cotangent_frame(vNormalW * vBumpInfos.y, -viewDir, vBumpUV);\n return normalize(TBN * map);\n}\n#endif\n\n#ifdef CLIPPLANE\nvarying float fClipDistance;\n#endif\n\n#ifdef LOGARITHMICDEPTH\nuniform float logarithmicDepthConstant;\nvarying float vFragmentDepth;\n#endif\n\n// Fog\n#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) {\n lightingInfo result;\n\n vec3 lightVectorW;\n float attenuation = 1.0;\n if (lightData.w == 0.)\n {\n vec3 direction = lightData.xyz - vPositionW;\n\n attenuation = max(0., 1.0 - length(direction) / range);\n lightVectorW = normalize(direction);\n }\n else\n {\n lightVectorW = normalize(-lightData.xyz);\n }\n\n // diffuse\n vec3 H = normalize(viewDirectionW + lightVectorW);\n float NdotL = max(0.00000000001, dot(vNormal, lightVectorW));\n float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));\n\n float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);\n result.diffuse = diffuseTerm * diffuseColor * attenuation;\n\n#ifdef SPECULARTERM\n // Specular\n float NdotH = max(0.00000000001, dot(vNormal, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * attenuation;\n#endif\n\n return result;\n}\n\nlightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 direction = lightData.xyz - vPositionW;\n vec3 lightVectorW = normalize(direction);\n float attenuation = max(0., 1.0 - length(direction) / range);\n\n // diffuse\n float cosAngle = max(0.0000001, dot(-lightDirection.xyz, lightVectorW));\n float spotAtten = 0.0;\n\n if (cosAngle >= lightDirection.w)\n {\n cosAngle = max(0., pow(cosAngle, lightData.w));\n spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);\n\n // Diffuse\n vec3 H = normalize(viewDirectionW - lightDirection.xyz);\n float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));\n float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);\n\n float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);\n result.diffuse = diffuseTerm * diffuseColor * attenuation * spotAtten;\n\n#ifdef SPECULARTERM\n // Specular\n float NdotH = max(0.00000000001, dot(vNormal, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * attenuation * spotAtten;\n#endif\n\n return result;\n }\n\n result.diffuse = vec3(0.);\n#ifdef SPECULARTERM\n result.specular = vec3(0.);\n#endif\n\n return result;\n}\n\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 lightVectorW = normalize(lightData.xyz);\n\n // Diffuse\n float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;\n result.diffuse = mix(groundColor, diffuseColor, ndl);\n\n#ifdef SPECULARTERM\n // Specular\n vec3 H = normalize(viewDirectionW + lightVectorW);\n float NdotH = max(0.00000000001, dot(vNormal, H));\n float NdotL = max(0.00000000001, ndl);\n float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm;\n#endif\n\n return result;\n}\n\nvoid main(void) {\n #ifdef PoissonSamplingEnvironment\n initSamplers();\n #endif\n\n // Clip plane\n #ifdef CLIPPLANE\n if (fClipDistance > 0.0)\n discard;\n #endif\n\n vec3 viewDirectionW = normalize(vEyePosition - vPositionW);\n\n // Albedo\n vec4 surfaceAlbedo = vec4(1., 1., 1., 1.);\n vec3 surfaceAlbedoContribution = vAlbedoColor.rgb;\n \n // Alpha\n float alpha = vAlbedoColor.a;\n\n #ifdef ALBEDO\n surfaceAlbedo = texture2D(albedoSampler, vAlbedoUV);\n surfaceAlbedo = vec4(toLinearSpace(surfaceAlbedo.rgb), surfaceAlbedo.a);\n\n #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 microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);\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 // Call rough to not conflict with previous one.\n float rough = clamp(1. - microSurface, 0.000001, 1.0);\n\n // Lighting\n vec3 lightDiffuseContribution = vec3(0., 0., 0.);\n \n#ifdef OVERLOADEDSHADOWVALUES\n vec3 shadowedOnlyLightDiffuseContribution = vec3(1., 1., 1.);\n#endif\n\n#ifdef SPECULARTERM\n vec3 lightSpecularContribution= vec3(0., 0., 0.);\n#endif\n float notShadowLevel = 1.; // 1 - shadowLevel\n\n#ifdef LIGHT0\n#ifndef SPECULARTERM\n vec3 vLightSpecular0 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT0\n lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);\n#endif\n#ifdef HEMILIGHT0\n lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, rough, NdotV);\n#endif\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\n lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);\n#endif\n#ifdef SHADOW0\n#ifdef SHADOWVSM0\n notShadowLevel = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);\n#else\n#ifdef SHADOWPCF0\n#if defined(POINTLIGHT0)\n notShadowLevel = computeShadowWithPCFCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\n#else\n notShadowLevel = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\n#endif\n#else\n#if defined(POINTLIGHT0)\n notShadowLevel = computeShadowCube(vLightData0.xyz, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\n#else\n notShadowLevel = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\n#endif\n#endif\n#endif\n#else\n notShadowLevel = 1.;\n#endif\n lightDiffuseContribution += info.diffuse * notShadowLevel;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution *= notShadowLevel;\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution += info.specular * notShadowLevel;\n#endif\n#endif\n\n#ifdef LIGHT1\n#ifndef SPECULARTERM\n vec3 vLightSpecular1 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT1\n info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);\n#endif\n#ifdef HEMILIGHT1\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, rough, NdotV);\n#endif\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\n info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);\n#endif\n#ifdef SHADOW1\n#ifdef SHADOWVSM1\n notShadowLevel = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);\n#else\n#ifdef SHADOWPCF1\n#if defined(POINTLIGHT1)\n notShadowLevel = computeShadowWithPCFCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\n#else\n notShadowLevel = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\n#endif\n#else\n#if defined(POINTLIGHT1)\n notShadowLevel = computeShadowCube(vLightData1.xyz, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\n#else\n notShadowLevel = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\n#endif\n#endif\n#endif\n#else\n notShadowLevel = 1.;\n#endif\n\n lightDiffuseContribution += info.diffuse * notShadowLevel;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution *= notShadowLevel;\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution += info.specular * notShadowLevel;\n#endif\n#endif\n\n#ifdef LIGHT2\n#ifndef SPECULARTERM\n vec3 vLightSpecular2 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT2\n info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);\n#endif\n#ifdef HEMILIGHT2\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, rough, NdotV);\n#endif\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\n info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);\n#endif\n#ifdef SHADOW2\n#ifdef SHADOWVSM2\n notShadowLevel = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);\n#else\n#ifdef SHADOWPCF2\n#if defined(POINTLIGHT2)\n notShadowLevel = computeShadowWithPCFCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\n#else\n notShadowLevel = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\n#endif\n#else\n#if defined(POINTLIGHT2)\n notShadowLevel = computeShadowCube(vLightData2.xyz, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\n#else\n notShadowLevel = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\n#endif\n#endif\t\n#endif\t\n#else\n notShadowLevel = 1.;\n#endif\n\n lightDiffuseContribution += info.diffuse * notShadowLevel;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution *= notShadowLevel;\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution += info.specular * notShadowLevel;\n#endif\n#endif\n\n#ifdef LIGHT3\n#ifndef SPECULARTERM\n vec3 vLightSpecular3 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT3\n info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);\n#endif\n#ifdef HEMILIGHT3\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, rough, NdotV);\n#endif\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\n info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);\n#endif\n#ifdef SHADOW3\n#ifdef SHADOWVSM3\n notShadowLevel = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);\n#else\n#ifdef SHADOWPCF3\n#if defined(POINTLIGHT3)\n notShadowLevel = computeShadowWithPCFCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\n#else\n notShadowLevel = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\n#endif\n#else\n#if defined(POINTLIGHT3)\n notShadowLevel = computeShadowCube(vLightData3.xyz, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\n#else\n notShadowLevel = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\n#endif\n#endif\t\n#endif\t\n#else\n notShadowLevel = 1.;\n#endif\n\n lightDiffuseContribution += info.diffuse * notShadowLevel;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyLightDiffuseContribution *= notShadowLevel;\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution += info.specular * notShadowLevel;\n#endif\n#endif\n\n#ifdef SPECULARTERM\n lightSpecularContribution *= vLightingIntensity.w;\n#endif\n\n#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#ifndef LODBASEDMICROSFURACE\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, Square(1.0 - microSurface + 0.0005));\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).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, Square(1.0 - microSurface + 0.0005));\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 float alphaG = convertRoughnessToAverageSlope(rough);\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).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 OVERLOADEDSHADOWVALUES\n finalSpecular = mix(finalSpecular, vec3(0.0), (1.0 - vOverloadedShadowIntensity.y));\n#endif\n\n#ifdef SPECULAROVERALPHA\n alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);\n#endif\n\n// Composition\n// Reflection already includes the environment intensity.\n#ifdef EMISSIVEASILLUMINATION\n vec4 finalColor = vec4(finalDiffuse * ambientColor * vLightingIntensity.x + surfaceAlbedo.rgb * environmentIrradiance + finalSpecular * vLightingIntensity.x + environmentRadiance + surfaceEmissiveColor * vLightingIntensity.y + 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<fogFragment>(color, finalColor)\n\n gl_FragColor = finalColor;\n}";
  1098. 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#ifdef CLIPPLANE\nuniform vec4 vClipPlane;\nvarying float fClipDistance;\n#endif\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\t// Clip plane\n#ifdef CLIPPLANE\n\tfClipDistance = dot(worldPos, vClipPlane);\n#endif\n\n\t// Vertex color\n#ifdef VERTEXCOLOR\n\tvColor = color;\n#endif\n}";
  1099. BABYLON.Effect.ShadersStore['legacypbrPixelShader'] = "precision mediump float;\n\n// Constants\n#define RECIPROCAL_PI2 0.15915494\n#define FRESNEL_MAXIMUM_ON_ROUGH 0.25\n\nuniform vec3 vEyePosition;\nuniform vec3 vAmbientColor;\nuniform vec4 vAlbedoColor;\nuniform vec3 vReflectionColor;\n\n// CUSTOM CONTROLS\nuniform vec4 vLightingIntensity;\nuniform vec4 vCameraInfos;\n\n#ifdef OVERLOADEDVALUES\nuniform vec4 vOverloadedIntensity;\nuniform vec3 vOverloadedAmbient;\nuniform vec3 vOverloadedAlbedo;\nuniform vec3 vOverloadedReflectivity;\nuniform vec3 vOverloadedEmissive;\nuniform vec3 vOverloadedReflection;\nuniform vec3 vOverloadedMicroSurface;\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\nuniform vec4 vOverloadedShadowIntensity;\n#endif\n\n// PBR CUSTOM CONSTANTS\nconst float kPi = 3.1415926535897932384626433832795;\n\n// PBR HELPER METHODS\nfloat Square(float value)\n{\n return value * value;\n}\n\nfloat getLuminance(vec3 color)\n{\n return clamp(dot(color, vec3(0.2126, 0.7152, 0.0722)), 0., 1.);\n}\n\nfloat convertRoughnessToAverageSlope(float roughness)\n{\n // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues\n const float kMinimumVariance = 0.0005;\n float alphaG = Square(roughness) + kMinimumVariance;\n return alphaG;\n}\n\n// From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007\nfloat smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)\n{\n float tanSquared = (1.0 - dot * dot) / (dot * dot);\n return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));\n}\n\nfloat smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)\n{\n return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);\n}\n\n// Trowbridge-Reitz (GGX)\n// Generalised Trowbridge-Reitz with gamma power=2.0\nfloat normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)\n{\n // Note: alphaG is average slope (gradient) of the normals in slope-space.\n // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have\n // a tangent (gradient) closer to the macrosurface than this slope.\n float a2 = Square(alphaG);\n float d = NdotH * NdotH * (a2 - 1.0) + 1.0;\n return a2 / (kPi * d * d);\n}\n\nvec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)\n{\n return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0);\n}\n\nvec3 FresnelSchlickEnvironmentGGX(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)\n{\n // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle\n float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);\n return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);\n}\n\n// Cook Torance Specular computation.\nvec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor)\n{\n float alphaG = convertRoughnessToAverageSlope(roughness);\n float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);\n float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG);\n visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes.\n\n vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.));\n\n float specTerm = max(0., visibility * distribution) * NdotL;\n return fresnel * specTerm;\n}\n\nfloat computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness)\n{\n // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of\n // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.\n float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);\n float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);\n float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;\n float diffuseFresnelTerm =\n (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *\n (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);\n\n return diffuseFresnelTerm * NdotL;\n}\n\nfloat computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)\n{\n if (microSurface == 0.)\n {\n float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;\n\n float reflectivityLuminance = getLuminance(reflectivityColor);\n float reflectivityLuma = sqrt(reflectivityLuminance);\n microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;\n }\n return microSurface;\n}\n\nvec3 toLinearSpace(vec3 color)\n{\n return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2));\n}\n\nvec3 toGammaSpace(vec3 color)\n{\n return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2));\n}\n\n#ifdef CAMERATONEMAP\n vec3 toneMaps(vec3 color)\n {\n color = max(color, 0.0);\n\n // TONE MAPPING / EXPOSURE\n color.rgb = color.rgb * vCameraInfos.x;\n\n float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately\n vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper\n color.rgb = mix(color.rgb, tonemapped, 1.0);\n return color;\n }\n#endif\n\n#ifdef CAMERACONTRAST\n vec4 contrasts(vec4 color)\n {\n color = clamp(color, 0.0, 1.0);\n\n vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb);\n float contrast = vCameraInfos.y;\n if (contrast < 1.0)\n {\n // Decrease contrast: interpolate towards zero-contrast image (flat grey)\n color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast);\n }\n else\n {\n // Increase contrast: apply simple shoulder-toe high contrast curve\n color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0);\n }\n\n return color;\n }\n#endif\n// END PBR HELPER METHODS\n\nuniform vec4 vReflectivityColor;\nuniform vec3 vEmissiveColor;\n\n// Input\nvarying vec3 vPositionW;\n\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n// Lights\n#ifdef LIGHT0\nuniform vec4 vLightData0;\nuniform vec4 vLightDiffuse0;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular0;\n#endif\n#ifdef SHADOW0\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\nvarying vec4 vPositionFromLight0;\nuniform sampler2D shadowSampler0;\n#else\nuniform samplerCube shadowSampler0;\n#endif\nuniform vec3 shadowsInfo0;\n#endif\n#ifdef SPOTLIGHT0\nuniform vec4 vLightDirection0;\n#endif\n#ifdef HEMILIGHT0\nuniform vec3 vLightGround0;\n#endif\n#endif\n\n#ifdef LIGHT1\nuniform vec4 vLightData1;\nuniform vec4 vLightDiffuse1;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular1;\n#endif\n#ifdef SHADOW1\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\nvarying vec4 vPositionFromLight1;\nuniform sampler2D shadowSampler1;\n#else\nuniform samplerCube shadowSampler1;\n#endif\nuniform vec3 shadowsInfo1;\n#endif\n#ifdef SPOTLIGHT1\nuniform vec4 vLightDirection1;\n#endif\n#ifdef HEMILIGHT1\nuniform vec3 vLightGround1;\n#endif\n#endif\n\n#ifdef LIGHT2\nuniform vec4 vLightData2;\nuniform vec4 vLightDiffuse2;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular2;\n#endif\n#ifdef SHADOW2\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\nvarying vec4 vPositionFromLight2;\nuniform sampler2D shadowSampler2;\n#else\nuniform samplerCube shadowSampler2;\n#endif\nuniform vec3 shadowsInfo2;\n#endif\n#ifdef SPOTLIGHT2\nuniform vec4 vLightDirection2;\n#endif\n#ifdef HEMILIGHT2\nuniform vec3 vLightGround2;\n#endif\n#endif\n\n#ifdef LIGHT3\nuniform vec4 vLightData3;\nuniform vec4 vLightDiffuse3;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular3;\n#endif\n#ifdef SHADOW3\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\nvarying vec4 vPositionFromLight3;\nuniform sampler2D shadowSampler3;\n#else\nuniform samplerCube shadowSampler3;\n#endif\nuniform vec3 shadowsInfo3;\n#endif\n#ifdef SPOTLIGHT3\nuniform vec4 vLightDirection3;\n#endif\n#ifdef HEMILIGHT3\nuniform vec3 vLightGround3;\n#endif\n#endif\n\n// Samplers\n#ifdef ALBEDO\nvarying vec2 vAlbedoUV;\nuniform sampler2D albedoSampler;\nuniform vec2 vAlbedoInfos;\n#endif\n\n#ifdef AMBIENT\nvarying vec2 vAmbientUV;\nuniform sampler2D ambientSampler;\nuniform vec2 vAmbientInfos;\n#endif\n\n#ifdef OPACITY\t\nvarying vec2 vOpacityUV;\nuniform sampler2D opacitySampler;\nuniform vec2 vOpacityInfos;\n#endif\n\n#ifdef EMISSIVE\nvarying vec2 vEmissiveUV;\nuniform vec2 vEmissiveInfos;\nuniform sampler2D emissiveSampler;\n#endif\n\n#ifdef LIGHTMAP\nvarying vec2 vLightmapUV;\nuniform vec2 vLightmapInfos;\nuniform sampler2D lightmapSampler;\n#endif\n\n#if defined(REFLECTIVITY)\nvarying vec2 vReflectivityUV;\nuniform vec2 vReflectivityInfos;\nuniform sampler2D reflectivitySampler;\n#endif\n\n#ifdef CLIPPLANE\nvarying float fClipDistance;\n#endif\n\n// Light Computing\nstruct lightingInfo\n{\n vec3 diffuse;\n#ifdef SPECULARTERM\n vec3 specular;\n#endif\n};\n\nlightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 lightVectorW;\n float attenuation = 1.0;\n if (lightData.w == 0.)\n {\n vec3 direction = lightData.xyz - vPositionW;\n\n attenuation = max(0., 1.0 - length(direction) / range);\n lightVectorW = normalize(direction);\n }\n else\n {\n lightVectorW = normalize(-lightData.xyz);\n }\n\n // diffuse\n vec3 H = normalize(viewDirectionW + lightVectorW);\n float NdotL = max(0.00000000001, dot(vNormal, lightVectorW));\n float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));\n\n float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);\n result.diffuse = diffuseTerm * diffuseColor * attenuation;\n\n#ifdef SPECULARTERM\n // Specular\n float NdotH = max(0.00000000001, dot(vNormal, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * specularColor * attenuation;\n#endif\n\n return result;\n}\n\nlightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, vec3 specularColor, float range, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 direction = lightData.xyz - vPositionW;\n vec3 lightVectorW = normalize(direction);\n float attenuation = max(0., 1.0 - length(direction) / range);\n\n // diffuse\n float cosAngle = max(0.0000001, dot(-lightDirection.xyz, lightVectorW));\n float spotAtten = 0.0;\n\n if (cosAngle >= lightDirection.w)\n {\n cosAngle = max(0., pow(cosAngle, lightData.w));\n spotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);\n\n // Diffuse\n vec3 H = normalize(viewDirectionW - lightDirection.xyz);\n float NdotL = max(0.00000000001, dot(vNormal, -lightDirection.xyz));\n float VdotH = clamp(dot(viewDirectionW, H), 0.00000000001, 1.0);\n\n float diffuseTerm = computeDiffuseTerm(NdotL, NdotV, VdotH, roughness);\n result.diffuse = diffuseTerm * diffuseColor * attenuation * spotAtten;\n\n#ifdef SPECULARTERM\n // Specular\n float NdotH = max(0.00000000001, dot(vNormal, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * specularColor * attenuation * spotAtten;\n#endif\n\n return result;\n }\n\n result.diffuse = vec3(0.);\n#ifdef SPECULARTERM\n result.specular = vec3(0.);\n#endif\n\n return result;\n}\n\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 specularColor, vec3 groundColor, float roughness, float NdotV) {\n lightingInfo result;\n\n vec3 lightVectorW = normalize(lightData.xyz);\n\n // Diffuse\n float ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;\n result.diffuse = mix(groundColor, diffuseColor, ndl);\n\n#ifdef SPECULARTERM\n // Specular\n vec3 H = normalize(viewDirectionW + lightVectorW);\n float NdotH = max(0.00000000001, dot(vNormal, H));\n float NdotL = max(0.00000000001, ndl);\n float VdotH = clamp(0.00000000001, 1.0, dot(viewDirectionW, H));\n\n vec3 specTerm = computeSpecularTerm(NdotH, NdotL, NdotV, VdotH, roughness, specularColor);\n result.specular = specTerm * specularColor;\n#endif\n\n return result;\n}\n\nvoid main(void) {\n // Clip plane\n#ifdef CLIPPLANE\n if (fClipDistance > 0.0)\n discard;\n#endif\n\n vec3 viewDirectionW = normalize(vEyePosition - vPositionW);\n\n // Base color\n vec4 baseColor = vec4(1., 1., 1., 1.);\n vec3 diffuseColor = vAlbedoColor.rgb;\n \n // Alpha\n float alpha = vAlbedoColor.a;\n\n#ifdef ALBEDO\n baseColor = texture2D(diffuseSampler, vAlbedoUV);\n baseColor = vec4(toLinearSpace(baseColor.rgb), baseColor.a);\n\n#ifdef ALPHATEST\n if (baseColor.a < 0.4)\n discard;\n#endif\n\n#ifdef ALPHAFROMALBEDO\n alpha *= baseColor.a;\n#endif\n\n baseColor.rgb *= vAlbedoInfos.y;\n#endif\n\n#ifdef VERTEXCOLOR\n baseColor.rgb *= vColor.rgb;\n#endif\n\n#ifdef OVERLOADEDVALUES\n baseColor.rgb = mix(baseColor.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);\n albedoColor.rgb = mix(albedoColor.rgb, vOverloadedAlbedo, vOverloadedIntensity.y);\n#endif\n\n // Bump\n#ifdef NORMAL\n vec3 normalW = normalize(vNormalW);\n#else\n vec3 normalW = vec3(1.0, 1.0, 1.0);\n#endif\n\n // Ambient color\n vec3 baseAmbientColor = vec3(1., 1., 1.);\n\n#ifdef AMBIENT\n baseAmbientColor = texture2D(ambientSampler, vAmbientUV).rgb * vAmbientInfos.y;\n #ifdef OVERLOADEDVALUES\n baseAmbientColor.rgb = mix(baseAmbientColor.rgb, vOverloadedAmbient, vOverloadedIntensity.x);\n #endif\n#endif\n\n // Reflectivity map\n float microSurface = vReflectivityColor.a;\n vec3 reflectivityColor = vReflectivityColor.rgb;\n\n #ifdef OVERLOADEDVALUES\n reflectivityColor.rgb = mix(reflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);\n #endif\n\n #ifdef REFLECTIVITY\n vec4 reflectivityMapColor = texture2D(reflectivitySampler, vReflectivityUV);\n reflectivityColor = toLinearSpace(reflectivityMapColor.rgb);\n\n #ifdef OVERLOADEDVALUES\n reflectivityColor.rgb = mix(reflectivityColor.rgb, vOverloadedReflectivity, vOverloadedIntensity.z);\n #endif\n\n #ifdef MICROSURFACEFROMREFLECTIVITYMAP\n microSurface = reflectivityMapColor.a;\n #else\n microSurface = computeDefaultMicroSurface(microSurface, reflectivityColor);\n #endif\n #endif\n\n #ifdef OVERLOADEDVALUES\n microSurface = mix(microSurface, vOverloadedMicroSurface.x, vOverloadedMicroSurface.y);\n #endif\n\n // Apply Energy Conservation taking in account the environment level only if the environment is present.\n float reflectance = max(max(reflectivityColor.r, reflectivityColor.g), reflectivityColor.b);\n baseColor.rgb = (1. - reflectance) * baseColor.rgb;\n\n // Compute Specular Fresnel + Reflectance.\n float NdotV = max(0.00000000001, dot(normalW, viewDirectionW));\n\n // Adapt microSurface.\n microSurface = clamp(microSurface, 0., 1.) * 0.98;\n\n // Call rough to not conflict with previous one.\n float rough = clamp(1. - microSurface, 0.000001, 1.0);\n\n // Lighting\n vec3 diffuseBase = vec3(0., 0., 0.);\n\n#ifdef OVERLOADEDSHADOWVALUES\n vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);\n#endif\n\n#ifdef SPECULARTERM\n vec3 specularBase = vec3(0., 0., 0.);\n#endif\n float shadow = 1.;\n\n#ifdef LIGHT0\n#ifndef SPECULARTERM\n vec3 vLightSpecular0 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT0\n lightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);\n#endif\n#ifdef HEMILIGHT0\n lightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightGround0, rough, NdotV);\n#endif\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\n lightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightSpecular0, vLightDiffuse0.a, rough, NdotV);\n#endif\n\n shadow = 1.;\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\n#ifdef LIGHT1\n#ifndef SPECULARTERM\n vec3 vLightSpecular1 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT1\n info = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);\n#endif\n#ifdef HEMILIGHT1\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightGround1, rough, NdotV);\n#endif\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\n info = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightSpecular1, vLightDiffuse1.a, rough, NdotV);\n#endif\n\n shadow = 1.;\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\n#ifdef LIGHT2\n#ifndef SPECULARTERM\n vec3 vLightSpecular2 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT2\n info = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);\n#endif\n#ifdef HEMILIGHT2\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightGround2, rough, NdotV);\n#endif\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\n info = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightSpecular2, vLightDiffuse2.a, rough, NdotV);\n#endif\n\n shadow = 1.;\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\n#ifdef LIGHT3\n#ifndef SPECULARTERM\n vec3 vLightSpecular3 = vec3(0.0);\n#endif\n#ifdef SPOTLIGHT3\n info = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);\n#endif\n#ifdef HEMILIGHT3\n info = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightGround3, rough, NdotV);\n#endif\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\n info = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightSpecular3, vLightDiffuse3.a, rough, NdotV);\n#endif\n\n shadow = 1.;\n diffuseBase += info.diffuse * shadow;\n#ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase *= shadow;\n#endif\n\n#ifdef SPECULARTERM\n specularBase += info.specular * shadow;\n#endif\n#endif\n\n// Reflection\nvec3 reflectionColor = vReflectionColor.rgb;\nvec3 ambientReflectionColor = vReflectionColor.rgb;\n\nreflectionColor *= vLightingIntensity.z;\nambientReflectionColor *= vLightingIntensity.z;\n\n// Compute reflection reflectivity fresnel\nvec3 reflectivityEnvironmentR0 = reflectivityColor.rgb;\nvec3 reflectivityEnvironmentR90 = vec3(1.0, 1.0, 1.0);\nvec3 reflectivityEnvironmentReflectanceViewer = FresnelSchlickEnvironmentGGX(clamp(NdotV, 0., 1.), reflectivityEnvironmentR0, reflectivityEnvironmentR90, sqrt(microSurface));\nreflectionColor *= reflectivityEnvironmentReflectanceViewer;\n\n#ifdef OVERLOADEDVALUES\n ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedMicroSurface.z);\n reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedMicroSurface.z);\n#endif\n\n#ifdef OPACITY\n vec4 opacityMap = texture2D(opacitySampler, vOpacityUV);\n\n#ifdef OPACITYRGB\n opacityMap.rgb = opacityMap.rgb * vec3(0.3, 0.59, 0.11);\n alpha *= (opacityMap.x + opacityMap.y + opacityMap.z)* vOpacityInfos.y;\n#else\n alpha *= opacityMap.a * vOpacityInfos.y;\n#endif\n\n#endif\n\n#ifdef VERTEXALPHA\n alpha *= vColor.a;\n#endif\n\n // Emissive\n vec3 emissiveColor = vEmissiveColor;\n#ifdef EMISSIVE\n vec3 emissiveColorTex = texture2D(emissiveSampler, vEmissiveUV).rgb;\n emissiveColor = toLinearSpace(emissiveColorTex.rgb) * emissiveColor * vEmissiveInfos.y;\n#endif\n\n#ifdef OVERLOADEDVALUES\n emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);\n#endif\n\n // Composition\n#ifdef EMISSIVEASILLUMINATION\n vec3 finalDiffuse = max(diffuseBase * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;\n\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n#else\n #ifdef LINKEMISSIVEWITHALBEDO\n vec3 finalDiffuse = max((diffuseBase + emissiveColor) * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * albedoColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n #else\n vec3 finalDiffuse = max(diffuseBase * albedoColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\n #ifdef OVERLOADEDSHADOWVALUES\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * albedoColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\n #endif\n #endif\n#endif\n\n#ifdef OVERLOADEDSHADOWVALUES\n finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));\n#endif\n\n// diffuse lighting from environment 0.2 replaces Harmonic...\n// Ambient Reflection already includes the environment intensity.\nfinalDiffuse += baseColor.rgb * ambientReflectionColor * 0.2;\n\n#ifdef SPECULARTERM\n vec3 finalSpecular = specularBase * reflectivityColor * vLightingIntensity.w;\n#else\n vec3 finalSpecular = vec3(0.0);\n#endif\n\n#ifdef SPECULAROVERALPHA\n alpha = clamp(alpha + dot(finalSpecular, vec3(0.3, 0.59, 0.11)), 0., 1.);\n#endif\n\n// Composition\n// Reflection already includes the environment intensity.\n#ifdef EMISSIVEASILLUMINATION\n vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor + emissiveColor * vLightingIntensity.y, alpha);\n#else\n vec4 color = vec4(finalDiffuse * baseAmbientColor * vLightingIntensity.x + finalSpecular * vLightingIntensity.x + reflectionColor, alpha);\n#endif\n\n color = max(color, 0.0);\n\n#ifdef CAMERATONEMAP\n color.rgb = toneMaps(color.rgb);\n#endif\n\n color.rgb = toGammaSpace(color.rgb);\n\n#ifdef CAMERACONTRAST\n color = contrasts(color);\n#endif\n\n gl_FragColor = color;\n}";