babylon.pbrBaseMaterial.ts 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. module BABYLON {
  2. class PBRMaterialDefines extends MaterialDefines implements IImageProcessingConfigurationDefines {
  3. public PBR = true;
  4. public MAINUV1 = false;
  5. public MAINUV2 = false;
  6. public UV1 = false;
  7. public UV2 = false;
  8. public ALBEDO = false;
  9. public ALBEDODIRECTUV = 0;
  10. public VERTEXCOLOR = false;
  11. public AMBIENT = false;
  12. public AMBIENTDIRECTUV = 0;
  13. public AMBIENTINGRAYSCALE = false;
  14. public OPACITY = false;
  15. public VERTEXALPHA = false;
  16. public OPACITYDIRECTUV = 0;
  17. public OPACITYRGB = false;
  18. public ALPHATEST = false;
  19. public ALPHABLEND = false;
  20. public ALPHAFROMALBEDO = false;
  21. public ALPHATESTVALUE = 0.5;
  22. public SPECULAROVERALPHA = false;
  23. public RADIANCEOVERALPHA = false;
  24. public ALPHAFRESNEL = false;
  25. public PREMULTIPLYALPHA = false;
  26. public EMISSIVE = false;
  27. public EMISSIVEDIRECTUV = 0;
  28. public REFLECTIVITY = false;
  29. public REFLECTIVITYDIRECTUV = 0;
  30. public SPECULARTERM = false;
  31. public MICROSURFACEFROMREFLECTIVITYMAP = false;
  32. public MICROSURFACEAUTOMATIC = false;
  33. public LODBASEDMICROSFURACE = false;
  34. public MICROSURFACEMAP = false;
  35. public MICROSURFACEMAPDIRECTUV = 0;
  36. public METALLICWORKFLOW = false;
  37. public ROUGHNESSSTOREINMETALMAPALPHA = false;
  38. public ROUGHNESSSTOREINMETALMAPGREEN = false;
  39. public METALLNESSSTOREINMETALMAPBLUE = false;
  40. public AOSTOREINMETALMAPRED = false;
  41. public ENVIRONMENTBRDF = false;
  42. public NORMAL = false;
  43. public TANGENT = false;
  44. public BUMP = false;
  45. public BUMPDIRECTUV = 0;
  46. public PARALLAX = false;
  47. public PARALLAXOCCLUSION = false;
  48. public INVERTNORMALMAPX = false;
  49. public INVERTNORMALMAPY = false;
  50. public NORMALXYSCALE = true;
  51. public LIGHTMAP = false;
  52. public LIGHTMAPDIRECTUV = 0;
  53. public USELIGHTMAPASSHADOWMAP = false;
  54. public REFLECTION = false;
  55. public REFLECTIONMAP_3D = false;
  56. public REFLECTIONMAP_SPHERICAL = false;
  57. public REFLECTIONMAP_PLANAR = false;
  58. public REFLECTIONMAP_CUBIC = false;
  59. public REFLECTIONMAP_PROJECTION = false;
  60. public REFLECTIONMAP_SKYBOX = false;
  61. public REFLECTIONMAP_EXPLICIT = false;
  62. public REFLECTIONMAP_EQUIRECTANGULAR = false;
  63. public REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  64. public REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  65. public INVERTCUBICMAP = false;
  66. public USESPHERICALFROMREFLECTIONMAP = false;
  67. public USESPHERICALINFRAGMENT = false;
  68. public REFLECTIONMAP_OPPOSITEZ = false;
  69. public LODINREFLECTIONALPHA = false;
  70. public GAMMAREFLECTION = false;
  71. public REFRACTION = false;
  72. public REFRACTIONMAP_3D = false;
  73. public REFRACTIONMAP_OPPOSITEZ = false;
  74. public LODINREFRACTIONALPHA = false;
  75. public GAMMAREFRACTION = false;
  76. public LINKREFRACTIONTOTRANSPARENCY = false;
  77. public INSTANCES = false;
  78. public NUM_BONE_INFLUENCERS = 0;
  79. public BonesPerMesh = 0;
  80. public MORPHTARGETS = false;
  81. public MORPHTARGETS_NORMAL = false;
  82. public MORPHTARGETS_TANGENT = false;
  83. public NUM_MORPH_INFLUENCERS = 0;
  84. public IMAGEPROCESSING = false;
  85. public VIGNETTE = false;
  86. public VIGNETTEBLENDMODEMULTIPLY = false;
  87. public VIGNETTEBLENDMODEOPAQUE = false;
  88. public TONEMAPPING = false;
  89. public CONTRAST = false;
  90. public COLORCURVES = false;
  91. public COLORGRADING = false;
  92. public SAMPLER3DGREENDEPTH = false;
  93. public SAMPLER3DBGRMAP = false;
  94. public IMAGEPROCESSINGPOSTPROCESS = false;
  95. public EXPOSURE = false;
  96. public USEPHYSICALLIGHTFALLOFF = false;
  97. public TWOSIDEDLIGHTING = false;
  98. public SHADOWFLOAT = false;
  99. public USERIGHTHANDEDSYSTEM = false;
  100. public CLIPPLANE = false;
  101. public POINTSIZE = false;
  102. public FOG = false;
  103. public LOGARITHMICDEPTH = false;
  104. constructor() {
  105. super();
  106. this.rebuild();
  107. }
  108. public reset(): void {
  109. super.reset();
  110. this.ALPHATESTVALUE = 0.5;
  111. this.PBR = true;
  112. }
  113. }
  114. /**
  115. * The Physically based material base class of BJS.
  116. *
  117. * This offers the main features of a standard PBR material.
  118. * For more information, please refer to the documentation :
  119. * http://doc.babylonjs.com/extensions/Physically_Based_Rendering
  120. */
  121. export abstract class PBRBaseMaterial extends PushMaterial {
  122. /**
  123. * Intensity of the direct lights e.g. the four lights available in your scene.
  124. * This impacts both the direct diffuse and specular highlights.
  125. */
  126. protected _directIntensity: number = 1.0;
  127. /**
  128. * Intensity of the emissive part of the material.
  129. * This helps controlling the emissive effect without modifying the emissive color.
  130. */
  131. protected _emissiveIntensity: number = 1.0;
  132. /**
  133. * Intensity of the environment e.g. how much the environment will light the object
  134. * either through harmonics for rough material or through the refelction for shiny ones.
  135. */
  136. protected _environmentIntensity: number = 1.0;
  137. /**
  138. * This is a special control allowing the reduction of the specular highlights coming from the
  139. * four lights of the scene. Those highlights may not be needed in full environment lighting.
  140. */
  141. protected _specularIntensity: number = 1.0;
  142. private _lightingInfos: Vector4 = new Vector4(this._directIntensity, this._emissiveIntensity, this._environmentIntensity, this._specularIntensity);
  143. /**
  144. * Debug Control allowing disabling the bump map on this material.
  145. */
  146. protected _disableBumpMap: boolean = false;
  147. /**
  148. * AKA Diffuse Texture in standard nomenclature.
  149. */
  150. protected _albedoTexture: BaseTexture;
  151. /**
  152. * AKA Occlusion Texture in other nomenclature.
  153. */
  154. protected _ambientTexture: BaseTexture;
  155. /**
  156. * AKA Occlusion Texture Intensity in other nomenclature.
  157. */
  158. protected _ambientTextureStrength: number = 1.0;
  159. protected _opacityTexture: BaseTexture;
  160. protected _reflectionTexture: BaseTexture;
  161. protected _refractionTexture: BaseTexture;
  162. protected _emissiveTexture: BaseTexture;
  163. /**
  164. * AKA Specular texture in other nomenclature.
  165. */
  166. protected _reflectivityTexture: BaseTexture;
  167. /**
  168. * Used to switch from specular/glossiness to metallic/roughness workflow.
  169. */
  170. protected _metallicTexture: BaseTexture;
  171. /**
  172. * Specifies the metallic scalar of the metallic/roughness workflow.
  173. * Can also be used to scale the metalness values of the metallic texture.
  174. */
  175. protected _metallic: number;
  176. /**
  177. * Specifies the roughness scalar of the metallic/roughness workflow.
  178. * Can also be used to scale the roughness values of the metallic texture.
  179. */
  180. protected _roughness: number;
  181. /**
  182. * Used to enable roughness/glossiness fetch from a separate chanel depending on the current mode.
  183. * Gray Scale represents roughness in metallic mode and glossiness in specular mode.
  184. */
  185. protected _microSurfaceTexture: BaseTexture;
  186. protected _bumpTexture: BaseTexture;
  187. protected _lightmapTexture: BaseTexture;
  188. protected _ambientColor = new Color3(0, 0, 0);
  189. /**
  190. * AKA Diffuse Color in other nomenclature.
  191. */
  192. protected _albedoColor = new Color3(1, 1, 1);
  193. /**
  194. * AKA Specular Color in other nomenclature.
  195. */
  196. protected _reflectivityColor = new Color3(1, 1, 1);
  197. protected _reflectionColor = new Color3(1, 1, 1);
  198. protected _emissiveColor = new Color3(0, 0, 0);
  199. /**
  200. * AKA Glossiness in other nomenclature.
  201. */
  202. protected _microSurface = 0.9;
  203. /**
  204. * source material index of refraction (IOR)' / 'destination material IOR.
  205. */
  206. protected _indexOfRefraction = 0.66;
  207. /**
  208. * Controls if refraction needs to be inverted on Y. This could be usefull for procedural texture.
  209. */
  210. protected _invertRefractionY = false;
  211. /**
  212. * This parameters will make the material used its opacity to control how much it is refracting aginst not.
  213. * Materials half opaque for instance using refraction could benefit from this control.
  214. */
  215. protected _linkRefractionWithTransparency = false;
  216. protected _useLightmapAsShadowmap = false;
  217. /**
  218. * Specifies that the alpha is coming form the albedo channel alpha channel.
  219. */
  220. protected _useAlphaFromAlbedoTexture = false;
  221. /**
  222. * Specifies that the material will keeps the specular highlights over a transparent surface (only the most limunous ones).
  223. * A car glass is a good exemple of that. When sun reflects on it you can not see what is behind.
  224. */
  225. protected _useSpecularOverAlpha = true;
  226. /**
  227. * Specifies if the reflectivity texture contains the glossiness information in its alpha channel.
  228. */
  229. protected _useMicroSurfaceFromReflectivityMapAlpha = false;
  230. /**
  231. * Specifies if the metallic texture contains the roughness information in its alpha channel.
  232. */
  233. protected _useRoughnessFromMetallicTextureAlpha = true;
  234. /**
  235. * Specifies if the metallic texture contains the roughness information in its green channel.
  236. */
  237. protected _useRoughnessFromMetallicTextureGreen = false;
  238. /**
  239. * Specifies if the metallic texture contains the metallness information in its blue channel.
  240. */
  241. protected _useMetallnessFromMetallicTextureBlue = false;
  242. /**
  243. * Specifies if the metallic texture contains the ambient occlusion information in its red channel.
  244. */
  245. protected _useAmbientOcclusionFromMetallicTextureRed = false;
  246. /**
  247. * Specifies if the ambient texture contains the ambient occlusion information in its red channel only.
  248. */
  249. protected _useAmbientInGrayScale = false;
  250. /**
  251. * In case the reflectivity map does not contain the microsurface information in its alpha channel,
  252. * The material will try to infer what glossiness each pixel should be.
  253. */
  254. protected _useAutoMicroSurfaceFromReflectivityMap = false;
  255. /**
  256. * BJS is using an harcoded light falloff based on a manually sets up range.
  257. * In PBR, one way to represents the fallof is to use the inverse squared root algorythm.
  258. * This parameter can help you switch back to the BJS mode in order to create scenes using both materials.
  259. */
  260. protected _usePhysicalLightFalloff = true;
  261. /**
  262. * Specifies that the material will keeps the reflection highlights over a transparent surface (only the most limunous ones).
  263. * A car glass is a good exemple of that. When the street lights reflects on it you can not see what is behind.
  264. */
  265. protected _useRadianceOverAlpha = true;
  266. /**
  267. * Allows using the bump map in parallax mode.
  268. */
  269. protected _useParallax = false;
  270. /**
  271. * Allows using the bump map in parallax occlusion mode.
  272. */
  273. protected _useParallaxOcclusion = false;
  274. /**
  275. * Controls the scale bias of the parallax mode.
  276. */
  277. protected _parallaxScaleBias = 0.05;
  278. /**
  279. * If sets to true, disables all the lights affecting the material.
  280. */
  281. protected _disableLighting = false;
  282. /**
  283. * Number of Simultaneous lights allowed on the material.
  284. */
  285. protected _maxSimultaneousLights = 4;
  286. /**
  287. * If sets to true, x component of normal map value will invert (x = 1.0 - x).
  288. */
  289. protected _invertNormalMapX = false;
  290. /**
  291. * If sets to true, y component of normal map value will invert (y = 1.0 - y).
  292. */
  293. protected _invertNormalMapY = false;
  294. /**
  295. * If sets to true and backfaceCulling is false, normals will be flipped on the backside.
  296. */
  297. protected _twoSidedLighting = false;
  298. /**
  299. * Defines the alpha limits in alpha test mode.
  300. */
  301. protected _alphaCutOff = 0.4;
  302. /**
  303. * Enforces alpha test in opaque or blend mode in order to improve the performances of some situations.
  304. */
  305. protected _forceAlphaTest = false;
  306. /**
  307. * Specifies that the alpha is premultiplied before output (this enables alpha premultiplied blending).
  308. * in your scene composition.
  309. */
  310. protected _preMultiplyAlpha = false;
  311. /**
  312. * A fresnel is applied to the alpha of the model to ensure grazing angles edges are not alpha tested.
  313. * And/Or occlude the blended part.
  314. */
  315. protected _useAlphaFresnel = false;
  316. /**
  317. * Specifies the environment BRDF texture used to comput the scale and offset roughness values
  318. * from cos thetav and roughness:
  319. * http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
  320. */
  321. protected _environmentBRDFTexture: BaseTexture = null;
  322. /**
  323. * Force the shader to compute irradiance in the fragment shader in order to take bump in account.
  324. */
  325. protected _forceIrradianceInFragment = false;
  326. /**
  327. * Default configuration related to image processing available in the PBR Material.
  328. */
  329. @serializeAsImageProcessingConfiguration()
  330. protected _imageProcessingConfiguration: ImageProcessingConfiguration;
  331. /**
  332. * Keep track of the image processing observer to allow dispose and replace.
  333. */
  334. private _imageProcessingObserver: Observer<ImageProcessingConfiguration>;
  335. /**
  336. * Attaches a new image processing configuration to the PBR Material.
  337. * @param configuration
  338. */
  339. protected _attachImageProcessingConfiguration(configuration: ImageProcessingConfiguration): void {
  340. if (configuration === this._imageProcessingConfiguration) {
  341. return;
  342. }
  343. // Detaches observer.
  344. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  345. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  346. }
  347. // Pick the scene configuration if needed.
  348. if (!configuration) {
  349. this._imageProcessingConfiguration = this.getScene().imageProcessingConfiguration;
  350. }
  351. else {
  352. this._imageProcessingConfiguration = configuration;
  353. }
  354. // Attaches observer.
  355. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(conf => {
  356. this._markAllSubMeshesAsImageProcessingDirty();
  357. });
  358. }
  359. private _renderTargets = new SmartArray<RenderTargetTexture>(16);
  360. private _worldViewProjectionMatrix = Matrix.Zero();
  361. private _globalAmbientColor = new Color3(0, 0, 0);
  362. private _tempColor = new Color3();
  363. private _renderId: number;
  364. private _useLogarithmicDepth: boolean;
  365. /**
  366. * Instantiates a new PBRMaterial instance.
  367. *
  368. * @param name The material name
  369. * @param scene The scene the material will be use in.
  370. */
  371. constructor(name: string, scene: Scene) {
  372. super(name, scene);
  373. // Setup the default processing configuration to the scene.
  374. this._attachImageProcessingConfiguration(null);
  375. this.getRenderTargetTextures = (): SmartArray<RenderTargetTexture> => {
  376. this._renderTargets.reset();
  377. if (StandardMaterial.ReflectionTextureEnabled && this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  378. this._renderTargets.push(this._reflectionTexture);
  379. }
  380. if (StandardMaterial.RefractionTextureEnabled && this._refractionTexture && this._refractionTexture.isRenderTarget) {
  381. this._renderTargets.push(this._refractionTexture);
  382. }
  383. return this._renderTargets;
  384. }
  385. this._environmentBRDFTexture = TextureTools.GetEnvironmentBRDFTexture(scene);
  386. }
  387. public abstract getClassName(): string;
  388. @serialize()
  389. public get useLogarithmicDepth(): boolean {
  390. return this._useLogarithmicDepth;
  391. }
  392. public set useLogarithmicDepth(value: boolean) {
  393. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  394. }
  395. public needAlphaBlending(): boolean {
  396. if (this._linkRefractionWithTransparency) {
  397. return false;
  398. }
  399. return (this.alpha < 1.0) || (this._opacityTexture != null) || this._shouldUseAlphaFromAlbedoTexture();
  400. }
  401. public needAlphaTesting(): boolean {
  402. if (this._forceAlphaTest) {
  403. return true;
  404. }
  405. if (this._linkRefractionWithTransparency) {
  406. return false;
  407. }
  408. return this._albedoTexture != null && this._albedoTexture.hasAlpha;
  409. }
  410. protected _shouldUseAlphaFromAlbedoTexture(): boolean {
  411. return this._albedoTexture != null && this._albedoTexture.hasAlpha && this._useAlphaFromAlbedoTexture;
  412. }
  413. public getAlphaTestTexture(): BaseTexture {
  414. return this._albedoTexture;
  415. }
  416. private static _scaledReflectivity = new Color3();
  417. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  418. if (this.isFrozen) {
  419. if (this._wasPreviouslyReady) {
  420. return true;
  421. }
  422. }
  423. if (!subMesh._materialDefines) {
  424. subMesh._materialDefines = new PBRMaterialDefines();
  425. }
  426. var scene = this.getScene();
  427. var defines = <PBRMaterialDefines>subMesh._materialDefines;
  428. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  429. if (defines._renderId === scene.getRenderId()) {
  430. return true;
  431. }
  432. }
  433. var engine = scene.getEngine();
  434. // Lights
  435. MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, true, this._maxSimultaneousLights, this._disableLighting);
  436. defines._needNormals = true;
  437. // Textures
  438. if (defines._areTexturesDirty) {
  439. defines._needUVs = false;
  440. if (scene.texturesEnabled) {
  441. if (scene.getEngine().getCaps().textureLOD) {
  442. defines.LODBASEDMICROSFURACE = true;
  443. }
  444. if (this._albedoTexture && StandardMaterial.DiffuseTextureEnabled) {
  445. if (!this._albedoTexture.isReadyOrNotBlocking()) {
  446. return false;
  447. }
  448. MaterialHelper.PrepareDefinesForMergedUV(this._albedoTexture, defines, "ALBEDO");
  449. } else {
  450. defines.ALBEDO = false;
  451. }
  452. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  453. if (!this._ambientTexture.isReadyOrNotBlocking()) {
  454. return false;
  455. }
  456. MaterialHelper.PrepareDefinesForMergedUV(this._ambientTexture, defines, "AMBIENT");
  457. defines.AMBIENTINGRAYSCALE = this._useAmbientInGrayScale;
  458. } else {
  459. defines.AMBIENT = false;
  460. }
  461. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  462. if (!this._opacityTexture.isReadyOrNotBlocking()) {
  463. return false;
  464. }
  465. MaterialHelper.PrepareDefinesForMergedUV(this._opacityTexture, defines, "OPACITY");
  466. defines.OPACITYRGB = this._opacityTexture.getAlphaFromRGB;
  467. } else {
  468. defines.OPACITY = false;
  469. }
  470. var reflectionTexture = this._getReflectionTexture();
  471. if (reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  472. if (!reflectionTexture.isReadyOrNotBlocking()) {
  473. return false;
  474. }
  475. defines.REFLECTION = true;
  476. defines.GAMMAREFLECTION = reflectionTexture.gammaSpace;
  477. defines.REFLECTIONMAP_OPPOSITEZ = this.getScene().useRightHandedSystem ? !reflectionTexture.invertZ : reflectionTexture.invertZ;
  478. defines.LODINREFLECTIONALPHA = reflectionTexture.lodLevelInAlpha;
  479. if (reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE) {
  480. defines.INVERTCUBICMAP = true;
  481. }
  482. defines.REFLECTIONMAP_3D = reflectionTexture.isCube;
  483. switch (reflectionTexture.coordinatesMode) {
  484. case Texture.CUBIC_MODE:
  485. case Texture.INVCUBIC_MODE:
  486. defines.REFLECTIONMAP_CUBIC = true;
  487. break;
  488. case Texture.EXPLICIT_MODE:
  489. defines.REFLECTIONMAP_EXPLICIT = true;
  490. break;
  491. case Texture.PLANAR_MODE:
  492. defines.REFLECTIONMAP_PLANAR = true;
  493. break;
  494. case Texture.PROJECTION_MODE:
  495. defines.REFLECTIONMAP_PROJECTION = true;
  496. break;
  497. case Texture.SKYBOX_MODE:
  498. defines.REFLECTIONMAP_SKYBOX = true;
  499. break;
  500. case Texture.SPHERICAL_MODE:
  501. defines.REFLECTIONMAP_SPHERICAL = true;
  502. break;
  503. case Texture.EQUIRECTANGULAR_MODE:
  504. defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  505. break;
  506. case Texture.FIXED_EQUIRECTANGULAR_MODE:
  507. defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = true;
  508. break;
  509. case Texture.FIXED_EQUIRECTANGULAR_MIRRORED_MODE:
  510. defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = true;
  511. break;
  512. }
  513. if (reflectionTexture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) {
  514. if (reflectionTexture.sphericalPolynomial) {
  515. defines.USESPHERICALFROMREFLECTIONMAP = true;
  516. if (this._forceIrradianceInFragment || scene.getEngine().getCaps().maxVaryingVectors <= 8) {
  517. defines.USESPHERICALINFRAGMENT = true;
  518. }
  519. }
  520. }
  521. } else {
  522. defines.REFLECTION = false;
  523. defines.REFLECTIONMAP_3D = false;
  524. defines.REFLECTIONMAP_SPHERICAL = false;
  525. defines.REFLECTIONMAP_PLANAR = false;
  526. defines.REFLECTIONMAP_CUBIC = false;
  527. defines.REFLECTIONMAP_PROJECTION = false;
  528. defines.REFLECTIONMAP_SKYBOX = false;
  529. defines.REFLECTIONMAP_EXPLICIT = false;
  530. defines.REFLECTIONMAP_EQUIRECTANGULAR = false;
  531. defines.REFLECTIONMAP_EQUIRECTANGULAR_FIXED = false;
  532. defines.REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED = false;
  533. defines.INVERTCUBICMAP = false;
  534. defines.USESPHERICALFROMREFLECTIONMAP = false;
  535. defines.USESPHERICALINFRAGMENT = false;
  536. defines.REFLECTIONMAP_OPPOSITEZ = false;
  537. defines.LODINREFLECTIONALPHA = false;
  538. defines.GAMMAREFLECTION = false;
  539. }
  540. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  541. if (!this._lightmapTexture.isReadyOrNotBlocking()) {
  542. return false;
  543. }
  544. MaterialHelper.PrepareDefinesForMergedUV(this._lightmapTexture, defines, "LIGHTMAP");
  545. defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
  546. } else {
  547. defines.LIGHTMAP = false;
  548. }
  549. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  550. if (!this._emissiveTexture.isReadyOrNotBlocking()) {
  551. return false;
  552. }
  553. MaterialHelper.PrepareDefinesForMergedUV(this._emissiveTexture, defines, "EMISSIVE");
  554. } else {
  555. defines.EMISSIVE = false;
  556. }
  557. if (StandardMaterial.SpecularTextureEnabled) {
  558. if (this._metallicTexture) {
  559. if (!this._metallicTexture.isReadyOrNotBlocking()) {
  560. return false;
  561. }
  562. MaterialHelper.PrepareDefinesForMergedUV(this._metallicTexture, defines, "REFLECTIVITY");
  563. defines.METALLICWORKFLOW = true;
  564. defines.ROUGHNESSSTOREINMETALMAPALPHA = this._useRoughnessFromMetallicTextureAlpha;
  565. defines.ROUGHNESSSTOREINMETALMAPGREEN = !this._useRoughnessFromMetallicTextureAlpha && this._useRoughnessFromMetallicTextureGreen;
  566. defines.METALLNESSSTOREINMETALMAPBLUE = this._useMetallnessFromMetallicTextureBlue;
  567. defines.AOSTOREINMETALMAPRED = this._useAmbientOcclusionFromMetallicTextureRed;
  568. }
  569. else if (this._reflectivityTexture) {
  570. if (!this._reflectivityTexture.isReadyOrNotBlocking()) {
  571. return false;
  572. }
  573. MaterialHelper.PrepareDefinesForMergedUV(this._reflectivityTexture, defines, "REFLECTIVITY");
  574. defines.MICROSURFACEFROMREFLECTIVITYMAP = this._useMicroSurfaceFromReflectivityMapAlpha;
  575. defines.MICROSURFACEAUTOMATIC = this._useAutoMicroSurfaceFromReflectivityMap;
  576. } else {
  577. defines.REFLECTIVITY = false;
  578. }
  579. if (this._microSurfaceTexture) {
  580. if (!this._microSurfaceTexture.isReadyOrNotBlocking()) {
  581. return false;
  582. }
  583. MaterialHelper.PrepareDefinesForMergedUV(this._microSurfaceTexture, defines, "MICROSURFACEMAP");
  584. } else {
  585. defines.MICROSURFACEMAP = false;
  586. }
  587. } else {
  588. defines.REFLECTIVITY = false;
  589. defines.MICROSURFACEMAP = false;
  590. }
  591. if (scene.getEngine().getCaps().standardDerivatives && this._bumpTexture && StandardMaterial.BumpTextureEnabled && !this._disableBumpMap) {
  592. // Bump texure can not be none blocking.
  593. if (!this._bumpTexture.isReady()) {
  594. return false;
  595. }
  596. MaterialHelper.PrepareDefinesForMergedUV(this._bumpTexture, defines, "BUMP");
  597. if (this._useParallax && this._albedoTexture && StandardMaterial.DiffuseTextureEnabled) {
  598. defines.PARALLAX = true;
  599. if (this._useParallaxOcclusion) {
  600. defines.PARALLAXOCCLUSION = true;
  601. }
  602. }
  603. if (this._invertNormalMapX) {
  604. defines.INVERTNORMALMAPX = true;
  605. }
  606. if (this._invertNormalMapY) {
  607. defines.INVERTNORMALMAPY = true;
  608. }
  609. if (scene._mirroredCameraPosition) {
  610. defines.INVERTNORMALMAPX = !defines.INVERTNORMALMAPX;
  611. defines.INVERTNORMALMAPY = !defines.INVERTNORMALMAPY;
  612. }
  613. defines.USERIGHTHANDEDSYSTEM = scene.useRightHandedSystem;
  614. } else {
  615. defines.BUMP = false;
  616. }
  617. var refractionTexture = this._getRefractionTexture();
  618. if (refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  619. if (!refractionTexture.isReadyOrNotBlocking()) {
  620. return false;
  621. }
  622. defines.REFRACTION = true;
  623. defines.REFRACTIONMAP_3D = refractionTexture.isCube;
  624. defines.GAMMAREFRACTION = refractionTexture.gammaSpace;
  625. defines.REFRACTIONMAP_OPPOSITEZ = reflectionTexture.invertZ;
  626. defines.LODINREFRACTIONALPHA = reflectionTexture.lodLevelInAlpha;
  627. if (this._linkRefractionWithTransparency) {
  628. defines.LINKREFRACTIONTOTRANSPARENCY = true;
  629. }
  630. } else {
  631. defines.REFRACTION = false;
  632. }
  633. if (this._environmentBRDFTexture && StandardMaterial.ReflectionTextureEnabled) {
  634. // This is blocking.
  635. if (!this._environmentBRDFTexture.isReady()) {
  636. return false;
  637. }
  638. defines.ENVIRONMENTBRDF = true;
  639. }
  640. if (this._shouldUseAlphaFromAlbedoTexture()) {
  641. defines.ALPHAFROMALBEDO = true;
  642. }
  643. }
  644. if (this._useSpecularOverAlpha) {
  645. defines.SPECULAROVERALPHA = true;
  646. }
  647. if (this._usePhysicalLightFalloff) {
  648. defines.USEPHYSICALLIGHTFALLOFF = true;
  649. }
  650. if (this._useRadianceOverAlpha) {
  651. defines.RADIANCEOVERALPHA = true;
  652. }
  653. if ((this._metallic !== undefined && this._metallic !== null) || (this._roughness !== undefined && this._roughness !== null)) {
  654. defines.METALLICWORKFLOW = true;
  655. }
  656. if (!this.backFaceCulling && this._twoSidedLighting) {
  657. defines.TWOSIDEDLIGHTING = true;
  658. }
  659. defines.ALPHATESTVALUE = this._alphaCutOff;
  660. defines.PREMULTIPLYALPHA = this._preMultiplyAlpha;
  661. defines.ALPHABLEND = this.needAlphaBlending();
  662. defines.ALPHAFRESNEL = this._useAlphaFresnel;
  663. }
  664. if (defines._areImageProcessingDirty) {
  665. if (!this._imageProcessingConfiguration.isReady()) {
  666. return false;
  667. }
  668. this._imageProcessingConfiguration.prepareDefines(defines);
  669. }
  670. // Misc.
  671. MaterialHelper.PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, defines);
  672. // Values that need to be evaluated on every frame
  673. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances, this._forceAlphaTest);
  674. // Attribs
  675. if (MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true, true)) {
  676. if (mesh) {
  677. if (!scene.getEngine().getCaps().standardDerivatives && !mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  678. mesh.createNormals(true);
  679. Tools.Warn("PBRMaterial: Normals have been created for the mesh: " + mesh.name);
  680. }
  681. }
  682. }
  683. // Get correct effect
  684. if (defines.isDirty) {
  685. defines.markAsProcessed();
  686. scene.resetCachedMaterial();
  687. // Fallbacks
  688. var fallbacks = new EffectFallbacks();
  689. if (defines.ENVIRONMENTBRDF) {
  690. fallbacks.addFallback(0, "ENVIRONMENTBRDF");
  691. }
  692. if (defines.REFLECTION) {
  693. fallbacks.addFallback(0, "REFLECTION");
  694. }
  695. if (defines.REFRACTION) {
  696. fallbacks.addFallback(0, "REFRACTION");
  697. }
  698. if (defines.REFLECTIVITY) {
  699. fallbacks.addFallback(0, "REFLECTIVITY");
  700. }
  701. if (defines.BUMP) {
  702. fallbacks.addFallback(0, "BUMP");
  703. }
  704. if (defines.PARALLAX) {
  705. fallbacks.addFallback(1, "PARALLAX");
  706. }
  707. if (defines.PARALLAXOCCLUSION) {
  708. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  709. }
  710. if (defines.SPECULAROVERALPHA) {
  711. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  712. }
  713. if (defines.FOG) {
  714. fallbacks.addFallback(1, "FOG");
  715. }
  716. if (defines.POINTSIZE) {
  717. fallbacks.addFallback(0, "POINTSIZE");
  718. }
  719. if (defines.LOGARITHMICDEPTH) {
  720. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  721. }
  722. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights);
  723. if (defines.SPECULARTERM) {
  724. fallbacks.addFallback(0, "SPECULARTERM");
  725. }
  726. if (defines.NUM_BONE_INFLUENCERS > 0) {
  727. fallbacks.addCPUSkinningFallback(0, mesh);
  728. }
  729. //Attributes
  730. var attribs = [VertexBuffer.PositionKind];
  731. if (defines.NORMAL) {
  732. attribs.push(VertexBuffer.NormalKind);
  733. }
  734. if (defines.TANGENT) {
  735. attribs.push(VertexBuffer.TangentKind);
  736. }
  737. if (defines.UV1) {
  738. attribs.push(VertexBuffer.UVKind);
  739. }
  740. if (defines.UV2) {
  741. attribs.push(VertexBuffer.UV2Kind);
  742. }
  743. if (defines.VERTEXCOLOR) {
  744. attribs.push(VertexBuffer.ColorKind);
  745. }
  746. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  747. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  748. MaterialHelper.PrepareAttributesForMorphTargets(attribs, mesh, defines);
  749. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vAlbedoColor", "vReflectivityColor", "vEmissiveColor", "vReflectionColor",
  750. "vFogInfos", "vFogColor", "pointSize",
  751. "vAlbedoInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vReflectivityInfos", "vMicroSurfaceSamplerInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  752. "mBones",
  753. "vClipPlane", "albedoMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "reflectivityMatrix", "microSurfaceSamplerMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  754. "vLightingIntensity",
  755. "logarithmicDepthConstant",
  756. "vSphericalX", "vSphericalY", "vSphericalZ",
  757. "vSphericalXX", "vSphericalYY", "vSphericalZZ",
  758. "vSphericalXY", "vSphericalYZ", "vSphericalZX",
  759. "vReflectionMicrosurfaceInfos", "vRefractionMicrosurfaceInfos"
  760. ];
  761. var samplers = ["albedoSampler", "reflectivitySampler", "ambientSampler", "emissiveSampler",
  762. "bumpSampler", "lightmapSampler", "opacitySampler",
  763. "refractionSampler", "refractionSamplerLow", "refractionSamplerHigh",
  764. "reflectionSampler", "reflectionSamplerLow", "reflectionSamplerHigh",
  765. "microSurfaceSampler", "environmentBrdfSampler"];
  766. var uniformBuffers = ["Material", "Scene"];
  767. ImageProcessingConfiguration.PrepareUniforms(uniforms, defines);
  768. ImageProcessingConfiguration.PrepareSamplers(samplers, defines);
  769. MaterialHelper.PrepareUniformsAndSamplersList(<EffectCreationOptions>{
  770. uniformsNames: uniforms,
  771. uniformBuffersNames: uniformBuffers,
  772. samplers: samplers,
  773. defines: defines,
  774. maxSimultaneousLights: this._maxSimultaneousLights
  775. });
  776. var onCompiled = function(effect) {
  777. if (this.onCompiled) {
  778. this.onCompiled(effect);
  779. }
  780. this.bindSceneUniformBuffer(effect, scene.getSceneUniformBuffer());
  781. }.bind(this);
  782. var join = defines.toString();
  783. subMesh.setEffect(scene.getEngine().createEffect("pbr", <EffectCreationOptions>{
  784. attributes: attribs,
  785. uniformsNames: uniforms,
  786. uniformBuffersNames: uniformBuffers,
  787. samplers: samplers,
  788. defines: join,
  789. fallbacks: fallbacks,
  790. onCompiled: onCompiled,
  791. onError: this.onError,
  792. indexParameters: { maxSimultaneousLights: this._maxSimultaneousLights, maxSimultaneousMorphTargets: defines.NUM_MORPH_INFLUENCERS }
  793. }, engine), defines);
  794. this.buildUniformLayout();
  795. }
  796. if (!subMesh.effect.isReady()) {
  797. return false;
  798. }
  799. defines._renderId = scene.getRenderId();
  800. this._wasPreviouslyReady = true;
  801. return true;
  802. }
  803. public buildUniformLayout(): void {
  804. // Order is important !
  805. this._uniformBuffer.addUniform("vAlbedoInfos", 2);
  806. this._uniformBuffer.addUniform("vAmbientInfos", 3);
  807. this._uniformBuffer.addUniform("vOpacityInfos", 2);
  808. this._uniformBuffer.addUniform("vEmissiveInfos", 2);
  809. this._uniformBuffer.addUniform("vLightmapInfos", 2);
  810. this._uniformBuffer.addUniform("vReflectivityInfos", 3);
  811. this._uniformBuffer.addUniform("vMicroSurfaceSamplerInfos", 2);
  812. this._uniformBuffer.addUniform("vRefractionInfos", 4);
  813. this._uniformBuffer.addUniform("vReflectionInfos", 2);
  814. this._uniformBuffer.addUniform("vBumpInfos", 3);
  815. this._uniformBuffer.addUniform("albedoMatrix", 16);
  816. this._uniformBuffer.addUniform("ambientMatrix", 16);
  817. this._uniformBuffer.addUniform("opacityMatrix", 16);
  818. this._uniformBuffer.addUniform("emissiveMatrix", 16);
  819. this._uniformBuffer.addUniform("lightmapMatrix", 16);
  820. this._uniformBuffer.addUniform("reflectivityMatrix", 16);
  821. this._uniformBuffer.addUniform("microSurfaceSamplerMatrix", 16);
  822. this._uniformBuffer.addUniform("bumpMatrix", 16);
  823. this._uniformBuffer.addUniform("refractionMatrix", 16);
  824. this._uniformBuffer.addUniform("reflectionMatrix", 16);
  825. this._uniformBuffer.addUniform("vReflectionColor", 3);
  826. this._uniformBuffer.addUniform("vAlbedoColor", 4);
  827. this._uniformBuffer.addUniform("vLightingIntensity", 4);
  828. this._uniformBuffer.addUniform("vRefractionMicrosurfaceInfos", 3);
  829. this._uniformBuffer.addUniform("vReflectionMicrosurfaceInfos", 3);
  830. this._uniformBuffer.addUniform("vReflectivityColor", 4);
  831. this._uniformBuffer.addUniform("vEmissiveColor", 3);
  832. this._uniformBuffer.addUniform("pointSize", 1);
  833. this._uniformBuffer.create();
  834. }
  835. public unbind(): void {
  836. if (this._reflectionTexture && this._reflectionTexture.isRenderTarget) {
  837. this._uniformBuffer.setTexture("reflectionSampler", null);
  838. }
  839. if (this._refractionTexture && this._refractionTexture.isRenderTarget) {
  840. this._uniformBuffer.setTexture("refractionSampler", null);
  841. }
  842. super.unbind();
  843. }
  844. public bindOnlyWorldMatrix(world: Matrix): void {
  845. this._activeEffect.setMatrix("world", world);
  846. }
  847. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  848. var scene = this.getScene();
  849. var defines = <PBRMaterialDefines>subMesh._materialDefines;
  850. if (!defines) {
  851. return;
  852. }
  853. var effect = subMesh.effect;
  854. this._activeEffect = effect;
  855. // Matrices
  856. this.bindOnlyWorldMatrix(world);
  857. // Bones
  858. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  859. if (this._mustRebind(scene, effect, mesh.visibility)) {
  860. this._uniformBuffer.bindToEffect(effect, "Material");
  861. this.bindViewProjection(effect);
  862. if (!this._uniformBuffer.useUbo || !this.isFrozen || !this._uniformBuffer.isSync) {
  863. // Texture uniforms
  864. if (scene.texturesEnabled) {
  865. if (this._albedoTexture && StandardMaterial.DiffuseTextureEnabled) {
  866. this._uniformBuffer.updateFloat2("vAlbedoInfos", this._albedoTexture.coordinatesIndex, this._albedoTexture.level);
  867. MaterialHelper.BindTextureMatrix(this._albedoTexture, this._uniformBuffer, "albedo");
  868. }
  869. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  870. this._uniformBuffer.updateFloat3("vAmbientInfos", this._ambientTexture.coordinatesIndex, this._ambientTexture.level, this._ambientTextureStrength);
  871. MaterialHelper.BindTextureMatrix(this._ambientTexture, this._uniformBuffer, "ambient");
  872. }
  873. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  874. this._uniformBuffer.updateFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
  875. MaterialHelper.BindTextureMatrix(this._opacityTexture, this._uniformBuffer, "opacity");
  876. }
  877. var reflectionTexture = this._getReflectionTexture();
  878. if (reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  879. this._uniformBuffer.updateMatrix("reflectionMatrix", reflectionTexture.getReflectionTextureMatrix());
  880. this._uniformBuffer.updateFloat2("vReflectionInfos", reflectionTexture.level, 0);
  881. if (defines.USESPHERICALFROMREFLECTIONMAP) {
  882. var polynomials = reflectionTexture.sphericalPolynomial;
  883. this._activeEffect.setFloat3("vSphericalX", polynomials.x.x, polynomials.x.y, polynomials.x.z);
  884. this._activeEffect.setFloat3("vSphericalY", polynomials.y.x, polynomials.y.y, polynomials.y.z);
  885. this._activeEffect.setFloat3("vSphericalZ", polynomials.z.x, polynomials.z.y, polynomials.z.z);
  886. this._activeEffect.setFloat3("vSphericalXX_ZZ", polynomials.xx.x - polynomials.zz.x,
  887. polynomials.xx.y - polynomials.zz.y,
  888. polynomials.xx.z - polynomials.zz.z);
  889. this._activeEffect.setFloat3("vSphericalYY_ZZ", polynomials.yy.x - polynomials.zz.x,
  890. polynomials.yy.y - polynomials.zz.y,
  891. polynomials.yy.z - polynomials.zz.z);
  892. this._activeEffect.setFloat3("vSphericalZZ", polynomials.zz.x, polynomials.zz.y, polynomials.zz.z);
  893. this._activeEffect.setFloat3("vSphericalXY", polynomials.xy.x, polynomials.xy.y, polynomials.xy.z);
  894. this._activeEffect.setFloat3("vSphericalYZ", polynomials.yz.x, polynomials.yz.y, polynomials.yz.z);
  895. this._activeEffect.setFloat3("vSphericalZX", polynomials.zx.x, polynomials.zx.y, polynomials.zx.z);
  896. }
  897. this._uniformBuffer.updateFloat3("vReflectionMicrosurfaceInfos",
  898. reflectionTexture.getSize().width,
  899. reflectionTexture.lodGenerationScale,
  900. reflectionTexture.lodGenerationOffset);
  901. }
  902. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  903. this._uniformBuffer.updateFloat2("vEmissiveInfos", this._emissiveTexture.coordinatesIndex, this._emissiveTexture.level);
  904. MaterialHelper.BindTextureMatrix(this._emissiveTexture, this._uniformBuffer, "emissive");
  905. }
  906. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  907. this._uniformBuffer.updateFloat2("vLightmapInfos", this._lightmapTexture.coordinatesIndex, this._lightmapTexture.level);
  908. MaterialHelper.BindTextureMatrix(this._lightmapTexture, this._uniformBuffer, "lightmap");
  909. }
  910. if (StandardMaterial.SpecularTextureEnabled) {
  911. if (this._metallicTexture) {
  912. this._uniformBuffer.updateFloat3("vReflectivityInfos", this._metallicTexture.coordinatesIndex, this._metallicTexture.level, this._ambientTextureStrength);
  913. MaterialHelper.BindTextureMatrix(this._metallicTexture, this._uniformBuffer, "reflectivity");
  914. }
  915. else if (this._reflectivityTexture) {
  916. this._uniformBuffer.updateFloat3("vReflectivityInfos", this._reflectivityTexture.coordinatesIndex, this._reflectivityTexture.level, 1.0);
  917. MaterialHelper.BindTextureMatrix(this._reflectivityTexture, this._uniformBuffer, "reflectivity");
  918. }
  919. if (this._microSurfaceTexture) {
  920. this._uniformBuffer.updateFloat2("vMicroSurfaceSamplerInfos", this._microSurfaceTexture.coordinatesIndex, this._microSurfaceTexture.level);
  921. MaterialHelper.BindTextureMatrix(this._microSurfaceTexture, this._uniformBuffer, "microSurfaceSampler");
  922. }
  923. }
  924. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled && !this._disableBumpMap) {
  925. this._uniformBuffer.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, this._bumpTexture.level, this._parallaxScaleBias);
  926. MaterialHelper.BindTextureMatrix(this._bumpTexture, this._uniformBuffer, "bump");
  927. }
  928. var refractionTexture = this._getRefractionTexture();
  929. if (refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  930. this._uniformBuffer.updateMatrix("refractionMatrix", refractionTexture.getReflectionTextureMatrix());
  931. var depth = 1.0;
  932. if (!refractionTexture.isCube) {
  933. if ((<any>refractionTexture).depth) {
  934. depth = (<any>refractionTexture).depth;
  935. }
  936. }
  937. this._uniformBuffer.updateFloat4("vRefractionInfos", refractionTexture.level, this._indexOfRefraction, depth, this._invertRefractionY ? -1 : 1);
  938. this._uniformBuffer.updateFloat3("vRefractionMicrosurfaceInfos",
  939. refractionTexture.getSize().width,
  940. refractionTexture.lodGenerationScale,
  941. refractionTexture.lodGenerationOffset);
  942. }
  943. }
  944. // Point size
  945. if (this.pointsCloud) {
  946. this._uniformBuffer.updateFloat("pointSize", this.pointSize);
  947. }
  948. // Colors
  949. if (defines.METALLICWORKFLOW) {
  950. PBRMaterial._scaledReflectivity.r = (this._metallic === undefined || this._metallic === null) ? 1 : this._metallic;
  951. PBRMaterial._scaledReflectivity.g = (this._roughness === undefined || this._roughness === null) ? 1 : this._roughness;
  952. this._uniformBuffer.updateColor4("vReflectivityColor", PBRMaterial._scaledReflectivity, 0);
  953. }
  954. else {
  955. this._uniformBuffer.updateColor4("vReflectivityColor", this._reflectivityColor, this._microSurface);
  956. }
  957. this._uniformBuffer.updateColor3("vEmissiveColor", this._emissiveColor);
  958. this._uniformBuffer.updateColor3("vReflectionColor", this._reflectionColor);
  959. this._uniformBuffer.updateColor4("vAlbedoColor", this._albedoColor, this.alpha * mesh.visibility);
  960. // Misc
  961. this._lightingInfos.x = this._directIntensity;
  962. this._lightingInfos.y = this._emissiveIntensity;
  963. this._lightingInfos.z = this._environmentIntensity;
  964. this._lightingInfos.w = this._specularIntensity;
  965. this._uniformBuffer.updateVector4("vLightingIntensity", this._lightingInfos);
  966. }
  967. // Textures
  968. if (scene.texturesEnabled) {
  969. if (this._albedoTexture && StandardMaterial.DiffuseTextureEnabled) {
  970. this._uniformBuffer.setTexture("albedoSampler", this._albedoTexture);
  971. }
  972. if (this._ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  973. this._uniformBuffer.setTexture("ambientSampler", this._ambientTexture);
  974. }
  975. if (this._opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  976. this._uniformBuffer.setTexture("opacitySampler", this._opacityTexture);
  977. }
  978. if (reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  979. if (defines.LODBASEDMICROSFURACE) {
  980. this._uniformBuffer.setTexture("reflectionSampler", reflectionTexture);
  981. }
  982. else {
  983. this._uniformBuffer.setTexture("reflectionSampler", reflectionTexture._lodTextureMid || reflectionTexture);
  984. this._uniformBuffer.setTexture("reflectionSamplerLow", reflectionTexture._lodTextureLow || reflectionTexture);
  985. this._uniformBuffer.setTexture("reflectionSamplerHigh", reflectionTexture._lodTextureHigh || reflectionTexture);
  986. }
  987. }
  988. if (defines.ENVIRONMENTBRDF) {
  989. this._uniformBuffer.setTexture("environmentBrdfSampler", this._environmentBRDFTexture);
  990. }
  991. if (refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  992. if (defines.LODBASEDMICROSFURACE) {
  993. this._uniformBuffer.setTexture("refractionSampler", refractionTexture);
  994. }
  995. else {
  996. this._uniformBuffer.setTexture("refractionSampler", refractionTexture._lodTextureMid || refractionTexture);
  997. this._uniformBuffer.setTexture("refractionSamplerLow", refractionTexture._lodTextureLow || refractionTexture);
  998. this._uniformBuffer.setTexture("refractionSamplerHigh", refractionTexture._lodTextureHigh || refractionTexture);
  999. }
  1000. }
  1001. if (this._emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  1002. this._uniformBuffer.setTexture("emissiveSampler", this._emissiveTexture);
  1003. }
  1004. if (this._lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  1005. this._uniformBuffer.setTexture("lightmapSampler", this._lightmapTexture);
  1006. }
  1007. if (StandardMaterial.SpecularTextureEnabled) {
  1008. if (this._metallicTexture) {
  1009. this._uniformBuffer.setTexture("reflectivitySampler", this._metallicTexture);
  1010. }
  1011. else if (this._reflectivityTexture) {
  1012. this._uniformBuffer.setTexture("reflectivitySampler", this._reflectivityTexture);
  1013. }
  1014. if (this._microSurfaceTexture) {
  1015. this._uniformBuffer.setTexture("microSurfaceSampler", this._microSurfaceTexture);
  1016. }
  1017. }
  1018. if (this._bumpTexture && scene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled && !this._disableBumpMap) {
  1019. this._uniformBuffer.setTexture("bumpSampler", this._bumpTexture);
  1020. }
  1021. }
  1022. // Clip plane
  1023. MaterialHelper.BindClipPlane(this._activeEffect, scene);
  1024. // Colors
  1025. scene.ambientColor.multiplyToRef(this._ambientColor, this._globalAmbientColor);
  1026. effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  1027. effect.setColor3("vAmbientColor", this._globalAmbientColor);
  1028. }
  1029. if (this._mustRebind(scene, effect) || !this.isFrozen) {
  1030. // Lights
  1031. if (scene.lightsEnabled && !this._disableLighting) {
  1032. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this._maxSimultaneousLights, this._usePhysicalLightFalloff);
  1033. }
  1034. // View
  1035. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE || reflectionTexture) {
  1036. this.bindView(effect);
  1037. }
  1038. // Fog
  1039. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  1040. // Morph targets
  1041. if (defines.NUM_MORPH_INFLUENCERS) {
  1042. MaterialHelper.BindMorphTargetParameters(mesh, this._activeEffect);
  1043. }
  1044. // image processing
  1045. this._imageProcessingConfiguration.bind(this._activeEffect);
  1046. // Log. depth
  1047. MaterialHelper.BindLogDepth(defines, this._activeEffect, scene);
  1048. }
  1049. this._uniformBuffer.update();
  1050. this._afterBind(mesh);
  1051. scene = null;
  1052. }
  1053. public getAnimatables(): IAnimatable[] {
  1054. var results = [];
  1055. if (this._albedoTexture && this._albedoTexture.animations && this._albedoTexture.animations.length > 0) {
  1056. results.push(this._albedoTexture);
  1057. }
  1058. if (this._ambientTexture && this._ambientTexture.animations && this._ambientTexture.animations.length > 0) {
  1059. results.push(this._ambientTexture);
  1060. }
  1061. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  1062. results.push(this._opacityTexture);
  1063. }
  1064. if (this._reflectionTexture && this._reflectionTexture.animations && this._reflectionTexture.animations.length > 0) {
  1065. results.push(this._reflectionTexture);
  1066. }
  1067. if (this._emissiveTexture && this._emissiveTexture.animations && this._emissiveTexture.animations.length > 0) {
  1068. results.push(this._emissiveTexture);
  1069. }
  1070. if (this._metallicTexture && this._metallicTexture.animations && this._metallicTexture.animations.length > 0) {
  1071. results.push(this._metallicTexture);
  1072. }
  1073. else if (this._reflectivityTexture && this._reflectivityTexture.animations && this._reflectivityTexture.animations.length > 0) {
  1074. results.push(this._reflectivityTexture);
  1075. }
  1076. if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
  1077. results.push(this._bumpTexture);
  1078. }
  1079. if (this._lightmapTexture && this._lightmapTexture.animations && this._lightmapTexture.animations.length > 0) {
  1080. results.push(this._lightmapTexture);
  1081. }
  1082. if (this._refractionTexture && this._refractionTexture.animations && this._refractionTexture.animations.length > 0) {
  1083. results.push(this._refractionTexture);
  1084. }
  1085. return results;
  1086. }
  1087. private _getReflectionTexture(): BaseTexture {
  1088. if (this._reflectionTexture) {
  1089. return this._reflectionTexture;
  1090. }
  1091. return this.getScene().environmentTexture;
  1092. }
  1093. private _getRefractionTexture(): BaseTexture {
  1094. if (this._refractionTexture) {
  1095. return this._refractionTexture;
  1096. }
  1097. if (this._linkRefractionWithTransparency) {
  1098. return this.getScene().environmentTexture;
  1099. }
  1100. return null;
  1101. }
  1102. public dispose(forceDisposeEffect?: boolean, forceDisposeTextures?: boolean): void {
  1103. if (forceDisposeTextures) {
  1104. if (this._albedoTexture) {
  1105. this._albedoTexture.dispose();
  1106. }
  1107. if (this._ambientTexture) {
  1108. this._ambientTexture.dispose();
  1109. }
  1110. if (this._opacityTexture) {
  1111. this._opacityTexture.dispose();
  1112. }
  1113. if (this._reflectionTexture) {
  1114. this._reflectionTexture.dispose();
  1115. }
  1116. if (this._environmentBRDFTexture) {
  1117. this._environmentBRDFTexture.dispose();
  1118. }
  1119. if (this._emissiveTexture) {
  1120. this._emissiveTexture.dispose();
  1121. }
  1122. if (this._metallicTexture) {
  1123. this._metallicTexture.dispose();
  1124. }
  1125. if (this._reflectivityTexture) {
  1126. this._reflectivityTexture.dispose();
  1127. }
  1128. if (this._bumpTexture) {
  1129. this._bumpTexture.dispose();
  1130. }
  1131. if (this._lightmapTexture) {
  1132. this._lightmapTexture.dispose();
  1133. }
  1134. if (this._refractionTexture) {
  1135. this._refractionTexture.dispose();
  1136. }
  1137. }
  1138. this._renderTargets.dispose();
  1139. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  1140. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  1141. }
  1142. super.dispose(forceDisposeEffect, forceDisposeTextures);
  1143. }
  1144. }
  1145. }