babylon.pbrMaterial.ts 63 KB

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