babylon.pbrMaterial.js 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var maxSimultaneousLights = 4;
  5. var PBRMaterialDefines = (function (_super) {
  6. __extends(PBRMaterialDefines, _super);
  7. function PBRMaterialDefines() {
  8. _super.call(this);
  9. this.DIFFUSE = false;
  10. this.AMBIENT = false;
  11. this.OPACITY = false;
  12. this.OPACITYRGB = false;
  13. this.REFLECTION = false;
  14. this.EMISSIVE = false;
  15. this.SPECULAR = false;
  16. this.BUMP = false;
  17. this.SPECULAROVERALPHA = false;
  18. this.CLIPPLANE = false;
  19. this.ALPHATEST = false;
  20. this.ALPHAFROMDIFFUSE = false;
  21. this.POINTSIZE = false;
  22. this.FOG = false;
  23. this.LIGHT0 = false;
  24. this.LIGHT1 = false;
  25. this.LIGHT2 = false;
  26. this.LIGHT3 = false;
  27. this.SPOTLIGHT0 = false;
  28. this.SPOTLIGHT1 = false;
  29. this.SPOTLIGHT2 = false;
  30. this.SPOTLIGHT3 = false;
  31. this.HEMILIGHT0 = false;
  32. this.HEMILIGHT1 = false;
  33. this.HEMILIGHT2 = false;
  34. this.HEMILIGHT3 = false;
  35. this.POINTLIGHT0 = false;
  36. this.POINTLIGHT1 = false;
  37. this.POINTLIGHT2 = false;
  38. this.POINTLIGHT3 = false;
  39. this.DIRLIGHT0 = false;
  40. this.DIRLIGHT1 = false;
  41. this.DIRLIGHT2 = false;
  42. this.DIRLIGHT3 = false;
  43. this.SPECULARTERM = false;
  44. this.SHADOW0 = false;
  45. this.SHADOW1 = false;
  46. this.SHADOW2 = false;
  47. this.SHADOW3 = false;
  48. this.SHADOWS = false;
  49. this.SHADOWVSM0 = false;
  50. this.SHADOWVSM1 = false;
  51. this.SHADOWVSM2 = false;
  52. this.SHADOWVSM3 = false;
  53. this.SHADOWPCF0 = false;
  54. this.SHADOWPCF1 = false;
  55. this.SHADOWPCF2 = false;
  56. this.SHADOWPCF3 = false;
  57. this.OPACITYFRESNEL = false;
  58. this.EMISSIVEFRESNEL = false;
  59. this.FRESNEL = false;
  60. this.NORMAL = false;
  61. this.UV1 = false;
  62. this.UV2 = false;
  63. this.VERTEXCOLOR = false;
  64. this.VERTEXALPHA = false;
  65. this.NUM_BONE_INFLUENCERS = 0;
  66. this.BonesPerMesh = 0;
  67. this.INSTANCES = false;
  68. this.GLOSSINESSFROMSPECULARMAP = false;
  69. this.EMISSIVEASILLUMINATION = false;
  70. this.LINKEMISSIVEWITHDIFFUSE = false;
  71. this.LIGHTMAP = false;
  72. this.USELIGHTMAPASSHADOWMAP = false;
  73. this.REFLECTIONMAP_3D = false;
  74. this.REFLECTIONMAP_SPHERICAL = false;
  75. this.REFLECTIONMAP_PLANAR = false;
  76. this.REFLECTIONMAP_CUBIC = false;
  77. this.REFLECTIONMAP_PROJECTION = false;
  78. this.REFLECTIONMAP_SKYBOX = false;
  79. this.REFLECTIONMAP_EXPLICIT = false;
  80. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  81. this.INVERTCUBICMAP = false;
  82. this.LOGARITHMICDEPTH = false;
  83. this.CAMERATONEMAP = false;
  84. this.CAMERACONTRAST = false;
  85. this.OVERLOADEDVALUES = false;
  86. this.OVERLOADEDSHADOWVALUES = false;
  87. this._keys = Object.keys(this);
  88. }
  89. return PBRMaterialDefines;
  90. })(BABYLON.MaterialDefines);
  91. var PBRMaterial = (function (_super) {
  92. __extends(PBRMaterial, _super);
  93. function PBRMaterial(name, scene) {
  94. var _this = this;
  95. _super.call(this, name, scene);
  96. this.directIntensity = 1.0;
  97. this.emissiveIntensity = 1.0;
  98. this.environmentIntensity = 1.0;
  99. this._lightingInfos = new BABYLON.Vector4(this.directIntensity, this.emissiveIntensity, this.environmentIntensity, 0.0);
  100. this.overloadedShadowIntensity = 1.0;
  101. this.overloadedShadeIntensity = 1.0;
  102. this._overloadedShadowInfos = new BABYLON.Vector4(this.overloadedShadowIntensity, this.overloadedShadeIntensity, 0.0, 0.0);
  103. this.cameraExposure = 1.0;
  104. this.cameraContrast = 1.0;
  105. this._cameraInfos = new BABYLON.Vector4(1.0, 1.0, 0.0, 0.0);
  106. this.overloadedAmbientIntensity = 0.0;
  107. this.overloadedDiffuseIntensity = 0.0;
  108. this.overloadedSpecularIntensity = 0.0;
  109. this.overloadedEmissiveIntensity = 0.0;
  110. this._overloadedIntensity = new BABYLON.Vector4(this.overloadedAmbientIntensity, this.overloadedDiffuseIntensity, this.overloadedSpecularIntensity, this.overloadedEmissiveIntensity);
  111. this.overloadedAmbient = BABYLON.Color3.White();
  112. this.overloadedDiffuse = BABYLON.Color3.White();
  113. this.overloadedSpecular = BABYLON.Color3.White();
  114. this.overloadedEmissive = BABYLON.Color3.White();
  115. this.overloadedReflection = BABYLON.Color3.White();
  116. this.overloadedGlossiness = 0.0;
  117. this.overloadedGlossinessIntensity = 0.0;
  118. this.overloadedReflectionIntensity = 0.0;
  119. this._overloadedGlossiness = new BABYLON.Vector3(this.overloadedGlossiness, this.overloadedGlossinessIntensity, this.overloadedReflectionIntensity);
  120. this.disableBumpMap = false;
  121. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  122. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  123. this.specularColor = new BABYLON.Color3(1, 1, 1);
  124. this.reflectionColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  125. this.glossiness = 0.5;
  126. this.emissiveColor = new BABYLON.Color3(0, 0, 0);
  127. this.useAlphaFromDiffuseTexture = false;
  128. this.useEmissiveAsIllumination = false;
  129. this.linkEmissiveWithDiffuse = false;
  130. this.useSpecularOverAlpha = true;
  131. this.disableLighting = false;
  132. this.useLightmapAsShadowmap = false;
  133. this.useGlossinessFromSpecularMapAlpha = false;
  134. this._renderTargets = new BABYLON.SmartArray(16);
  135. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  136. this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  137. this._tempColor = new BABYLON.Color3();
  138. this._defines = new PBRMaterialDefines();
  139. this._cachedDefines = new PBRMaterialDefines();
  140. this._myScene = null;
  141. this._myShadowGenerator = null;
  142. this._cachedDefines.BonesPerMesh = -1;
  143. this.getRenderTargetTextures = function () {
  144. _this._renderTargets.reset();
  145. if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
  146. _this._renderTargets.push(_this.reflectionTexture);
  147. }
  148. return _this._renderTargets;
  149. };
  150. }
  151. Object.defineProperty(PBRMaterial.prototype, "useLogarithmicDepth", {
  152. get: function () {
  153. return this._useLogarithmicDepth;
  154. },
  155. set: function (value) {
  156. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  157. },
  158. enumerable: true,
  159. configurable: true
  160. });
  161. PBRMaterial.prototype.needAlphaBlending = function () {
  162. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromDiffuseTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  163. };
  164. PBRMaterial.prototype.needAlphaTesting = function () {
  165. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha;
  166. };
  167. PBRMaterial.prototype._shouldUseAlphaFromDiffuseTexture = function () {
  168. return this.diffuseTexture != null && this.diffuseTexture.hasAlpha && this.useAlphaFromDiffuseTexture;
  169. };
  170. PBRMaterial.prototype.getAlphaTestTexture = function () {
  171. return this.diffuseTexture;
  172. };
  173. PBRMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  174. if (!mesh) {
  175. return true;
  176. }
  177. if (this._defines.INSTANCES !== useInstances) {
  178. return false;
  179. }
  180. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  181. return true;
  182. }
  183. return false;
  184. };
  185. PBRMaterial.PrepareDefinesForLights = function (scene, mesh, defines) {
  186. var lightIndex = 0;
  187. var needNormals = false;
  188. for (var index = 0; index < scene.lights.length; index++) {
  189. var light = scene.lights[index];
  190. if (!light.isEnabled()) {
  191. continue;
  192. }
  193. // Excluded check
  194. if (light._excludedMeshesIds.length > 0) {
  195. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  196. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  197. if (excludedMesh) {
  198. light.excludedMeshes.push(excludedMesh);
  199. }
  200. }
  201. light._excludedMeshesIds = [];
  202. }
  203. // Included check
  204. if (light._includedOnlyMeshesIds.length > 0) {
  205. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  206. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  207. if (includedOnlyMesh) {
  208. light.includedOnlyMeshes.push(includedOnlyMesh);
  209. }
  210. }
  211. light._includedOnlyMeshesIds = [];
  212. }
  213. if (!light.canAffectMesh(mesh)) {
  214. continue;
  215. }
  216. needNormals = true;
  217. defines["LIGHT" + lightIndex] = true;
  218. var type;
  219. if (light instanceof BABYLON.SpotLight) {
  220. type = "SPOTLIGHT" + lightIndex;
  221. }
  222. else if (light instanceof BABYLON.HemisphericLight) {
  223. type = "HEMILIGHT" + lightIndex;
  224. }
  225. else if (light instanceof BABYLON.PointLight) {
  226. type = "POINTLIGHT" + lightIndex;
  227. }
  228. else {
  229. type = "DIRLIGHT" + lightIndex;
  230. }
  231. defines[type] = true;
  232. // Specular
  233. if (!light.specular.equalsFloats(0, 0, 0)) {
  234. defines["SPECULARTERM"] = true;
  235. }
  236. // Shadows
  237. if (scene.shadowsEnabled) {
  238. var shadowGenerator = light.getShadowGenerator();
  239. if (mesh && mesh.receiveShadows && shadowGenerator) {
  240. defines["SHADOW" + lightIndex] = true;
  241. defines["SHADOWS"] = true;
  242. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  243. defines["SHADOWVSM" + lightIndex] = true;
  244. }
  245. if (shadowGenerator.usePoissonSampling) {
  246. defines["SHADOWPCF" + lightIndex] = true;
  247. }
  248. }
  249. }
  250. lightIndex++;
  251. if (lightIndex === maxSimultaneousLights)
  252. break;
  253. }
  254. return needNormals;
  255. };
  256. PBRMaterial.BindLights = function (scene, mesh, effect, defines) {
  257. var lightIndex = 0;
  258. for (var index = 0; index < scene.lights.length; index++) {
  259. var light = scene.lights[index];
  260. if (!light.isEnabled()) {
  261. continue;
  262. }
  263. if (!light.canAffectMesh(mesh)) {
  264. continue;
  265. }
  266. if (light instanceof BABYLON.PointLight) {
  267. // Point Light
  268. light.transferToEffect(effect, "vLightData" + lightIndex);
  269. }
  270. else if (light instanceof BABYLON.DirectionalLight) {
  271. // Directional Light
  272. light.transferToEffect(effect, "vLightData" + lightIndex);
  273. }
  274. else if (light instanceof BABYLON.SpotLight) {
  275. // Spot Light
  276. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  277. }
  278. else if (light instanceof BABYLON.HemisphericLight) {
  279. // Hemispheric Light
  280. light.transferToEffect(effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  281. }
  282. // GAMMA CORRECTION.
  283. light.diffuse.toLinearSpaceToRef(PBRMaterial._scaledDiffuse);
  284. PBRMaterial._scaledDiffuse.scaleToRef(light.intensity, PBRMaterial._scaledDiffuse);
  285. light.diffuse.scaleToRef(light.intensity, PBRMaterial._scaledDiffuse);
  286. effect.setColor4("vLightDiffuse" + lightIndex, PBRMaterial._scaledDiffuse, light.range);
  287. if (defines["SPECULARTERM"]) {
  288. light.specular.toLinearSpaceToRef(PBRMaterial._scaledSpecular);
  289. PBRMaterial._scaledSpecular.scaleToRef(light.intensity, PBRMaterial._scaledSpecular);
  290. effect.setColor3("vLightSpecular" + lightIndex, PBRMaterial._scaledSpecular);
  291. }
  292. // Shadows
  293. if (scene.shadowsEnabled) {
  294. var shadowGenerator = light.getShadowGenerator();
  295. if (mesh.receiveShadows && shadowGenerator) {
  296. if (!light.needCube()) {
  297. effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  298. }
  299. effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  300. effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  301. }
  302. }
  303. lightIndex++;
  304. if (lightIndex === maxSimultaneousLights)
  305. break;
  306. }
  307. };
  308. PBRMaterial.prototype.isReady = function (mesh, useInstances) {
  309. if (this.checkReadyOnlyOnce) {
  310. if (this._wasPreviouslyReady) {
  311. return true;
  312. }
  313. }
  314. var scene = this.getScene();
  315. if (!this.checkReadyOnEveryCall) {
  316. if (this._renderId === scene.getRenderId()) {
  317. if (this._checkCache(scene, mesh, useInstances)) {
  318. return true;
  319. }
  320. }
  321. }
  322. var engine = scene.getEngine();
  323. var needNormals = false;
  324. var needUVs = false;
  325. this._defines.reset();
  326. // Textures
  327. if (scene.texturesEnabled) {
  328. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  329. if (!this.diffuseTexture.isReady()) {
  330. return false;
  331. }
  332. else {
  333. needUVs = true;
  334. this._defines.DIFFUSE = true;
  335. }
  336. }
  337. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  338. if (!this.ambientTexture.isReady()) {
  339. return false;
  340. }
  341. else {
  342. needUVs = true;
  343. this._defines.AMBIENT = true;
  344. }
  345. }
  346. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  347. if (!this.opacityTexture.isReady()) {
  348. return false;
  349. }
  350. else {
  351. needUVs = true;
  352. this._defines.OPACITY = true;
  353. if (this.opacityTexture.getAlphaFromRGB) {
  354. this._defines.OPACITYRGB = true;
  355. }
  356. }
  357. }
  358. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  359. if (!this.reflectionTexture.isReady()) {
  360. return false;
  361. }
  362. else {
  363. needNormals = true;
  364. this._defines.REFLECTION = true;
  365. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  366. this._defines.INVERTCUBICMAP = true;
  367. }
  368. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  369. switch (this.reflectionTexture.coordinatesMode) {
  370. case BABYLON.Texture.CUBIC_MODE:
  371. case BABYLON.Texture.INVCUBIC_MODE:
  372. this._defines.REFLECTIONMAP_CUBIC = true;
  373. break;
  374. case BABYLON.Texture.EXPLICIT_MODE:
  375. this._defines.REFLECTIONMAP_EXPLICIT = true;
  376. break;
  377. case BABYLON.Texture.PLANAR_MODE:
  378. this._defines.REFLECTIONMAP_PLANAR = true;
  379. break;
  380. case BABYLON.Texture.PROJECTION_MODE:
  381. this._defines.REFLECTIONMAP_PROJECTION = true;
  382. break;
  383. case BABYLON.Texture.SKYBOX_MODE:
  384. this._defines.REFLECTIONMAP_SKYBOX = true;
  385. break;
  386. case BABYLON.Texture.SPHERICAL_MODE:
  387. this._defines.REFLECTIONMAP_SPHERICAL = true;
  388. break;
  389. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  390. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  391. break;
  392. }
  393. }
  394. }
  395. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapEnabled) {
  396. if (!this.lightmapTexture.isReady()) {
  397. return false;
  398. }
  399. else {
  400. needUVs = true;
  401. this._defines.LIGHTMAP = true;
  402. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  403. }
  404. }
  405. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  406. if (!this.emissiveTexture.isReady()) {
  407. return false;
  408. }
  409. else {
  410. needUVs = true;
  411. this._defines.EMISSIVE = true;
  412. }
  413. }
  414. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  415. if (!this.specularTexture.isReady()) {
  416. return false;
  417. }
  418. else {
  419. needUVs = true;
  420. this._defines.SPECULAR = true;
  421. this._defines.GLOSSINESSFROMSPECULARMAP = this.useGlossinessFromSpecularMapAlpha;
  422. }
  423. }
  424. }
  425. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  426. if (!this.bumpTexture.isReady()) {
  427. return false;
  428. }
  429. else {
  430. needUVs = true;
  431. this._defines.BUMP = true;
  432. }
  433. }
  434. // Effect
  435. if (scene.clipPlane) {
  436. this._defines.CLIPPLANE = true;
  437. }
  438. if (engine.getAlphaTesting()) {
  439. this._defines.ALPHATEST = true;
  440. }
  441. if (this._shouldUseAlphaFromDiffuseTexture()) {
  442. this._defines.ALPHAFROMDIFFUSE = true;
  443. }
  444. if (this.useEmissiveAsIllumination) {
  445. this._defines.EMISSIVEASILLUMINATION = true;
  446. }
  447. if (this.linkEmissiveWithDiffuse) {
  448. this._defines.LINKEMISSIVEWITHDIFFUSE = true;
  449. }
  450. if (this.useLogarithmicDepth) {
  451. this._defines.LOGARITHMICDEPTH = true;
  452. }
  453. if (this.cameraContrast != 1) {
  454. this._defines.CAMERACONTRAST = true;
  455. }
  456. if (this.cameraExposure != 1) {
  457. this._defines.CAMERATONEMAP = true;
  458. }
  459. if (this.overloadedShadeIntensity != 1 ||
  460. this.overloadedShadowIntensity != 1) {
  461. this._defines.OVERLOADEDSHADOWVALUES = true;
  462. }
  463. if (this.overloadedGlossinessIntensity > 0 ||
  464. this.overloadedEmissiveIntensity > 0 ||
  465. this.overloadedSpecularIntensity > 0 ||
  466. this.overloadedDiffuseIntensity > 0 ||
  467. this.overloadedAmbientIntensity > 0 ||
  468. this.overloadedReflectionIntensity > 0) {
  469. this._defines.OVERLOADEDVALUES = true;
  470. }
  471. // Point size
  472. if (this.pointsCloud || scene.forcePointsCloud) {
  473. this._defines.POINTSIZE = true;
  474. }
  475. // Fog
  476. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  477. this._defines.FOG = true;
  478. }
  479. if (scene.lightsEnabled && !this.disableLighting) {
  480. needNormals = PBRMaterial.PrepareDefinesForLights(scene, mesh, this._defines);
  481. }
  482. if (BABYLON.StandardMaterial.FresnelEnabled) {
  483. // Fresnel
  484. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  485. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  486. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  487. this._defines.OPACITYFRESNEL = true;
  488. }
  489. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  490. this._defines.EMISSIVEFRESNEL = true;
  491. }
  492. needNormals = true;
  493. this._defines.FRESNEL = true;
  494. }
  495. }
  496. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  497. this._defines.SPECULAROVERALPHA = true;
  498. }
  499. // Attribs
  500. if (mesh) {
  501. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  502. this._defines.NORMAL = true;
  503. }
  504. if (needUVs) {
  505. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  506. this._defines.UV1 = true;
  507. }
  508. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  509. this._defines.UV2 = true;
  510. }
  511. }
  512. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  513. this._defines.VERTEXCOLOR = true;
  514. if (mesh.hasVertexAlpha) {
  515. this._defines.VERTEXALPHA = true;
  516. }
  517. }
  518. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  519. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  520. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  521. }
  522. // Instances
  523. if (useInstances) {
  524. this._defines.INSTANCES = true;
  525. }
  526. }
  527. // Get correct effect
  528. if (!this._defines.isEqual(this._cachedDefines)) {
  529. this._defines.cloneTo(this._cachedDefines);
  530. scene.resetCachedMaterial();
  531. // Fallbacks
  532. var fallbacks = new BABYLON.EffectFallbacks();
  533. if (this._defines.REFLECTION) {
  534. fallbacks.addFallback(0, "REFLECTION");
  535. }
  536. if (this._defines.SPECULAR) {
  537. fallbacks.addFallback(0, "SPECULAR");
  538. }
  539. if (this._defines.BUMP) {
  540. fallbacks.addFallback(0, "BUMP");
  541. }
  542. if (this._defines.SPECULAROVERALPHA) {
  543. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  544. }
  545. if (this._defines.FOG) {
  546. fallbacks.addFallback(1, "FOG");
  547. }
  548. if (this._defines.POINTSIZE) {
  549. fallbacks.addFallback(0, "POINTSIZE");
  550. }
  551. if (this._defines.LOGARITHMICDEPTH) {
  552. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  553. }
  554. for (var lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  555. if (!this._defines["LIGHT" + lightIndex]) {
  556. continue;
  557. }
  558. if (lightIndex > 0) {
  559. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  560. }
  561. if (this._defines["SHADOW" + lightIndex]) {
  562. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  563. }
  564. if (this._defines["SHADOWPCF" + lightIndex]) {
  565. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  566. }
  567. if (this._defines["SHADOWVSM" + lightIndex]) {
  568. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  569. }
  570. }
  571. if (this._defines.SPECULARTERM) {
  572. fallbacks.addFallback(0, "SPECULARTERM");
  573. }
  574. if (this._defines.OPACITYFRESNEL) {
  575. fallbacks.addFallback(1, "OPACITYFRESNEL");
  576. }
  577. if (this._defines.EMISSIVEFRESNEL) {
  578. fallbacks.addFallback(2, "EMISSIVEFRESNEL");
  579. }
  580. if (this._defines.FRESNEL) {
  581. fallbacks.addFallback(3, "FRESNEL");
  582. }
  583. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  584. fallbacks.addCPUSkinningFallback(0, mesh);
  585. }
  586. //Attributes
  587. var attribs = [BABYLON.VertexBuffer.PositionKind];
  588. if (this._defines.NORMAL) {
  589. attribs.push(BABYLON.VertexBuffer.NormalKind);
  590. }
  591. if (this._defines.UV1) {
  592. attribs.push(BABYLON.VertexBuffer.UVKind);
  593. }
  594. if (this._defines.UV2) {
  595. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  596. }
  597. if (this._defines.VERTEXCOLOR) {
  598. attribs.push(BABYLON.VertexBuffer.ColorKind);
  599. }
  600. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  601. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  602. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  603. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  604. attribs.push(BABYLON.VertexBuffer.MatricesIndicesExtraKind);
  605. attribs.push(BABYLON.VertexBuffer.MatricesWeightsExtraKind);
  606. }
  607. }
  608. if (this._defines.INSTANCES) {
  609. attribs.push("world0");
  610. attribs.push("world1");
  611. attribs.push("world2");
  612. attribs.push("world3");
  613. }
  614. // Legacy browser patch
  615. var shaderName = "pbr";
  616. if (!scene.getEngine().getCaps().standardDerivatives) {
  617. shaderName = "legacypbr";
  618. }
  619. var join = this._defines.toString();
  620. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vDiffuseColor", "vSpecularColor", "vEmissiveColor", "vReflectionColor",
  621. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  622. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  623. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  624. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  625. "vFogInfos", "vFogColor", "pointSize",
  626. "vDiffuseInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vSpecularInfos", "vBumpInfos", "vLightmapInfos",
  627. "mBones",
  628. "vClipPlane", "diffuseMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "specularMatrix", "bumpMatrix", "lightmapMatrix",
  629. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  630. "opacityParts", "emissiveLeftColor", "emissiveRightColor",
  631. "vLightingIntensity", "vOverloadedShadowIntensity", "vOverloadedIntensity", "vCameraInfos", "vOverloadedDiffuse", "vOverloadedReflection", "vOverloadedSpecular", "vOverloadedEmissive", "vOverloadedGlossiness",
  632. "logarithmicDepthConstant"
  633. ], ["diffuseSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "specularSampler", "bumpSampler", "lightmapSampler",
  634. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  635. ], join, fallbacks, this.onCompiled, this.onError);
  636. }
  637. if (!this._effect.isReady()) {
  638. return false;
  639. }
  640. this._renderId = scene.getRenderId();
  641. this._wasPreviouslyReady = true;
  642. if (mesh) {
  643. if (!mesh._materialDefines) {
  644. mesh._materialDefines = new PBRMaterialDefines();
  645. }
  646. this._defines.cloneTo(mesh._materialDefines);
  647. }
  648. return true;
  649. };
  650. PBRMaterial.prototype.unbind = function () {
  651. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  652. this._effect.setTexture("reflection2DSampler", null);
  653. }
  654. _super.prototype.unbind.call(this);
  655. };
  656. PBRMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  657. this._effect.setMatrix("world", world);
  658. };
  659. PBRMaterial.prototype.bind = function (world, mesh) {
  660. this._myScene = this.getScene();
  661. // Matrices
  662. this.bindOnlyWorldMatrix(world);
  663. this._effect.setMatrix("viewProjection", this._myScene.getTransformMatrix());
  664. // Bones
  665. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  666. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  667. }
  668. if (this._myScene.getCachedMaterial() !== this) {
  669. if (BABYLON.StandardMaterial.FresnelEnabled) {
  670. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  671. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  672. }
  673. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  674. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  675. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  676. }
  677. }
  678. // Textures
  679. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  680. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  681. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  682. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  683. }
  684. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  685. this._effect.setTexture("ambientSampler", this.ambientTexture);
  686. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  687. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  688. }
  689. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  690. this._effect.setTexture("opacitySampler", this.opacityTexture);
  691. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  692. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  693. }
  694. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  695. if (this.reflectionTexture.isCube) {
  696. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  697. }
  698. else {
  699. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  700. }
  701. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  702. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, 0);
  703. }
  704. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  705. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  706. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  707. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  708. }
  709. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapEnabled) {
  710. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  711. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  712. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  713. }
  714. if (this.specularTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  715. this._effect.setTexture("specularSampler", this.specularTexture);
  716. this._effect.setFloat2("vSpecularInfos", this.specularTexture.coordinatesIndex, this.specularTexture.level);
  717. this._effect.setMatrix("specularMatrix", this.specularTexture.getTextureMatrix());
  718. }
  719. if (this.bumpTexture && this._myScene.getEngine().getCaps().standardDerivatives && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  720. this._effect.setTexture("bumpSampler", this.bumpTexture);
  721. this._effect.setFloat2("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level);
  722. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  723. }
  724. // Clip plane
  725. if (this._myScene.clipPlane) {
  726. this._effect.setFloat4("vClipPlane", this._myScene.clipPlane.normal.x, this._myScene.clipPlane.normal.y, this._myScene.clipPlane.normal.z, this._myScene.clipPlane.d);
  727. }
  728. // Point size
  729. if (this.pointsCloud) {
  730. this._effect.setFloat("pointSize", this.pointSize);
  731. }
  732. // Colors
  733. this._myScene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  734. // GAMMA CORRECTION.
  735. this.specularColor.toLinearSpaceToRef(PBRMaterial._scaledSpecular);
  736. this._effect.setVector3("vEyePosition", this._myScene._mirroredCameraPosition ? this._myScene._mirroredCameraPosition : this._myScene.activeCamera.position);
  737. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  738. if (this._defines.SPECULARTERM) {
  739. this._effect.setColor4("vSpecularColor", PBRMaterial._scaledSpecular, this.glossiness);
  740. }
  741. // GAMMA CORRECTION.
  742. this.emissiveColor.toLinearSpaceToRef(PBRMaterial._scaledEmissive);
  743. this._effect.setColor3("vEmissiveColor", PBRMaterial._scaledEmissive);
  744. // GAMMA CORRECTION.
  745. this.reflectionColor.toLinearSpaceToRef(PBRMaterial._scaledReflection);
  746. this._effect.setColor3("vReflectionColor", PBRMaterial._scaledReflection);
  747. }
  748. // GAMMA CORRECTION.
  749. this.diffuseColor.toLinearSpaceToRef(PBRMaterial._scaledDiffuse);
  750. this._effect.setColor4("vDiffuseColor", PBRMaterial._scaledDiffuse, this.alpha * mesh.visibility);
  751. // Lights
  752. if (this._myScene.lightsEnabled && !this.disableLighting) {
  753. PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines);
  754. }
  755. // View
  756. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  757. this._effect.setMatrix("view", this._myScene.getViewMatrix());
  758. }
  759. // Fog
  760. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  761. this._effect.setFloat4("vFogInfos", this._myScene.fogMode, this._myScene.fogStart, this._myScene.fogEnd, this._myScene.fogDensity);
  762. this._effect.setColor3("vFogColor", this._myScene.fogColor);
  763. }
  764. this._lightingInfos.x = this.directIntensity;
  765. this._lightingInfos.y = this.emissiveIntensity;
  766. this._lightingInfos.z = this.environmentIntensity;
  767. this._effect.setVector4("vLightingIntensity", this._lightingInfos);
  768. this._overloadedShadowInfos.x = this.overloadedShadowIntensity;
  769. this._overloadedShadowInfos.y = this.overloadedShadeIntensity;
  770. this._effect.setVector4("vOverloadedShadowIntensity", this._overloadedShadowInfos);
  771. this._cameraInfos.x = this.cameraExposure;
  772. this._cameraInfos.y = this.cameraContrast;
  773. this._effect.setVector4("vCameraInfos", this._cameraInfos);
  774. this._overloadedIntensity.x = this.overloadedAmbientIntensity;
  775. this._overloadedIntensity.y = this.overloadedDiffuseIntensity;
  776. this._overloadedIntensity.z = this.overloadedSpecularIntensity;
  777. this._overloadedIntensity.w = this.overloadedEmissiveIntensity;
  778. this._effect.setVector4("vOverloadedIntensity", this._overloadedIntensity);
  779. this.overloadedAmbient.toLinearSpaceToRef(this._tempColor);
  780. this._effect.setColor3("vOverloadedAmbient", this._tempColor);
  781. this.overloadedDiffuse.toLinearSpaceToRef(this._tempColor);
  782. this._effect.setColor3("vOverloadedDiffuse", this._tempColor);
  783. this.overloadedSpecular.toLinearSpaceToRef(this._tempColor);
  784. this._effect.setColor3("vOverloadedSpecular", this._tempColor);
  785. this.overloadedEmissive.toLinearSpaceToRef(this._tempColor);
  786. this._effect.setColor3("vOverloadedEmissive", this._tempColor);
  787. this.overloadedReflection.toLinearSpaceToRef(this._tempColor);
  788. this._effect.setColor3("vOverloadedReflection", this._tempColor);
  789. this._overloadedGlossiness.x = this.overloadedGlossiness;
  790. this._overloadedGlossiness.y = this.overloadedGlossinessIntensity;
  791. this._overloadedGlossiness.z = this.overloadedReflectionIntensity;
  792. this._effect.setVector3("vOverloadedGlossiness", this._overloadedGlossiness);
  793. // Log. depth
  794. if (this._defines.LOGARITHMICDEPTH) {
  795. this._effect.setFloat("logarithmicDepthConstant", 2.0 / (Math.log(this._myScene.activeCamera.maxZ + 1.0) / Math.LN2));
  796. }
  797. _super.prototype.bind.call(this, world, mesh);
  798. this._myScene = null;
  799. };
  800. PBRMaterial.prototype.getAnimatables = function () {
  801. var results = [];
  802. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  803. results.push(this.diffuseTexture);
  804. }
  805. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  806. results.push(this.ambientTexture);
  807. }
  808. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  809. results.push(this.opacityTexture);
  810. }
  811. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  812. results.push(this.reflectionTexture);
  813. }
  814. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  815. results.push(this.emissiveTexture);
  816. }
  817. if (this.specularTexture && this.specularTexture.animations && this.specularTexture.animations.length > 0) {
  818. results.push(this.specularTexture);
  819. }
  820. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  821. results.push(this.bumpTexture);
  822. }
  823. return results;
  824. };
  825. PBRMaterial.prototype.dispose = function (forceDisposeEffect) {
  826. if (this.diffuseTexture) {
  827. this.diffuseTexture.dispose();
  828. }
  829. if (this.ambientTexture) {
  830. this.ambientTexture.dispose();
  831. }
  832. if (this.opacityTexture) {
  833. this.opacityTexture.dispose();
  834. }
  835. if (this.reflectionTexture) {
  836. this.reflectionTexture.dispose();
  837. }
  838. if (this.emissiveTexture) {
  839. this.emissiveTexture.dispose();
  840. }
  841. if (this.specularTexture) {
  842. this.specularTexture.dispose();
  843. }
  844. if (this.bumpTexture) {
  845. this.bumpTexture.dispose();
  846. }
  847. _super.prototype.dispose.call(this, forceDisposeEffect);
  848. };
  849. PBRMaterial.prototype.clone = function (name) {
  850. var newPBRMaterial = new PBRMaterial(name, this.getScene());
  851. // Base material
  852. this.copyTo(newPBRMaterial);
  853. newPBRMaterial.directIntensity = this.directIntensity;
  854. newPBRMaterial.emissiveIntensity = this.emissiveIntensity;
  855. newPBRMaterial.environmentIntensity = this.environmentIntensity;
  856. newPBRMaterial.cameraExposure = this.cameraExposure;
  857. newPBRMaterial.cameraContrast = this.cameraContrast;
  858. newPBRMaterial.overloadedShadowIntensity = this.overloadedShadowIntensity;
  859. newPBRMaterial.overloadedShadeIntensity = this.overloadedShadeIntensity;
  860. newPBRMaterial.overloadedAmbientIntensity = this.overloadedAmbientIntensity;
  861. newPBRMaterial.overloadedDiffuseIntensity = this.overloadedDiffuseIntensity;
  862. newPBRMaterial.overloadedSpecularIntensity = this.overloadedSpecularIntensity;
  863. newPBRMaterial.overloadedEmissiveIntensity = this.overloadedEmissiveIntensity;
  864. newPBRMaterial.overloadedAmbient = this.overloadedAmbient;
  865. newPBRMaterial.overloadedDiffuse = this.overloadedDiffuse;
  866. newPBRMaterial.overloadedSpecular = this.overloadedSpecular;
  867. newPBRMaterial.overloadedEmissive = this.overloadedEmissive;
  868. newPBRMaterial.overloadedReflection = this.overloadedReflection;
  869. newPBRMaterial.overloadedGlossiness = this.overloadedGlossiness;
  870. newPBRMaterial.overloadedGlossinessIntensity = this.overloadedGlossinessIntensity;
  871. newPBRMaterial.overloadedReflectionIntensity = this.overloadedReflectionIntensity;
  872. newPBRMaterial.disableBumpMap = this.disableBumpMap;
  873. // Standard material
  874. if (this.diffuseTexture && this.diffuseTexture.clone) {
  875. newPBRMaterial.diffuseTexture = this.diffuseTexture.clone();
  876. }
  877. if (this.ambientTexture && this.ambientTexture.clone) {
  878. newPBRMaterial.ambientTexture = this.ambientTexture.clone();
  879. }
  880. if (this.opacityTexture && this.opacityTexture.clone) {
  881. newPBRMaterial.opacityTexture = this.opacityTexture.clone();
  882. }
  883. if (this.reflectionTexture && this.reflectionTexture.clone) {
  884. newPBRMaterial.reflectionTexture = this.reflectionTexture.clone();
  885. }
  886. if (this.emissiveTexture && this.emissiveTexture.clone) {
  887. newPBRMaterial.emissiveTexture = this.emissiveTexture.clone();
  888. }
  889. if (this.specularTexture && this.specularTexture.clone) {
  890. newPBRMaterial.specularTexture = this.specularTexture.clone();
  891. }
  892. if (this.bumpTexture && this.bumpTexture.clone) {
  893. newPBRMaterial.bumpTexture = this.bumpTexture.clone();
  894. }
  895. if (this.lightmapTexture && this.lightmapTexture.clone) {
  896. newPBRMaterial.lightmapTexture = this.lightmapTexture.clone();
  897. newPBRMaterial.useLightmapAsShadowmap = this.useLightmapAsShadowmap;
  898. }
  899. newPBRMaterial.ambientColor = this.ambientColor.clone();
  900. newPBRMaterial.diffuseColor = this.diffuseColor.clone();
  901. newPBRMaterial.specularColor = this.specularColor.clone();
  902. newPBRMaterial.reflectionColor = this.reflectionColor.clone();
  903. newPBRMaterial.glossiness = this.glossiness;
  904. newPBRMaterial.emissiveColor = this.emissiveColor.clone();
  905. newPBRMaterial.useAlphaFromDiffuseTexture = this.useAlphaFromDiffuseTexture;
  906. newPBRMaterial.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
  907. newPBRMaterial.useGlossinessFromSpecularMapAlpha = this.useGlossinessFromSpecularMapAlpha;
  908. newPBRMaterial.useSpecularOverAlpha = this.useSpecularOverAlpha;
  909. newPBRMaterial.emissiveFresnelParameters = this.emissiveFresnelParameters.clone();
  910. newPBRMaterial.opacityFresnelParameters = this.opacityFresnelParameters.clone();
  911. return newPBRMaterial;
  912. };
  913. PBRMaterial.prototype.serialize = function () {
  914. var serializationObject = _super.prototype.serialize.call(this);
  915. serializationObject.customType = "BABYLON.PBRMaterial";
  916. serializationObject.directIntensity = this.directIntensity;
  917. serializationObject.emissiveIntensity = this.emissiveIntensity;
  918. serializationObject.environmentIntensity = this.environmentIntensity;
  919. serializationObject.cameraExposure = this.cameraExposure;
  920. serializationObject.cameraContrast = this.cameraContrast;
  921. serializationObject.overloadedShadowIntensity = this.overloadedShadowIntensity;
  922. serializationObject.overloadedShadeIntensity = this.overloadedShadeIntensity;
  923. serializationObject.overloadedAmbientIntensity = this.overloadedAmbientIntensity;
  924. serializationObject.overloadedDiffuseIntensity = this.overloadedDiffuseIntensity;
  925. serializationObject.overloadedSpecularIntensity = this.overloadedSpecularIntensity;
  926. serializationObject.overloadedEmissiveIntensity = this.overloadedEmissiveIntensity;
  927. serializationObject.overloadedAmbient = this.overloadedAmbient.asArray();
  928. serializationObject.overloadedDiffuse = this.overloadedDiffuse.asArray();
  929. serializationObject.overloadedSpecular = this.overloadedSpecular.asArray();
  930. serializationObject.overloadedEmissive = this.overloadedEmissive.asArray();
  931. serializationObject.overloadedReflection = this.overloadedReflection.asArray();
  932. serializationObject.overloadedGlossiness = this.overloadedGlossiness;
  933. serializationObject.overloadedGlossinessIntensity = this.overloadedGlossinessIntensity;
  934. serializationObject.overloadedReflectionIntensity = this.overloadedReflectionIntensity;
  935. serializationObject.disableBumpMap = this.disableBumpMap;
  936. // Standard material
  937. if (this.diffuseTexture) {
  938. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  939. }
  940. if (this.ambientTexture) {
  941. serializationObject.ambientTexture = this.ambientTexture.serialize();
  942. }
  943. if (this.opacityTexture) {
  944. serializationObject.opacityTexture = this.opacityTexture.serialize();
  945. }
  946. if (this.reflectionTexture) {
  947. serializationObject.reflectionTexture = this.reflectionTexture.serialize();
  948. }
  949. if (this.emissiveTexture) {
  950. serializationObject.emissiveTexture = this.emissiveTexture.serialize();
  951. }
  952. if (this.specularTexture) {
  953. serializationObject.specularTexture = this.specularTexture.serialize();
  954. }
  955. if (this.bumpTexture) {
  956. serializationObject.bumpTexture = this.bumpTexture.serialize();
  957. }
  958. if (this.lightmapTexture) {
  959. serializationObject.lightmapTexture = this.lightmapTexture.serialize();
  960. serializationObject.useLightmapAsShadowmap = this.useLightmapAsShadowmap;
  961. }
  962. serializationObject.ambientColor = this.ambientColor.asArray();
  963. serializationObject.diffuseColor = this.diffuseColor.asArray();
  964. serializationObject.specularColor = this.specularColor.asArray();
  965. serializationObject.reflectionColor = this.reflectionColor.asArray();
  966. serializationObject.glossiness = this.glossiness;
  967. serializationObject.emissiveColor = this.emissiveColor.asArray();
  968. serializationObject.useAlphaFromDiffuseTexture = this.useAlphaFromDiffuseTexture;
  969. serializationObject.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
  970. serializationObject.useGlossinessFromSpecularMapAlpha = this.useGlossinessFromSpecularMapAlpha;
  971. serializationObject.useSpecularOverAlpha = this.useSpecularOverAlpha;
  972. serializationObject.emissiveFresnelParameters = this.emissiveFresnelParameters.serialize();
  973. serializationObject.opacityFresnelParameters = this.opacityFresnelParameters.serialize();
  974. return serializationObject;
  975. };
  976. PBRMaterial.Parse = function (source, scene, rootUrl) {
  977. var material = new PBRMaterial(source.name, scene);
  978. material.alpha = source.alpha;
  979. material.id = source.id;
  980. if (source.disableDepthWrite) {
  981. material.disableDepthWrite = source.disableDepthWrite;
  982. }
  983. if (source.checkReadyOnlyOnce) {
  984. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  985. }
  986. BABYLON.Tags.AddTagsTo(material, source.tags);
  987. material.backFaceCulling = source.backFaceCulling;
  988. material.wireframe = source.wireframe;
  989. material.directIntensity = source.directIntensity;
  990. material.emissiveIntensity = source.emissiveIntensity;
  991. material.environmentIntensity = source.environmentIntensity;
  992. material.cameraExposure = source.cameraExposure;
  993. material.cameraContrast = source.cameraContrast;
  994. material.overloadedShadowIntensity = source.overloadedShadowIntensity;
  995. material.overloadedShadeIntensity = source.overloadedShadeIntensity;
  996. material.overloadedAmbientIntensity = source.overloadedAmbientIntensity;
  997. material.overloadedDiffuseIntensity = source.overloadedDiffuseIntensity;
  998. material.overloadedSpecularIntensity = source.overloadedSpecularIntensity;
  999. material.overloadedEmissiveIntensity = source.overloadedEmissiveIntensity;
  1000. material.overloadedAmbient = BABYLON.Color3.FromArray(source.overloadedAmbient);
  1001. material.overloadedDiffuse = BABYLON.Color3.FromArray(source.overloadedDiffuse);
  1002. material.overloadedSpecular = BABYLON.Color3.FromArray(source.overloadedSpecular);
  1003. material.overloadedEmissive = BABYLON.Color3.FromArray(source.overloadedEmissive);
  1004. material.overloadedReflection = BABYLON.Color3.FromArray(source.overloadedReflection);
  1005. material.overloadedGlossiness = source.overloadedGlossiness;
  1006. material.overloadedGlossinessIntensity = source.overloadedGlossinessIntensity;
  1007. material.overloadedReflectionIntensity = source.overloadedReflectionIntensity;
  1008. material.disableBumpMap = source.disableBumpMap;
  1009. // Standard material
  1010. if (source.diffuseTexture) {
  1011. material.diffuseTexture = BABYLON.Texture.Parse(source.diffuseTexture, scene, rootUrl);
  1012. }
  1013. if (source.ambientTexture) {
  1014. material.ambientTexture = BABYLON.Texture.Parse(source.ambientTexture, scene, rootUrl);
  1015. }
  1016. if (source.opacityTexture) {
  1017. material.opacityTexture = BABYLON.Texture.Parse(source.opacityTexture, scene, rootUrl);
  1018. }
  1019. if (source.reflectionTexture) {
  1020. material.reflectionTexture = BABYLON.Texture.Parse(source.reflectionTexture, scene, rootUrl);
  1021. }
  1022. if (source.emissiveTexture) {
  1023. material.emissiveTexture = BABYLON.Texture.Parse(source.emissiveTexture, scene, rootUrl);
  1024. }
  1025. if (source.specularTexture) {
  1026. material.specularTexture = BABYLON.Texture.Parse(source.specularTexture, scene, rootUrl);
  1027. }
  1028. if (source.bumpTexture) {
  1029. material.bumpTexture = BABYLON.Texture.Parse(source.bumpTexture, scene, rootUrl);
  1030. }
  1031. if (source.lightmapTexture) {
  1032. material.lightmapTexture = BABYLON.Texture.Parse(source.lightmapTexture, scene, rootUrl);
  1033. material.useLightmapAsShadowmap = source.useLightmapAsShadowmap;
  1034. }
  1035. material.ambientColor = BABYLON.Color3.FromArray(source.ambient);
  1036. material.diffuseColor = BABYLON.Color3.FromArray(source.diffuse);
  1037. material.specularColor = BABYLON.Color3.FromArray(source.specular);
  1038. material.reflectionColor = BABYLON.Color3.FromArray(source.reflectionColor);
  1039. material.glossiness = source.glossiness;
  1040. material.emissiveColor = BABYLON.Color3.FromArray(source.emissive);
  1041. material.useAlphaFromDiffuseTexture = source.useAlphaFromDiffuseTexture;
  1042. material.useEmissiveAsIllumination = source.useEmissiveAsIllumination;
  1043. material.useGlossinessFromSpecularMapAlpha = source.useGlossinessFromSpecularMapAlpha;
  1044. material.useSpecularOverAlpha = source.useSpecularOverAlpha;
  1045. material.emissiveFresnelParameters = BABYLON.FresnelParameters.Parse(source.emissiveFresnelParameters);
  1046. material.opacityFresnelParameters = BABYLON.FresnelParameters.Parse(source.opacityFresnelParameters);
  1047. return material;
  1048. };
  1049. PBRMaterial._scaledDiffuse = new BABYLON.Color3();
  1050. PBRMaterial._scaledSpecular = new BABYLON.Color3();
  1051. PBRMaterial._scaledEmissive = new BABYLON.Color3();
  1052. PBRMaterial._scaledReflection = new BABYLON.Color3();
  1053. return PBRMaterial;
  1054. })(BABYLON.Material);
  1055. BABYLON.PBRMaterial = PBRMaterial;
  1056. })(BABYLON || (BABYLON = {}));
  1057. 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}";
  1058. 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 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 vOverloadedReflection;\r\n uniform vec3 vOverloadedGlossiness;\r\n#endif\r\n\r\n#ifdef OVERLOADEDSHADOWVALUES\r\n uniform vec4 vOverloadedShadowIntensity;\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#ifdef OVERLOADEDSHADOWVALUES\r\n return mix(1.0, darkness, vOverloadedShadowIntensity.x);\r\n#else\r\n return darkness;\r\n#endif\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#ifdef OVERLOADEDSHADOWVALUES\r\n return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));\r\n#else\r\n return min(1.0, visibility + darkness);\r\n#endif\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#ifdef OVERLOADEDSHADOWVALUES\r\n return mix(1.0, darkness, vOverloadedShadowIntensity.x);\r\n#else\r\n return darkness;\r\n#endif\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#ifdef OVERLOADEDSHADOWVALUES\r\n return min(1.0, mix(1.0, visibility + darkness, vOverloadedShadowIntensity.x));\r\n#else\r\n return min(1.0, visibility + darkness);\r\n#endif\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#ifdef OVERLOADEDSHADOWVALUES\r\n return min(1.0, mix(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness, vOverloadedShadowIntensity.x));\r\n#else\r\n return min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);\r\n#endif\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 VERTEXCOLOR\r\n baseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n#ifdef OVERLOADEDVALUES\r\n baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);\r\n diffuseColor.rgb = mix(diffuseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);\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, vOverloadedGlossiness.x, vOverloadedGlossiness.y);\r\n #endif\r\n#else\r\n float glossiness = 0.;\r\n #ifdef OVERLOADEDVALUES\r\n glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.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 \r\n#ifdef OVERLOADEDSHADOWVALUES\r\n vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);\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 OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase *= 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 OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase *= 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 OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase *= 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 OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase *= 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\n#ifdef OVERLOADEDVALUES\r\n ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);\r\n reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);\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#endif\r\n\r\n#ifdef OVERLOADEDVALUES\r\n emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);\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 \r\n #ifdef OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n #endif\r\n#else\r\n #ifdef LINKEMISSIVEWITHDIFFUSE\r\n vec3 finalDiffuse = max((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n\r\n #ifdef OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n #endif\r\n #else\r\n vec3 finalDiffuse = max(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n\r\n #ifdef OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n #endif\r\n #endif\r\n#endif\r\n\r\n#ifdef OVERLOADEDSHADOWVALUES\r\n finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));\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 OVERLOADEDSHADOWVALUES\r\n finalSpecular = mix(finalSpecular, vec3(0.0), (1.0 - vOverloadedShadowIntensity.y));\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 // 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}";
  1059. 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}";
  1060. BABYLON.Effect.ShadersStore['legacypbrPixelShader'] = "precision mediump 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 vec4 vDiffuseColor;\r\nuniform vec3 vReflectionColor;\r\n\r\n// CUSTOM CONTROLS\r\nuniform vec4 vLightingIntensity;\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 vOverloadedReflection;\r\nuniform vec3 vOverloadedGlossiness;\r\n#endif\r\n\r\n#ifdef OVERLOADEDSHADOWVALUES\r\nuniform vec4 vOverloadedShadowIntensity;\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;\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 VERTEXCOLOR\r\n baseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n#ifdef OVERLOADEDVALUES\r\n baseColor.rgb = mix(baseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);\r\n diffuseColor.rgb = mix(diffuseColor.rgb, vOverloadedDiffuse, vOverloadedIntensity.y);\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, vOverloadedGlossiness.x, vOverloadedGlossiness.y);\r\n #endif\r\n#else\r\n float glossiness = 0.;\r\n #ifdef OVERLOADEDVALUES\r\n glossiness = mix(glossiness, vOverloadedGlossiness.x, vOverloadedGlossiness.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\r\n#ifdef OVERLOADEDSHADOWVALUES\r\n vec3 shadowedOnlyDiffuseBase = vec3(1., 1., 1.);\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 OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase *= 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 OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase *= 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 OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase *= 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 OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase *= 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 OVERLOADEDVALUES\r\n ambientReflectionColor = mix(ambientReflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);\r\n reflectionColor = mix(reflectionColor, vOverloadedReflection, vOverloadedGlossiness.z);\r\n#endif\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#endif\r\n\r\n#ifdef OVERLOADEDVALUES\r\n emissiveColor = mix(emissiveColor, vOverloadedEmissive, vOverloadedIntensity.w);\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\r\n #ifdef OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n #endif\r\n#else\r\n #ifdef LINKEMISSIVEWITHDIFFUSE\r\n vec3 finalDiffuse = max((diffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n #ifdef OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase = max((shadowedOnlyDiffuseBase + emissiveColor) * diffuseColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n #endif\r\n #else\r\n vec3 finalDiffuse = max(diffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n #ifdef OVERLOADEDSHADOWVALUES\r\n shadowedOnlyDiffuseBase = max(shadowedOnlyDiffuseBase * diffuseColor + emissiveColor + vAmbientColor, 0.0) * baseColor.rgb;\r\n #endif\r\n #endif\r\n#endif\r\n\r\n#ifdef OVERLOADEDSHADOWVALUES\r\n finalDiffuse = mix(finalDiffuse, shadowedOnlyDiffuseBase, (1.0 - vOverloadedShadowIntensity.y));\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 gl_FragColor = color;\r\n}";