babylon.glTF2FileLoader.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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(view);
  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?: Object;
  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. }
  128. interface IGLTFAnimationChannel extends IGLTFProperty {
  129. sampler: number;
  130. target: IGLTFAnimationChannelTarget;
  131. }
  132. interface IGLTFAnimationChannelTarget extends IGLTFProperty {
  133. node: number;
  134. path: string;
  135. }
  136. interface IGLTFAnimationSampler extends IGLTFProperty {
  137. input: number;
  138. interpolation?: string;
  139. output: number;
  140. }
  141. interface IGLTFAnimation extends IGLTFChildRootProperty {
  142. channels: IGLTFAnimationChannel[];
  143. samplers: IGLTFAnimationSampler[];
  144. targets?: any[];
  145. }
  146. interface IGLTFAsset extends IGLTFChildRootProperty {
  147. copyright?: string;
  148. generator?: string;
  149. version: string;
  150. minVersion?: string;
  151. }
  152. interface IGLTFBuffer extends IGLTFChildRootProperty {
  153. uri?: string;
  154. byteLength: number;
  155. loadedData: ArrayBufferView;
  156. loadedObservable: Observable<IGLTFBuffer>;
  157. }
  158. interface IGLTFBufferView extends IGLTFChildRootProperty {
  159. buffer: number;
  160. byteOffset?: number;
  161. byteLength: number;
  162. byteStride?: number;
  163. }
  164. interface IGLTFCameraOrthographic extends IGLTFProperty {
  165. xmag: number;
  166. ymag: number;
  167. zfar: number;
  168. znear: number;
  169. }
  170. interface IGLTFCameraPerspective extends IGLTFProperty {
  171. aspectRatio: number;
  172. yfov: number;
  173. zfar: number;
  174. znear: number;
  175. }
  176. interface IGLTFCamera extends IGLTFChildRootProperty {
  177. orthographic?: IGLTFCameraOrthographic;
  178. perspective?: IGLTFCameraPerspective;
  179. type: string;
  180. }
  181. interface IGLTFImage extends IGLTFChildRootProperty {
  182. uri?: string;
  183. mimeType?: string;
  184. bufferView?: number;
  185. }
  186. interface IGLTFMaterialNormalTextureInfo extends IGLTFTextureInfo {
  187. scale: number;
  188. }
  189. interface IGLTFMaterialOcclusionTextureInfo extends IGLTFTextureInfo {
  190. strength: number;
  191. }
  192. interface IGLTFMaterialPbrMetallicRoughness {
  193. baseColorFactor: number[];
  194. baseColorTexture: IGLTFTextureInfo;
  195. metallicFactor: number;
  196. roughnessFactor: number;
  197. metallicRoughnessTexture: IGLTFTextureInfo;
  198. }
  199. interface IGLTFMaterial extends IGLTFChildRootProperty {
  200. pbrMetallicRoughness?: IGLTFMaterialPbrMetallicRoughness;
  201. normalTexture?: IGLTFMaterialNormalTextureInfo;
  202. occlusionTexture?: IGLTFMaterialOcclusionTextureInfo;
  203. emissiveTexture?: IGLTFTextureInfo;
  204. emissiveFactor?: number[];
  205. alphaMode?: string;
  206. alphaCutoff: number;
  207. doubleSided?: boolean;
  208. index?: number;
  209. babylonMaterial?: Material;
  210. }
  211. interface IGLTFMeshPrimitive extends IGLTFProperty {
  212. attributes: {
  213. [name: string]: number;
  214. };
  215. indices?: number;
  216. material?: number;
  217. mode?: EMeshPrimitiveMode;
  218. targets?: {
  219. [name: string]: number;
  220. }[];
  221. }
  222. interface IGLTFMesh extends IGLTFChildRootProperty {
  223. primitives: IGLTFMeshPrimitive[];
  224. weights?: number[];
  225. }
  226. interface IGLTFNode extends IGLTFChildRootProperty {
  227. camera?: number;
  228. children?: number[];
  229. skin?: number;
  230. matrix?: number[];
  231. mesh?: number;
  232. rotation?: number[];
  233. scale?: number[];
  234. translation?: number[];
  235. weights?: number[];
  236. index?: number;
  237. parent?: IGLTFNode;
  238. babylonMesh?: Mesh;
  239. babylonBones?: {
  240. [skin: number]: Bone;
  241. };
  242. babylonAnimationTargets?: Node[];
  243. }
  244. interface IGLTFSampler extends IGLTFChildRootProperty {
  245. magFilter?: ETextureMagFilter;
  246. minFilter?: ETextureMinFilter;
  247. wrapS?: ETextureWrapMode;
  248. wrapT?: ETextureWrapMode;
  249. }
  250. interface IGLTFScene extends IGLTFChildRootProperty {
  251. nodes: number[];
  252. }
  253. interface IGLTFSkin extends IGLTFChildRootProperty {
  254. inverseBindMatrices?: number;
  255. skeleton?: number;
  256. joints: number[];
  257. index?: number;
  258. babylonSkeleton?: Skeleton;
  259. }
  260. interface IGLTFTexture extends IGLTFChildRootProperty {
  261. sampler?: number;
  262. source: number;
  263. url?: string;
  264. dataReadyObservable?: Observable<IGLTFTexture>;
  265. }
  266. interface IGLTFTextureInfo {
  267. index: number;
  268. texCoord?: number;
  269. }
  270. interface IGLTF extends IGLTFProperty {
  271. accessors?: IGLTFAccessor[];
  272. animations?: IGLTFAnimation[];
  273. asset: IGLTFAsset;
  274. buffers?: IGLTFBuffer[];
  275. bufferViews?: IGLTFBufferView[];
  276. cameras?: IGLTFCamera[];
  277. extensionsUsed?: string[];
  278. extensionsRequired?: string[];
  279. images?: IGLTFImage[];
  280. materials?: IGLTFMaterial[];
  281. meshes?: IGLTFMesh[];
  282. nodes?: IGLTFNode[];
  283. samplers?: IGLTFSampler[];
  284. scene?: number;
  285. scenes?: IGLTFScene[];
  286. skins?: IGLTFSkin[];
  287. textures?: IGLTFTexture[];
  288. }
  289. }
  290. declare module BABYLON.GLTF2 {
  291. class GLTFLoader implements IGLTFLoader, IDisposable {
  292. _gltf: IGLTF;
  293. _babylonScene: Scene;
  294. private _parent;
  295. private _rootUrl;
  296. private _defaultMaterial;
  297. private _rootNode;
  298. private _successCallback;
  299. private _progressCallback;
  300. private _errorCallback;
  301. private _renderReady;
  302. private _disposed;
  303. private _renderReadyObservable;
  304. private _renderPendingCount;
  305. private _loaderPendingCount;
  306. private _loaderTrackers;
  307. static Extensions: {
  308. [name: string]: GLTFLoaderExtension;
  309. };
  310. static RegisterExtension(extension: GLTFLoaderExtension): 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. _onError(message: string): void;
  317. private _onProgress(event);
  318. _executeWhenRenderReady(func: () => void): void;
  319. private _onRenderReady();
  320. private _onComplete();
  321. private _loadData(data);
  322. private _getMeshes();
  323. private _getSkeletons();
  324. private _getAnimationTargets();
  325. private _startAnimations();
  326. private _loadScene(nodeNames);
  327. _loadNode(node: IGLTFNode): void;
  328. private _loadMesh(node, mesh);
  329. private _loadVertexDataAsync(primitive, onSuccess);
  330. private _createMorphTargets(node, mesh, primitive, babylonMesh);
  331. private _loadMorphTargetsData(mesh, primitive, vertexData, babylonMesh);
  332. private _loadTransform(node);
  333. private _loadSkin(skin);
  334. private _createBone(node, skin, parent, localMatrix, baseMatrix, index);
  335. private _loadBones(skin, inverseBindMatrixData);
  336. private _loadBone(node, skin, inverseBindMatrixData, babylonBones);
  337. private _getNodeMatrix(node);
  338. private _traverseNodes(indices, action, parentNode?);
  339. _traverseNode(index: number, action: (node: IGLTFNode, index: number, parentNode: IGLTFNode) => boolean, parentNode?: IGLTFNode): void;
  340. private _loadAnimations();
  341. private _loadAnimationChannel(animation, animationIndex, channelIndex);
  342. private _validateUri(uri);
  343. private _loadBufferAsync(buffer, 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. _addPendingData(data: any): void;
  355. _removePendingData(data: any): void;
  356. _addLoaderPendingData(data: any): void;
  357. _removeLoaderPendingData(data: any): void;
  358. _whenAction(action: () => void, onComplete: () => void): void;
  359. private _getDefaultMaterial();
  360. private _loadMaterialMetallicRoughnessProperties(material);
  361. _loadMaterial(material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): void;
  362. _createPbrMaterial(material: IGLTFMaterial): void;
  363. _loadMaterialBaseProperties(material: IGLTFMaterial): void;
  364. _loadMaterialAlphaProperties(material: IGLTFMaterial, colorFactor?: number[]): void;
  365. _loadTexture(textureInfo: IGLTFTextureInfo): Texture;
  366. _getArrayItem<T>(array: ArrayLike<T>, index: number, name: string): T;
  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 _traverseNode(loader: GLTFLoader, index: number, action: (node: IGLTFNode, index: number, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  399. protected _loadNode(loader: GLTFLoader, node: IGLTFNode): boolean;
  400. protected _loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  401. protected _loadExtension<T>(property: IGLTFProperty, action: (extension: T, onComplete: () => void) => void): boolean;
  402. static _Extensions: GLTFLoaderExtension[];
  403. static TraverseNode(loader: GLTFLoader, index: number, action: (node: IGLTFNode, index: number, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  404. static LoadNode(loader: GLTFLoader, node: IGLTFNode): boolean;
  405. static LoadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  406. private static _ApplyExtensions(action);
  407. }
  408. }
  409. declare module BABYLON.GLTF2.Extensions {
  410. class MSFTLOD extends GLTFLoaderExtension {
  411. /**
  412. * Specify the minimal delay between LODs in ms (default = 250)
  413. */
  414. static MinimalLODDelay: number;
  415. readonly name: string;
  416. protected _traverseNode(loader: GLTFLoader, index: number, action: (node: IGLTFNode, index: number, parentNode: IGLTFNode) => boolean, parentNode: IGLTFNode): boolean;
  417. protected _loadNode(loader: GLTFLoader, node: IGLTFNode): boolean;
  418. private _loadNodeLOD(loader, nodes, index, onComplete);
  419. protected _loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  420. private _loadMaterialLOD(loader, materials, index, assign, onComplete);
  421. }
  422. }
  423. declare module BABYLON.GLTF2.Extensions {
  424. class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
  425. readonly name: string;
  426. protected _loadMaterial(loader: GLTFLoader, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean;
  427. private _loadSpecularGlossinessProperties(loader, material, properties);
  428. }
  429. }