babylon.glTF2FileLoader.d.ts 14 KB

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