babylon.glTF2FileLoader.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. declare module BABYLON {
  2. enum GLTFLoaderCoordinateSystemMode {
  3. AUTO = 0,
  4. PASS_THROUGH = 1,
  5. FORCE_RIGHT_HANDED = 2,
  6. }
  7. interface IGLTFLoaderData {
  8. json: Object;
  9. bin: ArrayBufferView;
  10. }
  11. interface IGLTFLoader {
  12. 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;
  13. loadAsync: (scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void) => void;
  14. }
  15. class GLTFFileLoader implements ISceneLoaderPluginAsync {
  16. static CreateGLTFLoaderV1: (parent: GLTFFileLoader) => IGLTFLoader;
  17. static CreateGLTFLoaderV2: (parent: GLTFFileLoader) => IGLTFLoader;
  18. onParsed: (data: IGLTFLoaderData) => void;
  19. static HomogeneousCoordinates: boolean;
  20. static IncrementalLoading: boolean;
  21. coordinateSystemMode: GLTFLoaderCoordinateSystemMode;
  22. onTextureLoaded: (texture: BaseTexture) => void;
  23. onMaterialLoaded: (material: Material) => void;
  24. /**
  25. * Let the user decides if he needs to process the material (like precompilation) before affecting it to meshes
  26. */
  27. onBeforeMaterialReadyAsync: (material: Material, targetMesh: AbstractMesh, isLOD: boolean, callback: () => void) => void;
  28. /**
  29. * Raised when the visible components (geometry, materials, textures, etc.) are first ready to be rendered.
  30. * For assets with LODs, raised when the first LOD is complete.
  31. * For assets without LODs, raised when the model is complete just before onComplete.
  32. */
  33. onReady: () => void;
  34. /**
  35. * Raised when the asset is completely loaded, just before the loader is disposed.
  36. * For assets with LODs, raised when all of the LODs are complete.
  37. * For assets without LODs, raised when the model is complete just after onReady.
  38. */
  39. onComplete: () => void;
  40. name: string;
  41. extensions: ISceneLoaderPluginExtensions;
  42. 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;
  43. loadAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  44. canDirectLoad(data: string): boolean;
  45. private static _parse(data, onError);
  46. private _getLoader(loaderData, onError);
  47. private static _parseBinary(data, onError);
  48. private static _parseV1(binaryReader, onError);
  49. private static _parseV2(binaryReader, onError);
  50. private static _parseVersion(version);
  51. private static _compareVersion(a, b);
  52. private static _decodeBufferToText(view);
  53. }
  54. }
  55. declare module BABYLON.GLTF2 {
  56. /**
  57. * Enums
  58. */
  59. enum EComponentType {
  60. BYTE = 5120,
  61. UNSIGNED_BYTE = 5121,
  62. SHORT = 5122,
  63. UNSIGNED_SHORT = 5123,
  64. UNSIGNED_INT = 5125,
  65. FLOAT = 5126,
  66. }
  67. enum EMeshPrimitiveMode {
  68. POINTS = 0,
  69. LINES = 1,
  70. LINE_LOOP = 2,
  71. LINE_STRIP = 3,
  72. TRIANGLES = 4,
  73. TRIANGLE_STRIP = 5,
  74. TRIANGLE_FAN = 6,
  75. }
  76. enum ETextureMagFilter {
  77. NEAREST = 9728,
  78. LINEAR = 9729,
  79. }
  80. enum ETextureMinFilter {
  81. NEAREST = 9728,
  82. LINEAR = 9729,
  83. NEAREST_MIPMAP_NEAREST = 9984,
  84. LINEAR_MIPMAP_NEAREST = 9985,
  85. NEAREST_MIPMAP_LINEAR = 9986,
  86. LINEAR_MIPMAP_LINEAR = 9987,
  87. }
  88. enum ETextureWrapMode {
  89. CLAMP_TO_EDGE = 33071,
  90. MIRRORED_REPEAT = 33648,
  91. REPEAT = 10497,
  92. }
  93. /**
  94. * Interfaces
  95. */
  96. interface IGLTFProperty {
  97. extensions?: Object;
  98. extras?: any;
  99. }
  100. interface IGLTFChildRootProperty extends IGLTFProperty {
  101. name?: string;
  102. }
  103. interface IGLTFAccessorSparseIndices extends IGLTFProperty {
  104. bufferView: number;
  105. byteOffset?: number;
  106. componentType: EComponentType;
  107. }
  108. interface IGLTFAccessorSparseValues extends IGLTFProperty {
  109. bufferView: number;
  110. byteOffset?: number;
  111. }
  112. interface IGLTFAccessorSparse extends IGLTFProperty {
  113. count: number;
  114. indices: IGLTFAccessorSparseIndices;
  115. values: IGLTFAccessorSparseValues;
  116. }
  117. interface IGLTFAccessor extends IGLTFChildRootProperty {
  118. bufferView?: number;
  119. byteOffset?: number;
  120. componentType: EComponentType;
  121. normalized?: boolean;
  122. count: number;
  123. type: string;
  124. max: number[];
  125. min: number[];
  126. sparse?: IGLTFAccessorSparse;
  127. index?: number;
  128. }
  129. interface IGLTFAnimationChannel extends IGLTFProperty {
  130. sampler: number;
  131. target: IGLTFAnimationChannelTarget;
  132. }
  133. interface IGLTFAnimationChannelTarget extends IGLTFProperty {
  134. node: number;
  135. path: string;
  136. }
  137. interface IGLTFAnimationSampler extends IGLTFProperty {
  138. input: number;
  139. interpolation?: string;
  140. output: number;
  141. }
  142. interface IGLTFAnimation extends IGLTFChildRootProperty {
  143. channels: IGLTFAnimationChannel[];
  144. samplers: IGLTFAnimationSampler[];
  145. index?: number;
  146. targets?: any[];
  147. }
  148. interface IGLTFAsset extends IGLTFChildRootProperty {
  149. copyright?: string;
  150. generator?: string;
  151. version: string;
  152. minVersion?: string;
  153. }
  154. interface IGLTFBuffer extends IGLTFChildRootProperty {
  155. uri?: string;
  156. byteLength: number;
  157. index?: number;
  158. loadedData?: ArrayBufferView;
  159. loadedObservable?: Observable<IGLTFBuffer>;
  160. }
  161. interface IGLTFBufferView extends IGLTFChildRootProperty {
  162. buffer: number;
  163. byteOffset?: number;
  164. byteLength: number;
  165. byteStride?: number;
  166. index?: number;
  167. }
  168. interface IGLTFCameraOrthographic extends IGLTFProperty {
  169. xmag: number;
  170. ymag: number;
  171. zfar: number;
  172. znear: number;
  173. }
  174. interface IGLTFCameraPerspective extends IGLTFProperty {
  175. aspectRatio: number;
  176. yfov: number;
  177. zfar: number;
  178. znear: number;
  179. }
  180. interface IGLTFCamera extends IGLTFChildRootProperty {
  181. orthographic?: IGLTFCameraOrthographic;
  182. perspective?: IGLTFCameraPerspective;
  183. type: string;
  184. }
  185. interface IGLTFImage extends IGLTFChildRootProperty {
  186. uri?: string;
  187. mimeType?: string;
  188. bufferView?: number;
  189. index?: number;
  190. }
  191. interface IGLTFMaterialNormalTextureInfo extends IGLTFTextureInfo {
  192. scale: number;
  193. }
  194. interface IGLTFMaterialOcclusionTextureInfo extends IGLTFTextureInfo {
  195. strength: number;
  196. }
  197. interface IGLTFMaterialPbrMetallicRoughness {
  198. baseColorFactor: number[];
  199. baseColorTexture: IGLTFTextureInfo;
  200. metallicFactor: number;
  201. roughnessFactor: number;
  202. metallicRoughnessTexture: IGLTFTextureInfo;
  203. }
  204. interface IGLTFMaterial extends IGLTFChildRootProperty {
  205. pbrMetallicRoughness?: IGLTFMaterialPbrMetallicRoughness;
  206. normalTexture?: IGLTFMaterialNormalTextureInfo;
  207. occlusionTexture?: IGLTFMaterialOcclusionTextureInfo;
  208. emissiveTexture?: IGLTFTextureInfo;
  209. emissiveFactor?: number[];
  210. alphaMode?: string;
  211. alphaCutoff: number;
  212. doubleSided?: boolean;
  213. index?: number;
  214. babylonMaterial?: Material;
  215. }
  216. interface IGLTFMeshPrimitive extends IGLTFProperty {
  217. attributes: {
  218. [name: string]: number;
  219. };
  220. indices?: number;
  221. material?: number;
  222. mode?: EMeshPrimitiveMode;
  223. targets?: {
  224. [name: string]: number;
  225. }[];
  226. }
  227. interface IGLTFMesh extends IGLTFChildRootProperty {
  228. primitives: IGLTFMeshPrimitive[];
  229. weights?: number[];
  230. index?: number;
  231. }
  232. interface IGLTFNode extends IGLTFChildRootProperty {
  233. camera?: number;
  234. children?: number[];
  235. skin?: number;
  236. matrix?: number[];
  237. mesh?: number;
  238. rotation?: number[];
  239. scale?: number[];
  240. translation?: number[];
  241. weights?: number[];
  242. index?: number;
  243. parent?: IGLTFNode;
  244. babylonMesh?: Mesh;
  245. babylonBones?: {
  246. [skin: number]: Bone;
  247. };
  248. babylonAnimationTargets?: Node[];
  249. }
  250. interface IGLTFSampler extends IGLTFChildRootProperty {
  251. magFilter?: ETextureMagFilter;
  252. minFilter?: ETextureMinFilter;
  253. wrapS?: ETextureWrapMode;
  254. wrapT?: ETextureWrapMode;
  255. }
  256. interface IGLTFScene extends IGLTFChildRootProperty {
  257. nodes: number[];
  258. index?: number;
  259. }
  260. interface IGLTFSkin extends IGLTFChildRootProperty {
  261. inverseBindMatrices?: number;
  262. skeleton?: number;
  263. joints: number[];
  264. index?: number;
  265. babylonSkeleton?: Skeleton;
  266. }
  267. interface IGLTFTexture extends IGLTFChildRootProperty {
  268. sampler?: number;
  269. source: number;
  270. index?: number;
  271. url?: string;
  272. dataReadyObservable?: Observable<IGLTFTexture>;
  273. }
  274. interface IGLTFTextureInfo {
  275. index: number;
  276. texCoord?: number;
  277. }
  278. interface IGLTF extends IGLTFProperty {
  279. accessors?: IGLTFAccessor[];
  280. animations?: IGLTFAnimation[];
  281. asset: IGLTFAsset;
  282. buffers?: IGLTFBuffer[];
  283. bufferViews?: IGLTFBufferView[];
  284. cameras?: IGLTFCamera[];
  285. extensionsUsed?: string[];
  286. extensionsRequired?: string[];
  287. images?: IGLTFImage[];
  288. materials?: IGLTFMaterial[];
  289. meshes?: IGLTFMesh[];
  290. nodes?: IGLTFNode[];
  291. samplers?: IGLTFSampler[];
  292. scene?: number;
  293. scenes?: IGLTFScene[];
  294. skins?: IGLTFSkin[];
  295. textures?: IGLTFTexture[];
  296. }
  297. }
  298. declare module BABYLON.GLTF2 {
  299. class GLTFLoader implements IGLTFLoader, IDisposable {
  300. _gltf: IGLTF;
  301. _babylonScene: Scene;
  302. private _parent;
  303. private _rootUrl;
  304. private _defaultMaterial;
  305. private _rootNode;
  306. private _successCallback;
  307. private _progressCallback;
  308. private _errorCallback;
  309. private _renderReady;
  310. private _disposed;
  311. private _renderReadyObservable;
  312. private _renderPendingCount;
  313. private _loaderPendingCount;
  314. private _loaderTrackers;
  315. static Extensions: {
  316. [name: string]: GLTFLoaderExtension;
  317. };
  318. static RegisterExtension(extension: GLTFLoaderExtension): void;
  319. constructor(parent: GLTFFileLoader);
  320. dispose(): void;
  321. 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;
  322. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  323. private _loadAsync(nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError);
  324. private _onError(message);
  325. private _onProgress(event);
  326. _executeWhenRenderReady(func: () => void): void;
  327. private _onRenderReady();
  328. private _onComplete();
  329. private _loadData(data);
  330. private _getMeshes();
  331. private _getSkeletons();
  332. private _getAnimationTargets();
  333. private _startAnimations();
  334. private _loadDefaultScene(nodeNames);
  335. private _loadScene(context, scene, nodeNames);
  336. _loadNode(context: string, node: IGLTFNode): void;
  337. private _loadMesh(context, node, mesh);
  338. private _loadPrimitive(context, node, mesh, primitive, onSuccess);
  339. private _loadVertexDataAsync(context, mesh, primitive, onSuccess);
  340. private _createMorphTargets(node, mesh, primitive);
  341. private _loadMorphTargetsData(context, mesh, primitive, vertexData, babylonMesh);
  342. private _loadTransform(node);
  343. private _loadSkin(context, skin);
  344. private _createBone(node, skin, parent, localMatrix, baseMatrix, index);
  345. private _loadBones(context, skin, inverseBindMatrixData);
  346. private _loadBone(node, skin, inverseBindMatrixData, babylonBones);
  347. private _getNodeMatrix(node);
  348. private _traverseNodes(context, indices, action, parentNode?);
  349. _traverseNode(context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode?: IGLTFNode): void;
  350. private _loadAnimations();
  351. private _loadAnimationChannel(animation, channelContext, channel, samplerContext, sampler);
  352. private _loadBufferAsync(context, buffer, onSuccess);
  353. private _loadBufferViewAsync(context, bufferView, onSuccess);
  354. private _loadAccessorAsync(context, accessor, onSuccess);
  355. private _getNumComponentsOfType(type);
  356. private _buildArrayBuffer<T>(typedArray, context, data, byteOffset, count, numComponents, byteStride);
  357. _addPendingData(data: any): void;
  358. _removePendingData(data: any): void;
  359. _addLoaderPendingData(data: any): void;
  360. _removeLoaderPendingData(data: any): void;
  361. _whenAction(action: () => void, onComplete: () => void): void;
  362. private _getDefaultMaterial();
  363. private _loadMaterialMetallicRoughnessProperties(context, material);
  364. _loadMaterial(context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): void;
  365. _createPbrMaterial(material: IGLTFMaterial): void;
  366. _loadMaterialBaseProperties(context: string, material: IGLTFMaterial): void;
  367. _loadMaterialAlphaProperties(context: string, material: IGLTFMaterial, colorFactor: number[]): void;
  368. _loadTexture(context: string, texture: IGLTFTexture, coordinatesIndex: number): Texture;
  369. private _loadImage(context, image, onSuccess);
  370. _tryCatchOnError(handler: () => void): void;
  371. }
  372. }
  373. declare module BABYLON.GLTF2 {
  374. /**
  375. * Utils functions for GLTF
  376. */
  377. class GLTFUtils {
  378. /**
  379. * If the uri is a base64 string
  380. * @param uri: the uri to test
  381. */
  382. static IsBase64(uri: string): boolean;
  383. /**
  384. * Decode the base64 uri
  385. * @param uri: the uri to decode
  386. */
  387. static DecodeBase64(uri: string): ArrayBuffer;
  388. static ForEach(view: Uint16Array | Uint32Array | Float32Array, func: (nvalue: number, index: number) => void): void;
  389. static ValidateUri(uri: string): boolean;
  390. static AssignIndices(array: Array<{
  391. index?: number;
  392. }>): void;
  393. static GetArrayItem<T>(array: ArrayLike<T>, index: number): T;
  394. static GetTextureWrapMode(mode: ETextureWrapMode): number;
  395. static GetTextureSamplingMode(magFilter: ETextureMagFilter, minFilter: ETextureMinFilter): number;
  396. /**
  397. * Decodes a buffer view into a string
  398. * @param view: the buffer view
  399. */
  400. static DecodeBufferToText(view: ArrayBufferView): string;
  401. }
  402. }
  403. declare module BABYLON.GLTF2 {
  404. abstract class GLTFLoaderExtension {
  405. enabled: boolean;
  406. readonly abstract name: string;
  407. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  408. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  409. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  410. protected _loadExtension<T>(property: IGLTFProperty, action: (extension: T, onComplete: () => void) => void): boolean;
  411. static _Extensions: GLTFLoaderExtension[];
  412. static TraverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  413. static LoadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  414. static LoadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  415. private static _ApplyExtensions(action);
  416. }
  417. }
  418. declare module BABYLON.GLTF2.Extensions {
  419. class MSFTLOD extends GLTFLoaderExtension {
  420. /**
  421. * Specify the minimal delay between LODs in ms (default = 250)
  422. */
  423. static MinimalLODDelay: number;
  424. readonly name: string;
  425. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  426. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  427. private _loadNodeLOD(loader, context, nodes, index, onComplete);
  428. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  429. private _loadMaterialLOD(loader, context, materials, index, assign, onComplete);
  430. }
  431. }
  432. declare module BABYLON.GLTF2.Extensions {
  433. class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
  434. readonly name: string;
  435. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  436. private _loadSpecularGlossinessProperties(loader, context, material, properties);
  437. }
  438. }