MSFT_lod.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GLTF2.Extensions {
  3. interface IMSFTLOD {
  4. ids: number[];
  5. }
  6. export class MSFTLOD extends GLTFLoaderExtension {
  7. public get name() {
  8. return "MSFT_lod";
  9. }
  10. protected loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (material: Material) => void): boolean {
  11. if (!material.extensions) {
  12. return false;
  13. }
  14. var properties = material.extensions[this.name] as IMSFTLOD;
  15. if (!properties) {
  16. return false;
  17. }
  18. // Clear out the extension so that it won't get loaded again.
  19. material.extensions[this.name] = undefined;
  20. // Tell the loader not to clear its state until the highest LOD is loaded.
  21. loader.addLoaderPendingData(material);
  22. // Start with the lowest quality LOD.
  23. var materialLODs = [material.index, ...properties.ids];
  24. this.loadMaterialLOD(loader, material, materialLODs, materialLODs.length - 1, assign);
  25. return true;
  26. }
  27. private loadMaterialLOD(loader: GLTFLoader, material: IGLTFMaterial, materialLODs: number[], lod: number, assign: (material: Material) => void): void {
  28. loader.loadMaterial(materialLODs[lod], babylonMaterial => {
  29. assign(babylonMaterial);
  30. // Loading is complete if this is the highest quality LOD.
  31. if (lod === 0) {
  32. loader.removeLoaderPendingData(material);
  33. return;
  34. }
  35. // Load the next LOD when the loader is ready to render and
  36. // all active material textures of the current LOD are loaded.
  37. loader.executeWhenRenderReady(() => {
  38. BaseTexture.WhenAllReady(babylonMaterial.getActiveTextures(), () => {
  39. this.loadMaterialLOD(loader, material, materialLODs, lod - 1, assign);
  40. });
  41. });
  42. });
  43. }
  44. }
  45. GLTFLoader.RegisterExtension(new MSFTLOD());
  46. }