babylon.pbrMaterial.ts 63 KB

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