babylon.glTF2FileLoader.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 IGLTFLoader extends IDisposable {
  31. importMeshAsync: (meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void) => void;
  32. loadAsync: (scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void) => void;
  33. }
  34. class GLTFFileLoader implements IDisposable, ISceneLoaderPluginAsync, ISceneLoaderPluginFactory {
  35. static CreateGLTFLoaderV1: (parent: GLTFFileLoader) => IGLTFLoader;
  36. static CreateGLTFLoaderV2: (parent: GLTFFileLoader) => IGLTFLoader;
  37. /**
  38. * Raised when the asset has been parsed.
  39. * The data.json property stores the glTF JSON.
  40. * The data.bin property stores the BIN chunk from a glTF binary or null if the input is not a glTF binary.
  41. */
  42. onParsed: (data: IGLTFLoaderData) => void;
  43. static IncrementalLoading: boolean;
  44. static HomogeneousCoordinates: boolean;
  45. /**
  46. * The coordinate system mode (AUTO, FORCE_RIGHT_HANDED).
  47. */
  48. coordinateSystemMode: GLTFLoaderCoordinateSystemMode;
  49. /**
  50. * The animation start mode (NONE, FIRST, ALL).
  51. */
  52. animationStartMode: GLTFLoaderAnimationStartMode;
  53. /**
  54. * Set to true to compile materials before raising the success callback.
  55. */
  56. compileMaterials: boolean;
  57. /**
  58. * Set to true to also compile materials with clip planes.
  59. */
  60. useClipPlane: boolean;
  61. /**
  62. * Set to true to compile shadow generators before raising the success callback.
  63. */
  64. compileShadowGenerators: boolean;
  65. /**
  66. * Raised when the loader creates a mesh after parsing the glTF properties of the mesh.
  67. */
  68. onMeshLoaded: (mesh: AbstractMesh) => void;
  69. /**
  70. * Raised when the loader creates a texture after parsing the glTF properties of the texture.
  71. */
  72. onTextureLoaded: (texture: BaseTexture) => void;
  73. /**
  74. * Raised when the loader creates a material after parsing the glTF properties of the material.
  75. */
  76. onMaterialLoaded: (material: Material) => void;
  77. /**
  78. * Raised when the asset is completely loaded, immediately before the loader is disposed.
  79. * For assets with LODs, raised when all of the LODs are complete.
  80. * For assets without LODs, raised when the model is complete, immediately after onSuccess.
  81. */
  82. onComplete: () => void;
  83. private _loader;
  84. name: string;
  85. extensions: ISceneLoaderPluginExtensions;
  86. /**
  87. * Disposes the loader, releases resources during load, and cancels any outstanding requests.
  88. */
  89. dispose(): void;
  90. importMeshAsync(meshesNames: any, scene: Scene, data: any, rootUrl: string, onSuccess: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  91. loadAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  92. canDirectLoad(data: string): boolean;
  93. rewriteRootURL: (rootUrl: string, responseURL?: string) => string;
  94. createPlugin(): ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  95. private static _parse(data);
  96. private _getLoader(loaderData);
  97. private static _parseBinary(data);
  98. private static _parseV1(binaryReader);
  99. private static _parseV2(binaryReader);
  100. private static _parseVersion(version);
  101. private static _compareVersion(a, b);
  102. private static _decodeBufferToText(buffer);
  103. }
  104. }
  105. declare module BABYLON.GLTF2 {
  106. /**
  107. * Enums
  108. */
  109. enum EComponentType {
  110. BYTE = 5120,
  111. UNSIGNED_BYTE = 5121,
  112. SHORT = 5122,
  113. UNSIGNED_SHORT = 5123,
  114. UNSIGNED_INT = 5125,
  115. FLOAT = 5126,
  116. }
  117. enum EMeshPrimitiveMode {
  118. POINTS = 0,
  119. LINES = 1,
  120. LINE_LOOP = 2,
  121. LINE_STRIP = 3,
  122. TRIANGLES = 4,
  123. TRIANGLE_STRIP = 5,
  124. TRIANGLE_FAN = 6,
  125. }
  126. enum ETextureMagFilter {
  127. NEAREST = 9728,
  128. LINEAR = 9729,
  129. }
  130. enum ETextureMinFilter {
  131. NEAREST = 9728,
  132. LINEAR = 9729,
  133. NEAREST_MIPMAP_NEAREST = 9984,
  134. LINEAR_MIPMAP_NEAREST = 9985,
  135. NEAREST_MIPMAP_LINEAR = 9986,
  136. LINEAR_MIPMAP_LINEAR = 9987,
  137. }
  138. enum ETextureWrapMode {
  139. CLAMP_TO_EDGE = 33071,
  140. MIRRORED_REPEAT = 33648,
  141. REPEAT = 10497,
  142. }
  143. /**
  144. * Interfaces
  145. */
  146. interface IGLTFProperty {
  147. extensions?: {
  148. [key: string]: any;
  149. };
  150. extras?: any;
  151. }
  152. interface IGLTFChildRootProperty extends IGLTFProperty {
  153. name?: string;
  154. }
  155. interface IGLTFAccessorSparseIndices extends IGLTFProperty {
  156. bufferView: number;
  157. byteOffset?: number;
  158. componentType: EComponentType;
  159. }
  160. interface IGLTFAccessorSparseValues extends IGLTFProperty {
  161. bufferView: number;
  162. byteOffset?: number;
  163. }
  164. interface IGLTFAccessorSparse extends IGLTFProperty {
  165. count: number;
  166. indices: IGLTFAccessorSparseIndices;
  167. values: IGLTFAccessorSparseValues;
  168. }
  169. interface IGLTFAccessor extends IGLTFChildRootProperty {
  170. bufferView?: number;
  171. byteOffset?: number;
  172. componentType: EComponentType;
  173. normalized?: boolean;
  174. count: number;
  175. type: string;
  176. max: number[];
  177. min: number[];
  178. sparse?: IGLTFAccessorSparse;
  179. index: number;
  180. }
  181. interface IGLTFAnimationChannel extends IGLTFProperty {
  182. sampler: number;
  183. target: IGLTFAnimationChannelTarget;
  184. }
  185. interface IGLTFAnimationChannelTarget extends IGLTFProperty {
  186. node: number;
  187. path: string;
  188. }
  189. interface IGLTFAnimationSampler extends IGLTFProperty {
  190. input: number;
  191. interpolation?: string;
  192. output: number;
  193. }
  194. interface IGLTFAnimation extends IGLTFChildRootProperty {
  195. channels: IGLTFAnimationChannel[];
  196. samplers: IGLTFAnimationSampler[];
  197. index: number;
  198. targets: any[];
  199. }
  200. interface IGLTFAsset extends IGLTFChildRootProperty {
  201. copyright?: string;
  202. generator?: string;
  203. version: string;
  204. minVersion?: string;
  205. }
  206. interface IGLTFBuffer extends IGLTFChildRootProperty {
  207. uri?: string;
  208. byteLength: number;
  209. index: number;
  210. loadedData?: ArrayBufferView;
  211. loadedObservable?: Observable<IGLTFBuffer>;
  212. }
  213. interface IGLTFBufferView extends IGLTFChildRootProperty {
  214. buffer: number;
  215. byteOffset?: number;
  216. byteLength: number;
  217. byteStride?: number;
  218. index: number;
  219. }
  220. interface IGLTFCameraOrthographic extends IGLTFProperty {
  221. xmag: number;
  222. ymag: number;
  223. zfar: number;
  224. znear: number;
  225. }
  226. interface IGLTFCameraPerspective extends IGLTFProperty {
  227. aspectRatio: number;
  228. yfov: number;
  229. zfar: number;
  230. znear: number;
  231. }
  232. interface IGLTFCamera extends IGLTFChildRootProperty {
  233. orthographic?: IGLTFCameraOrthographic;
  234. perspective?: IGLTFCameraPerspective;
  235. type: string;
  236. }
  237. interface IGLTFImage extends IGLTFChildRootProperty {
  238. uri?: string;
  239. mimeType?: string;
  240. bufferView?: number;
  241. index: number;
  242. }
  243. interface IGLTFMaterialNormalTextureInfo extends IGLTFTextureInfo {
  244. scale: number;
  245. }
  246. interface IGLTFMaterialOcclusionTextureInfo extends IGLTFTextureInfo {
  247. strength: number;
  248. }
  249. interface IGLTFMaterialPbrMetallicRoughness {
  250. baseColorFactor: number[];
  251. baseColorTexture: IGLTFTextureInfo;
  252. metallicFactor: number;
  253. roughnessFactor: number;
  254. metallicRoughnessTexture: IGLTFTextureInfo;
  255. }
  256. interface IGLTFMaterial extends IGLTFChildRootProperty {
  257. pbrMetallicRoughness?: IGLTFMaterialPbrMetallicRoughness;
  258. normalTexture?: IGLTFMaterialNormalTextureInfo;
  259. occlusionTexture?: IGLTFMaterialOcclusionTextureInfo;
  260. emissiveTexture?: IGLTFTextureInfo;
  261. emissiveFactor?: number[];
  262. alphaMode?: string;
  263. alphaCutoff: number;
  264. doubleSided?: boolean;
  265. index: number;
  266. babylonMaterial: Material;
  267. }
  268. interface IGLTFMeshPrimitive extends IGLTFProperty {
  269. attributes: {
  270. [name: string]: number;
  271. };
  272. indices?: number;
  273. material?: number;
  274. mode?: EMeshPrimitiveMode;
  275. targets?: {
  276. [name: string]: number;
  277. }[];
  278. vertexData: VertexData;
  279. targetsVertexData: VertexData[];
  280. }
  281. interface IGLTFMesh extends IGLTFChildRootProperty {
  282. primitives: IGLTFMeshPrimitive[];
  283. weights?: number[];
  284. index: number;
  285. }
  286. interface IGLTFNode extends IGLTFChildRootProperty {
  287. camera?: number;
  288. children?: number[];
  289. skin?: number;
  290. matrix?: number[];
  291. mesh?: number;
  292. rotation?: number[];
  293. scale?: number[];
  294. translation?: number[];
  295. weights?: number[];
  296. index: number;
  297. parent: IGLTFNode;
  298. babylonMesh: Mesh;
  299. babylonAnimationTargets?: Node[];
  300. }
  301. interface IGLTFSampler extends IGLTFChildRootProperty {
  302. magFilter?: ETextureMagFilter;
  303. minFilter?: ETextureMinFilter;
  304. wrapS?: ETextureWrapMode;
  305. wrapT?: ETextureWrapMode;
  306. index: number;
  307. noMipMaps: boolean;
  308. samplingMode: number;
  309. wrapU: number;
  310. wrapV: number;
  311. }
  312. interface IGLTFScene extends IGLTFChildRootProperty {
  313. nodes: number[];
  314. index: number;
  315. }
  316. interface IGLTFSkin extends IGLTFChildRootProperty {
  317. inverseBindMatrices?: number;
  318. skeleton?: number;
  319. joints: number[];
  320. index: number;
  321. babylonSkeleton: Skeleton;
  322. }
  323. interface IGLTFTexture extends IGLTFChildRootProperty {
  324. sampler?: number;
  325. source: number;
  326. index: number;
  327. url?: string;
  328. dataReadyObservable?: Observable<IGLTFTexture>;
  329. }
  330. interface IGLTFTextureInfo {
  331. index: number;
  332. texCoord?: number;
  333. }
  334. interface IGLTF extends IGLTFProperty {
  335. accessors?: IGLTFAccessor[];
  336. animations?: IGLTFAnimation[];
  337. asset: IGLTFAsset;
  338. buffers?: IGLTFBuffer[];
  339. bufferViews?: IGLTFBufferView[];
  340. cameras?: IGLTFCamera[];
  341. extensionsUsed?: string[];
  342. extensionsRequired?: string[];
  343. images?: IGLTFImage[];
  344. materials?: IGLTFMaterial[];
  345. meshes?: IGLTFMesh[];
  346. nodes?: IGLTFNode[];
  347. samplers?: IGLTFSampler[];
  348. scene?: number;
  349. scenes?: IGLTFScene[];
  350. skins?: IGLTFSkin[];
  351. textures?: IGLTFTexture[];
  352. }
  353. }
  354. declare module BABYLON.GLTF2 {
  355. class GLTFLoader implements IGLTFLoader {
  356. _gltf: IGLTF;
  357. _babylonScene: Scene;
  358. private _disposed;
  359. private _parent;
  360. private _rootUrl;
  361. private _defaultMaterial;
  362. private _defaultSampler;
  363. private _rootNode;
  364. private _successCallback;
  365. private _progressCallback;
  366. private _errorCallback;
  367. private _renderReady;
  368. private _requests;
  369. private _renderReadyObservable;
  370. private _renderPendingCount;
  371. private _loaderPendingCount;
  372. private _loaderTrackers;
  373. static Extensions: {
  374. [name: string]: GLTFLoaderExtension;
  375. };
  376. static RegisterExtension(extension: GLTFLoaderExtension): void;
  377. private static _progressEventFactory;
  378. private static _createProgressEventByConstructor(name, data);
  379. private static _createProgressEventByDocument(name, data);
  380. constructor(parent: GLTFFileLoader);
  381. dispose(): void;
  382. importMeshAsync(meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: (meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]) => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  383. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  384. private _loadAsync(nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError);
  385. private _onProgress();
  386. _executeWhenRenderReady(func: () => void): void;
  387. private _onRenderReady();
  388. private _onComplete();
  389. private _loadData(data);
  390. private _getMeshes();
  391. private _getSkeletons();
  392. private _startAnimations();
  393. private _loadDefaultScene(nodeNames);
  394. private _loadScene(context, scene, nodeNames);
  395. _loadNode(context: string, node: IGLTFNode): void;
  396. private _loadMesh(context, node, mesh);
  397. private _loadAllVertexDataAsync(context, mesh, onSuccess);
  398. /**
  399. * Converts a data bufferview into a Float4 Texture Coordinate Array, based on the accessor component type
  400. * @param {ArrayBufferView} data
  401. * @param {IGLTFAccessor} accessor
  402. */
  403. private _convertToFloat4TextureCoordArray(context, data, accessor);
  404. /**
  405. * Converts a data bufferview into a Float4 Color Array, based on the accessor component type
  406. * @param {ArrayBufferView} data
  407. * @param {IGLTFAccessor} accessor
  408. */
  409. private _convertToFloat4ColorArray(context, data, accessor);
  410. private _loadVertexDataAsync(context, mesh, primitive, onSuccess);
  411. private _createMorphTargets(context, node, mesh);
  412. private _loadMorphTargets(context, node, mesh);
  413. private _loadAllMorphTargetVertexDataAsync(context, node, mesh, onSuccess);
  414. private _loadMorphTargetVertexDataAsync(context, vertexData, attributes, onSuccess);
  415. private _loadTransform(node);
  416. private _loadSkinAsync(context, skin, onSuccess);
  417. private _createBone(node, skin, parent, localMatrix, baseMatrix, index);
  418. private _loadBones(context, skin, inverseBindMatrixData);
  419. private _loadBone(node, skin, inverseBindMatrixData, babylonBones);
  420. private _getNodeMatrix(node);
  421. private _traverseNodes(context, indices, action, parentNode);
  422. _traverseNode(context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): void;
  423. private _loadAnimations();
  424. private _loadAnimation(context, animation);
  425. private _loadAnimationChannel(animation, channelContext, channel, samplerContext, sampler);
  426. private _loadBufferAsync(context, buffer, onSuccess);
  427. private _loadBufferViewAsync(context, bufferView, onSuccess);
  428. private _loadAccessorAsync(context, accessor, onSuccess);
  429. private _buildArrayBuffer<T>(typedArray, data, byteOffset, count, numComponents, byteStride?);
  430. _addPendingData(data: any): void;
  431. _removePendingData(data: any): void;
  432. _addLoaderPendingData(data: any): void;
  433. _removeLoaderPendingData(data: any): void;
  434. _whenAction(action: () => void, onComplete: () => void): void;
  435. private _getDefaultMaterial();
  436. private _loadMaterialMetallicRoughnessProperties(context, material);
  437. _loadMaterial(context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): void;
  438. _createPbrMaterial(material: IGLTFMaterial): void;
  439. _loadMaterialBaseProperties(context: string, material: IGLTFMaterial): void;
  440. _loadMaterialAlphaProperties(context: string, material: IGLTFMaterial, colorFactor: number[]): void;
  441. _loadTexture(context: string, texture: IGLTFTexture, coordinatesIndex?: number): Texture;
  442. private _loadSampler(context, sampler);
  443. private _loadImageAsync(context, image, onSuccess);
  444. _loadUriAsync(context: string, uri: string, onSuccess: (data: ArrayBufferView) => void): void;
  445. _tryCatchOnError(handler: () => void): void;
  446. private static _AssignIndices(array?);
  447. static _GetProperty<T extends IGLTFProperty>(array?: ArrayLike<T>, index?: number): Nullable<T>;
  448. private static _GetTextureWrapMode(context, mode?);
  449. private static _GetTextureSamplingMode(context, magFilter?, minFilter?);
  450. private static _GetNumComponents(context, type);
  451. private _compileMaterialAsync(babylonMaterial, babylonMesh, onSuccess);
  452. private _compileMaterialsAsync(onSuccess);
  453. private _compileShadowGeneratorsAsync(onSuccess);
  454. }
  455. }
  456. declare module BABYLON.GLTF2 {
  457. /**
  458. * Utils functions for GLTF
  459. */
  460. class GLTFUtils {
  461. /**
  462. * If the uri is a base64 string
  463. * @param uri: the uri to test
  464. */
  465. static IsBase64(uri: string): boolean;
  466. /**
  467. * Decode the base64 uri
  468. * @param uri: the uri to decode
  469. */
  470. static DecodeBase64(uri: string): ArrayBuffer;
  471. static ValidateUri(uri: string): boolean;
  472. }
  473. }
  474. declare module BABYLON.GLTF2 {
  475. abstract class GLTFLoaderExtension {
  476. enabled: boolean;
  477. readonly abstract name: string;
  478. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  479. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  480. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  481. protected _loadExtension<T>(context: string, property: IGLTFProperty, action: (context: string, extension: T, onComplete: () => void) => void): boolean;
  482. static _Extensions: GLTFLoaderExtension[];
  483. static TraverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  484. static LoadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  485. static LoadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  486. private static _ApplyExtensions(action);
  487. }
  488. }
  489. declare module BABYLON.GLTF2.Extensions {
  490. class MSFTLOD extends GLTFLoaderExtension {
  491. /**
  492. * Specify the minimal delay between LODs in ms (default = 250)
  493. */
  494. Delay: number;
  495. readonly name: string;
  496. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  497. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  498. private _loadNodeLOD(loader, context, nodes, index, onComplete);
  499. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  500. private _loadMaterialLOD(loader, context, materials, index, assign, onComplete);
  501. }
  502. }
  503. declare module BABYLON.GLTF2.Extensions {
  504. class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
  505. readonly name: string;
  506. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  507. private _loadSpecularGlossinessProperties(loader, context, material, properties);
  508. }
  509. }