babylon.glTF2FileLoader.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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: Nullable<ArrayBufferView>;
  10. }
  11. interface IGLTFLoader extends IDisposable {
  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 IDisposable, ISceneLoaderPluginAsync, ISceneLoaderPluginFactory {
  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 the asset is completely loaded, just before the loader is disposed.
  30. * For assets with LODs, raised when all of the LODs are complete.
  31. * For assets without LODs, raised when the model is complete just after onSuccess.
  32. */
  33. onComplete: () => void;
  34. private _loader;
  35. name: string;
  36. extensions: ISceneLoaderPluginExtensions;
  37. dispose(): void;
  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. rewriteRootURL: (rootUrl: string, responseURL?: string) => string;
  42. createPlugin(): ISceneLoaderPlugin | ISceneLoaderPluginAsync;
  43. private static _parse(data);
  44. private _getLoader(loaderData);
  45. private static _parseBinary(data);
  46. private static _parseV1(binaryReader);
  47. private static _parseV2(binaryReader);
  48. private static _parseVersion(version);
  49. private static _compareVersion(a, b);
  50. private static _decodeBufferToText(buffer);
  51. }
  52. }
  53. declare module BABYLON.GLTF2 {
  54. /**
  55. * Enums
  56. */
  57. enum EComponentType {
  58. BYTE = 5120,
  59. UNSIGNED_BYTE = 5121,
  60. SHORT = 5122,
  61. UNSIGNED_SHORT = 5123,
  62. UNSIGNED_INT = 5125,
  63. FLOAT = 5126,
  64. }
  65. enum EMeshPrimitiveMode {
  66. POINTS = 0,
  67. LINES = 1,
  68. LINE_LOOP = 2,
  69. LINE_STRIP = 3,
  70. TRIANGLES = 4,
  71. TRIANGLE_STRIP = 5,
  72. TRIANGLE_FAN = 6,
  73. }
  74. enum ETextureMagFilter {
  75. NEAREST = 9728,
  76. LINEAR = 9729,
  77. }
  78. enum ETextureMinFilter {
  79. NEAREST = 9728,
  80. LINEAR = 9729,
  81. NEAREST_MIPMAP_NEAREST = 9984,
  82. LINEAR_MIPMAP_NEAREST = 9985,
  83. NEAREST_MIPMAP_LINEAR = 9986,
  84. LINEAR_MIPMAP_LINEAR = 9987,
  85. }
  86. enum ETextureWrapMode {
  87. CLAMP_TO_EDGE = 33071,
  88. MIRRORED_REPEAT = 33648,
  89. REPEAT = 10497,
  90. }
  91. /**
  92. * Interfaces
  93. */
  94. interface IGLTFProperty {
  95. extensions?: {
  96. [key: string]: any;
  97. };
  98. extras?: any;
  99. }
  100. interface IGLTFChildRootProperty extends IGLTFProperty {
  101. name?: string;
  102. }
  103. interface IGLTFAccessorSparseIndices extends IGLTFProperty {
  104. bufferView: number;
  105. byteOffset?: number;
  106. componentType: EComponentType;
  107. }
  108. interface IGLTFAccessorSparseValues extends IGLTFProperty {
  109. bufferView: number;
  110. byteOffset?: number;
  111. }
  112. interface IGLTFAccessorSparse extends IGLTFProperty {
  113. count: number;
  114. indices: IGLTFAccessorSparseIndices;
  115. values: IGLTFAccessorSparseValues;
  116. }
  117. interface IGLTFAccessor extends IGLTFChildRootProperty {
  118. bufferView?: number;
  119. byteOffset?: number;
  120. componentType: EComponentType;
  121. normalized?: boolean;
  122. count: number;
  123. type: string;
  124. max: number[];
  125. min: number[];
  126. sparse?: IGLTFAccessorSparse;
  127. index: number;
  128. }
  129. interface IGLTFAnimationChannel extends IGLTFProperty {
  130. sampler: number;
  131. target: IGLTFAnimationChannelTarget;
  132. }
  133. interface IGLTFAnimationChannelTarget extends IGLTFProperty {
  134. node: number;
  135. path: string;
  136. }
  137. interface IGLTFAnimationSampler extends IGLTFProperty {
  138. input: number;
  139. interpolation?: string;
  140. output: number;
  141. }
  142. interface IGLTFAnimation extends IGLTFChildRootProperty {
  143. channels: IGLTFAnimationChannel[];
  144. samplers: IGLTFAnimationSampler[];
  145. index: number;
  146. targets: any[];
  147. }
  148. interface IGLTFAsset extends IGLTFChildRootProperty {
  149. copyright?: string;
  150. generator?: string;
  151. version: string;
  152. minVersion?: string;
  153. }
  154. interface IGLTFBuffer extends IGLTFChildRootProperty {
  155. uri?: string;
  156. byteLength: number;
  157. index: number;
  158. loadedData?: ArrayBufferView;
  159. loadedObservable?: Observable<IGLTFBuffer>;
  160. }
  161. interface IGLTFBufferView extends IGLTFChildRootProperty {
  162. buffer: number;
  163. byteOffset?: number;
  164. byteLength: number;
  165. byteStride?: number;
  166. index: number;
  167. }
  168. interface IGLTFCameraOrthographic extends IGLTFProperty {
  169. xmag: number;
  170. ymag: number;
  171. zfar: number;
  172. znear: number;
  173. }
  174. interface IGLTFCameraPerspective extends IGLTFProperty {
  175. aspectRatio: number;
  176. yfov: number;
  177. zfar: number;
  178. znear: number;
  179. }
  180. interface IGLTFCamera extends IGLTFChildRootProperty {
  181. orthographic?: IGLTFCameraOrthographic;
  182. perspective?: IGLTFCameraPerspective;
  183. type: string;
  184. }
  185. interface IGLTFImage extends IGLTFChildRootProperty {
  186. uri?: string;
  187. mimeType?: string;
  188. bufferView?: number;
  189. index: number;
  190. }
  191. interface IGLTFMaterialNormalTextureInfo extends IGLTFTextureInfo {
  192. scale: number;
  193. }
  194. interface IGLTFMaterialOcclusionTextureInfo extends IGLTFTextureInfo {
  195. strength: number;
  196. }
  197. interface IGLTFMaterialPbrMetallicRoughness {
  198. baseColorFactor: number[];
  199. baseColorTexture: IGLTFTextureInfo;
  200. metallicFactor: number;
  201. roughnessFactor: number;
  202. metallicRoughnessTexture: IGLTFTextureInfo;
  203. }
  204. interface IGLTFMaterial extends IGLTFChildRootProperty {
  205. pbrMetallicRoughness?: IGLTFMaterialPbrMetallicRoughness;
  206. normalTexture?: IGLTFMaterialNormalTextureInfo;
  207. occlusionTexture?: IGLTFMaterialOcclusionTextureInfo;
  208. emissiveTexture?: IGLTFTextureInfo;
  209. emissiveFactor?: number[];
  210. alphaMode?: string;
  211. alphaCutoff: number;
  212. doubleSided?: boolean;
  213. index: number;
  214. babylonMaterial: Material;
  215. }
  216. interface IGLTFMeshPrimitive extends IGLTFProperty {
  217. attributes: {
  218. [name: string]: number;
  219. };
  220. indices?: number;
  221. material?: number;
  222. mode?: EMeshPrimitiveMode;
  223. targets?: {
  224. [name: string]: number;
  225. }[];
  226. vertexData: VertexData;
  227. targetsVertexData: VertexData[];
  228. }
  229. interface IGLTFMesh extends IGLTFChildRootProperty {
  230. primitives: IGLTFMeshPrimitive[];
  231. weights?: number[];
  232. index: number;
  233. }
  234. interface IGLTFNode extends IGLTFChildRootProperty {
  235. camera?: number;
  236. children?: number[];
  237. skin?: number;
  238. matrix?: number[];
  239. mesh?: number;
  240. rotation?: number[];
  241. scale?: number[];
  242. translation?: number[];
  243. weights?: number[];
  244. index: number;
  245. parent?: IGLTFNode;
  246. babylonMesh: Mesh;
  247. babylonBones?: {
  248. [skin: number]: Bone;
  249. };
  250. babylonAnimationTargets?: Node[];
  251. }
  252. interface IGLTFSampler extends IGLTFChildRootProperty {
  253. magFilter?: ETextureMagFilter;
  254. minFilter?: ETextureMinFilter;
  255. wrapS?: ETextureWrapMode;
  256. wrapT?: ETextureWrapMode;
  257. }
  258. interface IGLTFScene extends IGLTFChildRootProperty {
  259. nodes: number[];
  260. index: number;
  261. }
  262. interface IGLTFSkin extends IGLTFChildRootProperty {
  263. inverseBindMatrices?: number;
  264. skeleton?: number;
  265. joints: number[];
  266. index: number;
  267. babylonSkeleton: Skeleton;
  268. }
  269. interface IGLTFTexture extends IGLTFChildRootProperty {
  270. sampler?: number;
  271. source: number;
  272. index: number;
  273. url?: string;
  274. dataReadyObservable?: Observable<IGLTFTexture>;
  275. }
  276. interface IGLTFTextureInfo {
  277. index: number;
  278. texCoord?: number;
  279. }
  280. interface IGLTF extends IGLTFProperty {
  281. accessors?: IGLTFAccessor[];
  282. animations?: IGLTFAnimation[];
  283. asset: IGLTFAsset;
  284. buffers?: IGLTFBuffer[];
  285. bufferViews?: IGLTFBufferView[];
  286. cameras?: IGLTFCamera[];
  287. extensionsUsed?: string[];
  288. extensionsRequired?: string[];
  289. images?: IGLTFImage[];
  290. materials?: IGLTFMaterial[];
  291. meshes?: IGLTFMesh[];
  292. nodes?: IGLTFNode[];
  293. samplers?: IGLTFSampler[];
  294. scene?: number;
  295. scenes?: IGLTFScene[];
  296. skins?: IGLTFSkin[];
  297. textures?: IGLTFTexture[];
  298. }
  299. }
  300. declare module BABYLON.GLTF2 {
  301. class GLTFLoader implements IGLTFLoader {
  302. _gltf: IGLTF;
  303. _babylonScene: Scene;
  304. private _disposed;
  305. private _parent;
  306. private _rootUrl;
  307. private _defaultMaterial;
  308. private _rootNode;
  309. private _successCallback;
  310. private _progressCallback;
  311. private _errorCallback;
  312. private _renderReady;
  313. private _requests;
  314. private _renderReadyObservable;
  315. private _renderPendingCount;
  316. private _loaderPendingCount;
  317. private _loaderTrackers;
  318. static Extensions: {
  319. [name: string]: GLTFLoaderExtension;
  320. };
  321. static RegisterExtension(extension: GLTFLoaderExtension): void;
  322. constructor(parent: GLTFFileLoader);
  323. dispose(): void;
  324. 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;
  325. loadAsync(scene: Scene, data: IGLTFLoaderData, rootUrl: string, onSuccess: () => void, onProgress: (event: ProgressEvent) => void, onError: (message: string) => void): void;
  326. private _loadAsync(nodeNames, scene, data, rootUrl, onSuccess, onProgress, onError);
  327. private _onProgress(event);
  328. _executeWhenRenderReady(func: () => void): void;
  329. private _onRenderReady();
  330. private _onComplete();
  331. private _loadData(data);
  332. private _getMeshes();
  333. private _getSkeletons();
  334. private _getAnimationTargets();
  335. private _startAnimations();
  336. private _loadDefaultScene(nodeNames);
  337. private _loadScene(context, scene, nodeNames);
  338. _loadNode(context: string, node: IGLTFNode): void;
  339. private _loadMesh(context, node, mesh);
  340. private _loadAllVertexDataAsync(context, mesh, onSuccess);
  341. /**
  342. * Converts a data bufferview into a Float4 Texture Coordinate Array, based on the accessor component type
  343. * @param {ArrayBufferView} data
  344. * @param {IGLTFAccessor} accessor
  345. */
  346. private _convertToFloat4TextureCoordArray(context, data, accessor);
  347. /**
  348. * Converts a data bufferview into a Float4 Color Array, based on the accessor component type
  349. * @param {ArrayBufferView} data
  350. * @param {IGLTFAccessor} accessor
  351. */
  352. private _convertToFloat4ColorArray(context, data, accessor);
  353. private _loadVertexDataAsync(context, mesh, primitive, onSuccess);
  354. private _createMorphTargets(context, node, mesh);
  355. private _loadMorphTargets(context, node, mesh);
  356. private _loadAllMorphTargetVertexDataAsync(context, node, mesh, onSuccess);
  357. private _loadMorphTargetVertexDataAsync(context, vertexData, attributes, onSuccess);
  358. private _loadTransform(node);
  359. private _loadSkin(context, skin);
  360. private _createBone(node, skin, parent, localMatrix, baseMatrix, index);
  361. private _loadBones(context, skin, inverseBindMatrixData);
  362. private _loadBone(node, skin, inverseBindMatrixData, babylonBones);
  363. private _getNodeMatrix(node);
  364. private _traverseNodes(context, indices, action, parentNode?);
  365. _traverseNode(context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: Nullable<IGLTFNode>) => boolean, parentNode?: Nullable<IGLTFNode>): void;
  366. private _loadAnimations();
  367. private _loadAnimation(context, animation);
  368. private _loadAnimationChannel(animation, channelContext, channel, samplerContext, sampler);
  369. private _loadBufferAsync(context, buffer, onSuccess);
  370. private _loadBufferViewAsync(context, bufferView, onSuccess);
  371. private _loadAccessorAsync(context, accessor, onSuccess);
  372. private _buildArrayBuffer<T>(typedArray, data, byteOffset, count, numComponents, byteStride?);
  373. _addPendingData(data: any): void;
  374. _removePendingData(data: any): void;
  375. _addLoaderPendingData(data: any): void;
  376. _removeLoaderPendingData(data: any): void;
  377. _whenAction(action: () => void, onComplete: () => void): void;
  378. private _getDefaultMaterial();
  379. private _loadMaterialMetallicRoughnessProperties(context, material);
  380. _loadMaterial(context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): void;
  381. _createPbrMaterial(material: IGLTFMaterial): void;
  382. _loadMaterialBaseProperties(context: string, material: IGLTFMaterial): void;
  383. _loadMaterialAlphaProperties(context: string, material: IGLTFMaterial, colorFactor: number[]): void;
  384. _loadTexture(context: string, texture: IGLTFTexture, coordinatesIndex?: number): Texture;
  385. private _loadImage(context, image, onSuccess);
  386. _loadUri(context: string, uri: string, onSuccess: (data: ArrayBufferView) => void): void;
  387. _tryCatchOnError(handler: () => void): void;
  388. private static _AssignIndices(array?);
  389. static _GetProperty<T extends IGLTFProperty>(array?: ArrayLike<T>, index?: number): Nullable<T>;
  390. private static _GetTextureWrapMode(mode?);
  391. private static _GetTextureSamplingMode(magFilter?, minFilter?);
  392. private static _GetNumComponents(type);
  393. }
  394. }
  395. declare module BABYLON.GLTF2 {
  396. /**
  397. * Utils functions for GLTF
  398. */
  399. class GLTFUtils {
  400. /**
  401. * If the uri is a base64 string
  402. * @param uri: the uri to test
  403. */
  404. static IsBase64(uri: string): boolean;
  405. /**
  406. * Decode the base64 uri
  407. * @param uri: the uri to decode
  408. */
  409. static DecodeBase64(uri: string): ArrayBuffer;
  410. static ValidateUri(uri: string): boolean;
  411. }
  412. }
  413. declare module BABYLON.GLTF2 {
  414. abstract class GLTFLoaderExtension {
  415. enabled: boolean;
  416. readonly abstract name: string;
  417. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: Nullable<IGLTFNode>): boolean;
  418. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  419. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  420. protected _loadExtension<T>(context: string, property: IGLTFProperty, action: (context: string, extension: T, onComplete: () => void) => void): boolean;
  421. static _Extensions: GLTFLoaderExtension[];
  422. static TraverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: Nullable<IGLTFNode>): boolean;
  423. static LoadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  424. static LoadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  425. private static _ApplyExtensions(action);
  426. }
  427. }
  428. declare module BABYLON.GLTF2.Extensions {
  429. class MSFTLOD extends GLTFLoaderExtension {
  430. /**
  431. * Specify the minimal delay between LODs in ms (default = 250)
  432. */
  433. static MinimalLODDelay: number;
  434. readonly name: string;
  435. protected _traverseNode(loader: GLTFLoader, context: string, node: IGLTFNode, action: (node: IGLTFNode, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  436. protected _loadNode(loader: GLTFLoader, context: string, node: IGLTFNode): boolean;
  437. private _loadNodeLOD(loader, context, nodes, index, onComplete);
  438. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  439. private _loadMaterialLOD(loader, context, materials, index, assign, onComplete);
  440. }
  441. }
  442. declare module BABYLON.GLTF2.Extensions {
  443. class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
  444. readonly name: string;
  445. protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  446. private _loadSpecularGlossinessProperties(loader, context, material, properties);
  447. }
  448. }