babylon.glTFData.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /// <reference path="../../../../dist/preview release/glTF2Interface/babylon.glTF2Interface.d.ts"/>
  2. module BABYLON {
  3. /**
  4. * Class for holding and downloading glTF file data
  5. */
  6. export class GLTFData {
  7. /**
  8. * Object which contains the file name as the key and its data as the value
  9. */
  10. glTFFiles: { [fileName: string]: string | Blob };
  11. /**
  12. * Initializes the glTF file object
  13. */
  14. public constructor() {
  15. this.glTFFiles = {};
  16. }
  17. /**
  18. * Downloads the glTF data as files based on their names and data
  19. */
  20. public downloadFiles(): void {
  21. /**
  22. * Checks for a matching suffix at the end of a string (for ES5 and lower)
  23. * @param str Source string
  24. * @param suffix Suffix to search for in the source string
  25. * @returns Boolean indicating whether the suffix was found (true) or not (false)
  26. */
  27. function endsWith(str: string, suffix: string): boolean {
  28. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  29. }
  30. for (let key in this.glTFFiles) {
  31. let link = document.createElement('a');
  32. document.body.appendChild(link);
  33. link.setAttribute("type", "hidden");
  34. link.download = key;
  35. let blob = this.glTFFiles[key];
  36. let mimeType;
  37. if (endsWith(key, ".glb")) {
  38. mimeType = { type: "model/gltf-binary" };
  39. }
  40. else if (endsWith(key, ".bin")) {
  41. mimeType = { type: "application/octet-stream" };
  42. }
  43. else if (endsWith(key, ".gltf")) {
  44. mimeType = { type: "model/gltf+json" };
  45. }
  46. else if (endsWith(key, ".jpeg" || ".jpg")) {
  47. mimeType = {type: GLTF2.ImageMimeType.JPEG};
  48. }
  49. else if (endsWith(key, ".png")) {
  50. mimeType = {type: GLTF2.ImageMimeType.PNG};
  51. }
  52. link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
  53. link.click();
  54. }
  55. }
  56. }
  57. }