babylon.pbrMaterial.js 122 KB

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