babylon.pbrMaterial.ts 50 KB

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