babylon.pbrMaterial.ts 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class PBRMaterialDefines extends MaterialDefines {
  5. public ALBEDO = false;
  6. public AMBIENT = false;
  7. public OPACITY = false;
  8. public OPACITYRGB = false;
  9. public REFLECTION = false;
  10. public EMISSIVE = false;
  11. public REFLECTIVITY = false;
  12. public BUMP = false;
  13. public PARALLAX = false;
  14. public PARALLAXOCCLUSION = false;
  15. public SPECULAROVERALPHA = false;
  16. public CLIPPLANE = false;
  17. public ALPHATEST = false;
  18. public ALPHAFROMALBEDO = false;
  19. public POINTSIZE = false;
  20. public FOG = false;
  21. public LIGHT0 = false;
  22. public LIGHT1 = false;
  23. public LIGHT2 = false;
  24. public LIGHT3 = false;
  25. public SPOTLIGHT0 = false;
  26. public SPOTLIGHT1 = false;
  27. public SPOTLIGHT2 = false;
  28. public SPOTLIGHT3 = false;
  29. public HEMILIGHT0 = false;
  30. public HEMILIGHT1 = false;
  31. public HEMILIGHT2 = false;
  32. public HEMILIGHT3 = false;
  33. public POINTLIGHT0 = false;
  34. public POINTLIGHT1 = false;
  35. public POINTLIGHT2 = false;
  36. public POINTLIGHT3 = false;
  37. public DIRLIGHT0 = false;
  38. public DIRLIGHT1 = false;
  39. public DIRLIGHT2 = false;
  40. public DIRLIGHT3 = false;
  41. public SPECULARTERM = false;
  42. public SHADOW0 = false;
  43. public SHADOW1 = false;
  44. public SHADOW2 = false;
  45. public SHADOW3 = false;
  46. public SHADOWS = false;
  47. public SHADOWVSM0 = false;
  48. public SHADOWVSM1 = false;
  49. public SHADOWVSM2 = false;
  50. public SHADOWVSM3 = false;
  51. public SHADOWPCF0 = false;
  52. public SHADOWPCF1 = false;
  53. public SHADOWPCF2 = false;
  54. public SHADOWPCF3 = false;
  55. public OPACITYFRESNEL = false;
  56. public EMISSIVEFRESNEL = false;
  57. public FRESNEL = false;
  58. public NORMAL = false;
  59. public UV1 = false;
  60. public UV2 = false;
  61. public VERTEXCOLOR = false;
  62. public VERTEXALPHA = false;
  63. public NUM_BONE_INFLUENCERS = 0;
  64. public BonesPerMesh = 0;
  65. public INSTANCES = false;
  66. public MICROSURFACEFROMREFLECTIVITYMAP = false;
  67. public MICROSURFACEAUTOMATIC = false;
  68. public EMISSIVEASILLUMINATION = false;
  69. public LINKEMISSIVEWITHALBEDO = false;
  70. public LIGHTMAP = false;
  71. public USELIGHTMAPASSHADOWMAP = false;
  72. public REFLECTIONMAP_3D = false;
  73. public REFLECTIONMAP_SPHERICAL = false;
  74. public REFLECTIONMAP_PLANAR = false;
  75. public REFLECTIONMAP_CUBIC = false;
  76. public REFLECTIONMAP_PROJECTION = false;
  77. public REFLECTIONMAP_SKYBOX = false;
  78. public REFLECTIONMAP_EXPLICIT = false;
  79. public REFLECTIONMAP_EQUIRECTANGULAR = false;
  80. public INVERTCUBICMAP = false;
  81. public LOGARITHMICDEPTH = false;
  82. public CAMERATONEMAP = false;
  83. public CAMERACONTRAST = false;
  84. public OVERLOADEDVALUES = false;
  85. public OVERLOADEDSHADOWVALUES = false;
  86. public USESPHERICALFROMREFLECTIONMAP = false;
  87. public REFRACTION = false;
  88. public REFRACTIONMAP_3D = false;
  89. public LINKREFRACTIONTOTRANSPARENCY = false;
  90. public REFRACTIONMAPINLINEARSPACE = false;
  91. public LODBASEDMICROSFURACE = false;
  92. public USEPHYSICALLIGHTFALLOFF = false;
  93. public RADIANCEOVERALPHA = false;
  94. public USEPMREMREFLECTION = false;
  95. public USEPMREMREFRACTION = false;
  96. constructor() {
  97. super();
  98. this._keys = Object.keys(this);
  99. }
  100. }
  101. export class PBRMaterial extends BABYLON.Material {
  102. @serialize()
  103. public directIntensity: number = 1.0;
  104. @serialize()
  105. public emissiveIntensity: number = 1.0;
  106. @serialize()
  107. public environmentIntensity: number = 1.0;
  108. @serialize()
  109. public specularIntensity: number = 1.0;
  110. private _lightingInfos: Vector4 = new Vector4(this.directIntensity, this.emissiveIntensity, this.environmentIntensity, this.specularIntensity);
  111. @serialize()
  112. public overloadedShadowIntensity: number = 1.0;
  113. @serialize()
  114. public overloadedShadeIntensity: number = 1.0;
  115. private _overloadedShadowInfos: Vector4 = new Vector4(this.overloadedShadowIntensity, this.overloadedShadeIntensity, 0.0, 0.0);
  116. @serialize()
  117. public cameraExposure: number = 1.0;
  118. @serialize()
  119. public cameraContrast: number = 1.0;
  120. private _cameraInfos: Vector4 = new Vector4(1.0, 1.0, 0.0, 0.0);
  121. private _microsurfaceTextureLods: Vector2 = new Vector2(0.0, 0.0);
  122. @serialize()
  123. public overloadedAmbientIntensity: number = 0.0;
  124. @serialize()
  125. public overloadedAlbedoIntensity: number = 0.0;
  126. @serialize()
  127. public overloadedReflectivityIntensity: number = 0.0;
  128. @serialize()
  129. public overloadedEmissiveIntensity: number = 0.0;
  130. private _overloadedIntensity: Vector4 = new Vector4(this.overloadedAmbientIntensity, this.overloadedAlbedoIntensity, this.overloadedReflectivityIntensity, this.overloadedEmissiveIntensity);
  131. @serializeAsColor3()
  132. public overloadedAmbient: Color3 = BABYLON.Color3.White();
  133. @serializeAsColor3()
  134. public overloadedAlbedo: Color3 = BABYLON.Color3.White();
  135. @serializeAsColor3()
  136. public overloadedReflectivity: Color3 = BABYLON.Color3.White();
  137. @serializeAsColor3()
  138. public overloadedEmissive: Color3 = BABYLON.Color3.White();
  139. @serializeAsColor3()
  140. public overloadedReflection: Color3 = BABYLON.Color3.White();
  141. @serialize()
  142. public overloadedMicroSurface: number = 0.0;
  143. @serialize()
  144. public overloadedMicroSurfaceIntensity: number = 0.0;
  145. @serialize()
  146. public overloadedReflectionIntensity: number = 0.0;
  147. private _overloadedMicroSurface: Vector3 = new Vector3(this.overloadedMicroSurface, this.overloadedMicroSurfaceIntensity, this.overloadedReflectionIntensity);
  148. @serialize()
  149. public disableBumpMap: boolean = false;
  150. @serializeAsTexture()
  151. public albedoTexture: BaseTexture;
  152. @serializeAsTexture()
  153. public ambientTexture: BaseTexture;
  154. @serializeAsTexture()
  155. public opacityTexture: BaseTexture;
  156. @serializeAsTexture()
  157. public reflectionTexture: BaseTexture;
  158. @serializeAsTexture()
  159. public emissiveTexture: BaseTexture;
  160. @serializeAsTexture()
  161. public reflectivityTexture: BaseTexture;
  162. @serializeAsTexture()
  163. public bumpTexture: BaseTexture;
  164. @serializeAsTexture()
  165. public lightmapTexture: BaseTexture;
  166. @serializeAsTexture()
  167. public refractionTexture: BaseTexture;
  168. @serializeAsColor3("ambient")
  169. public ambientColor = new Color3(0, 0, 0);
  170. @serializeAsColor3("albedo")
  171. public albedoColor = new Color3(1, 1, 1);
  172. @serializeAsColor3("reflectivity")
  173. public reflectivityColor = new Color3(1, 1, 1);
  174. @serializeAsColor3("reflection")
  175. public reflectionColor = new Color3(0.5, 0.5, 0.5);
  176. @serializeAsColor3("emissivie")
  177. public emissiveColor = new Color3(0, 0, 0);
  178. @serialize()
  179. public microSurface = 0.5;
  180. @serialize()
  181. public indexOfRefraction = 0.66;
  182. @serialize()
  183. public invertRefractionY = false;
  184. @serializeAsFresnelParameters()
  185. public opacityFresnelParameters: FresnelParameters;
  186. @serializeAsFresnelParameters()
  187. public emissiveFresnelParameters: FresnelParameters;
  188. @serialize()
  189. public linkRefractionWithTransparency = false;
  190. @serialize()
  191. public linkEmissiveWithAlbedo = false;
  192. @serialize()
  193. public useLightmapAsShadowmap = false;
  194. @serialize()
  195. public useEmissiveAsIllumination = false;
  196. @serialize()
  197. public useAlphaFromAlbedoTexture = false;
  198. @serialize()
  199. public useSpecularOverAlpha = true;
  200. @serialize()
  201. public useMicroSurfaceFromReflectivityMapAlpha = false;
  202. @serialize()
  203. public useAutoMicroSurfaceFromReflectivityMap = false;
  204. @serialize()
  205. public useScalarInLinearSpace = false;
  206. @serialize()
  207. public usePhysicalLightFalloff = true;
  208. @serialize()
  209. public useRadianceOverAlpha = true;
  210. @serialize()
  211. public useParallax = false;
  212. @serialize()
  213. public useParallaxOcclusion = false;
  214. @serialize()
  215. public parallaxScaleBias = 0.05;
  216. @serialize()
  217. public disableLighting = false;
  218. private _renderTargets = new SmartArray<RenderTargetTexture>(16);
  219. private _worldViewProjectionMatrix = Matrix.Zero();
  220. private _globalAmbientColor = new Color3(0, 0, 0);
  221. private _tempColor = new Color3();
  222. private _renderId: number;
  223. private _defines = new PBRMaterialDefines();
  224. private _cachedDefines = new PBRMaterialDefines();
  225. private _useLogarithmicDepth: boolean;
  226. constructor(name: string, scene: Scene) {
  227. super(name, scene);
  228. this._cachedDefines.BonesPerMesh = -1;
  229. this.getRenderTargetTextures = (): SmartArray<RenderTargetTexture> => {
  230. this._renderTargets.reset();
  231. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  232. this._renderTargets.push(this.reflectionTexture);
  233. }
  234. if (this.refractionTexture && this.refractionTexture.isRenderTarget) {
  235. this._renderTargets.push(this.refractionTexture);
  236. }
  237. return this._renderTargets;
  238. }
  239. }
  240. @serialize()
  241. public get useLogarithmicDepth(): boolean {
  242. return this._useLogarithmicDepth;
  243. }
  244. public set useLogarithmicDepth(value: boolean) {
  245. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  246. }
  247. public needAlphaBlending(): boolean {
  248. if (this.linkRefractionWithTransparency) {
  249. return false;
  250. }
  251. return (this.alpha < 1.0) || (this.opacityTexture != null) || this._shouldUseAlphaFromAlbedoTexture() || this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled;
  252. }
  253. public needAlphaTesting(): boolean {
  254. if (this.linkRefractionWithTransparency) {
  255. return false;
  256. }
  257. return this.albedoTexture != null && this.albedoTexture.hasAlpha;
  258. }
  259. private _shouldUseAlphaFromAlbedoTexture(): boolean {
  260. return this.albedoTexture != null && this.albedoTexture.hasAlpha && this.useAlphaFromAlbedoTexture;
  261. }
  262. public getAlphaTestTexture(): BaseTexture {
  263. return this.albedoTexture;
  264. }
  265. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  266. if (!mesh) {
  267. return true;
  268. }
  269. if (this._defines.INSTANCES !== useInstances) {
  270. return false;
  271. }
  272. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  273. return true;
  274. }
  275. return false;
  276. }
  277. private convertColorToLinearSpaceToRef (color: Color3, ref: Color3): void {
  278. PBRMaterial.convertColorToLinearSpaceToRef(color, ref, this.useScalarInLinearSpace);
  279. }
  280. private static convertColorToLinearSpaceToRef (color: Color3, ref: Color3, useScalarInLinear: boolean): void {
  281. if (!useScalarInLinear) {
  282. color.toLinearSpaceToRef(ref);
  283. } else {
  284. ref.r = color.r;
  285. ref.g = color.g;
  286. ref.b = color.b;
  287. }
  288. }
  289. private static _scaledAlbedo = new Color3();
  290. private static _scaledReflectivity = new Color3();
  291. private static _scaledEmissive = new Color3();
  292. private static _scaledReflection = new Color3();
  293. private static _lightRadiuses = [1, 1, 1, 1];
  294. public static BindLights(scene: Scene, mesh: AbstractMesh, effect: Effect, defines: MaterialDefines, useScalarInLinearSpace: boolean) {
  295. var lightIndex = 0;
  296. var depthValuesAlreadySet = false;
  297. for (var index = 0; index < scene.lights.length; index++) {
  298. var light = scene.lights[index];
  299. if (!light.isEnabled()) {
  300. continue;
  301. }
  302. if (!light.canAffectMesh(mesh)) {
  303. continue;
  304. }
  305. this._lightRadiuses[lightIndex] = light.radius;
  306. MaterialHelper.BindLightProperties(light, effect, lightIndex);
  307. // GAMMA CORRECTION.
  308. this.convertColorToLinearSpaceToRef(light.diffuse, PBRMaterial._scaledAlbedo, useScalarInLinearSpace);
  309. PBRMaterial._scaledAlbedo.scaleToRef(light.intensity, PBRMaterial._scaledAlbedo);
  310. effect.setColor4("vLightDiffuse" + lightIndex, PBRMaterial._scaledAlbedo, light.range);
  311. if (defines["SPECULARTERM"]) {
  312. this.convertColorToLinearSpaceToRef(light.specular, PBRMaterial._scaledReflectivity, useScalarInLinearSpace);
  313. PBRMaterial._scaledReflectivity.scaleToRef(light.intensity, PBRMaterial._scaledReflectivity);
  314. effect.setColor3("vLightSpecular" + lightIndex, PBRMaterial._scaledReflectivity);
  315. }
  316. // Shadows
  317. if (scene.shadowsEnabled) {
  318. depthValuesAlreadySet = MaterialHelper.BindLightShadow(light, scene, mesh, lightIndex, effect, depthValuesAlreadySet);
  319. }
  320. lightIndex++;
  321. if (lightIndex === maxSimultaneousLights)
  322. break;
  323. }
  324. effect.setFloat4("vLightRadiuses", this._lightRadiuses[0],
  325. this._lightRadiuses[1],
  326. this._lightRadiuses[2],
  327. this._lightRadiuses[3]);
  328. }
  329. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  330. if (this.checkReadyOnlyOnce) {
  331. if (this._wasPreviouslyReady) {
  332. return true;
  333. }
  334. }
  335. var scene = this.getScene();
  336. if (!this.checkReadyOnEveryCall) {
  337. if (this._renderId === scene.getRenderId()) {
  338. if (this._checkCache(scene, mesh, useInstances)) {
  339. return true;
  340. }
  341. }
  342. }
  343. var engine = scene.getEngine();
  344. var needNormals = false;
  345. var needUVs = false;
  346. this._defines.reset();
  347. if (scene.texturesEnabled) {
  348. // Textures
  349. if (scene.texturesEnabled) {
  350. if (scene.getEngine().getCaps().textureLOD) {
  351. this._defines.LODBASEDMICROSFURACE = true;
  352. }
  353. if (this.albedoTexture && StandardMaterial.DiffuseTextureEnabled) {
  354. if (!this.albedoTexture.isReady()) {
  355. return false;
  356. } else {
  357. needUVs = true;
  358. this._defines.ALBEDO = true;
  359. }
  360. }
  361. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  362. if (!this.ambientTexture.isReady()) {
  363. return false;
  364. } else {
  365. needUVs = true;
  366. this._defines.AMBIENT = true;
  367. }
  368. }
  369. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  370. if (!this.opacityTexture.isReady()) {
  371. return false;
  372. } else {
  373. needUVs = true;
  374. this._defines.OPACITY = true;
  375. if (this.opacityTexture.getAlphaFromRGB) {
  376. this._defines.OPACITYRGB = true;
  377. }
  378. }
  379. }
  380. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  381. if (!this.reflectionTexture.isReady()) {
  382. return false;
  383. } else {
  384. needNormals = true;
  385. this._defines.REFLECTION = true;
  386. if (this.reflectionTexture.coordinatesMode === Texture.INVCUBIC_MODE) {
  387. this._defines.INVERTCUBICMAP = true;
  388. }
  389. this._defines.REFLECTIONMAP_3D = this.reflectionTexture.isCube;
  390. switch (this.reflectionTexture.coordinatesMode) {
  391. case Texture.CUBIC_MODE:
  392. case Texture.INVCUBIC_MODE:
  393. this._defines.REFLECTIONMAP_CUBIC = true;
  394. break;
  395. case Texture.EXPLICIT_MODE:
  396. this._defines.REFLECTIONMAP_EXPLICIT = true;
  397. break;
  398. case Texture.PLANAR_MODE:
  399. this._defines.REFLECTIONMAP_PLANAR = true;
  400. break;
  401. case Texture.PROJECTION_MODE:
  402. this._defines.REFLECTIONMAP_PROJECTION = true;
  403. break;
  404. case Texture.SKYBOX_MODE:
  405. this._defines.REFLECTIONMAP_SKYBOX = true;
  406. break;
  407. case Texture.SPHERICAL_MODE:
  408. this._defines.REFLECTIONMAP_SPHERICAL = true;
  409. break;
  410. case Texture.EQUIRECTANGULAR_MODE:
  411. this._defines.REFLECTIONMAP_EQUIRECTANGULAR = true;
  412. break;
  413. }
  414. if (this.reflectionTexture instanceof HDRCubeTexture && (<HDRCubeTexture>this.reflectionTexture)) {
  415. this._defines.USESPHERICALFROMREFLECTIONMAP = true;
  416. needNormals = true;
  417. if ((<HDRCubeTexture>this.reflectionTexture).isPMREM) {
  418. this._defines.USEPMREMREFLECTION = true;
  419. }
  420. }
  421. }
  422. }
  423. if (this.lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  424. if (!this.lightmapTexture.isReady()) {
  425. return false;
  426. } else {
  427. needUVs = true;
  428. this._defines.LIGHTMAP = true;
  429. this._defines.USELIGHTMAPASSHADOWMAP = this.useLightmapAsShadowmap;
  430. }
  431. }
  432. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  433. if (!this.emissiveTexture.isReady()) {
  434. return false;
  435. } else {
  436. needUVs = true;
  437. this._defines.EMISSIVE = true;
  438. }
  439. }
  440. if (this.reflectivityTexture && StandardMaterial.SpecularTextureEnabled) {
  441. if (!this.reflectivityTexture.isReady()) {
  442. return false;
  443. } else {
  444. needUVs = true;
  445. this._defines.REFLECTIVITY = true;
  446. this._defines.MICROSURFACEFROMREFLECTIVITYMAP = this.useMicroSurfaceFromReflectivityMapAlpha;
  447. this._defines.MICROSURFACEAUTOMATIC = this.useAutoMicroSurfaceFromReflectivityMap;
  448. }
  449. }
  450. }
  451. if (scene.getEngine().getCaps().standardDerivatives && this.bumpTexture && StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  452. if (!this.bumpTexture.isReady()) {
  453. return false;
  454. } else {
  455. needUVs = true;
  456. this._defines.BUMP = true;
  457. if (this.useParallax) {
  458. this._defines.PARALLAX = true;
  459. if (this.useParallaxOcclusion) {
  460. this._defines.PARALLAXOCCLUSION = true;
  461. }
  462. }
  463. }
  464. }
  465. if (this.refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  466. if (!this.refractionTexture.isReady()) {
  467. return false;
  468. } else {
  469. needUVs = true;
  470. this._defines.REFRACTION = true;
  471. this._defines.REFRACTIONMAP_3D = this.refractionTexture.isCube;
  472. if (this.linkRefractionWithTransparency) {
  473. this._defines.LINKREFRACTIONTOTRANSPARENCY = true;
  474. }
  475. if (this.refractionTexture instanceof HDRCubeTexture) {
  476. this._defines.REFRACTIONMAPINLINEARSPACE = true;
  477. if ((<HDRCubeTexture>this.refractionTexture).isPMREM) {
  478. this._defines.USEPMREMREFRACTION = true;
  479. }
  480. }
  481. }
  482. }
  483. }
  484. // Effect
  485. if (scene.clipPlane) {
  486. this._defines.CLIPPLANE = true;
  487. }
  488. if (engine.getAlphaTesting()) {
  489. this._defines.ALPHATEST = true;
  490. }
  491. if (this._shouldUseAlphaFromAlbedoTexture()) {
  492. this._defines.ALPHAFROMALBEDO = true;
  493. }
  494. if (this.useEmissiveAsIllumination) {
  495. this._defines.EMISSIVEASILLUMINATION = true;
  496. }
  497. if (this.linkEmissiveWithAlbedo) {
  498. this._defines.LINKEMISSIVEWITHALBEDO = true;
  499. }
  500. if (this.useLogarithmicDepth) {
  501. this._defines.LOGARITHMICDEPTH = true;
  502. }
  503. if (this.cameraContrast != 1) {
  504. this._defines.CAMERACONTRAST = true;
  505. }
  506. if (this.cameraExposure != 1) {
  507. this._defines.CAMERATONEMAP = true;
  508. }
  509. if (this.overloadedShadeIntensity != 1 ||
  510. this.overloadedShadowIntensity != 1) {
  511. this._defines.OVERLOADEDSHADOWVALUES = true;
  512. }
  513. if (this.overloadedMicroSurfaceIntensity > 0 ||
  514. this.overloadedEmissiveIntensity > 0 ||
  515. this.overloadedReflectivityIntensity > 0 ||
  516. this.overloadedAlbedoIntensity > 0 ||
  517. this.overloadedAmbientIntensity > 0 ||
  518. this.overloadedReflectionIntensity > 0) {
  519. this._defines.OVERLOADEDVALUES = true;
  520. }
  521. // Point size
  522. if (this.pointsCloud || scene.forcePointsCloud) {
  523. this._defines.POINTSIZE = true;
  524. }
  525. // Fog
  526. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  527. this._defines.FOG = true;
  528. }
  529. if (scene.lightsEnabled && !this.disableLighting) {
  530. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines) || needNormals;
  531. }
  532. if (StandardMaterial.FresnelEnabled) {
  533. // Fresnel
  534. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled ||
  535. this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  536. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  537. this._defines.OPACITYFRESNEL = true;
  538. }
  539. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  540. this._defines.EMISSIVEFRESNEL = true;
  541. }
  542. needNormals = true;
  543. this._defines.FRESNEL = true;
  544. }
  545. }
  546. if (this._defines.SPECULARTERM && this.useSpecularOverAlpha) {
  547. this._defines.SPECULAROVERALPHA = true;
  548. }
  549. if (this.usePhysicalLightFalloff) {
  550. this._defines.USEPHYSICALLIGHTFALLOFF = true;
  551. }
  552. if (this.useRadianceOverAlpha) {
  553. this._defines.RADIANCEOVERALPHA = true;
  554. }
  555. // Attribs
  556. if (mesh) {
  557. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  558. this._defines.NORMAL = true;
  559. }
  560. if (needUVs) {
  561. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  562. this._defines.UV1 = true;
  563. }
  564. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  565. this._defines.UV2 = true;
  566. }
  567. }
  568. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  569. this._defines.VERTEXCOLOR = true;
  570. if (mesh.hasVertexAlpha) {
  571. this._defines.VERTEXALPHA = true;
  572. }
  573. }
  574. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  575. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  576. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  577. }
  578. // Instances
  579. if (useInstances) {
  580. this._defines.INSTANCES = true;
  581. }
  582. }
  583. // Get correct effect
  584. if (!this._defines.isEqual(this._cachedDefines)) {
  585. this._defines.cloneTo(this._cachedDefines);
  586. scene.resetCachedMaterial();
  587. // Fallbacks
  588. var fallbacks = new EffectFallbacks();
  589. if (this._defines.REFLECTION) {
  590. fallbacks.addFallback(0, "REFLECTION");
  591. }
  592. if (this._defines.REFRACTION) {
  593. fallbacks.addFallback(0, "REFRACTION");
  594. }
  595. if (this._defines.REFLECTIVITY) {
  596. fallbacks.addFallback(0, "REFLECTIVITY");
  597. }
  598. if (this._defines.BUMP) {
  599. fallbacks.addFallback(0, "BUMP");
  600. }
  601. if (this._defines.PARALLAX) {
  602. fallbacks.addFallback(1, "PARALLAX");
  603. }
  604. if (this._defines.PARALLAXOCCLUSION) {
  605. fallbacks.addFallback(0, "PARALLAXOCCLUSION");
  606. }
  607. if (this._defines.SPECULAROVERALPHA) {
  608. fallbacks.addFallback(0, "SPECULAROVERALPHA");
  609. }
  610. if (this._defines.FOG) {
  611. fallbacks.addFallback(1, "FOG");
  612. }
  613. if (this._defines.POINTSIZE) {
  614. fallbacks.addFallback(0, "POINTSIZE");
  615. }
  616. if (this._defines.LOGARITHMICDEPTH) {
  617. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  618. }
  619. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  620. if (this._defines.SPECULARTERM) {
  621. fallbacks.addFallback(0, "SPECULARTERM");
  622. }
  623. if (this._defines.OPACITYFRESNEL) {
  624. fallbacks.addFallback(1, "OPACITYFRESNEL");
  625. }
  626. if (this._defines.EMISSIVEFRESNEL) {
  627. fallbacks.addFallback(2, "EMISSIVEFRESNEL");
  628. }
  629. if (this._defines.FRESNEL) {
  630. fallbacks.addFallback(3, "FRESNEL");
  631. }
  632. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  633. fallbacks.addCPUSkinningFallback(0, mesh);
  634. }
  635. //Attributes
  636. var attribs = [VertexBuffer.PositionKind];
  637. if (this._defines.NORMAL) {
  638. attribs.push(VertexBuffer.NormalKind);
  639. }
  640. if (this._defines.UV1) {
  641. attribs.push(VertexBuffer.UVKind);
  642. }
  643. if (this._defines.UV2) {
  644. attribs.push(VertexBuffer.UV2Kind);
  645. }
  646. if (this._defines.VERTEXCOLOR) {
  647. attribs.push(VertexBuffer.ColorKind);
  648. }
  649. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  650. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  651. // Legacy browser patch
  652. var shaderName = "pbr";
  653. if (!scene.getEngine().getCaps().standardDerivatives) {
  654. shaderName = "legacypbr";
  655. }
  656. var join = this._defines.toString();
  657. this._effect = scene.getEngine().createEffect(shaderName,
  658. attribs,
  659. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vAmbientColor", "vAlbedoColor", "vReflectivityColor", "vEmissiveColor", "vReflectionColor",
  660. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  661. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  662. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  663. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  664. "vFogInfos", "vFogColor", "pointSize",
  665. "vAlbedoInfos", "vAmbientInfos", "vOpacityInfos", "vReflectionInfos", "vEmissiveInfos", "vReflectivityInfos", "vBumpInfos", "vLightmapInfos", "vRefractionInfos",
  666. "mBones",
  667. "vClipPlane", "albedoMatrix", "ambientMatrix", "opacityMatrix", "reflectionMatrix", "emissiveMatrix", "reflectivityMatrix", "bumpMatrix", "lightmapMatrix", "refractionMatrix",
  668. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues",
  669. "opacityParts", "emissiveLeftColor", "emissiveRightColor",
  670. "vLightingIntensity", "vOverloadedShadowIntensity", "vOverloadedIntensity", "vCameraInfos", "vOverloadedAlbedo", "vOverloadedReflection", "vOverloadedReflectivity", "vOverloadedEmissive", "vOverloadedMicroSurface",
  671. "logarithmicDepthConstant",
  672. "vSphericalX", "vSphericalY", "vSphericalZ",
  673. "vSphericalXX", "vSphericalYY", "vSphericalZZ",
  674. "vSphericalXY", "vSphericalYZ", "vSphericalZX",
  675. "vMicrosurfaceTextureLods", "vLightRadiuses"
  676. ],
  677. ["albedoSampler", "ambientSampler", "opacitySampler", "reflectionCubeSampler", "reflection2DSampler", "emissiveSampler", "reflectivitySampler", "bumpSampler", "lightmapSampler", "refractionCubeSampler", "refraction2DSampler",
  678. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  679. ],
  680. join, fallbacks, this.onCompiled, this.onError);
  681. }
  682. if (!this._effect.isReady()) {
  683. return false;
  684. }
  685. this._renderId = scene.getRenderId();
  686. this._wasPreviouslyReady = true;
  687. if (mesh) {
  688. if (!mesh._materialDefines) {
  689. mesh._materialDefines = new PBRMaterialDefines();
  690. }
  691. this._defines.cloneTo(mesh._materialDefines);
  692. }
  693. return true;
  694. }
  695. public unbind(): void {
  696. if (this.reflectionTexture && this.reflectionTexture.isRenderTarget) {
  697. this._effect.setTexture("reflection2DSampler", null);
  698. }
  699. if (this.refractionTexture && this.refractionTexture.isRenderTarget) {
  700. this._effect.setTexture("refraction2DSampler", null);
  701. }
  702. super.unbind();
  703. }
  704. public bindOnlyWorldMatrix(world: Matrix): void {
  705. this._effect.setMatrix("world", world);
  706. }
  707. private _myScene: BABYLON.Scene = null;
  708. private _myShadowGenerator: BABYLON.ShadowGenerator = null;
  709. public bind(world: Matrix, mesh?: Mesh): void {
  710. this._myScene = this.getScene();
  711. // Matrices
  712. this.bindOnlyWorldMatrix(world);
  713. // Bones
  714. MaterialHelper.BindBonesParameters(mesh, this._effect);
  715. if (this._myScene.getCachedMaterial() !== (<BABYLON.Material>this)) {
  716. this._effect.setMatrix("viewProjection", this._myScene.getTransformMatrix());
  717. if (StandardMaterial.FresnelEnabled) {
  718. if (this.opacityFresnelParameters && this.opacityFresnelParameters.isEnabled) {
  719. this._effect.setColor4("opacityParts", new Color3(this.opacityFresnelParameters.leftColor.toLuminance(), this.opacityFresnelParameters.rightColor.toLuminance(), this.opacityFresnelParameters.bias), this.opacityFresnelParameters.power);
  720. }
  721. if (this.emissiveFresnelParameters && this.emissiveFresnelParameters.isEnabled) {
  722. this._effect.setColor4("emissiveLeftColor", this.emissiveFresnelParameters.leftColor, this.emissiveFresnelParameters.power);
  723. this._effect.setColor4("emissiveRightColor", this.emissiveFresnelParameters.rightColor, this.emissiveFresnelParameters.bias);
  724. }
  725. }
  726. // Textures
  727. if (this._myScene.texturesEnabled) {
  728. if (this.albedoTexture && StandardMaterial.DiffuseTextureEnabled) {
  729. this._effect.setTexture("albedoSampler", this.albedoTexture);
  730. this._effect.setFloat2("vAlbedoInfos", this.albedoTexture.coordinatesIndex, this.albedoTexture.level);
  731. this._effect.setMatrix("albedoMatrix", this.albedoTexture.getTextureMatrix());
  732. }
  733. if (this.ambientTexture && StandardMaterial.AmbientTextureEnabled) {
  734. this._effect.setTexture("ambientSampler", this.ambientTexture);
  735. this._effect.setFloat2("vAmbientInfos", this.ambientTexture.coordinatesIndex, this.ambientTexture.level);
  736. this._effect.setMatrix("ambientMatrix", this.ambientTexture.getTextureMatrix());
  737. }
  738. if (this.opacityTexture && StandardMaterial.OpacityTextureEnabled) {
  739. this._effect.setTexture("opacitySampler", this.opacityTexture);
  740. this._effect.setFloat2("vOpacityInfos", this.opacityTexture.coordinatesIndex, this.opacityTexture.level);
  741. this._effect.setMatrix("opacityMatrix", this.opacityTexture.getTextureMatrix());
  742. }
  743. if (this.reflectionTexture && StandardMaterial.ReflectionTextureEnabled) {
  744. this._microsurfaceTextureLods.x = Math.round(Math.log(this.reflectionTexture.getSize().width) * Math.LOG2E);
  745. if (this.reflectionTexture.isCube) {
  746. this._effect.setTexture("reflectionCubeSampler", this.reflectionTexture);
  747. } else {
  748. this._effect.setTexture("reflection2DSampler", this.reflectionTexture);
  749. }
  750. this._effect.setMatrix("reflectionMatrix", this.reflectionTexture.getReflectionTextureMatrix());
  751. this._effect.setFloat2("vReflectionInfos", this.reflectionTexture.level, 0);
  752. if (this._defines.USESPHERICALFROMREFLECTIONMAP) {
  753. this._effect.setFloat3("vSphericalX", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.x.x,
  754. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.x.y,
  755. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.x.z);
  756. this._effect.setFloat3("vSphericalY", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.y.x,
  757. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.y.y,
  758. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.y.z);
  759. this._effect.setFloat3("vSphericalZ", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.z.x,
  760. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.z.y,
  761. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.z.z);
  762. this._effect.setFloat3("vSphericalXX", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.xx.x,
  763. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.xx.y,
  764. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.xx.z);
  765. this._effect.setFloat3("vSphericalYY", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.yy.x,
  766. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.yy.y,
  767. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.yy.z);
  768. this._effect.setFloat3("vSphericalZZ", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.zz.x,
  769. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.zz.y,
  770. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.zz.z);
  771. this._effect.setFloat3("vSphericalXY", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.xy.x,
  772. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.xy.y,
  773. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.xy.z);
  774. this._effect.setFloat3("vSphericalYZ", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.yz.x,
  775. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.yz.y,
  776. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.yz.z);
  777. this._effect.setFloat3("vSphericalZX", (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.zx.x,
  778. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.zx.y,
  779. (<HDRCubeTexture>this.reflectionTexture).sphericalPolynomial.zx.z);
  780. }
  781. }
  782. if (this.emissiveTexture && StandardMaterial.EmissiveTextureEnabled) {
  783. this._effect.setTexture("emissiveSampler", this.emissiveTexture);
  784. this._effect.setFloat2("vEmissiveInfos", this.emissiveTexture.coordinatesIndex, this.emissiveTexture.level);
  785. this._effect.setMatrix("emissiveMatrix", this.emissiveTexture.getTextureMatrix());
  786. }
  787. if (this.lightmapTexture && StandardMaterial.LightmapTextureEnabled) {
  788. this._effect.setTexture("lightmapSampler", this.lightmapTexture);
  789. this._effect.setFloat2("vLightmapInfos", this.lightmapTexture.coordinatesIndex, this.lightmapTexture.level);
  790. this._effect.setMatrix("lightmapMatrix", this.lightmapTexture.getTextureMatrix());
  791. }
  792. if (this.reflectivityTexture && StandardMaterial.SpecularTextureEnabled) {
  793. this._effect.setTexture("reflectivitySampler", this.reflectivityTexture);
  794. this._effect.setFloat2("vReflectivityInfos", this.reflectivityTexture.coordinatesIndex, this.reflectivityTexture.level);
  795. this._effect.setMatrix("reflectivityMatrix", this.reflectivityTexture.getTextureMatrix());
  796. }
  797. if (this.bumpTexture && this._myScene.getEngine().getCaps().standardDerivatives && StandardMaterial.BumpTextureEnabled && !this.disableBumpMap) {
  798. this._effect.setTexture("bumpSampler", this.bumpTexture);
  799. this._effect.setFloat3("vBumpInfos", this.bumpTexture.coordinatesIndex, 1.0 / this.bumpTexture.level, this.parallaxScaleBias);
  800. this._effect.setMatrix("bumpMatrix", this.bumpTexture.getTextureMatrix());
  801. }
  802. if (this.refractionTexture && StandardMaterial.RefractionTextureEnabled) {
  803. this._microsurfaceTextureLods.y = Math.round(Math.log(this.refractionTexture.getSize().width) * Math.LOG2E);
  804. var depth = 1.0;
  805. if (this.refractionTexture.isCube) {
  806. this._effect.setTexture("refractionCubeSampler", this.refractionTexture);
  807. } else {
  808. this._effect.setTexture("refraction2DSampler", this.refractionTexture);
  809. this._effect.setMatrix("refractionMatrix", this.refractionTexture.getReflectionTextureMatrix());
  810. if ((<any>this.refractionTexture).depth) {
  811. depth = (<any>this.refractionTexture).depth;
  812. }
  813. }
  814. this._effect.setFloat4("vRefractionInfos", this.refractionTexture.level, this.indexOfRefraction, depth, this.invertRefractionY ? -1 : 1);
  815. }
  816. if ((this.reflectionTexture || this.refractionTexture)) {
  817. this._effect.setFloat2("vMicrosurfaceTextureLods", this._microsurfaceTextureLods.x, this._microsurfaceTextureLods.y);
  818. }
  819. }
  820. // Clip plane
  821. MaterialHelper.BindClipPlane(this._effect, this._myScene);
  822. // Point size
  823. if (this.pointsCloud) {
  824. this._effect.setFloat("pointSize", this.pointSize);
  825. }
  826. // Colors
  827. this._myScene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
  828. // GAMMA CORRECTION.
  829. this.convertColorToLinearSpaceToRef(this.reflectivityColor, PBRMaterial._scaledReflectivity);
  830. this._effect.setVector3("vEyePosition", this._myScene._mirroredCameraPosition ? this._myScene._mirroredCameraPosition : this._myScene.activeCamera.position);
  831. this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
  832. this._effect.setColor4("vReflectivityColor", PBRMaterial._scaledReflectivity, this.microSurface);
  833. // GAMMA CORRECTION.
  834. this.convertColorToLinearSpaceToRef(this.emissiveColor,PBRMaterial._scaledEmissive);
  835. this._effect.setColor3("vEmissiveColor", PBRMaterial._scaledEmissive);
  836. // GAMMA CORRECTION.
  837. this.convertColorToLinearSpaceToRef(this.reflectionColor, PBRMaterial._scaledReflection);
  838. this._effect.setColor3("vReflectionColor", PBRMaterial._scaledReflection);
  839. }
  840. if (this._myScene.getCachedMaterial() !== this || !this.isFrozen) {
  841. // GAMMA CORRECTION.
  842. this.convertColorToLinearSpaceToRef(this.albedoColor, PBRMaterial._scaledAlbedo);
  843. this._effect.setColor4("vAlbedoColor", PBRMaterial._scaledAlbedo, this.alpha * mesh.visibility);
  844. // Lights
  845. if (this._myScene.lightsEnabled && !this.disableLighting) {
  846. PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines, this.useScalarInLinearSpace);
  847. }
  848. // View
  849. if (this._myScene.fogEnabled && mesh.applyFog && this._myScene.fogMode !== Scene.FOGMODE_NONE || this.reflectionTexture) {
  850. this._effect.setMatrix("view", this._myScene.getViewMatrix());
  851. }
  852. // Fog
  853. MaterialHelper.BindFogParameters(this._myScene, mesh, this._effect);
  854. this._lightingInfos.x = this.directIntensity;
  855. this._lightingInfos.y = this.emissiveIntensity;
  856. this._lightingInfos.z = this.environmentIntensity;
  857. this._lightingInfos.w = this.specularIntensity;
  858. this._effect.setVector4("vLightingIntensity", this._lightingInfos);
  859. this._overloadedShadowInfos.x = this.overloadedShadowIntensity;
  860. this._overloadedShadowInfos.y = this.overloadedShadeIntensity;
  861. this._effect.setVector4("vOverloadedShadowIntensity", this._overloadedShadowInfos);
  862. this._cameraInfos.x = this.cameraExposure;
  863. this._cameraInfos.y = this.cameraContrast;
  864. this._effect.setVector4("vCameraInfos", this._cameraInfos);
  865. this._overloadedIntensity.x = this.overloadedAmbientIntensity;
  866. this._overloadedIntensity.y = this.overloadedAlbedoIntensity;
  867. this._overloadedIntensity.z = this.overloadedReflectivityIntensity;
  868. this._overloadedIntensity.w = this.overloadedEmissiveIntensity;
  869. this._effect.setVector4("vOverloadedIntensity", this._overloadedIntensity);
  870. this.convertColorToLinearSpaceToRef(this.overloadedAmbient,this._tempColor);
  871. this._effect.setColor3("vOverloadedAmbient", this._tempColor);
  872. this.convertColorToLinearSpaceToRef(this.overloadedAlbedo, this._tempColor);
  873. this._effect.setColor3("vOverloadedAlbedo", this._tempColor);
  874. this.convertColorToLinearSpaceToRef(this.overloadedReflectivity, this._tempColor);
  875. this._effect.setColor3("vOverloadedReflectivity", this._tempColor);
  876. this.convertColorToLinearSpaceToRef(this.overloadedEmissive, this._tempColor);
  877. this._effect.setColor3("vOverloadedEmissive", this._tempColor);
  878. this.convertColorToLinearSpaceToRef(this.overloadedReflection, this._tempColor);
  879. this._effect.setColor3("vOverloadedReflection", this._tempColor);
  880. this._overloadedMicroSurface.x = this.overloadedMicroSurface;
  881. this._overloadedMicroSurface.y = this.overloadedMicroSurfaceIntensity;
  882. this._overloadedMicroSurface.z = this.overloadedReflectionIntensity;
  883. this._effect.setVector3("vOverloadedMicroSurface", this._overloadedMicroSurface);
  884. // Log. depth
  885. MaterialHelper.BindLogDepth(this._defines, this._effect, this._myScene);
  886. }
  887. super.bind(world, mesh);
  888. this._myScene = null;
  889. }
  890. public getAnimatables(): IAnimatable[] {
  891. var results = [];
  892. if (this.albedoTexture && this.albedoTexture.animations && this.albedoTexture.animations.length > 0) {
  893. results.push(this.albedoTexture);
  894. }
  895. if (this.ambientTexture && this.ambientTexture.animations && this.ambientTexture.animations.length > 0) {
  896. results.push(this.ambientTexture);
  897. }
  898. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  899. results.push(this.opacityTexture);
  900. }
  901. if (this.reflectionTexture && this.reflectionTexture.animations && this.reflectionTexture.animations.length > 0) {
  902. results.push(this.reflectionTexture);
  903. }
  904. if (this.emissiveTexture && this.emissiveTexture.animations && this.emissiveTexture.animations.length > 0) {
  905. results.push(this.emissiveTexture);
  906. }
  907. if (this.reflectivityTexture && this.reflectivityTexture.animations && this.reflectivityTexture.animations.length > 0) {
  908. results.push(this.reflectivityTexture);
  909. }
  910. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  911. results.push(this.bumpTexture);
  912. }
  913. if (this.lightmapTexture && this.lightmapTexture.animations && this.lightmapTexture.animations.length > 0) {
  914. results.push(this.lightmapTexture);
  915. }
  916. if (this.refractionTexture && this.refractionTexture.animations && this.refractionTexture.animations.length > 0) {
  917. results.push(this.refractionTexture);
  918. }
  919. return results;
  920. }
  921. public dispose(forceDisposeEffect?: boolean): void {
  922. if (this.albedoTexture) {
  923. this.albedoTexture.dispose();
  924. }
  925. if (this.ambientTexture) {
  926. this.ambientTexture.dispose();
  927. }
  928. if (this.opacityTexture) {
  929. this.opacityTexture.dispose();
  930. }
  931. if (this.reflectionTexture) {
  932. this.reflectionTexture.dispose();
  933. }
  934. if (this.emissiveTexture) {
  935. this.emissiveTexture.dispose();
  936. }
  937. if (this.reflectivityTexture) {
  938. this.reflectivityTexture.dispose();
  939. }
  940. if (this.bumpTexture) {
  941. this.bumpTexture.dispose();
  942. }
  943. if (this.lightmapTexture) {
  944. this.lightmapTexture.dispose();
  945. }
  946. if (this.refractionTexture) {
  947. this.refractionTexture.dispose();
  948. }
  949. super.dispose(forceDisposeEffect);
  950. }
  951. public clone(name: string): PBRMaterial {
  952. return SerializationHelper.Clone(() => new PBRMaterial(name, this.getScene()), this);
  953. }
  954. public serialize(): any {
  955. var serializationObject = SerializationHelper.Serialize(this);
  956. serializationObject.customType = "BABYLON.PBRMaterial";
  957. return serializationObject;
  958. }
  959. // Statics
  960. public static Parse(source: any, scene: Scene, rootUrl: string): PBRMaterial {
  961. return SerializationHelper.Parse(() => new PBRMaterial(source.name, scene), source, scene, rootUrl);
  962. }
  963. }
  964. }