babylon.glTF2FileLoader.d.ts 16 KB

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