MSFT_lod.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GLTF2.Extensions {
  3. // https://github.com/sbtron/glTF/tree/MSFT_lod/extensions/Vendor/MSFT_lod
  4. const NAME = "MSFT_lod";
  5. interface IMSFTLOD {
  6. ids: number[];
  7. }
  8. export class MSFT_lod extends GLTFLoaderExtension {
  9. public readonly name = NAME;
  10. /**
  11. * Maximum number of LODs to load, starting from the lowest LOD.
  12. */
  13. public maxLODsToLoad = Number.MAX_VALUE;
  14. private _loadingNodeLOD: Nullable<ILoaderNode> = null;
  15. private _loadNodeSignals: { [nodeIndex: number]: Deferred<void> } = {};
  16. private _loadingMaterialLOD: Nullable<ILoaderMaterial> = null;
  17. private _loadMaterialSignals: { [materialIndex: number]: Deferred<void> } = {};
  18. protected _loadNodeAsync(context: string, node: ILoaderNode): Nullable<Promise<void>> {
  19. return this._loadExtensionAsync<IMSFTLOD>(context, node, (context, extension) => {
  20. let firstPromise: Promise<void>;
  21. const nodeLODs = this._getLODs(context, node, this._loader._gltf.nodes, extension.ids);
  22. for (let indexLOD = 0; indexLOD < nodeLODs.length; indexLOD++) {
  23. const nodeLOD = nodeLODs[indexLOD];
  24. if (indexLOD !== 0) {
  25. this._loadingNodeLOD = nodeLOD;
  26. if (!this._loadNodeSignals[nodeLOD._index]) {
  27. this._loadNodeSignals[nodeLOD._index] = new Deferred<void>();
  28. }
  29. }
  30. const promise = this._loader._loadNodeAsync(`#/nodes/${nodeLOD._index}`, nodeLOD).then(() => {
  31. if (indexLOD !== 0) {
  32. const previousNodeLOD = nodeLODs[indexLOD - 1];
  33. if (previousNodeLOD._babylonMesh) {
  34. previousNodeLOD._babylonMesh.dispose();
  35. delete previousNodeLOD._babylonMesh;
  36. }
  37. }
  38. if (indexLOD !== nodeLODs.length - 1) {
  39. const nodeIndex = nodeLODs[indexLOD + 1]._index;
  40. if (this._loadNodeSignals[nodeIndex]) {
  41. this._loadNodeSignals[nodeIndex].resolve();
  42. delete this._loadNodeSignals[nodeIndex];
  43. }
  44. }
  45. });
  46. if (indexLOD === 0) {
  47. firstPromise = promise;
  48. }
  49. else {
  50. this._loader._completePromises.push(promise);
  51. this._loadingNodeLOD = null;
  52. }
  53. }
  54. return firstPromise!;
  55. });
  56. }
  57. protected _loadMaterialAsync(context: string, material: ILoaderMaterial, babylonMesh: Mesh, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>> {
  58. // Don't load material LODs if already loading a node LOD.
  59. if (this._loadingNodeLOD) {
  60. return null;
  61. }
  62. return this._loadExtensionAsync<IMSFTLOD>(context, material, (context, extension) => {
  63. let firstPromise: Promise<void>;
  64. const materialLODs = this._getLODs(context, material, this._loader._gltf.materials, extension.ids);
  65. for (let indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  66. const materialLOD = materialLODs[indexLOD];
  67. if (indexLOD !== 0) {
  68. this._loadingMaterialLOD = materialLOD;
  69. if (!this._loadMaterialSignals[materialLOD._index]) {
  70. this._loadMaterialSignals[materialLOD._index] = new Deferred<void>();
  71. }
  72. }
  73. const promise = this._loader._loadMaterialAsync(`#/materials/${materialLOD._index}`, materialLOD, babylonMesh, indexLOD === 0 ? assign : () => {}).then(() => {
  74. if (indexLOD !== 0) {
  75. assign(materialLOD._babylonMaterial!);
  76. const previousMaterialLOD = materialLODs[indexLOD - 1];
  77. if (previousMaterialLOD._babylonMaterial) {
  78. previousMaterialLOD._babylonMaterial.dispose();
  79. delete previousMaterialLOD._babylonMaterial;
  80. }
  81. }
  82. if (indexLOD !== materialLODs.length - 1) {
  83. const materialIndex = materialLODs[indexLOD + 1]._index;
  84. if (this._loadMaterialSignals[materialIndex]) {
  85. this._loadMaterialSignals[materialIndex].resolve();
  86. delete this._loadMaterialSignals[materialIndex];
  87. }
  88. }
  89. });
  90. if (indexLOD === 0) {
  91. firstPromise = promise;
  92. }
  93. else {
  94. this._loader._completePromises.push(promise);
  95. this._loadingMaterialLOD = null;
  96. }
  97. }
  98. return firstPromise!;
  99. });
  100. }
  101. protected _loadUriAsync(context: string, uri: string): Nullable<Promise<ArrayBufferView>> {
  102. // Defer the loading of uris if loading a material or node LOD.
  103. if (this._loadingMaterialLOD) {
  104. const index = this._loadingMaterialLOD._index;
  105. return this._loadMaterialSignals[index].promise.then(() => {
  106. return this._loader._loadUriAsync(context, uri);
  107. });
  108. }
  109. else if (this._loadingNodeLOD) {
  110. const index = this._loadingNodeLOD._index;
  111. return this._loadNodeSignals[index].promise.then(() => {
  112. return this._loader._loadUriAsync(context, uri);
  113. });
  114. }
  115. return null;
  116. }
  117. /**
  118. * Gets an array of LOD properties from lowest to highest.
  119. */
  120. private _getLODs<T>(context: string, property: T, array: ArrayLike<T> | undefined, ids: number[]): T[] {
  121. if (this.maxLODsToLoad <= 0) {
  122. throw new Error("maxLODsToLoad must be greater than zero");
  123. }
  124. const properties = new Array<T>();
  125. for (let i = ids.length - 1; i >= 0; i--) {
  126. properties.push(GLTFLoader._GetProperty(`${context}/ids/${ids[i]}`, array, ids[i]));
  127. if (properties.length === this.maxLODsToLoad) {
  128. return properties;
  129. }
  130. }
  131. properties.push(property);
  132. return properties;
  133. }
  134. }
  135. GLTFLoader._Register(NAME, loader => new MSFT_lod(loader));
  136. }