glTFLoader.d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import type { IndicesArray, Nullable } from "@babylonjs/core/types.js";
  2. import { Camera } from "@babylonjs/core/Cameras/camera.js";
  3. import type { Animation } from "@babylonjs/core/Animations/animation.js";
  4. import type { IAnimatable } from "@babylonjs/core/Animations/animatable.interface.js";
  5. import { AnimationGroup } from "@babylonjs/core/Animations/animationGroup.js";
  6. import { Material } from "@babylonjs/core/Materials/material.js";
  7. import type { BaseTexture } from "@babylonjs/core/Materials/Textures/baseTexture.js";
  8. import { TransformNode } from "@babylonjs/core/Meshes/transformNode.js";
  9. import { Buffer, VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
  10. import { AbstractMesh } from "@babylonjs/core/Meshes/abstractMesh.js";
  11. import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
  12. import type { ISceneLoaderAsyncResult, ISceneLoaderProgressEvent } from "@babylonjs/core/Loading/sceneLoader.js";
  13. import type { Scene } from "@babylonjs/core/scene.js";
  14. import type { IProperty } from "babylonjs-gltf2interface";
  15. import type { IGLTF, ISampler, INode, IScene, IMesh, IAccessor, ICamera, IAnimation, IBuffer, IBufferView, IMaterial, ITextureInfo, ITexture, IImage, IMeshPrimitive, IArrayItem, IAnimationChannel } from "./glTFLoaderInterfaces";
  16. import type { IGLTFLoaderExtension } from "./glTFLoaderExtension";
  17. import type { IGLTFLoader, IGLTFLoaderData } from "../glTFFileLoader";
  18. import { GLTFFileLoader } from "../glTFFileLoader";
  19. import type { IDataBuffer } from "@babylonjs/core/Misc/dataReader.js";
  20. import type { Light } from "@babylonjs/core/Lights/light.js";
  21. import type { AssetContainer } from "@babylonjs/core/assetContainer.js";
  22. import type { AnimationPropertyInfo } from "./glTFLoaderAnimation";
  23. import type { IObjectInfo } from "@babylonjs/core/ObjectModel/objectModelInterfaces.js";
  24. interface IWithMetadata {
  25. metadata: any;
  26. _internalMetadata: any;
  27. }
  28. /**
  29. * Helper class for working with arrays when loading the glTF asset
  30. */
  31. export declare class ArrayItem {
  32. /**
  33. * Gets an item from the given array.
  34. * @param context The context when loading the asset
  35. * @param array The array to get the item from
  36. * @param index The index to the array
  37. * @returns The array item
  38. */
  39. static Get<T>(context: string, array: ArrayLike<T> | undefined, index: number | undefined): T;
  40. /**
  41. * Gets an item from the given array or returns null if not available.
  42. * @param array The array to get the item from
  43. * @param index The index to the array
  44. * @returns The array item or null
  45. */
  46. static TryGet<T>(array: ArrayLike<T> | undefined, index: number | undefined): Nullable<T>;
  47. /**
  48. * Assign an `index` field to each item of the given array.
  49. * @param array The array of items
  50. */
  51. static Assign(array?: IArrayItem[]): void;
  52. }
  53. /** @internal */
  54. export interface IAnimationTargetInfo {
  55. /** @internal */
  56. target: any;
  57. /** @internal */
  58. properties: Array<AnimationPropertyInfo>;
  59. }
  60. /**
  61. * The glTF 2.0 loader
  62. */
  63. export declare class GLTFLoader implements IGLTFLoader {
  64. /** @internal */
  65. readonly _completePromises: Promise<any>[];
  66. /** @internal */
  67. _assetContainer: Nullable<AssetContainer>;
  68. /** Storage */
  69. _babylonLights: Light[];
  70. /** @internal */
  71. _disableInstancedMesh: number;
  72. /** @internal */
  73. _allMaterialsDirtyRequired: boolean;
  74. private readonly _parent;
  75. private readonly _extensions;
  76. private _disposed;
  77. private _rootUrl;
  78. private _fileName;
  79. private _uniqueRootUrl;
  80. private _gltf;
  81. private _bin;
  82. private _babylonScene;
  83. private _rootBabylonMesh;
  84. private _defaultBabylonMaterialData;
  85. private readonly _postSceneLoadActions;
  86. private static _RegisteredExtensions;
  87. /**
  88. * The default glTF sampler.
  89. */
  90. static readonly DefaultSampler: ISampler;
  91. /**
  92. * Registers a loader extension.
  93. * @param name The name of the loader extension.
  94. * @param factory The factory function that creates the loader extension.
  95. */
  96. static RegisterExtension(name: string, factory: (loader: GLTFLoader) => IGLTFLoaderExtension): void;
  97. /**
  98. * Unregisters a loader extension.
  99. * @param name The name of the loader extension.
  100. * @returns A boolean indicating whether the extension has been unregistered
  101. */
  102. static UnregisterExtension(name: string): boolean;
  103. /**
  104. * The object that represents the glTF JSON.
  105. */
  106. get gltf(): IGLTF;
  107. /**
  108. * The BIN chunk of a binary glTF.
  109. */
  110. get bin(): Nullable<IDataBuffer>;
  111. /**
  112. * The parent file loader.
  113. */
  114. get parent(): GLTFFileLoader;
  115. /**
  116. * The Babylon scene when loading the asset.
  117. */
  118. get babylonScene(): Scene;
  119. /**
  120. * The root Babylon node when loading the asset.
  121. */
  122. get rootBabylonMesh(): Nullable<TransformNode>;
  123. /**
  124. * The root url when loading the asset.
  125. */
  126. get rootUrl(): Nullable<string>;
  127. /**
  128. * @internal
  129. */
  130. constructor(parent: GLTFFileLoader);
  131. /** @internal */
  132. dispose(): void;
  133. /**
  134. * @internal
  135. */
  136. importMeshAsync(meshesNames: any, scene: Scene, container: Nullable<AssetContainer>, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: ISceneLoaderProgressEvent) => void, fileName?: string): Promise<ISceneLoaderAsyncResult>;
  137. /**
  138. * @internal
  139. */
  140. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onProgress?: (event: ISceneLoaderProgressEvent) => void, fileName?: string): Promise<void>;
  141. private _loadAsync;
  142. private _loadData;
  143. private _setupData;
  144. private _loadExtensions;
  145. private _checkExtensions;
  146. private _createRootNode;
  147. /**
  148. * Loads a glTF scene.
  149. * @param context The context when loading the asset
  150. * @param scene The glTF scene property
  151. * @returns A promise that resolves when the load is complete
  152. */
  153. loadSceneAsync(context: string, scene: IScene): Promise<void>;
  154. private _forEachPrimitive;
  155. private _getGeometries;
  156. private _getMeshes;
  157. private _getTransformNodes;
  158. private _getSkeletons;
  159. private _getAnimationGroups;
  160. private _startAnimations;
  161. /**
  162. * Loads a glTF node.
  163. * @param context The context when loading the asset
  164. * @param node The glTF node property
  165. * @param assign A function called synchronously after parsing the glTF properties
  166. * @returns A promise that resolves with the loaded Babylon mesh when the load is complete
  167. */
  168. loadNodeAsync(context: string, node: INode, assign?: (babylonTransformNode: TransformNode) => void): Promise<TransformNode>;
  169. private _loadMeshAsync;
  170. /**
  171. * @internal Define this method to modify the default behavior when loading data for mesh primitives.
  172. * @param context The context when loading the asset
  173. * @param name The mesh name when loading the asset
  174. * @param node The glTF node when loading the asset
  175. * @param mesh The glTF mesh when loading the asset
  176. * @param primitive The glTF mesh primitive property
  177. * @param assign A function called synchronously after parsing the glTF properties
  178. * @returns A promise that resolves with the loaded mesh when the load is complete or null if not handled
  179. */
  180. _loadMeshPrimitiveAsync(context: string, name: string, node: INode, mesh: IMesh, primitive: IMeshPrimitive, assign: (babylonMesh: AbstractMesh) => void): Promise<AbstractMesh>;
  181. private _loadVertexDataAsync;
  182. private _createMorphTargets;
  183. private _loadMorphTargetsAsync;
  184. private _loadMorphTargetVertexDataAsync;
  185. private static _LoadTransform;
  186. private _loadSkinAsync;
  187. private _loadBones;
  188. private _findSkeletonRootNode;
  189. private _loadBone;
  190. private _loadSkinInverseBindMatricesDataAsync;
  191. private _updateBoneMatrices;
  192. private _getNodeMatrix;
  193. /**
  194. * Loads a glTF camera.
  195. * @param context The context when loading the asset
  196. * @param camera The glTF camera property
  197. * @param assign A function called synchronously after parsing the glTF properties
  198. * @returns A promise that resolves with the loaded Babylon camera when the load is complete
  199. */
  200. loadCameraAsync(context: string, camera: ICamera, assign?: (babylonCamera: Camera) => void): Promise<Camera>;
  201. private _loadAnimationsAsync;
  202. /**
  203. * Loads a glTF animation.
  204. * @param context The context when loading the asset
  205. * @param animation The glTF animation property
  206. * @returns A promise that resolves with the loaded Babylon animation group when the load is complete
  207. */
  208. loadAnimationAsync(context: string, animation: IAnimation): Promise<AnimationGroup>;
  209. /**
  210. * @hidden
  211. * Loads a glTF animation channel.
  212. * @param context The context when loading the asset
  213. * @param animationContext The context of the animation when loading the asset
  214. * @param animation The glTF animation property
  215. * @param channel The glTF animation channel property
  216. * @param onLoad Called for each animation loaded
  217. * @returns A void promise that resolves when the load is complete
  218. */
  219. _loadAnimationChannelAsync(context: string, animationContext: string, animation: IAnimation, channel: IAnimationChannel, onLoad: (babylonAnimatable: IAnimatable, babylonAnimation: Animation) => void): Promise<void>;
  220. /**
  221. * @hidden
  222. * Loads a glTF animation channel.
  223. * @param context The context when loading the asset
  224. * @param animationContext The context of the animation when loading the asset
  225. * @param animation The glTF animation property
  226. * @param channel The glTF animation channel property
  227. * @param targetInfo The glTF target and properties
  228. * @param onLoad Called for each animation loaded
  229. * @returns A void promise that resolves when the load is complete
  230. */
  231. _loadAnimationChannelFromTargetInfoAsync(context: string, animationContext: string, animation: IAnimation, channel: IAnimationChannel, targetInfo: IObjectInfo<AnimationPropertyInfo[]>, onLoad: (babylonAnimatable: IAnimatable, babylonAnimation: Animation) => void): Promise<void>;
  232. private _loadAnimationSamplerAsync;
  233. /**
  234. * Loads a glTF buffer.
  235. * @param context The context when loading the asset
  236. * @param buffer The glTF buffer property
  237. * @param byteOffset The byte offset to use
  238. * @param byteLength The byte length to use
  239. * @returns A promise that resolves with the loaded data when the load is complete
  240. */
  241. loadBufferAsync(context: string, buffer: IBuffer, byteOffset: number, byteLength: number): Promise<ArrayBufferView>;
  242. /**
  243. * Loads a glTF buffer view.
  244. * @param context The context when loading the asset
  245. * @param bufferView The glTF buffer view property
  246. * @returns A promise that resolves with the loaded data when the load is complete
  247. */
  248. loadBufferViewAsync(context: string, bufferView: IBufferView): Promise<ArrayBufferView>;
  249. private _loadAccessorAsync;
  250. /**
  251. * @internal
  252. */
  253. _loadFloatAccessorAsync(context: string, accessor: IAccessor): Promise<Float32Array>;
  254. /**
  255. * @internal
  256. */
  257. _loadIndicesAccessorAsync(context: string, accessor: IAccessor): Promise<IndicesArray>;
  258. /**
  259. * @internal
  260. */
  261. _loadVertexBufferViewAsync(bufferView: IBufferView): Promise<Buffer>;
  262. /**
  263. * @internal
  264. */
  265. _loadVertexAccessorAsync(context: string, accessor: IAccessor, kind: string): Promise<VertexBuffer>;
  266. private _loadMaterialMetallicRoughnessPropertiesAsync;
  267. /**
  268. * @internal
  269. */
  270. _loadMaterialAsync(context: string, material: IMaterial, babylonMesh: Nullable<Mesh>, babylonDrawMode: number, assign?: (babylonMaterial: Material) => void): Promise<Material>;
  271. private _createDefaultMaterial;
  272. /**
  273. * Creates a Babylon material from a glTF material.
  274. * @param context The context when loading the asset
  275. * @param material The glTF material property
  276. * @param babylonDrawMode The draw mode for the Babylon material
  277. * @returns The Babylon material
  278. */
  279. createMaterial(context: string, material: IMaterial, babylonDrawMode: number): Material;
  280. /**
  281. * Loads properties from a glTF material into a Babylon material.
  282. * @param context The context when loading the asset
  283. * @param material The glTF material property
  284. * @param babylonMaterial The Babylon material
  285. * @returns A promise that resolves when the load is complete
  286. */
  287. loadMaterialPropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Promise<void>;
  288. /**
  289. * Loads the normal, occlusion, and emissive properties from a glTF material into a Babylon material.
  290. * @param context The context when loading the asset
  291. * @param material The glTF material property
  292. * @param babylonMaterial The Babylon material
  293. * @returns A promise that resolves when the load is complete
  294. */
  295. loadMaterialBasePropertiesAsync(context: string, material: IMaterial, babylonMaterial: Material): Promise<void>;
  296. /**
  297. * Loads the alpha properties from a glTF material into a Babylon material.
  298. * Must be called after the setting the albedo texture of the Babylon material when the material has an albedo texture.
  299. * @param context The context when loading the asset
  300. * @param material The glTF material property
  301. * @param babylonMaterial The Babylon material
  302. */
  303. loadMaterialAlphaProperties(context: string, material: IMaterial, babylonMaterial: Material): void;
  304. /**
  305. * Loads a glTF texture info.
  306. * @param context The context when loading the asset
  307. * @param textureInfo The glTF texture info property
  308. * @param assign A function called synchronously after parsing the glTF properties
  309. * @returns A promise that resolves with the loaded Babylon texture when the load is complete
  310. */
  311. loadTextureInfoAsync(context: string, textureInfo: ITextureInfo, assign?: (babylonTexture: BaseTexture) => void): Promise<BaseTexture>;
  312. /**
  313. * @internal
  314. */
  315. _loadTextureAsync(context: string, texture: ITexture, assign?: (babylonTexture: BaseTexture) => void): Promise<BaseTexture>;
  316. /**
  317. * @internal
  318. */
  319. _createTextureAsync(context: string, sampler: ISampler, image: IImage, assign?: (babylonTexture: BaseTexture) => void, textureLoaderOptions?: any, useSRGBBuffer?: boolean): Promise<BaseTexture>;
  320. private _loadSampler;
  321. /**
  322. * Loads a glTF image.
  323. * @param context The context when loading the asset
  324. * @param image The glTF image property
  325. * @returns A promise that resolves with the loaded data when the load is complete
  326. */
  327. loadImageAsync(context: string, image: IImage): Promise<ArrayBufferView>;
  328. /**
  329. * Loads a glTF uri.
  330. * @param context The context when loading the asset
  331. * @param property The glTF property associated with the uri
  332. * @param uri The base64 or relative uri
  333. * @returns A promise that resolves with the loaded data when the load is complete
  334. */
  335. loadUriAsync(context: string, property: IProperty, uri: string): Promise<ArrayBufferView>;
  336. /**
  337. * Adds a JSON pointer to the _internalMetadata of the Babylon object at `<object>._internalMetadata.gltf.pointers`.
  338. * @param babylonObject the Babylon object with _internalMetadata
  339. * @param pointer the JSON pointer
  340. */
  341. static AddPointerMetadata(babylonObject: IWithMetadata, pointer: string): void;
  342. private static _GetTextureWrapMode;
  343. private static _GetTextureSamplingMode;
  344. private static _GetTypedArrayConstructor;
  345. private static _GetTypedArray;
  346. private static _GetNumComponents;
  347. private static _ValidateUri;
  348. /**
  349. * @internal
  350. */
  351. static _GetDrawMode(context: string, mode: number | undefined): number;
  352. private _compileMaterialsAsync;
  353. private _compileShadowGeneratorsAsync;
  354. private _forEachExtensions;
  355. private _applyExtensions;
  356. private _extensionsOnLoading;
  357. private _extensionsOnReady;
  358. private _extensionsLoadSceneAsync;
  359. private _extensionsLoadNodeAsync;
  360. private _extensionsLoadCameraAsync;
  361. private _extensionsLoadVertexDataAsync;
  362. private _extensionsLoadMeshPrimitiveAsync;
  363. private _extensionsLoadMaterialAsync;
  364. private _extensionsCreateMaterial;
  365. private _extensionsLoadMaterialPropertiesAsync;
  366. private _extensionsLoadTextureInfoAsync;
  367. private _extensionsLoadTextureAsync;
  368. private _extensionsLoadAnimationAsync;
  369. private _extensionsLoadAnimationChannelAsync;
  370. private _extensionsLoadSkinAsync;
  371. private _extensionsLoadUriAsync;
  372. private _extensionsLoadBufferViewAsync;
  373. private _extensionsLoadBufferAsync;
  374. /**
  375. * Helper method called by a loader extension to load an glTF extension.
  376. * @param context The context when loading the asset
  377. * @param property The glTF property to load the extension from
  378. * @param extensionName The name of the extension to load
  379. * @param actionAsync The action to run
  380. * @returns The promise returned by actionAsync or null if the extension does not exist
  381. */
  382. static LoadExtensionAsync<TExtension = any, TResult = void>(context: string, property: IProperty, extensionName: string, actionAsync: (extensionContext: string, extension: TExtension) => Nullable<Promise<TResult>>): Nullable<Promise<TResult>>;
  383. /**
  384. * Helper method called by a loader extension to load a glTF extra.
  385. * @param context The context when loading the asset
  386. * @param property The glTF property to load the extra from
  387. * @param extensionName The name of the extension to load
  388. * @param actionAsync The action to run
  389. * @returns The promise returned by actionAsync or null if the extra does not exist
  390. */
  391. static LoadExtraAsync<TExtra = any, TResult = void>(context: string, property: IProperty, extensionName: string, actionAsync: (extraContext: string, extra: TExtra) => Nullable<Promise<TResult>>): Nullable<Promise<TResult>>;
  392. /**
  393. * Checks for presence of an extension.
  394. * @param name The name of the extension to check
  395. * @returns A boolean indicating the presence of the given extension name in `extensionsUsed`
  396. */
  397. isExtensionUsed(name: string): boolean;
  398. /**
  399. * Increments the indentation level and logs a message.
  400. * @param message The message to log
  401. */
  402. logOpen(message: string): void;
  403. /**
  404. * Decrements the indentation level.
  405. */
  406. logClose(): void;
  407. /**
  408. * Logs a message
  409. * @param message The message to log
  410. */
  411. log(message: string): void;
  412. /**
  413. * Starts a performance counter.
  414. * @param counterName The name of the performance counter
  415. */
  416. startPerformanceCounter(counterName: string): void;
  417. /**
  418. * Ends a performance counter.
  419. * @param counterName The name of the performance counter
  420. */
  421. endPerformanceCounter(counterName: string): void;
  422. }
  423. export {};