babylon.glTF2FileLoader.d.ts 16 KB

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