babylon.pbrMaterial.js 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  7. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  8. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  9. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  10. return c > 3 && r && Object.defineProperty(target, key, r), r;
  11. };
  12. var BABYLON;
  13. (function (BABYLON) {
  14. var PBRMaterialDefines = (function (_super) {
  15. __extends(PBRMaterialDefines, _super);
  16. function PBRMaterialDefines() {
  17. _super.call(this);
  18. this.ALBEDO = false;
  19. this.AMBIENT = false;
  20. this.OPACITY = false;
  21. this.OPACITYRGB = false;
  22. this.REFLECTION = false;
  23. this.EMISSIVE = false;
  24. this.REFLECTIVITY = false;
  25. this.BUMP = false;
  26. this.PARALLAX = false;
  27. this.PARALLAXOCCLUSION = false;
  28. this.SPECULAROVERALPHA = false;
  29. this.CLIPPLANE = false;
  30. this.ALPHATEST = false;
  31. this.ALPHAFROMALBEDO = false;
  32. this.POINTSIZE = false;
  33. this.FOG = false;
  34. this.SPECULARTERM = false;
  35. this.OPACITYFRESNEL = false;
  36. this.EMISSIVEFRESNEL = false;
  37. this.FRESNEL = false;
  38. this.NORMAL = false;
  39. this.UV1 = false;
  40. this.UV2 = false;
  41. this.VERTEXCOLOR = false;
  42. this.VERTEXALPHA = false;
  43. this.NUM_BONE_INFLUENCERS = 0;
  44. this.BonesPerMesh = 0;
  45. this.INSTANCES = false;
  46. this.MICROSURFACEFROMREFLECTIVITYMAP = false;
  47. this.MICROSURFACEAUTOMATIC = false;
  48. this.EMISSIVEASILLUMINATION = false;
  49. this.LINKEMISSIVEWITHALBEDO = false;
  50. this.LIGHTMAP = false;
  51. this.USELIGHTMAPASSHADOWMAP = false;
  52. this.REFLECTIONMAP_3D = false;
  53. this.REFLECTIONMAP_SPHERICAL = false;
  54. this.REFLECTIONMAP_PLANAR = false;
  55. this.REFLECTIONMAP_CUBIC = false;
  56. this.REFLECTIONMAP_PROJECTION = false;
  57. this.REFLECTIONMAP_SKYBOX = false;
  58. this.REFLECTIONMAP_EXPLICIT = false;
  59. this.REFLECTIONMAP_EQUIRECTANGULAR = false;
  60. this.INVERTCUBICMAP = false;
  61. this.LOGARITHMICDEPTH = false;
  62. this.CAMERATONEMAP = false;
  63. this.CAMERACONTRAST = false;
  64. this.CAMERACOLORGRADING = false;
  65. this.CAMERACOLORCURVES = false;
  66. this.OVERLOADEDVALUES = false;
  67. this.OVERLOADEDSHADOWVALUES = false;
  68. this.USESPHERICALFROMREFLECTIONMAP = false;
  69. this.REFRACTION = false;
  70. this.REFRACTIONMAP_3D = false;
  71. this.LINKREFRACTIONTOTRANSPARENCY = false;
  72. this.REFRACTIONMAPINLINEARSPACE = false;
  73. this.LODBASEDMICROSFURACE = false;
  74. this.USEPHYSICALLIGHTFALLOFF = false;
  75. this.RADIANCEOVERALPHA = false;
  76. this.USEPMREMREFLECTION = false;
  77. this.USEPMREMREFRACTION = false;
  78. this.OPENGLNORMALMAP = false;
  79. this.INVERTNORMALMAPX = false;
  80. this.INVERTNORMALMAPY = false;
  81. this.rebuild();
  82. }
  83. return PBRMaterialDefines;
  84. })(BABYLON.MaterialDefines);
  85. /**
  86. * The Physically based material of BJS.
  87. *
  88. * This offers the main features of a standard PBR material.
  89. * For more information, please refer to the documentation :
  90. * http://doc.babylonjs.com/extensions/Physically_Based_Rendering
  91. */
  92. var PBRMaterial = (function (_super) {
  93. __extends(PBRMaterial, _super);
  94. /**
  95. * Instantiates a new PBRMaterial instance.
  96. *
  97. * @param name The material name
  98. * @param scene The scene the material will be use in.
  99. */
  100. function PBRMaterial(name, scene) {
  101. var _this = this;
  102. _super.call(this, name, scene);
  103. /**
  104. * Intensity of the direct lights e.g. the four lights available in your scene.
  105. * This impacts both the direct diffuse and specular highlights.
  106. */
  107. this.directIntensity = 1.0;
  108. /**
  109. * Intensity of the emissive part of the material.
  110. * This helps controlling the emissive effect without modifying the emissive color.
  111. */
  112. this.emissiveIntensity = 1.0;
  113. /**
  114. * Intensity of the environment e.g. how much the environment will light the object
  115. * either through harmonics for rough material or through the refelction for shiny ones.
  116. */
  117. this.environmentIntensity = 1.0;
  118. /**
  119. * This is a special control allowing the reduction of the specular highlights coming from the
  120. * four lights of the scene. Those highlights may not be needed in full environment lighting.
  121. */
  122. this.specularIntensity = 1.0;
  123. this._lightingInfos = new BABYLON.Vector4(this.directIntensity, this.emissiveIntensity, this.environmentIntensity, this.specularIntensity);
  124. /**
  125. * Debug Control allowing disabling the bump map on this material.
  126. */
  127. this.disableBumpMap = false;
  128. /**
  129. * Debug Control helping enforcing or dropping the darkness of shadows.
  130. * 1.0 means the shadows have their normal darkness, 0.0 means the shadows are not visible.
  131. */
  132. this.overloadedShadowIntensity = 1.0;
  133. /**
  134. * Debug Control helping dropping the shading effect coming from the diffuse lighting.
  135. * 1.0 means the shade have their normal impact, 0.0 means no shading at all.
  136. */
  137. this.overloadedShadeIntensity = 1.0;
  138. this._overloadedShadowInfos = new BABYLON.Vector4(this.overloadedShadowIntensity, this.overloadedShadeIntensity, 0.0, 0.0);
  139. /**
  140. * The camera exposure used on this material.
  141. * This property is here and not in the camera to allow controlling exposure without full screen post process.
  142. * This corresponds to a photographic exposure.
  143. */
  144. this.cameraExposure = 1.0;
  145. /**
  146. * The camera contrast used on this material.
  147. * This property is here and not in the camera to allow controlling contrast without full screen post process.
  148. */
  149. this.cameraContrast = 1.0;
  150. /**
  151. * Color Grading 2D Lookup Texture.
  152. * This allows special effects like sepia, black and white to sixties rendering style.
  153. */
  154. this.cameraColorGradingTexture = null;
  155. this._cameraColorGradingScaleOffset = new BABYLON.Vector4(1.0, 1.0, 0.0, 0.0);
  156. this._cameraColorGradingInfos = new BABYLON.Vector4(1.0, 1.0, 0.0, 0.0);
  157. /**
  158. * The color grading curves provide additional color adjustmnent that is applied after any color grading transform (3D LUT).
  159. * They allow basic adjustment of saturation and small exposure adjustments, along with color filter tinting to provide white balance adjustment or more stylistic effects.
  160. * These are similar to controls found in many professional imaging or colorist software. The global controls are applied to the entire image. For advanced tuning, extra controls are provided to adjust the shadow, midtone and highlight areas of the image;
  161. * corresponding to low luminance, medium luminance, and high luminance areas respectively.
  162. */
  163. this.cameraColorCurves = null;
  164. this._cameraInfos = new BABYLON.Vector4(1.0, 1.0, 0.0, 0.0);
  165. this._microsurfaceTextureLods = new BABYLON.Vector2(0.0, 0.0);
  166. /**
  167. * Debug Control allowing to overload the ambient color.
  168. * This as to be use with the overloadedAmbientIntensity parameter.
  169. */
  170. this.overloadedAmbient = BABYLON.Color3.White();
  171. /**
  172. * Debug Control indicating how much the overloaded ambient color is used against the default one.
  173. */
  174. this.overloadedAmbientIntensity = 0.0;
  175. /**
  176. * Debug Control allowing to overload the albedo color.
  177. * This as to be use with the overloadedAlbedoIntensity parameter.
  178. */
  179. this.overloadedAlbedo = BABYLON.Color3.White();
  180. /**
  181. * Debug Control indicating how much the overloaded albedo color is used against the default one.
  182. */
  183. this.overloadedAlbedoIntensity = 0.0;
  184. /**
  185. * Debug Control allowing to overload the reflectivity color.
  186. * This as to be use with the overloadedReflectivityIntensity parameter.
  187. */
  188. this.overloadedReflectivity = new BABYLON.Color3(0.3, 0.3, 0.3);
  189. /**
  190. * Debug Control indicating how much the overloaded reflectivity color is used against the default one.
  191. */
  192. this.overloadedReflectivityIntensity = 0.0;
  193. /**
  194. * Debug Control allowing to overload the emissive color.
  195. * This as to be use with the overloadedEmissiveIntensity parameter.
  196. */
  197. this.overloadedEmissive = BABYLON.Color3.White();
  198. /**
  199. * Debug Control indicating how much the overloaded emissive color is used against the default one.
  200. */
  201. this.overloadedEmissiveIntensity = 0.0;
  202. this._overloadedIntensity = new BABYLON.Vector4(this.overloadedAmbientIntensity, this.overloadedAlbedoIntensity, this.overloadedReflectivityIntensity, this.overloadedEmissiveIntensity);
  203. /**
  204. * Debug Control allowing to overload the reflection color.
  205. * This as to be use with the overloadedReflectionIntensity parameter.
  206. */
  207. this.overloadedReflection = BABYLON.Color3.White();
  208. /**
  209. * Debug Control indicating how much the overloaded reflection color is used against the default one.
  210. */
  211. this.overloadedReflectionIntensity = 0.0;
  212. /**
  213. * Debug Control allowing to overload the microsurface.
  214. * This as to be use with the overloadedMicroSurfaceIntensity parameter.
  215. */
  216. this.overloadedMicroSurface = 0.0;
  217. /**
  218. * Debug Control indicating how much the overloaded microsurface is used against the default one.
  219. */
  220. this.overloadedMicroSurfaceIntensity = 0.0;
  221. this._overloadedMicroSurface = new BABYLON.Vector3(this.overloadedMicroSurface, this.overloadedMicroSurfaceIntensity, this.overloadedReflectionIntensity);
  222. this.ambientColor = new BABYLON.Color3(0, 0, 0);
  223. /**
  224. * AKA Diffuse Color in other nomenclature.
  225. */
  226. this.albedoColor = new BABYLON.Color3(1, 1, 1);
  227. /**
  228. * AKA Specular Color in other nomenclature.
  229. */
  230. this.reflectivityColor = new BABYLON.Color3(1, 1, 1);
  231. this.reflectionColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  232. this.emissiveColor = new BABYLON.Color3(0, 0, 0);
  233. /**
  234. * AKA Glossiness in other nomenclature.
  235. */
  236. this.microSurface = 0.9;
  237. /**
  238. * source material index of refraction (IOR)' / 'destination material IOR.
  239. */
  240. this.indexOfRefraction = 0.66;
  241. /**
  242. * Controls if refraction needs to be inverted on Y. This could be usefull for procedural texture.
  243. */
  244. this.invertRefractionY = false;
  245. /**
  246. * This parameters will make the material used its opacity to control how much it is refracting aginst not.
  247. * Materials half opaque for instance using refraction could benefit from this control.
  248. */
  249. this.linkRefractionWithTransparency = false;
  250. /**
  251. * The emissive and albedo are linked to never be more than one (Energy conservation).
  252. */
  253. this.linkEmissiveWithAlbedo = false;
  254. this.useLightmapAsShadowmap = false;
  255. /**
  256. * In this mode, the emissive informtaion will always be added to the lighting once.
  257. * A light for instance can be thought as emissive.
  258. */
  259. this.useEmissiveAsIllumination = false;
  260. /**
  261. * Secifies that the alpha is coming form the albedo channel alpha channel.
  262. */
  263. this.useAlphaFromAlbedoTexture = false;
  264. /**
  265. * Specifies that the material will keeps the specular highlights over a transparent surface (only the most limunous ones).
  266. * A car glass is a good exemple of that. When sun reflects on it you can not see what is behind.
  267. */
  268. this.useSpecularOverAlpha = true;
  269. /**
  270. * Specifies if the reflectivity texture contains the glossiness information in its alpha channel.
  271. */
  272. this.useMicroSurfaceFromReflectivityMapAlpha = false;
  273. /**
  274. * In case the reflectivity map does not contain the microsurface information in its alpha channel,
  275. * The material will try to infer what glossiness each pixel should be.
  276. */
  277. this.useAutoMicroSurfaceFromReflectivityMap = false;
  278. /**
  279. * Allows to work with scalar in linear mode. This is definitely a matter of preferences and tools used during
  280. * the creation of the material.
  281. */
  282. this.useScalarInLinearSpace = false;
  283. /**
  284. * BJS is using an harcoded light falloff based on a manually sets up range.
  285. * In PBR, one way to represents the fallof is to use the inverse squared root algorythm.
  286. * This parameter can help you switch back to the BJS mode in order to create scenes using both materials.
  287. */
  288. this.usePhysicalLightFalloff = true;
  289. /**
  290. * Specifies that the material will keeps the reflection highlights over a transparent surface (only the most limunous ones).
  291. * A car glass is a good exemple of that. When the street lights reflects on it you can not see what is behind.
  292. */
  293. this.useRadianceOverAlpha = true;
  294. /**
  295. * Allows using the bump map in parallax mode.
  296. */
  297. this.useParallax = false;
  298. /**
  299. * Allows using the bump map in parallax occlusion mode.
  300. */
  301. this.useParallaxOcclusion = false;
  302. /**
  303. * Controls the scale bias of the parallax mode.
  304. */
  305. this.parallaxScaleBias = 0.05;
  306. /**
  307. * If sets to true, disables all the lights affecting the material.
  308. */
  309. this.disableLighting = false;
  310. /**
  311. * Number of Simultaneous lights allowed on the material.
  312. */
  313. this.maxSimultaneousLights = 4;
  314. /**
  315. * If sets to true, x component of normal map value will invert (x = 1.0 - x).
  316. */
  317. this.invertNormalMapX = false;
  318. /**
  319. * If sets to true, y component of normal map value will invert (y = 1.0 - y).
  320. */
  321. this.invertNormalMapY = false;
  322. this._renderTargets = new BABYLON.SmartArray(16);
  323. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  324. this._globalAmbientColor = new BABYLON.Color3(0, 0, 0);
  325. this._tempColor = new BABYLON.Color3();
  326. this._defines = new PBRMaterialDefines();
  327. this._cachedDefines = new PBRMaterialDefines();
  328. this._myScene = null;
  329. this._myShadowGenerator = null;
  330. this._cachedDefines.BonesPerMesh = -1;
  331. this.getRenderTargetTextures = function () {
  332. _this._renderTargets.reset();
  333. if (_this.reflectionTexture && _this.reflectionTexture.isRenderTarget) {
  334. _this._renderTargets.push(_this.reflectionTexture);
  335. }
  336. if (_this.refractionTexture && _this.refractionTexture.isRenderTarget) {
  337. _this._renderTargets.push(_this.refractionTexture);
  338. }
  339. return _this._renderTargets;
  340. };
  341. }
  342. Object.defineProperty(PBRMaterial.prototype, "useLogarithmicDepth", {
  343. get: function () {
  344. return this._useLogarithmicDepth;
  345. },
  346. set: function (value) {
  347. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  348. },
  349. enumerable: true,
  350. configurable: true
  351. });
  352. PBRMaterial.prototype.needAlphaBlending = function () {
  353. if (this.linkRefractionWithTransparency) {
  354. return false;
  355. }
  356. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromAlbedoTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  357. };
  358. PBRMaterial.prototype.needAlphaTesting = function () {
  359. if (this.linkRefractionWithTransparency) {
  360. return false;
  361. }
  362. return this.albedoTexture != null && this.albedoTexture.hasAlpha;
  363. };
  364. PBRMaterial.prototype._shouldUseAlphaFromAlbedoTexture = function () {
  365. return this.albedoTexture != null && this.albedoTexture.hasAlpha && this.useAlphaFromAlbedoTexture;
  366. };
  367. PBRMaterial.prototype.getAlphaTestTexture = function () {
  368. return this.albedoTexture;
  369. };
  370. PBRMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  371. if (!mesh) {
  372. return true;
  373. }
  374. if (this._defines.INSTANCES !== useInstances) {
  375. return false;
  376. }
  377. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  378. return true;
  379. }
  380. return false;
  381. };
  382. PBRMaterial.prototype.convertColorToLinearSpaceToRef = function (color, ref) {
  383. PBRMaterial.convertColorToLinearSpaceToRef(color, ref, this.useScalarInLinearSpace);
  384. };
  385. PBRMaterial.convertColorToLinearSpaceToRef = function (color, ref, useScalarInLinear) {
  386. if (!useScalarInLinear) {
  387. color.toLinearSpaceToRef(ref);
  388. }
  389. else {
  390. ref.r = color.r;
  391. ref.g = color.g;
  392. ref.b = color.b;
  393. }
  394. };
  395. PBRMaterial.BindLights = function (scene, mesh, effect, defines, useScalarInLinearSpace, maxSimultaneousLights, usePhysicalLightFalloff) {
  396. var lightIndex = 0;
  397. var depthValuesAlreadySet = false;
  398. for (var index = 0; index < scene.lights.length; index++) {
  399. var light = scene.lights[index];
  400. if (!light.isEnabled()) {
  401. continue;
  402. }
  403. if (!light.canAffectMesh(mesh)) {
  404. continue;
  405. }
  406. BABYLON.MaterialHelper.BindLightProperties(light, effect, lightIndex);
  407. // GAMMA CORRECTION.
  408. this.convertColorToLinearSpaceToRef(light.diffuse, PBRMaterial._scaledAlbedo, useScalarInLinearSpace);
  409. PBRMaterial._scaledAlbedo.scaleToRef(light.intensity, PBRMaterial._scaledAlbedo);
  410. effect.setColor4("vLightDiffuse" + lightIndex, PBRMaterial._scaledAlbedo, usePhysicalLightFalloff ? light.radius : light.range);
  411. if (defines["SPECULARTERM"]) {
  412. this.convertColorToLinearSpaceToRef(light.specular, PBRMaterial._scaledReflectivity, useScalarInLinearSpace);
  413. PBRMaterial._scaledReflectivity.scaleToRef(light.intensity, PBRMaterial._scaledReflectivity);
  414. effect.setColor3("vLightSpecular" + lightIndex, PBRMaterial._scaledReflectivity);
  415. }
  416. // Shadows
  417. if (scene.shadowsEnabled) {
  418. depthValuesAlreadySet = BABYLON.MaterialHelper.BindLightShadow(light, scene, mesh, lightIndex, effect, depthValuesAlreadySet);
  419. }
  420. lightIndex++;
  421. if (lightIndex === maxSimultaneousLights)
  422. break;
  423. }
  424. };
  425. PBRMaterial.prototype.isReady = function (mesh, useInstances) {
  426. if (this.checkReadyOnlyOnce) {
  427. if (this._wasPreviouslyReady) {
  428. return true;
  429. }
  430. }
  431. var scene = this.getScene();
  432. if (!this.checkReadyOnEveryCall) {
  433. if (this._renderId === scene.getRenderId()) {
  434. if (this._checkCache(scene, mesh, useInstances)) {
  435. return true;
  436. }
  437. }
  438. }
  439. var engine = scene.getEngine();
  440. var needNormals = false;
  441. var needUVs = false;
  442. this._defines.reset();
  443. if (scene.texturesEnabled) {
  444. // Textures
  445. if (scene.texturesEnabled) {
  446. if (scene.getEngine().getCaps().textureLOD) {
  447. this._defines.LODBASEDMICROSFURACE = true;
  448. }
  449. if (this.albedoTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  450. if (!this.albedoTexture.isReady()) {
  451. return false;
  452. }
  453. else {
  454. needUVs = true;
  455. this._defines.ALBEDO = true;
  456. }
  457. }
  458. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  459. if (!this.ambientTexture.isReady()) {
  460. return false;
  461. }
  462. else {
  463. needUVs = true;
  464. this._defines.AMBIENT = true;
  465. }
  466. }
  467. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  468. if (!this.opacityTexture.isReady()) {
  469. return false;
  470. }
  471. else {
  472. needUVs = true;
  473. this._defines.OPACITY = true;
  474. if (this.opacityTexture.getAlphaFromRGB) {
  475. this._defines.OPACITYRGB = true;
  476. }
  477. }
  478. }
  479. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  480. if (!this.reflectionTexture.isReady()) {
  481. return false;
  482. }
  483. else {
  484. needNormals = true;
  485. this._defines.REFLECTION = true;
  486. if (this.reflectionTexture.coordinatesMode === BABYLON.Texture.INVCUBIC_MODE) {
  487. this._defines.INVERTCUBICMAP = true;
  488. }
  489. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  490. switch (this.reflectionTexture.coordinatesMode) {
  491. case BABYLON.Texture.CUBIC_MODE:
  492. case BABYLON.Texture.INVCUBIC_MODE:
  493. this._defines.REFLECTIONMAP_CUBIC = true;
  494. break;
  495. case BABYLON.Texture.EXPLICIT_MODE:
  496. this._defines.REFLECTIONMAP_EXPLICIT = true;
  497. break;
  498. case BABYLON.Texture.PLANAR_MODE:
  499. this._defines.REFLECTIONMAP_PLANAR = true;
  500. break;
  501. case BABYLON.Texture.PROJECTION_MODE:
  502. this._defines.REFLECTIONMAP_PROJECTION = true;
  503. break;
  504. case BABYLON.Texture.SKYBOX_MODE:
  505. this._defines.REFLECTIONMAP_SKYBOX = true;
  506. break;
  507. case BABYLON.Texture.SPHERICAL_MODE:
  508. this._defines.REFLECTIONMAP_SPHERICAL = true;
  509. break;
  510. case BABYLON.Texture.EQUIRECTANGULAR_MODE:
  511. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  512. break;
  513. }
  514. if (this.reflectionTexture instanceof BABYLON.HDRCubeTexture && this.reflectionTexture) {
  515. this._defines.USESPHERICALFROMREFLECTIONMAP = true;
  516. needNormals = true;
  517. if (this.reflectionTexture.isPMREM) {
  518. this._defines.USEPMREMREFLECTION = true;
  519. }
  520. }
  521. }
  522. }
  523. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  524. if (!this.lightmapTexture.isReady()) {
  525. return false;
  526. }
  527. else {
  528. needUVs = true;
  529. this._defines.LIGHTMAP = true;
  530. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  531. }
  532. }
  533. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  534. if (!this.emissiveTexture.isReady()) {
  535. return false;
  536. }
  537. else {
  538. needUVs = true;
  539. this._defines.EMISSIVE = true;
  540. }
  541. }
  542. if (this.reflectivityTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  543. if (!this.reflectivityTexture.isReady()) {
  544. return false;
  545. }
  546. else {
  547. needUVs = true;
  548. this._defines.REFLECTIVITY = true;
  549. this._defines.MICROSURFACEFROMREFLECTIVITYMAP = this.useMicroSurfaceFromReflectivityMapAlpha;
  550. this._defines.MICROSURFACEAUTOMATIC = this.useAutoMicroSurfaceFromReflectivityMap;
  551. }
  552. }
  553. }
  554. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  555. if (!this.bumpTexture.isReady()) {
  556. return false;
  557. }
  558. else {
  559. needUVs = true;
  560. this._defines.BUMP = true;
  561. if (this.useParallax) {
  562. this._defines.PARALLAX = true;
  563. if (this.useParallaxOcclusion) {
  564. this._defines.PARALLAXOCCLUSION = true;
  565. }
  566. }
  567. if (this.invertNormalMapX) {
  568. this._defines.INVERTNORMALMAPX = true;
  569. }
  570. if (this.invertNormalMapY) {
  571. this._defines.INVERTNORMALMAPY = true;
  572. }
  573. }
  574. }
  575. if (this.refractionTexture && BABYLON.StandardMaterial.RefractionTextureEnabled) {
  576. if (!this.refractionTexture.isReady()) {
  577. return false;
  578. }
  579. else {
  580. needUVs = true;
  581. this._defines.REFRACTION = true;
  582. this._defines.REFRACTIONMAP_3D = this.refractionTexture.isCube;
  583. if (this.linkRefractionWithTransparency) {
  584. this._defines.LINKREFRACTIONTOTRANSPARENCY = true;
  585. }
  586. if (this.refractionTexture instanceof BABYLON.HDRCubeTexture) {
  587. this._defines.REFRACTIONMAPINLINEARSPACE = true;
  588. if (this.refractionTexture.isPMREM) {
  589. this._defines.USEPMREMREFRACTION = true;
  590. }
  591. }
  592. }
  593. }
  594. if (this.cameraColorGradingTexture) {
  595. if (!this.cameraColorGradingTexture.isReady()) {
  596. return false;
  597. }
  598. else {
  599. this._defines.CAMERACOLORGRADING = true;
  600. }
  601. }
  602. }
  603. // Effect
  604. if (scene.clipPlane) {
  605. this._defines.CLIPPLANE = true;
  606. }
  607. if (engine.getAlphaTesting()) {
  608. this._defines.ALPHATEST = true;
  609. }
  610. if (this._shouldUseAlphaFromAlbedoTexture()) {
  611. this._defines.ALPHAFROMALBEDO = true;
  612. }
  613. if (this.useEmissiveAsIllumination) {
  614. this._defines.EMISSIVEASILLUMINATION = true;
  615. }
  616. if (this.linkEmissiveWithAlbedo) {
  617. this._defines.LINKEMISSIVEWITHALBEDO = true;
  618. }
  619. if (this.useLogarithmicDepth) {
  620. this._defines.LOGARITHMICDEPTH = true;
  621. }
  622. if (this.cameraContrast != 1) {
  623. this._defines.CAMERACONTRAST = true;
  624. }
  625. if (this.cameraExposure != 1) {
  626. this._defines.CAMERATONEMAP = true;
  627. }
  628. if (this.cameraColorCurves) {
  629. this._defines.CAMERACOLORCURVES = true;
  630. }
  631. if (this.overloadedShadeIntensity != 1 ||
  632. this.overloadedShadowIntensity != 1) {
  633. this._defines.OVERLOADEDSHADOWVALUES = true;
  634. }
  635. if (this.overloadedMicroSurfaceIntensity > 0 ||
  636. this.overloadedEmissiveIntensity > 0 ||
  637. this.overloadedReflectivityIntensity > 0 ||
  638. this.overloadedAlbedoIntensity > 0 ||
  639. this.overloadedAmbientIntensity > 0 ||
  640. this.overloadedReflectionIntensity > 0) {
  641. this._defines.OVERLOADEDVALUES = true;
  642. }
  643. // Point size
  644. if (this.pointsCloud || scene.forcePointsCloud) {
  645. this._defines.POINTSIZE = true;
  646. }
  647. // Fog
  648. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  649. this._defines.FOG = true;
  650. }
  651. if (scene.lightsEnabled && !this.disableLighting) {
  652. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights) || needNormals;
  653. }
  654. if (BABYLON.StandardMaterial.FresnelEnabled) {
  655. // Fresnel
  656. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  657. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  658. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  659. this._defines.OPACITYFRESNEL = true;
  660. }
  661. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  662. this._defines.EMISSIVEFRESNEL = true;
  663. }
  664. needNormals = true;
  665. this._defines.FRESNEL = true;
  666. }
  667. }
  668. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  669. this._defines.SPECULAROVERALPHA = true;
  670. }
  671. if (this.usePhysicalLightFalloff) {
  672. this._defines.USEPHYSICALLIGHTFALLOFF = true;
  673. }
  674. if (this.useRadianceOverAlpha) {
  675. this._defines.RADIANCEOVERALPHA = true;
  676. }
  677. // Attribs
  678. if (mesh) {
  679. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  680. this._defines.NORMAL = true;
  681. }
  682. if (needUVs) {
  683. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  684. this._defines.UV1 = true;
  685. }
  686. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  687. this._defines.UV2 = true;
  688. }
  689. }
  690. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  691. this._defines.VERTEXCOLOR = true;
  692. if (mesh.hasVertexAlpha) {
  693. this._defines.VERTEXALPHA = true;
  694. }
  695. }
  696. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  697. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  698. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  699. }
  700. // Instances
  701. if (useInstances) {
  702. this._defines.INSTANCES = true;
  703. }
  704. }
  705. // Get correct effect
  706. if (!this._defines.isEqual(this._cachedDefines)) {
  707. this._defines.cloneTo(this._cachedDefines);
  708. scene.resetCachedMaterial();
  709. // Fallbacks
  710. var fallbacks = new BABYLON.EffectFallbacks();
  711. if (this._defines.REFLECTION) {
  712. fallbacks.addFallback(0, "REFLECTION");
  713. }
  714. if (this._defines.REFRACTION) {
  715. fallbacks.addFallback(0, "REFRACTION");
  716. }
  717. if (this._defines.REFLECTIVITY) {
  718. fallbacks.addFallback(0, "REFLECTIVITY");
  719. }
  720. if (this._defines.BUMP) {
  721. fallbacks.addFallback(0, "BUMP");
  722. }
  723. if (this._defines.PARALLAX) {
  724. fallbacks.addFallback(1, "PARALLAX");
  725. }
  726. if (this._defines.PARALLAXOCCLUSION) {
  727. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  728. }
  729. if (this._defines.SPECULAROVERALPHA) {
  730. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  731. }
  732. if (this._defines.FOG) {
  733. fallbacks.addFallback(1, "FOG");
  734. }
  735. if (this._defines.POINTSIZE) {
  736. fallbacks.addFallback(0, "POINTSIZE");
  737. }
  738. if (this._defines.LOGARITHMICDEPTH) {
  739. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  740. }
  741. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  742. if (this._defines.SPECULARTERM) {
  743. fallbacks.addFallback(0, "SPECULARTERM");
  744. }
  745. if (this._defines.OPACITYFRESNEL) {
  746. fallbacks.addFallback(1, "OPACITYFRESNEL");
  747. }
  748. if (this._defines.EMISSIVEFRESNEL) {
  749. fallbacks.addFallback(2, "EMISSIVEFRESNEL");
  750. }
  751. if (this._defines.FRESNEL) {
  752. fallbacks.addFallback(3, "FRESNEL");
  753. }
  754. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  755. fallbacks.addCPUSkinningFallback(0, mesh);
  756. }
  757. //Attributes
  758. var attribs = [BABYLON.VertexBuffer.PositionKind];
  759. if (this._defines.NORMAL) {
  760. attribs.push(BABYLON.VertexBuffer.NormalKind);
  761. }
  762. if (this._defines.UV1) {
  763. attribs.push(BABYLON.VertexBuffer.UVKind);
  764. }
  765. if (this._defines.UV2) {
  766. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  767. }
  768. if (this._defines.VERTEXCOLOR) {
  769. attribs.push(BABYLON.VertexBuffer.ColorKind);
  770. }
  771. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  772. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  773. // Legacy browser patch
  774. var shaderName = "pbr";
  775. if (!scene.getEngine().getCaps().standardDerivatives) {
  776. shaderName = "legacypbr";
  777. }
  778. var join = this._defines.toString();
  779. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vAlbedoColor", "vReflectivityColor", "vEmissiveColor", "vReflectionColor",
  780. "vFogInfos", "vFogColor", "pointSize",
  781. "vAlbedoInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vReflectivityInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  782. "mBones",
  783. "vClipPlane", "albedoMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "reflectivityMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  784. "depthValues",
  785. "opacityParts", "emissiveLeftColor", "emissiveRightColor",
  786. "vLightingIntensity", "vOverloadedShadowIntensity", "vOverloadedIntensity", "vOverloadedAlbedo", "vOverloadedReflection", "vOverloadedReflectivity", "vOverloadedEmissive", "vOverloadedMicroSurface",
  787. "logarithmicDepthConstant",
  788. "vSphericalX", "vSphericalY", "vSphericalZ",
  789. "vSphericalXX", "vSphericalYY", "vSphericalZZ",
  790. "vSphericalXY", "vSphericalYZ", "vSphericalZX",
  791. "vMicrosurfaceTextureLods",
  792. "vCameraInfos", "vCameraColorGradingInfos", "vCameraColorGradingScaleOffset"
  793. ];
  794. var samplers = ["albedoSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "reflectivitySampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler",
  795. "cameraColorGrading2DSampler"];
  796. BABYLON.ColorCurves.PrepareUniforms(uniforms);
  797. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  798. this._effect = scene.getEngine().createEffect(shaderName, attribs, uniforms, samplers, join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights });
  799. }
  800. if (!this._effect.isReady()) {
  801. return false;
  802. }
  803. this._renderId = scene.getRenderId();
  804. this._wasPreviouslyReady = true;
  805. if (mesh) {
  806. if (!mesh._materialDefines) {
  807. mesh._materialDefines = new PBRMaterialDefines();
  808. }
  809. this._defines.cloneTo(mesh._materialDefines);
  810. }
  811. return true;
  812. };
  813. PBRMaterial.prototype.unbind = function () {
  814. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  815. this._effect.setTexture("reflection2DSampler", null);
  816. }
  817. if (this.refractionTexture && this.refractionTexture.isRenderTarget) {
  818. this._effect.setTexture("refraction2DSampler", null);
  819. }
  820. _super.prototype.unbind.call(this);
  821. };
  822. PBRMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  823. this._effect.setMatrix("world", world);
  824. };
  825. PBRMaterial.prototype.bind = function (world, mesh) {
  826. this._myScene = this.getScene();
  827. // Matrices
  828. this.bindOnlyWorldMatrix(world);
  829. // Bones
  830. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  831. if (this._myScene.getCachedMaterial() !== this) {
  832. this._effect.setMatrix("viewProjection", this._myScene.getTransformMatrix());
  833. if (BABYLON.StandardMaterial.FresnelEnabled) {
  834. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  835. this._effect.setColor4("opacityParts", new BABYLON.Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  836. }
  837. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  838. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  839. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  840. }
  841. }
  842. // Textures
  843. if (this._myScene.texturesEnabled) {
  844. if (this.albedoTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  845. this._effect.setTexture("albedoSampler", this.albedoTexture);
  846. this._effect.setFloat2("vAlbedoInfos", this.albedoTexture.coordinatesIndex, this.albedoTexture.level);
  847. this._effect.setMatrix("albedoMatrix", this.albedoTexture.getTextureMatrix());
  848. }
  849. if (this.ambientTexture && BABYLON.StandardMaterial.AmbientTextureEnabled) {
  850. this._effect.setTexture("ambientSampler", this.ambientTexture);
  851. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  852. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  853. }
  854. if (this.opacityTexture && BABYLON.StandardMaterial.OpacityTextureEnabled) {
  855. this._effect.setTexture("opacitySampler", this.opacityTexture);
  856. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  857. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  858. }
  859. if (this.reflectionTexture && BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  860. this._microsurfaceTextureLods.x = Math.round(Math.log(this.reflectionTexture.getSize().width) * Math.LOG2E);
  861. if (this.reflectionTexture.isCube) {
  862. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  863. }
  864. else {
  865. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  866. }
  867. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  868. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, 0);
  869. if (this._defines.USESPHERICALFROMREFLECTIONMAP) {
  870. this._effect.setFloat3("vSphericalX", this.reflectionTexture.sphericalPolynomial.x.x, this.reflectionTexture.sphericalPolynomial.x.y, this.reflectionTexture.sphericalPolynomial.x.z);
  871. this._effect.setFloat3("vSphericalY", this.reflectionTexture.sphericalPolynomial.y.x, this.reflectionTexture.sphericalPolynomial.y.y, this.reflectionTexture.sphericalPolynomial.y.z);
  872. this._effect.setFloat3("vSphericalZ", this.reflectionTexture.sphericalPolynomial.z.x, this.reflectionTexture.sphericalPolynomial.z.y, this.reflectionTexture.sphericalPolynomial.z.z);
  873. this._effect.setFloat3("vSphericalXX", this.reflectionTexture.sphericalPolynomial.xx.x, this.reflectionTexture.sphericalPolynomial.xx.y, this.reflectionTexture.sphericalPolynomial.xx.z);
  874. this._effect.setFloat3("vSphericalYY", this.reflectionTexture.sphericalPolynomial.yy.x, this.reflectionTexture.sphericalPolynomial.yy.y, this.reflectionTexture.sphericalPolynomial.yy.z);
  875. this._effect.setFloat3("vSphericalZZ", this.reflectionTexture.sphericalPolynomial.zz.x, this.reflectionTexture.sphericalPolynomial.zz.y, this.reflectionTexture.sphericalPolynomial.zz.z);
  876. this._effect.setFloat3("vSphericalXY", this.reflectionTexture.sphericalPolynomial.xy.x, this.reflectionTexture.sphericalPolynomial.xy.y, this.reflectionTexture.sphericalPolynomial.xy.z);
  877. this._effect.setFloat3("vSphericalYZ", this.reflectionTexture.sphericalPolynomial.yz.x, this.reflectionTexture.sphericalPolynomial.yz.y, this.reflectionTexture.sphericalPolynomial.yz.z);
  878. this._effect.setFloat3("vSphericalZX", this.reflectionTexture.sphericalPolynomial.zx.x, this.reflectionTexture.sphericalPolynomial.zx.y, this.reflectionTexture.sphericalPolynomial.zx.z);
  879. }
  880. }
  881. if (this.emissiveTexture && BABYLON.StandardMaterial.EmissiveTextureEnabled) {
  882. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  883. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  884. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  885. }
  886. if (this.lightmapTexture && BABYLON.StandardMaterial.LightmapTextureEnabled) {
  887. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  888. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  889. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  890. }
  891. if (this.reflectivityTexture && BABYLON.StandardMaterial.SpecularTextureEnabled) {
  892. this._effect.setTexture("reflectivitySampler", this.reflectivityTexture);
  893. this._effect.setFloat2("vReflectivityInfos", this.reflectivityTexture.coordinatesIndex, this.reflectivityTexture.level);
  894. this._effect.setMatrix("reflectivityMatrix", this.reflectivityTexture.getTextureMatrix());
  895. }
  896. if (this.bumpTexture && this._myScene.getEngine().getCaps().standardDerivatives && BABYLON.StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  897. this._effect.setTexture("bumpSampler", this.bumpTexture);
  898. this._effect.setFloat3("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level, this.parallaxScaleBias);
  899. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  900. }
  901. if (this.refractionTexture && BABYLON.StandardMaterial.RefractionTextureEnabled) {
  902. this._microsurfaceTextureLods.y = Math.round(Math.log(this.refractionTexture.getSize().width) * Math.LOG2E);
  903. var depth = 1.0;
  904. if (this.refractionTexture.isCube) {
  905. this._effect.setTexture("refractionCubeSampler", this.refractionTexture);
  906. }
  907. else {
  908. this._effect.setTexture("refraction2DSampler", this.refractionTexture);
  909. this._effect.setMatrix("refractionMatrix", this.refractionTexture.getReflectionTextureMatrix());
  910. if (this.refractionTexture.depth) {
  911. depth = this.refractionTexture.depth;
  912. }
  913. }
  914. this._effect.setFloat4("vRefractionInfos", this.refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  915. }
  916. if ((this.reflectionTexture || this.refractionTexture)) {
  917. this._effect.setFloat2("vMicrosurfaceTextureLods", this._microsurfaceTextureLods.x, this._microsurfaceTextureLods.y);
  918. }
  919. if (this.cameraColorGradingTexture) {
  920. this._effect.setTexture("cameraColorGrading2DSampler", this.cameraColorGradingTexture);
  921. this._cameraColorGradingInfos.x = this.cameraColorGradingTexture.level; // Texture Level
  922. this._cameraColorGradingInfos.y = this.cameraColorGradingTexture.getSize().height; // Texture Size example with 8
  923. this._cameraColorGradingInfos.z = this._cameraColorGradingInfos.y - 1.0; // SizeMinusOne 8 - 1
  924. this._cameraColorGradingInfos.w = 1 / this._cameraColorGradingInfos.y; // Space of 1 slice 1 / 8
  925. this._effect.setFloat4("vCameraColorGradingInfos", this._cameraColorGradingInfos.x, this._cameraColorGradingInfos.y, this._cameraColorGradingInfos.z, this._cameraColorGradingInfos.w);
  926. var slicePixelSizeU = this._cameraColorGradingInfos.w / this._cameraColorGradingInfos.y; // Space of 1 pixel in U direction, e.g. 1/64
  927. var slicePixelSizeV = 1.0 / this._cameraColorGradingInfos.y; // Space of 1 pixel in V direction, e.g. 1/8
  928. this._cameraColorGradingScaleOffset.x = this._cameraColorGradingInfos.z * slicePixelSizeU; // Extent of lookup range in U for a single slice so that range corresponds to (size-1) texels, for example 7/64
  929. this._cameraColorGradingScaleOffset.y = this._cameraColorGradingInfos.z /
  930. this._cameraColorGradingInfos.y; // Extent of lookup range in V for a single slice so that range corresponds to (size-1) texels, for example 7/8
  931. this._cameraColorGradingScaleOffset.z = 0.5 * slicePixelSizeU; // Offset of lookup range in U to align sample position with texel centre, for example 0.5/64
  932. this._cameraColorGradingScaleOffset.w = 0.5 * slicePixelSizeV; // Offset of lookup range in V to align sample position with texel centre, for example 0.5/8
  933. this._effect.setFloat4("vCameraColorGradingScaleOffset", this._cameraColorGradingScaleOffset.x, this._cameraColorGradingScaleOffset.y, this._cameraColorGradingScaleOffset.z, this._cameraColorGradingScaleOffset.w);
  934. }
  935. }
  936. // Clip plane
  937. BABYLON.MaterialHelper.BindClipPlane(this._effect, this._myScene);
  938. // Point size
  939. if (this.pointsCloud) {
  940. this._effect.setFloat("pointSize", this.pointSize);
  941. }
  942. // Colors
  943. this._myScene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  944. // GAMMA CORRECTION.
  945. this.convertColorToLinearSpaceToRef(this.reflectivityColor, PBRMaterial._scaledReflectivity);
  946. this._effect.setVector3("vEyePosition", this._myScene._mirroredCameraPosition ? this._myScene._mirroredCameraPosition : this._myScene.activeCamera.position);
  947. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  948. this._effect.setColor4("vReflectivityColor", PBRMaterial._scaledReflectivity, this.microSurface);
  949. // GAMMA CORRECTION.
  950. this.convertColorToLinearSpaceToRef(this.emissiveColor, PBRMaterial._scaledEmissive);
  951. this._effect.setColor3("vEmissiveColor", PBRMaterial._scaledEmissive);
  952. // GAMMA CORRECTION.
  953. this.convertColorToLinearSpaceToRef(this.reflectionColor, PBRMaterial._scaledReflection);
  954. this._effect.setColor3("vReflectionColor", PBRMaterial._scaledReflection);
  955. }
  956. if (this._myScene.getCachedMaterial() !== this || !this.isFrozen) {
  957. // GAMMA CORRECTION.
  958. this.convertColorToLinearSpaceToRef(this.albedoColor, PBRMaterial._scaledAlbedo);
  959. this._effect.setColor4("vAlbedoColor", PBRMaterial._scaledAlbedo, this.alpha * mesh.visibility);
  960. // Lights
  961. if (this._myScene.lightsEnabled && !this.disableLighting) {
  962. PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines, this.useScalarInLinearSpace, this.maxSimultaneousLights, this.usePhysicalLightFalloff);
  963. }
  964. // View
  965. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== BABYLON.Scene.FOGMODE_NONE || this.reflectionTexture) {
  966. this._effect.setMatrix("view", this._myScene.getViewMatrix());
  967. }
  968. // Fog
  969. BABYLON.MaterialHelper.BindFogParameters(this._myScene, mesh, this._effect);
  970. this._lightingInfos.x = this.directIntensity;
  971. this._lightingInfos.y = this.emissiveIntensity;
  972. this._lightingInfos.z = this.environmentIntensity;
  973. this._lightingInfos.w = this.specularIntensity;
  974. this._effect.setVector4("vLightingIntensity", this._lightingInfos);
  975. this._overloadedShadowInfos.x = this.overloadedShadowIntensity;
  976. this._overloadedShadowInfos.y = this.overloadedShadeIntensity;
  977. this._effect.setVector4("vOverloadedShadowIntensity", this._overloadedShadowInfos);
  978. this._cameraInfos.x = this.cameraExposure;
  979. this._cameraInfos.y = this.cameraContrast;
  980. this._effect.setVector4("vCameraInfos", this._cameraInfos);
  981. if (this.cameraColorCurves) {
  982. BABYLON.ColorCurves.Bind(this.cameraColorCurves, this._effect);
  983. }
  984. this._overloadedIntensity.x = this.overloadedAmbientIntensity;
  985. this._overloadedIntensity.y = this.overloadedAlbedoIntensity;
  986. this._overloadedIntensity.z = this.overloadedReflectivityIntensity;
  987. this._overloadedIntensity.w = this.overloadedEmissiveIntensity;
  988. this._effect.setVector4("vOverloadedIntensity", this._overloadedIntensity);
  989. this.convertColorToLinearSpaceToRef(this.overloadedAmbient, this._tempColor);
  990. this._effect.setColor3("vOverloadedAmbient", this._tempColor);
  991. this.convertColorToLinearSpaceToRef(this.overloadedAlbedo, this._tempColor);
  992. this._effect.setColor3("vOverloadedAlbedo", this._tempColor);
  993. this.convertColorToLinearSpaceToRef(this.overloadedReflectivity, this._tempColor);
  994. this._effect.setColor3("vOverloadedReflectivity", this._tempColor);
  995. this.convertColorToLinearSpaceToRef(this.overloadedEmissive, this._tempColor);
  996. this._effect.setColor3("vOverloadedEmissive", this._tempColor);
  997. this.convertColorToLinearSpaceToRef(this.overloadedReflection, this._tempColor);
  998. this._effect.setColor3("vOverloadedReflection", this._tempColor);
  999. this._overloadedMicroSurface.x = this.overloadedMicroSurface;
  1000. this._overloadedMicroSurface.y = this.overloadedMicroSurfaceIntensity;
  1001. this._overloadedMicroSurface.z = this.overloadedReflectionIntensity;
  1002. this._effect.setVector3("vOverloadedMicroSurface", this._overloadedMicroSurface);
  1003. // Log. depth
  1004. BABYLON.MaterialHelper.BindLogDepth(this._defines, this._effect, this._myScene);
  1005. }
  1006. _super.prototype.bind.call(this, world, mesh);
  1007. this._myScene = null;
  1008. };
  1009. PBRMaterial.prototype.getAnimatables = function () {
  1010. var results = [];
  1011. if (this.albedoTexture && this.albedoTexture.animations && this.albedoTexture.animations.length > 0) {
  1012. results.push(this.albedoTexture);
  1013. }
  1014. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  1015. results.push(this.ambientTexture);
  1016. }
  1017. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  1018. results.push(this.opacityTexture);
  1019. }
  1020. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  1021. results.push(this.reflectionTexture);
  1022. }
  1023. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  1024. results.push(this.emissiveTexture);
  1025. }
  1026. if (this.reflectivityTexture && this.reflectivityTexture.animations && this.reflectivityTexture.animations.length > 0) {
  1027. results.push(this.reflectivityTexture);
  1028. }
  1029. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  1030. results.push(this.bumpTexture);
  1031. }
  1032. if (this.lightmapTexture && this.lightmapTexture.animations && this.lightmapTexture.animations.length > 0) {
  1033. results.push(this.lightmapTexture);
  1034. }
  1035. if (this.refractionTexture && this.refractionTexture.animations && this.refractionTexture.animations.length > 0) {
  1036. results.push(this.refractionTexture);
  1037. }
  1038. if (this.cameraColorGradingTexture && this.cameraColorGradingTexture.animations && this.cameraColorGradingTexture.animations.length > 0) {
  1039. results.push(this.cameraColorGradingTexture);
  1040. }
  1041. return results;
  1042. };
  1043. PBRMaterial.prototype.dispose = function (forceDisposeEffect, forceDisposeTextures) {
  1044. if (forceDisposeTextures) {
  1045. if (this.albedoTexture) {
  1046. this.albedoTexture.dispose();
  1047. }
  1048. if (this.ambientTexture) {
  1049. this.ambientTexture.dispose();
  1050. }
  1051. if (this.opacityTexture) {
  1052. this.opacityTexture.dispose();
  1053. }
  1054. if (this.reflectionTexture) {
  1055. this.reflectionTexture.dispose();
  1056. }
  1057. if (this.emissiveTexture) {
  1058. this.emissiveTexture.dispose();
  1059. }
  1060. if (this.reflectivityTexture) {
  1061. this.reflectivityTexture.dispose();
  1062. }
  1063. if (this.bumpTexture) {
  1064. this.bumpTexture.dispose();
  1065. }
  1066. if (this.lightmapTexture) {
  1067. this.lightmapTexture.dispose();
  1068. }
  1069. if (this.refractionTexture) {
  1070. this.refractionTexture.dispose();
  1071. }
  1072. if (this.cameraColorGradingTexture) {
  1073. this.cameraColorGradingTexture.dispose();
  1074. }
  1075. }
  1076. _super.prototype.dispose.call(this, forceDisposeEffect, forceDisposeTextures);
  1077. };
  1078. PBRMaterial.prototype.clone = function (name) {
  1079. var _this = this;
  1080. return BABYLON.SerializationHelper.Clone(function () { return new PBRMaterial(name, _this.getScene()); }, this);
  1081. };
  1082. PBRMaterial.prototype.serialize = function () {
  1083. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  1084. serializationObject.customType = "BABYLON.PBRMaterial";
  1085. return serializationObject;
  1086. };
  1087. // Statics
  1088. PBRMaterial.Parse = function (source, scene, rootUrl) {
  1089. return BABYLON.SerializationHelper.Parse(function () { return new PBRMaterial(source.name, scene); }, source, scene, rootUrl);
  1090. };
  1091. PBRMaterial._scaledAlbedo = new BABYLON.Color3();
  1092. PBRMaterial._scaledReflectivity = new BABYLON.Color3();
  1093. PBRMaterial._scaledEmissive = new BABYLON.Color3();
  1094. PBRMaterial._scaledReflection = new BABYLON.Color3();
  1095. __decorate([
  1096. BABYLON.serialize()
  1097. ], PBRMaterial.prototype, "directIntensity", void 0);
  1098. __decorate([
  1099. BABYLON.serialize()
  1100. ], PBRMaterial.prototype, "emissiveIntensity", void 0);
  1101. __decorate([
  1102. BABYLON.serialize()
  1103. ], PBRMaterial.prototype, "environmentIntensity", void 0);
  1104. __decorate([
  1105. BABYLON.serialize()
  1106. ], PBRMaterial.prototype, "specularIntensity", void 0);
  1107. __decorate([
  1108. BABYLON.serialize()
  1109. ], PBRMaterial.prototype, "disableBumpMap", void 0);
  1110. __decorate([
  1111. BABYLON.serialize()
  1112. ], PBRMaterial.prototype, "overloadedShadowIntensity", void 0);
  1113. __decorate([
  1114. BABYLON.serialize()
  1115. ], PBRMaterial.prototype, "overloadedShadeIntensity", void 0);
  1116. __decorate([
  1117. BABYLON.serialize()
  1118. ], PBRMaterial.prototype, "cameraExposure", void 0);
  1119. __decorate([
  1120. BABYLON.serialize()
  1121. ], PBRMaterial.prototype, "cameraContrast", void 0);
  1122. __decorate([
  1123. BABYLON.serializeAsTexture()
  1124. ], PBRMaterial.prototype, "cameraColorGradingTexture", void 0);
  1125. __decorate([
  1126. BABYLON.serializeAsColorCurves()
  1127. ], PBRMaterial.prototype, "cameraColorCurves", void 0);
  1128. __decorate([
  1129. BABYLON.serializeAsColor3()
  1130. ], PBRMaterial.prototype, "overloadedAmbient", void 0);
  1131. __decorate([
  1132. BABYLON.serialize()
  1133. ], PBRMaterial.prototype, "overloadedAmbientIntensity", void 0);
  1134. __decorate([
  1135. BABYLON.serializeAsColor3()
  1136. ], PBRMaterial.prototype, "overloadedAlbedo", void 0);
  1137. __decorate([
  1138. BABYLON.serialize()
  1139. ], PBRMaterial.prototype, "overloadedAlbedoIntensity", void 0);
  1140. __decorate([
  1141. BABYLON.serializeAsColor3()
  1142. ], PBRMaterial.prototype, "overloadedReflectivity", void 0);
  1143. __decorate([
  1144. BABYLON.serialize()
  1145. ], PBRMaterial.prototype, "overloadedReflectivityIntensity", void 0);
  1146. __decorate([
  1147. BABYLON.serializeAsColor3()
  1148. ], PBRMaterial.prototype, "overloadedEmissive", void 0);
  1149. __decorate([
  1150. BABYLON.serialize()
  1151. ], PBRMaterial.prototype, "overloadedEmissiveIntensity", void 0);
  1152. __decorate([
  1153. BABYLON.serializeAsColor3()
  1154. ], PBRMaterial.prototype, "overloadedReflection", void 0);
  1155. __decorate([
  1156. BABYLON.serialize()
  1157. ], PBRMaterial.prototype, "overloadedReflectionIntensity", void 0);
  1158. __decorate([
  1159. BABYLON.serialize()
  1160. ], PBRMaterial.prototype, "overloadedMicroSurface", void 0);
  1161. __decorate([
  1162. BABYLON.serialize()
  1163. ], PBRMaterial.prototype, "overloadedMicroSurfaceIntensity", void 0);
  1164. __decorate([
  1165. BABYLON.serializeAsTexture()
  1166. ], PBRMaterial.prototype, "albedoTexture", void 0);
  1167. __decorate([
  1168. BABYLON.serializeAsTexture()
  1169. ], PBRMaterial.prototype, "ambientTexture", void 0);
  1170. __decorate([
  1171. BABYLON.serializeAsTexture()
  1172. ], PBRMaterial.prototype, "opacityTexture", void 0);
  1173. __decorate([
  1174. BABYLON.serializeAsTexture()
  1175. ], PBRMaterial.prototype, "reflectionTexture", void 0);
  1176. __decorate([
  1177. BABYLON.serializeAsTexture()
  1178. ], PBRMaterial.prototype, "emissiveTexture", void 0);
  1179. __decorate([
  1180. BABYLON.serializeAsTexture()
  1181. ], PBRMaterial.prototype, "reflectivityTexture", void 0);
  1182. __decorate([
  1183. BABYLON.serializeAsTexture()
  1184. ], PBRMaterial.prototype, "bumpTexture", void 0);
  1185. __decorate([
  1186. BABYLON.serializeAsTexture()
  1187. ], PBRMaterial.prototype, "lightmapTexture", void 0);
  1188. __decorate([
  1189. BABYLON.serializeAsTexture()
  1190. ], PBRMaterial.prototype, "refractionTexture", void 0);
  1191. __decorate([
  1192. BABYLON.serializeAsColor3("ambient")
  1193. ], PBRMaterial.prototype, "ambientColor", void 0);
  1194. __decorate([
  1195. BABYLON.serializeAsColor3("albedo")
  1196. ], PBRMaterial.prototype, "albedoColor", void 0);
  1197. __decorate([
  1198. BABYLON.serializeAsColor3("reflectivity")
  1199. ], PBRMaterial.prototype, "reflectivityColor", void 0);
  1200. __decorate([
  1201. BABYLON.serializeAsColor3("reflection")
  1202. ], PBRMaterial.prototype, "reflectionColor", void 0);
  1203. __decorate([
  1204. BABYLON.serializeAsColor3("emissive")
  1205. ], PBRMaterial.prototype, "emissiveColor", void 0);
  1206. __decorate([
  1207. BABYLON.serialize()
  1208. ], PBRMaterial.prototype, "microSurface", void 0);
  1209. __decorate([
  1210. BABYLON.serialize()
  1211. ], PBRMaterial.prototype, "indexOfRefraction", void 0);
  1212. __decorate([
  1213. BABYLON.serialize()
  1214. ], PBRMaterial.prototype, "invertRefractionY", void 0);
  1215. __decorate([
  1216. BABYLON.serializeAsFresnelParameters()
  1217. ], PBRMaterial.prototype, "opacityFresnelParameters", void 0);
  1218. __decorate([
  1219. BABYLON.serializeAsFresnelParameters()
  1220. ], PBRMaterial.prototype, "emissiveFresnelParameters", void 0);
  1221. __decorate([
  1222. BABYLON.serialize()
  1223. ], PBRMaterial.prototype, "linkRefractionWithTransparency", void 0);
  1224. __decorate([
  1225. BABYLON.serialize()
  1226. ], PBRMaterial.prototype, "linkEmissiveWithAlbedo", void 0);
  1227. __decorate([
  1228. BABYLON.serialize()
  1229. ], PBRMaterial.prototype, "useLightmapAsShadowmap", void 0);
  1230. __decorate([
  1231. BABYLON.serialize()
  1232. ], PBRMaterial.prototype, "useEmissiveAsIllumination", void 0);
  1233. __decorate([
  1234. BABYLON.serialize()
  1235. ], PBRMaterial.prototype, "useAlphaFromAlbedoTexture", void 0);
  1236. __decorate([
  1237. BABYLON.serialize()
  1238. ], PBRMaterial.prototype, "useSpecularOverAlpha", void 0);
  1239. __decorate([
  1240. BABYLON.serialize()
  1241. ], PBRMaterial.prototype, "useMicroSurfaceFromReflectivityMapAlpha", void 0);
  1242. __decorate([
  1243. BABYLON.serialize()
  1244. ], PBRMaterial.prototype, "useAutoMicroSurfaceFromReflectivityMap", void 0);
  1245. __decorate([
  1246. BABYLON.serialize()
  1247. ], PBRMaterial.prototype, "useScalarInLinearSpace", void 0);
  1248. __decorate([
  1249. BABYLON.serialize()
  1250. ], PBRMaterial.prototype, "usePhysicalLightFalloff", void 0);
  1251. __decorate([
  1252. BABYLON.serialize()
  1253. ], PBRMaterial.prototype, "useRadianceOverAlpha", void 0);
  1254. __decorate([
  1255. BABYLON.serialize()
  1256. ], PBRMaterial.prototype, "useParallax", void 0);
  1257. __decorate([
  1258. BABYLON.serialize()
  1259. ], PBRMaterial.prototype, "useParallaxOcclusion", void 0);
  1260. __decorate([
  1261. BABYLON.serialize()
  1262. ], PBRMaterial.prototype, "parallaxScaleBias", void 0);
  1263. __decorate([
  1264. BABYLON.serialize()
  1265. ], PBRMaterial.prototype, "disableLighting", void 0);
  1266. __decorate([
  1267. BABYLON.serialize()
  1268. ], PBRMaterial.prototype, "maxSimultaneousLights", void 0);
  1269. __decorate([
  1270. BABYLON.serialize()
  1271. ], PBRMaterial.prototype, "invertNormalMapX", void 0);
  1272. __decorate([
  1273. BABYLON.serialize()
  1274. ], PBRMaterial.prototype, "invertNormalMapY", void 0);
  1275. __decorate([
  1276. BABYLON.serialize()
  1277. ], PBRMaterial.prototype, "useLogarithmicDepth", null);
  1278. return PBRMaterial;
  1279. })(BABYLON.Material);
  1280. BABYLON.PBRMaterial = PBRMaterial;
  1281. })(BABYLON || (BABYLON = {}));