babylon.pbrMaterial.ts 66 KB

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