babylon.glTF2FileLoader.d.ts 18 KB

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