babylon.pbrMaterial.ts 75 KB

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