basisTextureLoader.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Nullable } from "../../../types";
  2. import { Engine } from "../../../Engines/engine";
  3. import { InternalTexture } from "../../../Materials/Textures/internalTexture";
  4. import { IInternalTextureLoader } from "../../../Materials/Textures/internalTextureLoader";
  5. import { BasisTools } from "../../../Misc/basis";
  6. import { Tools } from '../../../Misc/tools';
  7. import { StringTools } from '../../../Misc/stringTools';
  8. /**
  9. * Loader for .basis file format
  10. */
  11. export class _BasisTextureLoader implements IInternalTextureLoader {
  12. /**
  13. * Defines whether the loader supports cascade loading the different faces.
  14. */
  15. public readonly supportCascades = false;
  16. /**
  17. * This returns if the loader support the current file information.
  18. * @param extension defines the file extension of the file being loaded
  19. * @returns true if the loader can load the specified file
  20. */
  21. public canLoad(extension: string): boolean {
  22. return StringTools.EndsWith(extension, ".basis");
  23. }
  24. /**
  25. * Uploads the cube texture data to the WebGL texture. It has already been bound.
  26. * @param data contains the texture data
  27. * @param texture defines the BabylonJS internal texture
  28. * @param createPolynomials will be true if polynomials have been requested
  29. * @param onLoad defines the callback to trigger once the texture is ready
  30. * @param onError defines the callback to trigger in case of error
  31. */
  32. public loadCubeData(data: ArrayBufferView | ArrayBufferView[], texture: InternalTexture, createPolynomials: boolean, onLoad: Nullable<(data?: any) => void>, onError: Nullable<(message?: string, exception?: any) => void>): void {
  33. if (Array.isArray(data)) {
  34. return;
  35. }
  36. var caps = texture.getEngine().getCaps();
  37. var transcodeConfig = {
  38. supportedCompressionFormats: {
  39. etc1: caps.etc1 ? true : false,
  40. s3tc: caps.s3tc ? true : false,
  41. pvrtc: caps.pvrtc ? true : false,
  42. etc2: caps.etc2 ? true : false
  43. }
  44. };
  45. BasisTools.TranscodeAsync(data, transcodeConfig).then((result) => {
  46. var hasMipmap = result.fileInfo.images[0].levels.length > 1 && texture.generateMipMaps;
  47. BasisTools.LoadTextureFromTranscodeResult(texture, result);
  48. (texture.getEngine() as Engine)._setCubeMapTextureParams(hasMipmap);
  49. texture.isReady = true;
  50. texture.onLoadedObservable.notifyObservers(texture);
  51. texture.onLoadedObservable.clear();
  52. if (onLoad) {
  53. onLoad();
  54. }
  55. }).catch((err) => {
  56. Tools.Warn("Failed to transcode Basis file, transcoding may not be supported on this device");
  57. texture.isReady = true;
  58. });
  59. }
  60. /**
  61. * Uploads the 2D texture data to the WebGL texture. It has already been bound once in the callback.
  62. * @param data contains the texture data
  63. * @param texture defines the BabylonJS internal texture
  64. * @param callback defines the method to call once ready to upload
  65. */
  66. public loadData(data: ArrayBufferView, texture: InternalTexture,
  67. callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void) => void): void {
  68. var caps = texture.getEngine().getCaps();
  69. var transcodeConfig = {
  70. supportedCompressionFormats: {
  71. etc1: caps.etc1 ? true : false,
  72. s3tc: caps.s3tc ? true : false,
  73. pvrtc: caps.pvrtc ? true : false,
  74. etc2: caps.etc2 ? true : false
  75. }
  76. };
  77. BasisTools.TranscodeAsync(data, transcodeConfig).then((result) => {
  78. var rootImage = result.fileInfo.images[0].levels[0];
  79. var hasMipmap = result.fileInfo.images[0].levels.length > 1 && texture.generateMipMaps;
  80. callback(rootImage.width, rootImage.height, hasMipmap, result.format !== -1, () => {
  81. BasisTools.LoadTextureFromTranscodeResult(texture, result);
  82. });
  83. }).catch((err) => {
  84. Tools.Warn("Failed to transcode Basis file, transcoding may not be supported on this device");
  85. callback(0, 0, false, false, () => {
  86. });
  87. });
  88. }
  89. }
  90. // Register the loader.
  91. Engine._TextureLoaders.push(new _BasisTextureLoader());