babylon.glTF2FileLoader.d.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. declare module BABYLON {
  2. enum GLTFLoaderCoordinateSystemMode {
  3. /**
  4. * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
  5. */
  6. AUTO = 0,
  7. /**
  8. * Sets the useRightHandedSystem flag on the scene.
  9. */
  10. FORCE_RIGHT_HANDED = 1,
  11. }
  12. enum GLTFLoaderAnimationStartMode {
  13. /**
  14. * No animation will start.
  15. */
  16. NONE = 0,
  17. /**
  18. * The first animation will start.
  19. */
  20. FIRST = 1,
  21. /**
  22. * All animations will start.
  23. */
  24. ALL = 2,
  25. }
  26. interface IGLTFLoaderData {
  27. json: Object;
  28. bin: Nullable<ArrayBufferView>;
  29. }
  30. interface IGLTFLoaderExtension {
  31. /**
  32. * The name of this extension.
  33. */
  34. readonly name: string;
  35. /**
  36. * Whether this extension is enabled.
  37. */
  38. enabled: boolean;
  39. }
  40. enum GLTFLoaderState {
  41. /**
  42. * The asset is loading.
  43. */
  44. LOADING = 0,
  45. /**
  46. * The asset is ready for rendering.
  47. */
  48. READY = 1,
  49. /**
  50. * The asset is completely loaded.
  51. */
  52. COMPLETE = 2,
  53. }
  54. interface IGLTFLoader extends IDisposable {
  55. coordinateSystemMode: GLTFLoaderCoordinateSystemMode;
  56. animationStartMode: GLTFLoaderAnimationStartMode;
  57. compileMaterials: boolean;
  58. useClipPlane: boolean;
  59. compileShadowGenerators: boolean;
  60. onMeshLoadedObservable: Observable<AbstractMesh>;
  61. onTextureLoadedObservable: Observable<BaseTexture>;
  62. onMaterialLoadedObservable: Observable<Material>;
  63. onCompleteObservable: Observable<IGLTFLoader>;
  64. onDisposeObservable: Observable<IGLTFLoader>;
  65. onExtensionLoadedObservable: Observable<IGLTFLoaderExtension>;
  66. state: Nullable<GLTFLoaderState>;
  67. importMeshAsync: (meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void) => Promise<{
  68. meshes: AbstractMesh[];
  69. particleSystems: ParticleSystem[];
  70. skeletons: Skeleton[];
  71. animationGroups: AnimationGroup[];
  72. }>;
  73. loadAsync: (scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void) => Promise<void>;
  74. }
  75. class GLTFFileLoader implements IDisposable, ISceneLoaderPluginAsync, ISceneLoaderPluginFactory {
  76. static CreateGLTFLoaderV1: () => IGLTFLoader;
  77. static CreateGLTFLoaderV2: () => IGLTFLoader;
  78. /**
  79. * Raised when the asset has been parsed.
  80. * The data.json property stores the glTF JSON.
  81. * The data.bin property stores the BIN chunk from a glTF binary or null if the input is not a glTF binary.
  82. */
  83. onParsedObservable: Observable<IGLTFLoaderData>;
  84. private _onParsedObserver;
  85. onParsed: (loaderData: IGLTFLoaderData) => void;
  86. static IncrementalLoading: boolean;
  87. static HomogeneousCoordinates: boolean;
  88. /**
  89. * The coordinate system mode (AUTO, FORCE_RIGHT_HANDED).
  90. */
  91. coordinateSystemMode: GLTFLoaderCoordinateSystemMode;
  92. /**
  93. * The animation start mode (NONE, FIRST, ALL).
  94. */
  95. animationStartMode: GLTFLoaderAnimationStartMode;
  96. /**
  97. * Set to true to compile materials before raising the success callback.
  98. */
  99. compileMaterials: boolean;
  100. /**
  101. * Set to true to also compile materials with clip planes.
  102. */
  103. useClipPlane: boolean;
  104. /**
  105. * Set to true to compile shadow generators before raising the success callback.
  106. */
  107. compileShadowGenerators: boolean;
  108. /**
  109. * Raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  110. */
  111. readonly onMeshLoadedObservable: Observable<AbstractMesh>;
  112. private _onMeshLoadedObserver;
  113. onMeshLoaded: (mesh: AbstractMesh) => void;
  114. /**
  115. * Raised when the loader creates a texture after parsing the glTF properties of the texture.
  116. */
  117. readonly onTextureLoadedObservable: Observable<BaseTexture>;
  118. private _onTextureLoadedObserver;
  119. onTextureLoaded: (texture: BaseTexture) => void;
  120. /**
  121. * Raised when the loader creates a material after parsing the glTF properties of the material.
  122. */
  123. readonly onMaterialLoadedObservable: Observable<Material>;
  124. private _onMaterialLoadedObserver;
  125. onMaterialLoaded: (material: Material) => void;
  126. /**
  127. * Raised when the asset is completely loaded, immediately before the loader is disposed.
  128. * For assets with LODs, raised when all of the LODs are complete.
  129. * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
  130. */
  131. readonly onCompleteObservable: Observable<GLTFFileLoader>;
  132. private _onCompleteObserver;
  133. onComplete: () => void;
  134. /**
  135. * Raised after the loader is disposed.
  136. */
  137. readonly onDisposeObservable: Observable<GLTFFileLoader>;
  138. private _onDisposeObserver;
  139. onDispose: () => void;
  140. /**
  141. * Raised after a loader extension is created.
  142. * Set additional options for a loader extension in this event.
  143. */
  144. readonly onExtensionLoadedObservable: Observable<IGLTFLoaderExtension>;
  145. private _onExtensionLoadedObserver;
  146. onExtensionLoaded: (extension: IGLTFLoaderExtension) => void;
  147. /**
  148. * Returns a promise that resolves when the asset is completely loaded.
  149. * @returns A promise that resolves when the asset is completely loaded.
  150. */
  151. whenCompleteAsync(): Promise<void>;
  152. /**
  153. * The loader state (LOADING, READY, COMPLETE) or null if the loader is not active.
  154. */
  155. readonly loaderState: Nullable<GLTFLoaderState>;
  156. private _loader;
  157. name: string;
  158. extensions: ISceneLoaderPluginExtensions;
  159. /**
  160. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  161. */
  162. dispose(): void;
  163. importMeshAsync(meshesNames: any, scene: Scene, data: any, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<{
  164. meshes: AbstractMesh[];
  165. particleSystems: ParticleSystem[];
  166. skeletons: Skeleton[];
  167. animationGroups: AnimationGroup[];
  168. }>;
  169. loadAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<void>;
  170. loadAssetContainerAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<AssetContainer>;
  171. canDirectLoad(data: string): boolean;
  172. rewriteRootURL: (rootUrl: string, responseURL?: string) => string;
  173. createPlugin(): ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  174. private _parse(data);
  175. private _getLoader(loaderData);
  176. private static _parseBinary(data);
  177. private static _parseV1(binaryReader);
  178. private static _parseV2(binaryReader);
  179. private static _parseVersion(version);
  180. private static _compareVersion(a, b);
  181. private static _decodeBufferToText(buffer);
  182. }
  183. }
  184. declare module BABYLON.GLTF2 {
  185. interface IArrayItem {
  186. _index: number;
  187. }
  188. class ArrayItem {
  189. static Assign(values?: IArrayItem[]): void;
  190. }
  191. }
  192. declare module BABYLON.GLTF2 {
  193. interface ILoaderAccessor extends IAccessor, IArrayItem {
  194. _data?: Promise<ArrayBufferView>;
  195. _babylonVertexBuffer?: Promise<VertexBuffer>;
  196. }
  197. interface ILoaderAnimationChannel extends IAnimationChannel, IArrayItem {
  198. }
  199. interface ILoaderAnimationSamplerData {
  200. input: Float32Array;
  201. interpolation: AnimationSamplerInterpolation;
  202. output: Float32Array;
  203. }
  204. interface ILoaderAnimationSampler extends IAnimationSampler, IArrayItem {
  205. _data: Promise<ILoaderAnimationSamplerData>;
  206. }
  207. interface ILoaderAnimation extends IAnimation, IArrayItem {
  208. channels: ILoaderAnimationChannel[];
  209. samplers: ILoaderAnimationSampler[];
  210. _babylonAnimationGroup?: AnimationGroup;
  211. }
  212. interface ILoaderBuffer extends IBuffer, IArrayItem {
  213. _data?: Promise<ArrayBufferView>;
  214. }
  215. interface ILoaderBufferView extends IBufferView, IArrayItem {
  216. _data?: Promise<ArrayBufferView>;
  217. _babylonBuffer?: Promise<Buffer>;
  218. }
  219. interface ILoaderCamera extends ICamera, IArrayItem {
  220. }
  221. interface ILoaderImage extends IImage, IArrayItem {
  222. _objectURL?: Promise<string>;
  223. }
  224. interface ILoaderMaterial extends IMaterial, IArrayItem {
  225. _babylonData?: {
  226. [drawMode: number]: {
  227. material: Material;
  228. meshes: AbstractMesh[];
  229. loaded: Promise<void>;
  230. };
  231. };
  232. }
  233. interface ILoaderMesh extends IMesh, IArrayItem {
  234. primitives: ILoaderMeshPrimitive[];
  235. }
  236. interface ILoaderMeshPrimitive extends IMeshPrimitive, IArrayItem {
  237. }
  238. interface ILoaderNode extends INode, IArrayItem {
  239. _parent: ILoaderNode;
  240. _babylonMesh?: Mesh;
  241. _primitiveBabylonMeshes?: Mesh[];
  242. _babylonAnimationTargets?: Node[];
  243. _numMorphTargets?: number;
  244. }
  245. interface ILoaderSamplerData {
  246. noMipMaps: boolean;
  247. samplingMode: number;
  248. wrapU: number;
  249. wrapV: number;
  250. }
  251. interface ILoaderSampler extends ISampler, IArrayItem {
  252. _data?: ILoaderSamplerData;
  253. }
  254. interface ILoaderScene extends IScene, IArrayItem {
  255. }
  256. interface ILoaderSkin extends ISkin, IArrayItem {
  257. _babylonSkeleton?: Skeleton;
  258. _loaded?: Promise<void>;
  259. }
  260. interface ILoaderTexture extends ITexture, IArrayItem {
  261. }
  262. interface ILoaderGLTF extends IGLTF {
  263. accessors?: ILoaderAccessor[];
  264. animations?: ILoaderAnimation[];
  265. buffers?: ILoaderBuffer[];
  266. bufferViews?: ILoaderBufferView[];
  267. cameras?: ILoaderCamera[];
  268. images?: ILoaderImage[];
  269. materials?: ILoaderMaterial[];
  270. meshes?: ILoaderMesh[];
  271. nodes?: ILoaderNode[];
  272. samplers?: ILoaderSampler[];
  273. scenes?: ILoaderScene[];
  274. skins?: ILoaderSkin[];
  275. textures?: ILoaderTexture[];
  276. }
  277. }
  278. declare module BABYLON.GLTF2 {
  279. interface MaterialConstructor<T extends Material> {
  280. readonly prototype: T;
  281. new (name: string, scene: Scene): T;
  282. }
  283. class GLTFLoader implements IGLTFLoader {
  284. _gltf: ILoaderGLTF;
  285. _babylonScene: Scene;
  286. _completePromises: Promise<void>[];
  287. private _disposed;
  288. private _state;
  289. private _extensions;
  290. private _rootUrl;
  291. private _rootBabylonMesh;
  292. private _defaultSampler;
  293. private _defaultBabylonMaterials;
  294. private _progressCallback?;
  295. private _requests;
  296. private static _Names;
  297. private static _Factories;
  298. static _Register(name: string, factory: (loader: GLTFLoader) => GLTFLoaderExtension): void;
  299. coordinateSystemMode: GLTFLoaderCoordinateSystemMode;
  300. animationStartMode: GLTFLoaderAnimationStartMode;
  301. compileMaterials: boolean;
  302. useClipPlane: boolean;
  303. compileShadowGenerators: boolean;
  304. readonly onDisposeObservable: Observable<IGLTFLoader>;
  305. readonly onMeshLoadedObservable: Observable<AbstractMesh>;
  306. readonly onTextureLoadedObservable: Observable<BaseTexture>;
  307. readonly onMaterialLoadedObservable: Observable<Material>;
  308. readonly onExtensionLoadedObservable: Observable<IGLTFLoaderExtension>;
  309. readonly onCompleteObservable: Observable<IGLTFLoader>;
  310. readonly state: Nullable<GLTFLoaderState>;
  311. dispose(): void;
  312. importMeshAsync(meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<{
  313. meshes: AbstractMesh[];
  314. particleSystems: ParticleSystem[];
  315. skeletons: Skeleton[];
  316. animationGroups: AnimationGroup[];
  317. }>;
  318. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: SceneLoaderProgressEvent) => void): Promise<void>;
  319. private _loadAsync(nodes, scene, data, rootUrl, onProgress?);
  320. private _loadExtensions();
  321. private _loadData(data);
  322. private _setupData();
  323. private _checkExtensions();
  324. private _createRootNode();
  325. private _loadNodesAsync(nodes);
  326. _loadSceneAsync(context: string, scene: ILoaderScene): Promise<void>;
  327. private _forEachPrimitive(node, callback);
  328. private _getMeshes();
  329. private _getSkeletons();
  330. private _getAnimationGroups();
  331. private _startAnimations();
  332. _loadNodeAsync(context: string, node: ILoaderNode): Promise<void>;
  333. private _loadMeshAsync(context, node, mesh, babylonMesh);
  334. private _loadPrimitiveAsync(context, node, mesh, primitive, babylonMesh);
  335. private _loadVertexDataAsync(context, primitive, babylonMesh);
  336. private _createMorphTargets(context, node, mesh, primitive, babylonMesh);
  337. private _loadMorphTargetsAsync(context, primitive, babylonMesh, babylonGeometry);
  338. private _loadMorphTargetVertexDataAsync(context, babylonGeometry, attributes, babylonMorphTarget);
  339. private static _LoadTransform(node, babylonNode);
  340. private _loadSkinAsync(context, node, mesh, skin);
  341. private _loadSkinInverseBindMatricesDataAsync(context, skin);
  342. private _createBone(node, skin, parent, localMatrix, baseMatrix, index);
  343. private _loadBones(context, skin, inverseBindMatricesData);
  344. private _loadBone(node, skin, inverseBindMatricesData, babylonBones);
  345. private _getNodeMatrix(node);
  346. private _loadAnimationsAsync();
  347. private _loadAnimationAsync(context, animation);
  348. private _loadAnimationChannelAsync(context, animationContext, animation, channel, babylonAnimationGroup);
  349. private _loadAnimationSamplerAsync(context, sampler);
  350. private _loadBufferAsync(context, buffer);
  351. _loadBufferViewAsync(context: string, bufferView: ILoaderBufferView): Promise<ArrayBufferView>;
  352. private _loadAccessorAsync(context, accessor);
  353. _loadVertexBufferViewAsync(context: string, bufferView: ILoaderBufferView, kind: string): Promise<Buffer>;
  354. private _loadVertexAccessorAsync(context, accessor, kind);
  355. private _getDefaultMaterial(drawMode);
  356. private _loadMaterialMetallicRoughnessPropertiesAsync(context, material, babylonMaterial);
  357. _loadMaterialAsync(context: string, material: ILoaderMaterial, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Promise<void>;
  358. _createMaterial<T extends Material>(type: MaterialConstructor<T>, name: string, drawMode: number): T;
  359. _loadMaterialBasePropertiesAsync(context: string, material: ILoaderMaterial, babylonMaterial: PBRMaterial): Promise<void>;
  360. _loadMaterialAlphaProperties(context: string, material: ILoaderMaterial, babylonMaterial: PBRMaterial): void;
  361. _loadTextureAsync(context: string, textureInfo: ITextureInfo, assign: (texture: Texture) => void): Promise<void>;
  362. private _loadSampler(context, sampler);
  363. private _loadImageAsync(context, image);
  364. _loadUriAsync(context: string, uri: string): Promise<ArrayBufferView>;
  365. private _onProgress();
  366. static _GetProperty<T>(context: string, array: ArrayLike<T> | undefined, index: number | undefined): T;
  367. private static _GetTextureWrapMode(context, mode);
  368. private static _GetTextureSamplingMode(context, magFilter?, minFilter?);
  369. private static _GetNumComponents(context, type);
  370. private static _ValidateUri(uri);
  371. private static _GetDrawMode(context, mode);
  372. private _compileMaterialsAsync();
  373. private _compileShadowGeneratorsAsync();
  374. private _clear();
  375. _applyExtensions<T>(actionAsync: (extension: GLTFLoaderExtension) => Nullable<Promise<T>>): Nullable<Promise<T>>;
  376. }
  377. }
  378. declare module BABYLON.GLTF2 {
  379. abstract class GLTFLoaderExtension implements IGLTFLoaderExtension, IDisposable {
  380. enabled: boolean;
  381. readonly abstract name: string;
  382. protected _loader: GLTFLoader;
  383. constructor(loader: GLTFLoader);
  384. dispose(): void;
  385. /** Override this method to modify the default behavior for loading scenes. */
  386. protected _loadSceneAsync(context: string, node: ILoaderScene): Nullable<Promise<void>>;
  387. /** Override this method to modify the default behavior for loading nodes. */
  388. protected _loadNodeAsync(context: string, node: ILoaderNode): Nullable<Promise<void>>;
  389. /** Override this method to modify the default behavior for loading mesh primitive vertex data. */
  390. protected _loadVertexDataAsync(context: string, primitive: ILoaderMeshPrimitive, babylonMesh: Mesh): Nullable<Promise<Geometry>>;
  391. /** Override this method to modify the default behavior for loading materials. */
  392. protected _loadMaterialAsync(context: string, material: ILoaderMaterial, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  393. /** Override this method to modify the default behavior for loading uris. */
  394. protected _loadUriAsync(context: string, uri: string): Nullable<Promise<ArrayBufferView>>;
  395. /** Helper method called by a loader extension to load an glTF extension. */
  396. protected _loadExtensionAsync<TProperty, TResult = void>(context: string, property: IProperty, actionAsync: (extensionContext: string, extension: TProperty) => Promise<TResult>): Nullable<Promise<TResult>>;
  397. /** Helper method called by the loader to allow extensions to override loading scenes. */
  398. static _LoadSceneAsync(loader: GLTFLoader, context: string, scene: ILoaderScene): Nullable<Promise<void>>;
  399. /** Helper method called by the loader to allow extensions to override loading nodes. */
  400. static _LoadNodeAsync(loader: GLTFLoader, context: string, node: ILoaderNode): Nullable<Promise<void>>;
  401. /** Helper method called by the loader to allow extensions to override loading mesh primitive vertex data. */
  402. static _LoadVertexDataAsync(loader: GLTFLoader, context: string, primitive: ILoaderMeshPrimitive, babylonMesh: Mesh): Nullable<Promise<Geometry>>;
  403. /** Helper method called by the loader to allow extensions to override loading materials. */
  404. static _LoadMaterialAsync(loader: GLTFLoader, context: string, material: ILoaderMaterial, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  405. /** Helper method called by the loader to allow extensions to override loading uris. */
  406. static _LoadUriAsync(loader: GLTFLoader, context: string, uri: string): Nullable<Promise<ArrayBufferView>>;
  407. }
  408. }
  409. declare module BABYLON.GLTF2.Extensions {
  410. class MSFT_lod extends GLTFLoaderExtension {
  411. readonly name: string;
  412. /**
  413. * Maximum number of LODs to load, starting from the lowest LOD.
  414. */
  415. maxLODsToLoad: number;
  416. private _loadingNodeLOD;
  417. private _loadNodeSignals;
  418. private _loadingMaterialLOD;
  419. private _loadMaterialSignals;
  420. protected _loadNodeAsync(context: string, node: ILoaderNode): Nullable<Promise<void>>;
  421. protected _loadMaterialAsync(context: string, material: ILoaderMaterial, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  422. protected _loadUriAsync(context: string, uri: string): Nullable<Promise<ArrayBufferView>>;
  423. /**
  424. * Gets an array of LOD properties from lowest to highest.
  425. */
  426. private _getLODs<T>(context, property, array, ids);
  427. }
  428. }
  429. declare module BABYLON.GLTF2.Extensions {
  430. class KHR_draco_mesh_compression extends GLTFLoaderExtension {
  431. readonly name: string;
  432. private _dracoCompression;
  433. constructor(loader: GLTFLoader);
  434. dispose(): void;
  435. protected _loadVertexDataAsync(context: string, primitive: ILoaderMeshPrimitive, babylonMesh: Mesh): Nullable<Promise<Geometry>>;
  436. }
  437. }
  438. declare module BABYLON.GLTF2.Extensions {
  439. class KHR_materials_pbrSpecularGlossiness extends GLTFLoaderExtension {
  440. readonly name: string;
  441. protected _loadMaterialAsync(context: string, material: ILoaderMaterial, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  442. private _loadSpecularGlossinessPropertiesAsync(context, material, properties, babylonMaterial);
  443. }
  444. }
  445. declare module BABYLON.GLTF2.Extensions {
  446. class KHR_materials_unlit extends GLTFLoaderExtension {
  447. readonly name: string;
  448. protected _loadMaterialAsync(context: string, material: ILoaderMaterial, babylonMesh: Mesh, babylonDrawMode: number, assign: (babylonMaterial: Material) => void): Nullable<Promise<void>>;
  449. private _loadUnlitPropertiesAsync(context, material, babylonMaterial);
  450. }
  451. }
  452. declare module BABYLON.GLTF2.Extensions {
  453. class KHR_lights extends GLTFLoaderExtension {
  454. readonly name: string;
  455. protected _loadSceneAsync(context: string, scene: ILoaderScene): Nullable<Promise<void>>;
  456. protected _loadNodeAsync(context: string, node: ILoaderNode): Nullable<Promise<void>>;
  457. private readonly _lights;
  458. }
  459. }