babylon.glTF2FileLoader.d.ts 18 KB

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