babylon.glTF2FileLoader.d.ts 15 KB

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