babylon.glTF2FileLoader.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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: Nullable<ArrayBufferView>;
  10. }
  11. interface IGLTFLoader extends IDisposable {
  12. importMeshAsync: (meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: (meshes: Nullable<AbstractMesh[]>, particleSystems: Nullable<ParticleSystem[]>, skeletons: Nullable<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 IDisposable, 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 asset is completely loaded, just before the loader is disposed.
  30. * For assets with LODs, raised when all of the LODs are complete.
  31. * For assets without LODs, raised when the model is complete just after onSuccess.
  32. */
  33. onComplete: () => void;
  34. private _loader;
  35. name: string;
  36. extensions: ISceneLoaderPluginExtensions;
  37. dispose(): void;
  38. 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;
  39. loadAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  40. canDirectLoad(data: string): boolean;
  41. private static _parse(data, onError);
  42. private _getLoader(loaderData, onError);
  43. private static _parseBinary(data, onError);
  44. private static _parseV1(binaryReader, onError);
  45. private static _parseV2(binaryReader, onError);
  46. private static _parseVersion(version);
  47. private static _compareVersion(a, b);
  48. private static _decodeBufferToText(buffer);
  49. }
  50. }
  51. declare module BABYLON.GLTF2 {
  52. /**
  53. * Enums
  54. */
  55. enum EComponentType {
  56. BYTE = 5120,
  57. UNSIGNED_BYTE = 5121,
  58. SHORT = 5122,
  59. UNSIGNED_SHORT = 5123,
  60. UNSIGNED_INT = 5125,
  61. FLOAT = 5126,
  62. }
  63. enum EMeshPrimitiveMode {
  64. POINTS = 0,
  65. LINES = 1,
  66. LINE_LOOP = 2,
  67. LINE_STRIP = 3,
  68. TRIANGLES = 4,
  69. TRIANGLE_STRIP = 5,
  70. TRIANGLE_FAN = 6,
  71. }
  72. enum ETextureMagFilter {
  73. NEAREST = 9728,
  74. LINEAR = 9729,
  75. }
  76. enum ETextureMinFilter {
  77. NEAREST = 9728,
  78. LINEAR = 9729,
  79. NEAREST_MIPMAP_NEAREST = 9984,
  80. LINEAR_MIPMAP_NEAREST = 9985,
  81. NEAREST_MIPMAP_LINEAR = 9986,
  82. LINEAR_MIPMAP_LINEAR = 9987,
  83. }
  84. enum ETextureWrapMode {
  85. CLAMP_TO_EDGE = 33071,
  86. MIRRORED_REPEAT = 33648,
  87. REPEAT = 10497,
  88. }
  89. /**
  90. * Interfaces
  91. */
  92. interface IGLTFProperty {
  93. extensions?: {
  94. [key: string]: any;
  95. };
  96. extras?: any;
  97. }
  98. interface IGLTFChildRootProperty extends IGLTFProperty {
  99. name?: string;
  100. }
  101. interface IGLTFAccessorSparseIndices extends IGLTFProperty {
  102. bufferView: number;
  103. byteOffset?: number;
  104. componentType: EComponentType;
  105. }
  106. interface IGLTFAccessorSparseValues extends IGLTFProperty {
  107. bufferView: number;
  108. byteOffset?: number;
  109. }
  110. interface IGLTFAccessorSparse extends IGLTFProperty {
  111. count: number;
  112. indices: IGLTFAccessorSparseIndices;
  113. values: IGLTFAccessorSparseValues;
  114. }
  115. interface IGLTFAccessor extends IGLTFChildRootProperty {
  116. bufferView?: number;
  117. byteOffset?: number;
  118. componentType: EComponentType;
  119. normalized?: boolean;
  120. count: number;
  121. type: string;
  122. max: number[];
  123. min: number[];
  124. sparse?: IGLTFAccessorSparse;
  125. index?: number;
  126. }
  127. interface IGLTFAnimationChannel extends IGLTFProperty {
  128. sampler: number;
  129. target: IGLTFAnimationChannelTarget;
  130. }
  131. interface IGLTFAnimationChannelTarget extends IGLTFProperty {
  132. node: number;
  133. path: string;
  134. }
  135. interface IGLTFAnimationSampler extends IGLTFProperty {
  136. input: number;
  137. interpolation?: string;
  138. output: number;
  139. }
  140. interface IGLTFAnimation extends IGLTFChildRootProperty {
  141. channels: IGLTFAnimationChannel[];
  142. samplers: IGLTFAnimationSampler[];
  143. index?: number;
  144. targets?: any[];
  145. }
  146. interface IGLTFAsset extends IGLTFChildRootProperty {
  147. copyright?: string;
  148. generator?: string;
  149. version: string;
  150. minVersion?: string;
  151. }
  152. interface IGLTFBuffer extends IGLTFChildRootProperty {
  153. uri?: string;
  154. byteLength: number;
  155. index?: number;
  156. loadedData?: ArrayBufferView;
  157. loadedObservable?: Observable<IGLTFBuffer>;
  158. }
  159. interface IGLTFBufferView extends IGLTFChildRootProperty {
  160. buffer: number;
  161. byteOffset?: number;
  162. byteLength: number;
  163. byteStride?: number;
  164. index?: number;
  165. }
  166. interface IGLTFCameraOrthographic extends IGLTFProperty {
  167. xmag: number;
  168. ymag: number;
  169. zfar: number;
  170. znear: number;
  171. }
  172. interface IGLTFCameraPerspective extends IGLTFProperty {
  173. aspectRatio: number;
  174. yfov: number;
  175. zfar: number;
  176. znear: number;
  177. }
  178. interface IGLTFCamera extends IGLTFChildRootProperty {
  179. orthographic?: IGLTFCameraOrthographic;
  180. perspective?: IGLTFCameraPerspective;
  181. type: string;
  182. }
  183. interface IGLTFImage extends IGLTFChildRootProperty {
  184. uri?: string;
  185. mimeType?: string;
  186. bufferView?: number;
  187. index?: number;
  188. }
  189. interface IGLTFMaterialNormalTextureInfo extends IGLTFTextureInfo {
  190. scale: number;
  191. }
  192. interface IGLTFMaterialOcclusionTextureInfo extends IGLTFTextureInfo {
  193. strength: number;
  194. }
  195. interface IGLTFMaterialPbrMetallicRoughness {
  196. baseColorFactor: number[];
  197. baseColorTexture: IGLTFTextureInfo;
  198. metallicFactor: number;
  199. roughnessFactor: number;
  200. metallicRoughnessTexture: IGLTFTextureInfo;
  201. }
  202. interface IGLTFMaterial extends IGLTFChildRootProperty {
  203. pbrMetallicRoughness?: IGLTFMaterialPbrMetallicRoughness;
  204. normalTexture?: IGLTFMaterialNormalTextureInfo;
  205. occlusionTexture?: IGLTFMaterialOcclusionTextureInfo;
  206. emissiveTexture?: IGLTFTextureInfo;
  207. emissiveFactor?: number[];
  208. alphaMode?: string;
  209. alphaCutoff: number;
  210. doubleSided?: boolean;
  211. index?: number;
  212. babylonMaterial?: Material;
  213. }
  214. interface IGLTFMeshPrimitive extends IGLTFProperty {
  215. attributes: {
  216. [name: string]: number;
  217. };
  218. indices?: number;
  219. material?: number;
  220. mode?: EMeshPrimitiveMode;
  221. targets?: {
  222. [name: string]: number;
  223. }[];
  224. vertexData: VertexData;
  225. targetsVertexData: VertexData[];
  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 {
  300. _gltf: IGLTF;
  301. _babylonScene: Scene;
  302. private _disposed;
  303. private _parent;
  304. private _rootUrl;
  305. private _defaultMaterial;
  306. private _rootNode;
  307. private _successCallback;
  308. private _progressCallback;
  309. private _errorCallback;
  310. private _renderReady;
  311. private _requests;
  312. private _renderReadyObservable;
  313. private _renderPendingCount;
  314. private _loaderPendingCount;
  315. private _loaderTrackers;
  316. static Extensions: {
  317. [name: string]: GLTFLoaderExtension;
  318. };
  319. static RegisterExtension(extension: GLTFLoaderExtension): void;
  320. constructor(parent: GLTFFileLoader);
  321. dispose(): void;
  322. importMeshAsync(meshesNames: any, scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: (meshes: Nullable<AbstractMesh[]>, particleSystems: Nullable<ParticleSystem[]>, skeletons: Nullable<Skeleton[]>) => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  323. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  324. private _loadAsync(nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError);
  325. private _onError(message);
  326. private _onProgress(event);
  327. _executeWhenRenderReady(func: () => void): void;
  328. private _onRenderReady();
  329. private _onComplete();
  330. private _loadData(data);
  331. private _getMeshes();
  332. private _getSkeletons();
  333. private _getAnimationTargets();
  334. private _startAnimations();
  335. private _loadDefaultScene(nodeNames);
  336. private _loadScene(context, scene, nodeNames);
  337. _loadNode(context: string, node: IGLTFNode): void;
  338. private _loadMesh(context, node, mesh);
  339. private _loadAllVertexDataAsync(context, mesh, onSuccess);
  340. private _loadVertexDataAsync(context, mesh, primitive, onSuccess);
  341. private _createMorphTargets(context, node, mesh);
  342. private _loadMorphTargets(context, node, mesh);
  343. private _loadAllMorphTargetVertexDataAsync(context, node, mesh, onSuccess);
  344. private _loadMorphTargetVertexDataAsync(context, vertexData, attributes, onSuccess);
  345. private _loadTransform(node);
  346. private _loadSkin(context, skin);
  347. private _createBone(node, skin, parent, localMatrix, baseMatrix, index);
  348. private _loadBones(context, skin, inverseBindMatrixData);
  349. private _loadBone(node, skin, inverseBindMatrixData, babylonBones);
  350. private _getNodeMatrix(node);
  351. private _traverseNodes(context, indices, action, parentNode?);
  352. _traverseNode(context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: Nullable<IGLTFNode>) => boolean, parentNode?: Nullable<IGLTFNode>): void;
  353. private _loadAnimations();
  354. private _loadAnimationChannel(animation, channelContext, channel, samplerContext, sampler);
  355. private _loadBufferAsync(context, buffer, onSuccess);
  356. private _loadBufferViewAsync(context, bufferView, onSuccess);
  357. private _loadAccessorAsync(context, accessor, onSuccess);
  358. private _getNumComponentsOfType(type);
  359. private _buildArrayBuffer<T>(typedArray, data, byteOffset, count, numComponents, byteStride);
  360. _addPendingData(data: any): void;
  361. _removePendingData(data: any): void;
  362. _addLoaderPendingData(data: any): void;
  363. _removeLoaderPendingData(data: any): void;
  364. _whenAction(action: () => void, onComplete: () => void): void;
  365. private _getDefaultMaterial();
  366. private _loadMaterialMetallicRoughnessProperties(context, material);
  367. _loadMaterial(context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): void;
  368. _createPbrMaterial(material: IGLTFMaterial): void;
  369. _loadMaterialBaseProperties(context: string, material: IGLTFMaterial): void;
  370. _loadMaterialAlphaProperties(context: string, material: IGLTFMaterial, colorFactor: number[]): void;
  371. _loadTexture(context: string, texture: IGLTFTexture, coordinatesIndex: number): Texture;
  372. private _loadImage(context, image, onSuccess);
  373. _loadUri(context: string, uri: string, onSuccess: (data: ArrayBufferView) => void): void;
  374. _tryCatchOnError(handler: () => void): void;
  375. }
  376. }
  377. declare module BABYLON.GLTF2 {
  378. /**
  379. * Utils functions for GLTF
  380. */
  381. class GLTFUtils {
  382. /**
  383. * If the uri is a base64 string
  384. * @param uri: the uri to test
  385. */
  386. static IsBase64(uri: string): boolean;
  387. /**
  388. * Decode the base64 uri
  389. * @param uri: the uri to decode
  390. */
  391. static DecodeBase64(uri: string): ArrayBuffer;
  392. static ValidateUri(uri: string): boolean;
  393. static AssignIndices(array?: Array<{
  394. index?: number;
  395. }>): void;
  396. static GetArrayItem<T>(array: Nullable<ArrayLike<T>> | undefined, index: number): Nullable<T>;
  397. static GetTextureWrapMode(mode: ETextureWrapMode): number;
  398. static GetTextureSamplingMode(magFilter?: ETextureMagFilter, minFilter?: ETextureMinFilter): number;
  399. }
  400. }
  401. declare module BABYLON.GLTF2 {
  402. abstract class GLTFLoaderExtension {
  403. enabled: boolean;
  404. readonly abstract name: string;
  405. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: Nullable<IGLTFNode>): boolean;
  406. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  407. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  408. protected _loadExtension<T>(property: IGLTFProperty, action: (extension: T, onComplete: () => void) => void): boolean;
  409. static _Extensions: GLTFLoaderExtension[];
  410. static TraverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: Nullable<IGLTFNode>): boolean;
  411. static LoadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  412. static LoadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  413. private static _ApplyExtensions(action);
  414. }
  415. }
  416. declare module BABYLON.GLTF2.Extensions {
  417. class MSFTLOD extends GLTFLoaderExtension {
  418. /**
  419. * Specify the minimal delay between LODs in ms (default = 250)
  420. */
  421. static MinimalLODDelay: number;
  422. readonly name: string;
  423. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  424. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  425. private _loadNodeLOD(loader, context, nodes, index, onComplete);
  426. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  427. private _loadMaterialLOD(loader, context, materials, index, assign, onComplete);
  428. }
  429. }
  430. declare module BABYLON.GLTF2.Extensions {
  431. class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
  432. readonly name: string;
  433. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  434. private _loadSpecularGlossinessProperties(loader, context, material, properties);
  435. }
  436. }