babylon.pbrMaterial.ts 62 KB

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