babylon.pbrMaterial.ts 57 KB

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