KHR_texture_basisu.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { IGLTFLoaderExtension } from "../glTFLoaderExtension";
  2. import { GLTFLoader, ArrayItem } from "../glTFLoader";
  3. import { ITexture } from "../glTFLoaderInterfaces";
  4. import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
  5. import { Nullable } from "babylonjs/types";
  6. import { IKHRTextureBasisU } from 'babylonjs-gltf2interface';
  7. const NAME = "KHR_texture_basisu";
  8. /**
  9. * [Proposed Specification](https://github.com/KhronosGroup/glTF/pull/1751)
  10. * !!! Experimental Extension Subject to Changes !!!
  11. */
  12. export class KHR_texture_basisu implements IGLTFLoaderExtension {
  13. /** The name of this extension. */
  14. public readonly name = NAME;
  15. /** Defines whether this extension is enabled. */
  16. public enabled: boolean;
  17. private _loader: GLTFLoader;
  18. /** @hidden */
  19. constructor(loader: GLTFLoader) {
  20. this._loader = loader;
  21. this.enabled = loader.isExtensionUsed(NAME);
  22. }
  23. /** @hidden */
  24. public dispose() {
  25. (this._loader as any) = null;
  26. }
  27. /** @hidden */
  28. public _loadTextureAsync(context: string, texture: ITexture, assign: (babylonTexture: BaseTexture) => void, isColorData = true): Nullable<Promise<BaseTexture>> {
  29. return GLTFLoader.LoadExtensionAsync<IKHRTextureBasisU, BaseTexture>(context, texture, this.name, (extensionContext, extension) => {
  30. const sampler = (texture.sampler == undefined ? GLTFLoader.DefaultSampler : ArrayItem.Get(`${context}/sampler`, this._loader.gltf.samplers, texture.sampler));
  31. const image = ArrayItem.Get(`${extensionContext}/source`, this._loader.gltf.images, extension.source);
  32. return this._loader._createTextureAsync(context, sampler, image, (babylonTexture) => {
  33. assign(babylonTexture);
  34. }, isColorData ? undefined : { useRGBAIfASTCBC7NotAvailableWhenUASTC: true });
  35. });
  36. }
  37. }
  38. GLTFLoader.RegisterExtension(NAME, (loader) => new KHR_texture_basisu(loader));