babylon.glTF2FileLoader.d.ts 15 KB

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