babylon.glTF2FileLoader.d.ts 21 KB

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