MSFT_lod.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. previousNodeLOD._babylonMesh!.setEnabled(false);
  34. }
  35. if (indexLOD !== nodeLODs.length - 1) {
  36. const nodeIndex = nodeLODs[indexLOD + 1]._index;
  37. if (this._loadNodeSignals[nodeIndex]) {
  38. this._loadNodeSignals[nodeIndex].resolve();
  39. delete this._loadNodeSignals[nodeIndex];
  40. }
  41. }
  42. });
  43. if (indexLOD === 0) {
  44. firstPromise = promise;
  45. }
  46. else {
  47. this._loader._completePromises.push(promise);
  48. this._loadingNodeLOD = null;
  49. }
  50. }
  51. return firstPromise!;
  52. });
  53. }
  54. protected _loadMaterialAsync(context: string, material: ILoaderMaterial, babylonMesh: Mesh, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>> {
  55. // Don't load material LODs if already loading a node LOD.
  56. if (this._loadingNodeLOD) {
  57. return null;
  58. }
  59. return this._loadExtensionAsync<IMSFTLOD>(context, material, (context, extension) => {
  60. let firstPromise: Promise<void>;
  61. const materialLODs = this._getLODs(context, material, this._loader._gltf.materials, extension.ids);
  62. for (let indexLOD = 0; indexLOD < materialLODs.length; indexLOD++) {
  63. const materialLOD = materialLODs[indexLOD];
  64. if (indexLOD !== 0) {
  65. this._loadingMaterialLOD = materialLOD;
  66. if (!this._loadMaterialSignals[materialLOD._index]) {
  67. this._loadMaterialSignals[materialLOD._index] = new Deferred<void>();
  68. }
  69. }
  70. const promise = this._loader._loadMaterialAsync("#/materials/" + materialLOD._index, materialLOD, babylonMesh, indexLOD === 0 ? assign : () => {}).then(() => {
  71. if (indexLOD !== 0) {
  72. assign(materialLOD._babylonMaterial!);
  73. }
  74. if (indexLOD !== materialLODs.length - 1) {
  75. const materialIndex = materialLODs[indexLOD + 1]._index;
  76. if (this._loadMaterialSignals[materialIndex]) {
  77. this._loadMaterialSignals[materialIndex].resolve();
  78. delete this._loadMaterialSignals[materialIndex];
  79. }
  80. }
  81. });
  82. if (indexLOD === 0) {
  83. firstPromise = promise;
  84. }
  85. else {
  86. this._loader._completePromises.push(promise);
  87. this._loadingMaterialLOD = null;
  88. }
  89. }
  90. return firstPromise!;
  91. });
  92. }
  93. protected _loadUriAsync(context: string, uri: string): Nullable<Promise<ArrayBufferView>> {
  94. // Defer the loading of uris if loading a material or node LOD.
  95. if (this._loadingMaterialLOD) {
  96. const index = this._loadingMaterialLOD._index;
  97. return this._loadMaterialSignals[index].promise.then(() => {
  98. return this._loader._loadUriAsync(context, uri);
  99. });
  100. }
  101. else if (this._loadingNodeLOD) {
  102. const index = this._loadingNodeLOD._index;
  103. return this._loadNodeSignals[index].promise.then(() => {
  104. return this._loader._loadUriAsync(context, uri);
  105. });
  106. }
  107. return null;
  108. }
  109. /**
  110. * Gets an array of LOD properties from lowest to highest.
  111. */
  112. private _getLODs<T>(context: string, property: T, array: ArrayLike<T> | undefined, ids: number[]): T[] {
  113. if (this.maxLODsToLoad <= 0) {
  114. throw new Error("maxLODsToLoad must be greater than zero");
  115. }
  116. const properties = new Array<T>();
  117. for (let i = ids.length - 1; i >= 0; i--) {
  118. properties.push(GLTFLoader._GetProperty(context + "/ids/" + ids[i], array, ids[i]));
  119. if (properties.length === this.maxLODsToLoad) {
  120. return properties;
  121. }
  122. }
  123. properties.push(property);
  124. return properties;
  125. }
  126. }
  127. GLTFLoader._Register(NAME, loader => new MSFT_lod(loader));
  128. }