babylon.glTF2FileLoader.d.ts 14 KB

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