babylon.glTF2FileLoader.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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?: {
  98. [key: string]: any;
  99. };
  100. extras?: any;
  101. }
  102. interface IGLTFChildRootProperty extends IGLTFProperty {
  103. name?: string;
  104. }
  105. interface IGLTFAccessorSparseIndices extends IGLTFProperty {
  106. bufferView: number;
  107. byteOffset?: number;
  108. componentType: EComponentType;
  109. }
  110. interface IGLTFAccessorSparseValues extends IGLTFProperty {
  111. bufferView: number;
  112. byteOffset?: number;
  113. }
  114. interface IGLTFAccessorSparse extends IGLTFProperty {
  115. count: number;
  116. indices: IGLTFAccessorSparseIndices;
  117. values: IGLTFAccessorSparseValues;
  118. }
  119. interface IGLTFAccessor extends IGLTFChildRootProperty {
  120. bufferView?: number;
  121. byteOffset?: number;
  122. componentType: EComponentType;
  123. normalized?: boolean;
  124. count: number;
  125. type: string;
  126. max: number[];
  127. min: number[];
  128. sparse?: IGLTFAccessorSparse;
  129. index?: number;
  130. }
  131. interface IGLTFAnimationChannel extends IGLTFProperty {
  132. sampler: number;
  133. target: IGLTFAnimationChannelTarget;
  134. }
  135. interface IGLTFAnimationChannelTarget extends IGLTFProperty {
  136. node: number;
  137. path: string;
  138. }
  139. interface IGLTFAnimationSampler extends IGLTFProperty {
  140. input: number;
  141. interpolation?: string;
  142. output: number;
  143. }
  144. interface IGLTFAnimation extends IGLTFChildRootProperty {
  145. channels: IGLTFAnimationChannel[];
  146. samplers: IGLTFAnimationSampler[];
  147. index?: number;
  148. targets?: any[];
  149. }
  150. interface IGLTFAsset extends IGLTFChildRootProperty {
  151. copyright?: string;
  152. generator?: string;
  153. version: string;
  154. minVersion?: string;
  155. }
  156. interface IGLTFBuffer extends IGLTFChildRootProperty {
  157. uri?: string;
  158. byteLength: number;
  159. index?: number;
  160. loadedData?: ArrayBufferView;
  161. loadedObservable?: Observable<IGLTFBuffer>;
  162. }
  163. interface IGLTFBufferView extends IGLTFChildRootProperty {
  164. buffer: number;
  165. byteOffset?: number;
  166. byteLength: number;
  167. byteStride?: number;
  168. index?: number;
  169. }
  170. interface IGLTFCameraOrthographic extends IGLTFProperty {
  171. xmag: number;
  172. ymag: number;
  173. zfar: number;
  174. znear: number;
  175. }
  176. interface IGLTFCameraPerspective extends IGLTFProperty {
  177. aspectRatio: number;
  178. yfov: number;
  179. zfar: number;
  180. znear: number;
  181. }
  182. interface IGLTFCamera extends IGLTFChildRootProperty {
  183. orthographic?: IGLTFCameraOrthographic;
  184. perspective?: IGLTFCameraPerspective;
  185. type: string;
  186. }
  187. interface IGLTFImage extends IGLTFChildRootProperty {
  188. uri?: string;
  189. mimeType?: string;
  190. bufferView?: number;
  191. index?: number;
  192. }
  193. interface IGLTFMaterialNormalTextureInfo extends IGLTFTextureInfo {
  194. scale: number;
  195. }
  196. interface IGLTFMaterialOcclusionTextureInfo extends IGLTFTextureInfo {
  197. strength: number;
  198. }
  199. interface IGLTFMaterialPbrMetallicRoughness {
  200. baseColorFactor: number[];
  201. baseColorTexture: IGLTFTextureInfo;
  202. metallicFactor: number;
  203. roughnessFactor: number;
  204. metallicRoughnessTexture: IGLTFTextureInfo;
  205. }
  206. interface IGLTFMaterial extends IGLTFChildRootProperty {
  207. pbrMetallicRoughness?: IGLTFMaterialPbrMetallicRoughness;
  208. normalTexture?: IGLTFMaterialNormalTextureInfo;
  209. occlusionTexture?: IGLTFMaterialOcclusionTextureInfo;
  210. emissiveTexture?: IGLTFTextureInfo;
  211. emissiveFactor?: number[];
  212. alphaMode?: string;
  213. alphaCutoff: number;
  214. doubleSided?: boolean;
  215. index?: number;
  216. babylonMaterial?: Material;
  217. }
  218. interface IGLTFMeshPrimitive extends IGLTFProperty {
  219. attributes: {
  220. [name: string]: number;
  221. };
  222. indices?: number;
  223. material?: number;
  224. mode?: EMeshPrimitiveMode;
  225. targets?: {
  226. [name: string]: number;
  227. }[];
  228. }
  229. interface IGLTFMesh extends IGLTFChildRootProperty {
  230. primitives: IGLTFMeshPrimitive[];
  231. weights?: number[];
  232. index?: number;
  233. }
  234. interface IGLTFNode extends IGLTFChildRootProperty {
  235. camera?: number;
  236. children?: number[];
  237. skin?: number;
  238. matrix?: number[];
  239. mesh?: number;
  240. rotation?: number[];
  241. scale?: number[];
  242. translation?: number[];
  243. weights?: number[];
  244. index?: number;
  245. parent?: IGLTFNode;
  246. babylonMesh?: Mesh;
  247. babylonBones?: {
  248. [skin: number]: Bone;
  249. };
  250. babylonAnimationTargets?: Node[];
  251. }
  252. interface IGLTFSampler extends IGLTFChildRootProperty {
  253. magFilter?: ETextureMagFilter;
  254. minFilter?: ETextureMinFilter;
  255. wrapS?: ETextureWrapMode;
  256. wrapT?: ETextureWrapMode;
  257. }
  258. interface IGLTFScene extends IGLTFChildRootProperty {
  259. nodes: number[];
  260. index?: number;
  261. }
  262. interface IGLTFSkin extends IGLTFChildRootProperty {
  263. inverseBindMatrices?: number;
  264. skeleton?: number;
  265. joints: number[];
  266. index?: number;
  267. babylonSkeleton?: Skeleton;
  268. }
  269. interface IGLTFTexture extends IGLTFChildRootProperty {
  270. sampler?: number;
  271. source: number;
  272. index?: number;
  273. url?: string;
  274. dataReadyObservable?: Observable<IGLTFTexture>;
  275. }
  276. interface IGLTFTextureInfo {
  277. index: number;
  278. texCoord?: number;
  279. }
  280. interface IGLTF extends IGLTFProperty {
  281. accessors?: IGLTFAccessor[];
  282. animations?: IGLTFAnimation[];
  283. asset: IGLTFAsset;
  284. buffers?: IGLTFBuffer[];
  285. bufferViews?: IGLTFBufferView[];
  286. cameras?: IGLTFCamera[];
  287. extensionsUsed?: string[];
  288. extensionsRequired?: string[];
  289. images?: IGLTFImage[];
  290. materials?: IGLTFMaterial[];
  291. meshes?: IGLTFMesh[];
  292. nodes?: IGLTFNode[];
  293. samplers?: IGLTFSampler[];
  294. scene?: number;
  295. scenes?: IGLTFScene[];
  296. skins?: IGLTFSkin[];
  297. textures?: IGLTFTexture[];
  298. }
  299. }
  300. declare module BABYLON.GLTF2 {
  301. class GLTFLoader implements IGLTFLoader, IDisposable {
  302. _gltf: IGLTF;
  303. _babylonScene: Scene;
  304. private _parent;
  305. private _rootUrl;
  306. private _defaultMaterial;
  307. private _rootNode;
  308. private _successCallback;
  309. private _progressCallback;
  310. private _errorCallback;
  311. private _renderReady;
  312. private _disposed;
  313. private _renderReadyObservable;
  314. private _renderPendingCount;
  315. private _loaderPendingCount;
  316. private _loaderTrackers;
  317. static Extensions: {
  318. [name: string]: GLTFLoaderExtension;
  319. };
  320. static RegisterExtension(extension: GLTFLoaderExtension): void;
  321. constructor(parent: GLTFFileLoader);
  322. dispose(): void;
  323. 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;
  324. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  325. private _loadAsync(nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError);
  326. private _onError(message);
  327. private _onProgress(event);
  328. _executeWhenRenderReady(func: () => void): void;
  329. private _onRenderReady();
  330. private _onComplete();
  331. private _loadData(data);
  332. private _getMeshes();
  333. private _getSkeletons();
  334. private _getAnimationTargets();
  335. private _startAnimations();
  336. private _loadDefaultScene(nodeNames);
  337. private _loadScene(context, scene, nodeNames);
  338. _loadNode(context: string, node: IGLTFNode): void;
  339. private _loadMesh(context, node, mesh);
  340. private _loadPrimitive(context, node, mesh, primitive, onSuccess);
  341. private _loadVertexDataAsync(context, mesh, primitive, onSuccess);
  342. private _createMorphTargets(node, mesh, primitive);
  343. private _loadMorphTargetsData(context, mesh, primitive, vertexData, babylonMesh);
  344. private _loadTransform(node);
  345. private _loadSkin(context, skin);
  346. private _createBone(node, skin, parent, localMatrix, baseMatrix, index);
  347. private _loadBones(context, skin, inverseBindMatrixData);
  348. private _loadBone(node, skin, inverseBindMatrixData, babylonBones);
  349. private _getNodeMatrix(node);
  350. private _traverseNodes(context, indices, action, parentNode?);
  351. _traverseNode(context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode?: IGLTFNode): void;
  352. private _loadAnimations();
  353. private _loadAnimationChannel(animation, channelContext, channel, samplerContext, sampler);
  354. private _loadBufferAsync(context, buffer, onSuccess);
  355. private _loadBufferViewAsync(context, bufferView, onSuccess);
  356. private _loadAccessorAsync(context, accessor, onSuccess);
  357. private _getNumComponentsOfType(type);
  358. private _buildArrayBuffer<T>(typedArray, context, data, byteOffset, count, numComponents, byteStride);
  359. _addPendingData(data: any): void;
  360. _removePendingData(data: any): void;
  361. _addLoaderPendingData(data: any): void;
  362. _removeLoaderPendingData(data: any): void;
  363. _whenAction(action: () => void, onComplete: () => void): void;
  364. private _getDefaultMaterial();
  365. private _loadMaterialMetallicRoughnessProperties(context, material);
  366. _loadMaterial(context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): void;
  367. _createPbrMaterial(material: IGLTFMaterial): void;
  368. _loadMaterialBaseProperties(context: string, material: IGLTFMaterial): void;
  369. _loadMaterialAlphaProperties(context: string, material: IGLTFMaterial, colorFactor: number[]): void;
  370. _loadTexture(context: string, texture: IGLTFTexture, coordinatesIndex: number): Texture;
  371. private _loadImage(context, image, onSuccess);
  372. _tryCatchOnError(handler: () => void): void;
  373. }
  374. }
  375. declare module BABYLON.GLTF2 {
  376. /**
  377. * Utils functions for GLTF
  378. */
  379. class GLTFUtils {
  380. /**
  381. * If the uri is a base64 string
  382. * @param uri: the uri to test
  383. */
  384. static IsBase64(uri: string): boolean;
  385. /**
  386. * Decode the base64 uri
  387. * @param uri: the uri to decode
  388. */
  389. static DecodeBase64(uri: string): ArrayBuffer;
  390. static ForEach(view: Uint16Array | Uint32Array | Float32Array, func: (nvalue: number, index: number) => void): void;
  391. static ValidateUri(uri: string): boolean;
  392. static AssignIndices(array: Array<{
  393. index?: number;
  394. }>): void;
  395. static GetArrayItem<T>(array: ArrayLike<T>, index: number): T;
  396. static GetTextureWrapMode(mode: ETextureWrapMode): number;
  397. static GetTextureSamplingMode(magFilter: ETextureMagFilter, minFilter: ETextureMinFilter): number;
  398. /**
  399. * Decodes a buffer view into a string
  400. * @param view: the buffer view
  401. */
  402. static DecodeBufferToText(view: ArrayBufferView): string;
  403. }
  404. }
  405. declare module BABYLON.GLTF2 {
  406. abstract class GLTFLoaderExtension {
  407. enabled: boolean;
  408. readonly abstract name: string;
  409. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  410. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  411. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  412. protected _loadExtension<T>(property: IGLTFProperty, action: (extension: T, onComplete: () => void) => void): boolean;
  413. static _Extensions: GLTFLoaderExtension[];
  414. static TraverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  415. static LoadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  416. static LoadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  417. private static _ApplyExtensions(action);
  418. }
  419. }
  420. declare module BABYLON.GLTF2.Extensions {
  421. class MSFTLOD extends GLTFLoaderExtension {
  422. /**
  423. * Specify the minimal delay between LODs in ms (default = 250)
  424. */
  425. static MinimalLODDelay: number;
  426. readonly name: string;
  427. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  428. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  429. private _loadNodeLOD(loader, context, nodes, index, onComplete);
  430. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  431. private _loadMaterialLOD(loader, context, materials, index, assign, onComplete);
  432. }
  433. }
  434. declare module BABYLON.GLTF2.Extensions {
  435. class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
  436. readonly name: string;
  437. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  438. private _loadSpecularGlossinessProperties(loader, context, material, properties);
  439. }
  440. }