babylon.pbrMaterial.ts 62 KB

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