KHR_materials_anisotropy.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
  2. import { GLTFLoader } from "../glTFLoader.js";
  3. const NAME = "KHR_materials_anisotropy";
  4. /**
  5. * [Specification](https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_anisotropy)
  6. */
  7. // eslint-disable-next-line @typescript-eslint/naming-convention
  8. export class KHR_materials_anisotropy {
  9. /**
  10. * @internal
  11. */
  12. constructor(loader) {
  13. /**
  14. * The name of this extension.
  15. */
  16. this.name = NAME;
  17. /**
  18. * Defines a number that determines the order the extensions are applied.
  19. */
  20. this.order = 195;
  21. this._loader = loader;
  22. this.enabled = this._loader.isExtensionUsed(NAME);
  23. }
  24. /** @internal */
  25. dispose() {
  26. this._loader = null;
  27. }
  28. /**
  29. * @internal
  30. */
  31. loadMaterialPropertiesAsync(context, material, babylonMaterial) {
  32. return GLTFLoader.LoadExtensionAsync(context, material, this.name, (extensionContext, extension) => {
  33. const promises = new Array();
  34. promises.push(this._loader.loadMaterialPropertiesAsync(context, material, babylonMaterial));
  35. promises.push(this._loadIridescencePropertiesAsync(extensionContext, extension, babylonMaterial));
  36. return Promise.all(promises).then(() => { });
  37. });
  38. }
  39. _loadIridescencePropertiesAsync(context, properties, babylonMaterial) {
  40. if (!(babylonMaterial instanceof PBRMaterial)) {
  41. throw new Error(`${context}: Material type not supported`);
  42. }
  43. const promises = new Array();
  44. babylonMaterial.anisotropy.isEnabled = true;
  45. babylonMaterial.anisotropy.intensity = properties.anisotropyStrength ?? 0;
  46. babylonMaterial.anisotropy.angle = properties.anisotropyRotation ?? 0;
  47. if (properties.anisotropyTexture) {
  48. promises.push(this._loader.loadTextureInfoAsync(`${context}/anisotropyTexture`, properties.anisotropyTexture, (texture) => {
  49. texture.name = `${babylonMaterial.name} (Anisotropy Intensity)`;
  50. babylonMaterial.anisotropy.texture = texture;
  51. }));
  52. }
  53. return Promise.all(promises).then(() => { });
  54. }
  55. }
  56. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_anisotropy(loader));
  57. //# sourceMappingURL=KHR_materials_anisotropy.js.map