KHR_materials_pbrSpecularGlossiness.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Color3 } from "@babylonjs/core/Maths/math.color.js";
  2. import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
  3. import { GLTFLoader } from "../glTFLoader.js";
  4. const NAME = "KHR_materials_pbrSpecularGlossiness";
  5. /**
  6. * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Archived/KHR_materials_pbrSpecularGlossiness/README.md)
  7. */
  8. // eslint-disable-next-line @typescript-eslint/naming-convention
  9. export class KHR_materials_pbrSpecularGlossiness {
  10. /**
  11. * @internal
  12. */
  13. constructor(loader) {
  14. /**
  15. * The name of this extension.
  16. */
  17. this.name = NAME;
  18. /**
  19. * Defines a number that determines the order the extensions are applied.
  20. */
  21. this.order = 200;
  22. this._loader = loader;
  23. this.enabled = this._loader.isExtensionUsed(NAME);
  24. }
  25. /** @internal */
  26. dispose() {
  27. this._loader = null;
  28. }
  29. /**
  30. * @internal
  31. */
  32. loadMaterialPropertiesAsync(context, material, babylonMaterial) {
  33. return GLTFLoader.LoadExtensionAsync(context, material, this.name, (extensionContext, extension) => {
  34. const promises = new Array();
  35. promises.push(this._loader.loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
  36. promises.push(this._loadSpecularGlossinessPropertiesAsync(extensionContext, extension, babylonMaterial));
  37. this._loader.loadMaterialAlphaProperties(context, material, babylonMaterial);
  38. return Promise.all(promises).then(() => { });
  39. });
  40. }
  41. _loadSpecularGlossinessPropertiesAsync(context, properties, babylonMaterial) {
  42. if (!(babylonMaterial instanceof PBRMaterial)) {
  43. throw new Error(`${context}: Material type not supported`);
  44. }
  45. const promises = new Array();
  46. babylonMaterial.metallic = null;
  47. babylonMaterial.roughness = null;
  48. if (properties.diffuseFactor) {
  49. babylonMaterial.albedoColor = Color3.FromArray(properties.diffuseFactor);
  50. babylonMaterial.alpha = properties.diffuseFactor[3];
  51. }
  52. else {
  53. babylonMaterial.albedoColor = Color3.White();
  54. }
  55. babylonMaterial.reflectivityColor = properties.specularFactor ? Color3.FromArray(properties.specularFactor) : Color3.White();
  56. babylonMaterial.microSurface = properties.glossinessFactor == undefined ? 1 : properties.glossinessFactor;
  57. if (properties.diffuseTexture) {
  58. promises.push(this._loader.loadTextureInfoAsync(`${context}/diffuseTexture`, properties.diffuseTexture, (texture) => {
  59. texture.name = `${babylonMaterial.name} (Diffuse)`;
  60. babylonMaterial.albedoTexture = texture;
  61. }));
  62. }
  63. if (properties.specularGlossinessTexture) {
  64. promises.push(this._loader.loadTextureInfoAsync(`${context}/specularGlossinessTexture`, properties.specularGlossinessTexture, (texture) => {
  65. texture.name = `${babylonMaterial.name} (Specular Glossiness)`;
  66. babylonMaterial.reflectivityTexture = texture;
  67. babylonMaterial.reflectivityTexture.hasAlpha = true;
  68. }));
  69. babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
  70. }
  71. return Promise.all(promises).then(() => { });
  72. }
  73. }
  74. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_pbrSpecularGlossiness(loader));
  75. //# sourceMappingURL=KHR_materials_pbrSpecularGlossiness.js.map