babylon.glTF2FileLoader.d.ts 20 KB

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