babylon.glTF2FileLoader.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. extensions: ISceneLoaderPluginExtensions;
  25. 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;
  26. loadAsync(scene: Scene, data: string | ArrayBuffer, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  27. canDirectLoad(data: string): boolean;
  28. private static _parse(data, onError);
  29. private _getLoader(loaderData, onError);
  30. private static _parseBinary(data, onError);
  31. private static _parseV1(binaryReader, onError);
  32. private static _parseV2(binaryReader, onError);
  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. }
  248. interface IGLTFTextureInfo {
  249. index: number;
  250. texCoord?: number;
  251. }
  252. interface IGLTF extends IGLTFProperty {
  253. accessors?: IGLTFAccessor[];
  254. animations?: IGLTFAnimation[];
  255. asset: IGLTFAsset;
  256. buffers?: IGLTFBuffer[];
  257. bufferViews?: IGLTFBufferView[];
  258. cameras?: IGLTFCamera[];
  259. extensionsUsed?: string[];
  260. extensionsRequired?: string[];
  261. glExtensionsUsed?: string[];
  262. images?: IGLTFImage[];
  263. materials?: IGLTFMaterial[];
  264. meshes?: IGLTFMesh[];
  265. nodes?: IGLTFNode[];
  266. samplers?: IGLTFSampler[];
  267. scene?: number;
  268. scenes?: IGLTFScene[];
  269. skins?: IGLTFSkin[];
  270. textures?: IGLTFTexture[];
  271. }
  272. }
  273. declare module BABYLON.GLTF2 {
  274. class GLTFLoader implements IGLTFLoader, IDisposable {
  275. private _parent;
  276. private _gltf;
  277. private _babylonScene;
  278. private _rootUrl;
  279. private _defaultMaterial;
  280. private _successCallback;
  281. private _progressCallback;
  282. private _errorCallback;
  283. private _renderReady;
  284. private _disposed;
  285. private _objectURLs;
  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: () => void): void;
  296. constructor(parent: GLTFFileLoader);
  297. dispose(): void;
  298. 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;
  299. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  300. private _loadAsync(nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError);
  301. private _onError(message);
  302. private _onProgress(event);
  303. private _onRenderReady();
  304. private _onLoaderComplete();
  305. private _loadData(data);
  306. private _addRightHandToLeftHandRootTransform();
  307. private _showMeshes();
  308. private _startAnimations();
  309. private _loadScene(nodeNames);
  310. private _loadSkin(node);
  311. private _updateBone(node, parentNode, skin, inverseBindMatrixData);
  312. private _createBone(node, skin);
  313. private _loadMesh(node);
  314. private _loadMeshData(node, mesh, babylonMesh);
  315. private _assignMaterial(multiMaterial, index, subMaterial);
  316. private _loadVertexDataAsync(primitive, onSuccess);
  317. private _createMorphTargets(node, mesh, primitive, babylonMesh);
  318. private _loadMorphTargetsData(mesh, primitive, vertexData, babylonMesh);
  319. private _loadTransform(node, babylonMesh);
  320. private _traverseNodes(indices, action, parentNode?);
  321. private _traverseNode(index, action, parentNode?);
  322. private _loadAnimations();
  323. private _loadAnimationChannel(animation, animationIndex, channelIndex);
  324. private _loadBufferAsync(index, onSuccess);
  325. private _loadBufferViewAsync(bufferView, byteOffset, byteLength, componentType, onSuccess);
  326. private _loadAccessorAsync(accessor, onSuccess);
  327. private _getByteStrideFromType(accessor);
  328. addPendingData(data: any): void;
  329. removePendingData(data: any): void;
  330. addLoaderPendingData(data: any): void;
  331. removeLoaderPendingData(data: any): void;
  332. private _getDefaultMaterial();
  333. private _loadMaterialMetallicRoughnessProperties(material);
  334. loadMaterial(index: number, assign: (material: Material) => void): void;
  335. createPbrMaterial(material: IGLTFMaterial): void;
  336. loadMaterialBaseProperties(material: IGLTFMaterial): void;
  337. loadMaterialAlphaProperties(material: IGLTFMaterial, colorFactor?: number[]): void;
  338. loadTexture(textureInfo: IGLTFTextureInfo): Texture;
  339. }
  340. }
  341. declare module BABYLON.GLTF2 {
  342. /**
  343. * Utils functions for GLTF
  344. */
  345. class GLTFUtils {
  346. /**
  347. * If the uri is a base64 string
  348. * @param uri: the uri to test
  349. */
  350. static IsBase64(uri: string): boolean;
  351. /**
  352. * Decode the base64 uri
  353. * @param uri: the uri to decode
  354. */
  355. static DecodeBase64(uri: string): ArrayBuffer;
  356. static ForEach(view: Uint16Array | Uint32Array | Float32Array, func: (nvalue: number, index: number) => void): void;
  357. static GetTextureWrapMode(mode: ETextureWrapMode): number;
  358. static GetTextureSamplingMode(magFilter: ETextureMagFilter, minFilter: ETextureMinFilter): number;
  359. /**
  360. * Decodes a buffer view into a string
  361. * @param view: the buffer view
  362. */
  363. static DecodeBufferToText(view: ArrayBufferView): string;
  364. }
  365. }
  366. declare module BABYLON.GLTF2 {
  367. abstract class GLTFLoaderExtension {
  368. enabled: boolean;
  369. readonly abstract name: string;
  370. protected loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (material: Material) => void): boolean;
  371. static _Extensions: GLTFLoaderExtension[];
  372. static LoadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (material: Material) => void): boolean;
  373. private static _ApplyExtensions(action);
  374. }
  375. }
  376. declare module BABYLON.GLTF2.Extensions {
  377. class MSFTLOD extends GLTFLoaderExtension {
  378. readonly name: string;
  379. protected loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (material: Material) => void): boolean;
  380. private loadMaterialLOD(loader, material, materialLODs, lod, assign);
  381. }
  382. }
  383. declare module BABYLON.GLTF2.Extensions {
  384. class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
  385. readonly name: string;
  386. protected loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (material: Material) => void): boolean;
  387. private _loadSpecularGlossinessProperties(loader, material, properties);
  388. }
  389. }