basisTextureLoader.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 { _TimeToken } from "../../../Instrumentation/timeToken";
  6. import { _DepthCullingState, _StencilState, _AlphaState } from "../../../States/index";
  7. import { BasisTools } from "../../../Misc/basis";
  8. import { Tools } from '../../../Misc/tools';
  9. /**
  10. * Loader for .basis file format
  11. */
  12. export class _BasisTextureLoader implements IInternalTextureLoader {
  13. /**
  14. * Defines whether the loader supports cascade loading the different faces.
  15. */
  16. public readonly supportCascades = true;
  17. /**
  18. * This returns if the loader support the current file information.
  19. * @param extension defines the file extension of the file being loaded
  20. * @param textureFormatInUse defines the current compressed format in use iun the engine
  21. * @param fallback defines the fallback internal texture if any
  22. * @param isBase64 defines whether the texture is encoded as a base64
  23. * @param isBuffer defines whether the texture data are stored as a buffer
  24. * @returns true if the loader can load the specified file
  25. */
  26. public canLoad(extension: string, textureFormatInUse: Nullable<string>, fallback: Nullable<InternalTexture>, isBase64: boolean, isBuffer: boolean): boolean {
  27. return extension.indexOf(".basis") === 0;
  28. }
  29. /**
  30. * Transform the url before loading if required.
  31. * @param rootUrl the url of the texture
  32. * @param textureFormatInUse defines the current compressed format in use iun the engine
  33. * @returns the transformed texture
  34. */
  35. public transformUrl(rootUrl: string, textureFormatInUse: Nullable<string>): string {
  36. return rootUrl;
  37. }
  38. /**
  39. * Gets the fallback url in case the load fail. This can return null to allow the default fallback mecanism to work
  40. * @param rootUrl the url of the texture
  41. * @param textureFormatInUse defines the current compressed format in use iun the engine
  42. * @returns the fallback texture
  43. */
  44. public getFallbackTextureUrl(rootUrl: string, textureFormatInUse: Nullable<string>): Nullable<string> {
  45. return null;
  46. }
  47. /**
  48. * Uploads the cube texture data to the WebGl Texture. It has already been bound.
  49. * @param data contains the texture data
  50. * @param texture defines the BabylonJS internal texture
  51. * @param createPolynomials will be true if polynomials have been requested
  52. * @param onLoad defines the callback to trigger once the texture is ready
  53. * @param onError defines the callback to trigger in case of error
  54. */
  55. public loadCubeData(data: string | ArrayBuffer | (string | ArrayBuffer)[], texture: InternalTexture, createPolynomials: boolean, onLoad: Nullable<(data?: any) => void>, onError: Nullable<(message?: string, exception?: any) => void>): void {
  56. if (Array.isArray(data)) {
  57. return;
  58. }
  59. var caps = texture.getEngine().getCaps();
  60. var transcodeConfig = {
  61. supportedCompressionFormats: {
  62. etc1: caps.etc1 ? true : false,
  63. s3tc: caps.s3tc ? true : false,
  64. pvrtc: caps.pvrtc ? true : false,
  65. etc2: caps.etc2 ? true : false
  66. }
  67. };
  68. BasisTools.TranscodeAsync(data as ArrayBuffer, transcodeConfig).then((result) => {
  69. var hasMipmap = result.fileInfo.images[0].levels.length > 1 && texture.generateMipMaps;
  70. BasisTools.LoadTextureFromTranscodeResult(texture, result);
  71. texture.getEngine()._setCubeMapTextureParams(hasMipmap);
  72. texture.isReady = true;
  73. texture.onLoadedObservable.notifyObservers(texture);
  74. texture.onLoadedObservable.clear();
  75. if (onLoad) {
  76. onLoad();
  77. }
  78. }).catch((err) => {
  79. Tools.Warn("Failed to transcode Basis file, transcoding may not be supported on this device");
  80. texture.isReady = true;
  81. });
  82. }
  83. /**
  84. * Uploads the 2D texture data to the WebGl Texture. It has alreday been bound once in the callback.
  85. * @param data contains the texture data
  86. * @param texture defines the BabylonJS internal texture
  87. * @param callback defines the method to call once ready to upload
  88. */
  89. public loadData(data: ArrayBuffer, texture: InternalTexture,
  90. callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void) => void): void {
  91. var caps = texture.getEngine().getCaps();
  92. var transcodeConfig = {
  93. supportedCompressionFormats: {
  94. etc1: caps.etc1 ? true : false,
  95. s3tc: caps.s3tc ? true : false,
  96. pvrtc: caps.pvrtc ? true : false,
  97. etc2: caps.etc2 ? true : false
  98. }
  99. };
  100. BasisTools.TranscodeAsync(data, transcodeConfig).then((result) => {
  101. var rootImage = result.fileInfo.images[0].levels[0];
  102. var hasMipmap = result.fileInfo.images[0].levels.length > 1 && texture.generateMipMaps;
  103. callback(rootImage.width, rootImage.height, hasMipmap, result.format !== -1, () => {
  104. BasisTools.LoadTextureFromTranscodeResult(texture, result);
  105. });
  106. }).catch((err) => {
  107. Tools.Warn("Failed to transcode Basis file, transcoding may not be supported on this device");
  108. callback(0, 0, false, false, () => {
  109. });
  110. });
  111. }
  112. }
  113. // Register the loader.
  114. Engine._TextureLoaders.push(new _BasisTextureLoader());