babylon.pbrMaterial.ts 62 KB

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