babylon.ktxTextureLoader.ts 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module BABYLON {
  2. /**
  3. * Implementation of the KTX Texture Loader.
  4. */
  5. class KTXTextureLoader implements IInternalTextureLoader {
  6. /**
  7. * Defines wether the loader supports cascade loading the different faces.
  8. */
  9. public readonly supportCascades = false;
  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. if (textureFormatInUse && !isBase64 && !fallback && !isBuffer) {
  21. return true;
  22. }
  23. return false;
  24. }
  25. /**
  26. * Transform the url before loading if required.
  27. * @param rootUrl the url of the texture
  28. * @param textureFormatInUse defines the current compressed format in use iun the engine
  29. * @returns the transformed texture
  30. */
  31. public transformUrl(rootUrl: string, textureFormatInUse: Nullable<string>): string {
  32. var lastDot = rootUrl.lastIndexOf('.');
  33. return (lastDot > -1 ? rootUrl.substring(0, lastDot) : rootUrl) + textureFormatInUse;
  34. }
  35. /**
  36. * Gets the fallback url in case the load fail. This can return null to allow the default fallback mecanism to work
  37. * @param rootUrl the url of the texture
  38. * @param textureFormatInUse defines the current compressed format in use iun the engine
  39. * @returns the fallback texture
  40. */
  41. public getFallbackTextureUrl(rootUrl: string, textureFormatInUse: Nullable<string>): Nullable<string> {
  42. // remove the format appended to the rootUrl in the original createCubeTexture call.
  43. var exp = new RegExp("" + textureFormatInUse! + "$");
  44. return rootUrl.replace(exp, "");
  45. }
  46. /**
  47. * Uploads the cube texture data to the WebGl Texture. It has alreday been bound.
  48. * @param data contains the texture data
  49. * @param texture defines the BabylonJS internal texture
  50. * @param createPolynomials will be true if polynomials have been requested
  51. * @param onLoad defines the callback to trigger once the texture is ready
  52. * @param onError defines the callback to trigger in case of error
  53. */
  54. public loadCubeData(data: string | ArrayBuffer | (string | ArrayBuffer)[], texture: InternalTexture, createPolynomials: boolean, onLoad: Nullable<(data?: any) => void>, onError: Nullable<(message?: string, exception?: any) => void>): void {
  55. if (Array.isArray(data)) {
  56. return;
  57. }
  58. var engine = texture.getEngine();
  59. var ktx = new KhronosTextureContainer(data, 6);
  60. var loadMipmap = ktx.numberOfMipmapLevels > 1 && texture.generateMipMaps;
  61. engine._unpackFlipY(true);
  62. ktx.uploadLevels(texture, texture.generateMipMaps);
  63. texture.width = ktx.pixelWidth;
  64. texture.height = ktx.pixelHeight;
  65. engine._setCubeMapTextureParams(loadMipmap);
  66. texture.isReady = true;
  67. }
  68. /**
  69. * Uploads the 2D texture data to the WebGl Texture. It has alreday been bound once in the callback.
  70. * @param data contains the texture data
  71. * @param texture defines the BabylonJS internal texture
  72. * @param callback defines the method to call once ready to upload
  73. */
  74. public loadData(data: ArrayBuffer, texture: InternalTexture,
  75. callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void) => void): void {
  76. var ktx = new KhronosTextureContainer(data, 1);
  77. callback(ktx.pixelWidth, ktx.pixelHeight, false, true, () => {
  78. ktx.uploadLevels(texture, texture.generateMipMaps);
  79. });
  80. }
  81. }
  82. // Register the loader.
  83. Engine._TextureLoaders.unshift(new KTXTextureLoader());
  84. }