babylon.pbrMaterial.ts 67 KB

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