ddsTextureLoader.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Nullable } from "../../../types";
  2. import { SphericalPolynomial } from "../../../Maths/sphericalPolynomial";
  3. import { Engine } from "../../../Engines/engine";
  4. import { InternalTexture } from "../../../Materials/Textures/internalTexture";
  5. import { IInternalTextureLoader } from "../../../Materials/Textures/internalTextureLoader";
  6. import { DDSTools, DDSInfo } from "../../../Misc/dds";
  7. import { StringTools } from '../../../Misc/stringTools';
  8. /**
  9. * Implementation of the DDS Texture Loader.
  10. * @hidden
  11. */
  12. export class _DDSTextureLoader 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. * @returns true if the loader can load the specified file
  21. */
  22. public canLoad(extension: string): boolean {
  23. return StringTools.EndsWith(extension, ".dds");
  24. }
  25. /**
  26. * Uploads the cube texture data to the WebGL texture. It has already been bound.
  27. * @param data contains the texture data
  28. * @param texture defines the BabylonJS internal texture
  29. * @param createPolynomials will be true if polynomials have been requested
  30. * @param onLoad defines the callback to trigger once the texture is ready
  31. * @param onError defines the callback to trigger in case of error
  32. */
  33. public loadCubeData(imgs: ArrayBufferView | ArrayBufferView[], texture: InternalTexture, createPolynomials: boolean, onLoad: Nullable<(data?: any) => void>, onError: Nullable<(message?: string, exception?: any) => void>): void {
  34. var engine = texture.getEngine() as Engine;
  35. var info: DDSInfo | undefined;
  36. var loadMipmap: boolean = false;
  37. if (Array.isArray(imgs)) {
  38. for (let index = 0; index < imgs.length; index++) {
  39. let data = imgs[index];
  40. info = DDSTools.GetDDSInfo(data);
  41. texture.width = info.width;
  42. texture.height = info.height;
  43. loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && texture.generateMipMaps;
  44. engine._unpackFlipY(info.isCompressed);
  45. DDSTools.UploadDDSLevels(engine, texture, data, info, loadMipmap, 6, -1, index);
  46. if (!info.isFourCC && info.mipmapCount === 1) {
  47. engine.generateMipMapsForCubemap(texture);
  48. }
  49. }
  50. }
  51. else {
  52. var data = imgs;
  53. info = DDSTools.GetDDSInfo(data);
  54. texture.width = info.width;
  55. texture.height = info.height;
  56. if (createPolynomials) {
  57. info.sphericalPolynomial = new SphericalPolynomial();
  58. }
  59. loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && texture.generateMipMaps;
  60. engine._unpackFlipY(info.isCompressed);
  61. DDSTools.UploadDDSLevels(engine, texture, data, info, loadMipmap, 6);
  62. if (!info.isFourCC && info.mipmapCount === 1) {
  63. // Do not unbind as we still need to set the parameters.
  64. engine.generateMipMapsForCubemap(texture, false);
  65. }
  66. }
  67. engine._setCubeMapTextureParams(texture, loadMipmap);
  68. texture.isReady = true;
  69. texture.onLoadedObservable.notifyObservers(texture);
  70. texture.onLoadedObservable.clear();
  71. if (onLoad) {
  72. onLoad({ isDDS: true, width: texture.width, info, data: imgs, texture });
  73. }
  74. }
  75. /**
  76. * Uploads the 2D texture data to the WebGL texture. It has already been bound once in the callback.
  77. * @param data contains the texture data
  78. * @param texture defines the BabylonJS internal texture
  79. * @param callback defines the method to call once ready to upload
  80. */
  81. public loadData(data: ArrayBufferView, texture: InternalTexture,
  82. callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void) => void): void {
  83. var info = DDSTools.GetDDSInfo(data);
  84. var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && texture.generateMipMaps && ((info.width >> (info.mipmapCount - 1)) === 1);
  85. callback(info.width, info.height, loadMipmap, info.isFourCC, () => {
  86. DDSTools.UploadDDSLevels(texture.getEngine(), texture, data, info, loadMipmap, 1);
  87. });
  88. }
  89. }
  90. // Register the loader.
  91. Engine._TextureLoaders.push(new _DDSTextureLoader());