babylon.pbrBaseMaterial.ts 66 KB

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