babylon.pbrMaterial.ts 59 KB

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