KHR_materials_specular.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
  2. import { GLTFLoader } from "../glTFLoader.js";
  3. import { Color3 } from "@babylonjs/core/Maths/math.color.js";
  4. const NAME = "KHR_materials_specular";
  5. /**
  6. * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_specular/README.md)
  7. */
  8. // eslint-disable-next-line @typescript-eslint/naming-convention
  9. export class KHR_materials_specular {
  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 = 190;
  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.loadMaterialPropertiesAsync(context, material, babylonMaterial));
  36. promises.push(this._loadSpecularPropertiesAsync(extensionContext, extension, babylonMaterial));
  37. return Promise.all(promises).then(() => { });
  38. });
  39. }
  40. _loadSpecularPropertiesAsync(context, properties, babylonMaterial) {
  41. if (!(babylonMaterial instanceof PBRMaterial)) {
  42. throw new Error(`${context}: Material type not supported`);
  43. }
  44. const promises = new Array();
  45. if (properties.specularFactor !== undefined) {
  46. babylonMaterial.metallicF0Factor = properties.specularFactor;
  47. }
  48. if (properties.specularColorFactor !== undefined) {
  49. babylonMaterial.metallicReflectanceColor = Color3.FromArray(properties.specularColorFactor);
  50. }
  51. if (properties.specularTexture) {
  52. properties.specularTexture.nonColorData = true;
  53. promises.push(this._loader.loadTextureInfoAsync(`${context}/specularTexture`, properties.specularTexture, (texture) => {
  54. texture.name = `${babylonMaterial.name} (Specular F0 Strength)`;
  55. babylonMaterial.metallicReflectanceTexture = texture;
  56. babylonMaterial.useOnlyMetallicFromMetallicReflectanceTexture = true;
  57. }));
  58. }
  59. if (properties.specularColorTexture) {
  60. promises.push(this._loader.loadTextureInfoAsync(`${context}/specularColorTexture`, properties.specularColorTexture, (texture) => {
  61. texture.name = `${babylonMaterial.name} (Specular F0 Color)`;
  62. babylonMaterial.reflectanceTexture = texture;
  63. }));
  64. }
  65. return Promise.all(promises).then(() => { });
  66. }
  67. }
  68. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_specular(loader));
  69. //# sourceMappingURL=KHR_materials_specular.js.map