KHR_materials_iridescence.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
  2. import { GLTFLoader } from "../glTFLoader.js";
  3. const NAME = "KHR_materials_iridescence";
  4. /**
  5. * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_iridescence/README.md)
  6. */
  7. // eslint-disable-next-line @typescript-eslint/naming-convention
  8. export class KHR_materials_iridescence {
  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.iridescence.isEnabled = true;
  45. babylonMaterial.iridescence.intensity = properties.iridescenceFactor ?? 0;
  46. babylonMaterial.iridescence.indexOfRefraction = properties.iridescenceIor ?? properties.iridescenceIOR ?? 1.3;
  47. babylonMaterial.iridescence.minimumThickness = properties.iridescenceThicknessMinimum ?? 100;
  48. babylonMaterial.iridescence.maximumThickness = properties.iridescenceThicknessMaximum ?? 400;
  49. if (properties.iridescenceTexture) {
  50. promises.push(this._loader.loadTextureInfoAsync(`${context}/iridescenceTexture`, properties.iridescenceTexture, (texture) => {
  51. texture.name = `${babylonMaterial.name} (Iridescence Intensity)`;
  52. babylonMaterial.iridescence.texture = texture;
  53. }));
  54. }
  55. if (properties.iridescenceThicknessTexture) {
  56. promises.push(this._loader.loadTextureInfoAsync(`${context}/iridescenceThicknessTexture`, properties.iridescenceThicknessTexture, (texture) => {
  57. texture.name = `${babylonMaterial.name} (Iridescence Thickness)`;
  58. babylonMaterial.iridescence.thicknessTexture = texture;
  59. }));
  60. }
  61. return Promise.all(promises).then(() => { });
  62. }
  63. }
  64. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_iridescence(loader));
  65. //# sourceMappingURL=KHR_materials_iridescence.js.map