babylon.glTFLoaderUtils.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GLTF2 {
  3. /**
  4. * Utils functions for GLTF
  5. */
  6. export class GLTFUtils {
  7. /**
  8. * If the uri is a base64 string
  9. * @param uri: the uri to test
  10. */
  11. public static IsBase64(uri: string): boolean {
  12. return uri.length < 5 ? false : uri.substr(0, 5) === "data:";
  13. }
  14. /**
  15. * Decode the base64 uri
  16. * @param uri: the uri to decode
  17. */
  18. public static DecodeBase64(uri: string): ArrayBuffer {
  19. var decodedString = atob(uri.split(",")[1]);
  20. var bufferLength = decodedString.length;
  21. var bufferView = new Uint8Array(new ArrayBuffer(bufferLength));
  22. for (var i = 0; i < bufferLength; i++) {
  23. bufferView[i] = decodedString.charCodeAt(i);
  24. }
  25. return bufferView.buffer;
  26. }
  27. public static ForEach(view: Uint16Array | Uint32Array | Float32Array, func: (nvalue: number, index: number) => void): void {
  28. for (var index = 0; index < view.length; index++) {
  29. func(view[index], index);
  30. }
  31. }
  32. public static GetTextureWrapMode(mode: ETextureWrapMode): number {
  33. // Set defaults if undefined
  34. mode = mode === undefined ? ETextureWrapMode.REPEAT : mode;
  35. switch (mode) {
  36. case ETextureWrapMode.CLAMP_TO_EDGE: Texture.CLAMP_ADDRESSMODE;
  37. case ETextureWrapMode.MIRRORED_REPEAT: return Texture.MIRROR_ADDRESSMODE;
  38. case ETextureWrapMode.REPEAT: return Texture.WRAP_ADDRESSMODE;
  39. default:
  40. Tools.Warn("Invalid texture wrap mode (" + mode + ")");
  41. return Texture.WRAP_ADDRESSMODE;
  42. }
  43. }
  44. public static GetTextureSamplingMode(magFilter: ETextureMagFilter, minFilter: ETextureMinFilter): number {
  45. // Set defaults if undefined
  46. magFilter = magFilter === undefined ? ETextureMagFilter.LINEAR : magFilter;
  47. minFilter = minFilter === undefined ? ETextureMinFilter.LINEAR_MIPMAP_NEAREST : minFilter;
  48. if (magFilter === ETextureMagFilter.LINEAR) {
  49. switch (minFilter) {
  50. case ETextureMinFilter.NEAREST: return Texture.LINEAR_NEAREST;
  51. case ETextureMinFilter.LINEAR: return Texture.LINEAR_LINEAR;
  52. case ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return Texture.LINEAR_NEAREST_MIPNEAREST;
  53. case ETextureMinFilter.LINEAR_MIPMAP_NEAREST: return Texture.LINEAR_LINEAR_MIPNEAREST;
  54. case ETextureMinFilter.NEAREST_MIPMAP_LINEAR: return Texture.LINEAR_NEAREST_MIPLINEAR;
  55. case ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return Texture.LINEAR_LINEAR_MIPLINEAR;
  56. default:
  57. Tools.Warn("Invalid texture minification filter (" + minFilter + ")");
  58. return Texture.LINEAR_LINEAR_MIPLINEAR;
  59. }
  60. }
  61. else {
  62. if (magFilter !== ETextureMagFilter.NEAREST) {
  63. Tools.Warn("Invalid texture magnification filter (" + magFilter + ")");
  64. }
  65. switch (minFilter) {
  66. case ETextureMinFilter.NEAREST: return Texture.NEAREST_NEAREST;
  67. case ETextureMinFilter.LINEAR: return Texture.NEAREST_LINEAR;
  68. case ETextureMinFilter.NEAREST_MIPMAP_NEAREST: return Texture.NEAREST_NEAREST_MIPNEAREST;
  69. case ETextureMinFilter.LINEAR_MIPMAP_NEAREST: return Texture.NEAREST_LINEAR_MIPNEAREST;
  70. case ETextureMinFilter.NEAREST_MIPMAP_LINEAR: return Texture.NEAREST_NEAREST_MIPLINEAR;
  71. case ETextureMinFilter.LINEAR_MIPMAP_LINEAR: return Texture.NEAREST_LINEAR_MIPLINEAR;
  72. default:
  73. Tools.Warn("Invalid texture minification filter (" + minFilter + ")");
  74. return Texture.NEAREST_NEAREST_MIPNEAREST;
  75. }
  76. }
  77. }
  78. /**
  79. * Decodes a buffer view into a string
  80. * @param view: the buffer view
  81. */
  82. public static DecodeBufferToText(view: ArrayBufferView): string {
  83. var result = "";
  84. var length = view.byteLength;
  85. for (var i = 0; i < length; ++i) {
  86. result += String.fromCharCode(view[i]);
  87. }
  88. return result;
  89. }
  90. }
  91. }