babylon.ddsTextureLoader.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. module BABYLON {
  2. /**
  3. * Implementation of the DDS Texture Loader.
  4. */
  5. class DDSTextureLoader implements IInternalTextureLoader {
  6. /**
  7. * Defines wether the loader supports cascade loading the different faces.
  8. */
  9. public readonly supportCascades = true;
  10. /**
  11. * This returns if the loader support the current file information.
  12. * @param extension defines the file extension of the file being loaded
  13. * @param textureFormatInUse defines the current compressed format in use iun the engine
  14. * @param fallback defines the fallback internal texture if any
  15. * @param isBase64 defines whether the texture is encoded as a base64
  16. * @param isBuffer defines whether the texture data are stored as a buffer
  17. * @returns true if the loader can load the specified file
  18. */
  19. public canLoad(extension: string, textureFormatInUse: Nullable<string>, fallback: Nullable<InternalTexture>, isBase64: boolean, isBuffer: boolean): boolean {
  20. return extension.indexOf(".dds") === 0;
  21. }
  22. /**
  23. * Transform the url before loading if required.
  24. * @param rootUrl the url of the texture
  25. * @param textureFormatInUse defines the current compressed format in use iun the engine
  26. * @returns the transformed texture
  27. */
  28. public transformUrl(rootUrl: string, textureFormatInUse: Nullable<string>): string {
  29. return rootUrl;
  30. }
  31. /**
  32. * Gets the fallback url in case the load fail. This can return null to allow the default fallback mecanism to work
  33. * @param rootUrl the url of the texture
  34. * @param textureFormatInUse defines the current compressed format in use iun the engine
  35. * @returns the fallback texture
  36. */
  37. public getFallbackTextureUrl(rootUrl: string, textureFormatInUse: Nullable<string>): Nullable<string> {
  38. return null;
  39. }
  40. /**
  41. * Uploads the cube texture data to the WebGl Texture. It has alreday been bound.
  42. * @param data contains the texture data
  43. * @param texture defines the BabylonJS internal texture
  44. * @param createPolynomials will be true if polynomials have been requested
  45. * @param onLoad defines the callback to trigger once the texture is ready
  46. * @param onError defines the callback to trigger in case of error
  47. */
  48. public loadCubeData(imgs: string | ArrayBuffer | (string | ArrayBuffer)[], texture: InternalTexture, createPolynomials: boolean, onLoad: Nullable<(data?: any) => void>, onError: Nullable<(message?: string, exception?: any) => void>): void {
  49. var engine = texture.getEngine();
  50. var info: DDSInfo | undefined;
  51. var loadMipmap: boolean = false;
  52. if (Array.isArray(imgs)) {
  53. for (let index = 0; index < imgs.length; index++) {
  54. let data = imgs[index];
  55. info = DDSTools.GetDDSInfo(data);
  56. texture.width = info.width;
  57. texture.height = info.height;
  58. loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && texture.generateMipMaps;
  59. engine._unpackFlipY(info.isCompressed);
  60. DDSTools.UploadDDSLevels(engine, texture, data, info, loadMipmap, 6, -1, index);
  61. if (!info.isFourCC && info.mipmapCount === 1) {
  62. engine.generateMipMapsForCubemap(texture);
  63. }
  64. }
  65. }
  66. else {
  67. var data = imgs;
  68. info = DDSTools.GetDDSInfo(data);
  69. texture.width = info.width;
  70. texture.height = info.height;
  71. if (createPolynomials) {
  72. info.sphericalPolynomial = new SphericalPolynomial();
  73. }
  74. loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && texture.generateMipMaps;
  75. engine._unpackFlipY(info.isCompressed);
  76. DDSTools.UploadDDSLevels(engine, texture, data, info, loadMipmap, 6);
  77. if (!info.isFourCC && info.mipmapCount === 1) {
  78. engine.generateMipMapsForCubemap(texture);
  79. }
  80. }
  81. engine._setCubeMapTextureParams(loadMipmap);
  82. texture.isReady = true;
  83. if (onLoad) {
  84. onLoad({ isDDS: true, width: texture.width, info, imgs, texture });
  85. }
  86. }
  87. /**
  88. * Uploads the 2D texture data to the WebGl Texture. It has alreday been bound once in the callback.
  89. * @param data contains the texture data
  90. * @param texture defines the BabylonJS internal texture
  91. * @param callback defines the method to call once ready to upload
  92. */
  93. public loadData(data: ArrayBuffer, texture: InternalTexture,
  94. callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void) => void): void {
  95. var info = DDSTools.GetDDSInfo(data);
  96. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && texture.generateMipMaps && ((info.width >> (info.mipmapCount - 1)) === 1);
  97. callback(info.width, info.height, !loadMipmap, info.isFourCC, () => {
  98. DDSTools.UploadDDSLevels(texture.getEngine(), texture, data, info, loadMipmap, 1);
  99. });
  100. }
  101. }
  102. // Register the loader.
  103. Engine._TextureLoaders.push(new DDSTextureLoader());
  104. }