KHR_materials_unlit.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_unlit";
  5. /**
  6. * [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_unlit/README.md)
  7. */
  8. // eslint-disable-next-line @typescript-eslint/naming-convention
  9. export class KHR_materials_unlit {
  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 = 210;
  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, () => {
  34. return this._loadUnlitPropertiesAsync(context, material, babylonMaterial);
  35. });
  36. }
  37. _loadUnlitPropertiesAsync(context, material, babylonMaterial) {
  38. if (!(babylonMaterial instanceof PBRMaterial)) {
  39. throw new Error(`${context}: Material type not supported`);
  40. }
  41. const promises = new Array();
  42. babylonMaterial.unlit = true;
  43. const properties = material.pbrMetallicRoughness;
  44. if (properties) {
  45. if (properties.baseColorFactor) {
  46. babylonMaterial.albedoColor = Color3.FromArray(properties.baseColorFactor);
  47. babylonMaterial.alpha = properties.baseColorFactor[3];
  48. }
  49. else {
  50. babylonMaterial.albedoColor = Color3.White();
  51. }
  52. if (properties.baseColorTexture) {
  53. promises.push(this._loader.loadTextureInfoAsync(`${context}/baseColorTexture`, properties.baseColorTexture, (texture) => {
  54. texture.name = `${babylonMaterial.name} (Base Color)`;
  55. babylonMaterial.albedoTexture = texture;
  56. }));
  57. }
  58. }
  59. if (material.doubleSided) {
  60. babylonMaterial.backFaceCulling = false;
  61. babylonMaterial.twoSidedLighting = true;
  62. }
  63. this._loader.loadMaterialAlphaProperties(context, material, babylonMaterial);
  64. return Promise.all(promises).then(() => { });
  65. }
  66. }
  67. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_unlit(loader));
  68. //# sourceMappingURL=KHR_materials_unlit.js.map