babylon.pbrMaterial.ts 56 KB

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