babylon.pbrMaterial.ts 61 KB

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