babylon.pbrMaterial.ts 63 KB

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