KHR_materials_specular.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { Nullable } from "babylonjs/types";
  2. import { PBRMaterial } from "babylonjs/Materials/PBR/pbrMaterial";
  3. import { Material } from "babylonjs/Materials/material";
  4. import { IMaterial, ITextureInfo } from "../glTFLoaderInterfaces";
  5. import { IGLTFLoaderExtension } from "../glTFLoaderExtension";
  6. import { GLTFLoader } from "../glTFLoader";
  7. const NAME = "KHR_materials_specular";
  8. interface IKHR_materials_specular {
  9. specularFactor: number;
  10. specularTexture: ITextureInfo;
  11. }
  12. /**
  13. * [Proposed Specification](https://github.com/KhronosGroup/glTF/pull/1677)
  14. * !!! Experimental Extension Subject to Changes !!!
  15. */
  16. export class KHR_materials_specular implements IGLTFLoaderExtension {
  17. /**
  18. * The name of this extension.
  19. */
  20. public readonly name = NAME;
  21. /**
  22. * Defines whether this extension is enabled.
  23. */
  24. public enabled: boolean;
  25. /**
  26. * Defines a number that determines the order the extensions are applied.
  27. */
  28. public order = 230;
  29. private _loader: GLTFLoader;
  30. /** @hidden */
  31. constructor(loader: GLTFLoader) {
  32. this._loader = loader;
  33. this.enabled = this._loader.isExtensionUsed(NAME);
  34. }
  35. /** @hidden */
  36. public dispose() {
  37. delete this._loader;
  38. }
  39. /** @hidden */
  40. public loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Nullable<Promise<void>> {
  41. return GLTFLoader.LoadExtensionAsync<IKHR_materials_specular>(context, material, this.name, (extensionContext, extension) => {
  42. const promises = new Array<Promise<any>>();
  43. promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
  44. promises.push(this._loadSpecularPropertiesAsync(extensionContext, extension, babylonMaterial));
  45. return Promise.all(promises).then(() => { });
  46. });
  47. }
  48. private _loadSpecularPropertiesAsync(context: string, properties: IKHR_materials_specular, babylonMaterial: Material): Promise<void> {
  49. if (!(babylonMaterial instanceof PBRMaterial)) {
  50. throw new Error(`${context}: Material type not supported`);
  51. }
  52. if (properties.specularFactor !== undefined) {
  53. babylonMaterial.metallicF0Factor = properties.specularFactor;
  54. }
  55. if (properties.specularTexture) {
  56. // This does not allow a separate sampler for it at the moment but is currently under discussion.
  57. babylonMaterial.useMetallicF0FactorFromMetallicTexture = true;
  58. }
  59. return Promise.resolve();
  60. }
  61. }
  62. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_specular(loader));