CompressedTextureLoader.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * @author mrdoob / http://mrdoob.com/
  3. *
  4. * Abstract Base class to block based textures loader (dds, pvr, ...)
  5. */
  6. THREE.CompressedTextureLoader = function () {
  7. // override in sub classes
  8. this._parser = null;
  9. };
  10. THREE.CompressedTextureLoader.prototype = {
  11. constructor: THREE.CompressedTextureLoader,
  12. load: function ( url, onLoad, onError ) {
  13. var scope = this;
  14. var images = [];
  15. var texture = new THREE.CompressedTexture();
  16. texture.image = images;
  17. // no flipping for cube textures
  18. // (also flipping doesn't work for compressed textures )
  19. texture.flipY = false;
  20. // can't generate mipmaps for compressed textures
  21. // mips must be embedded in DDS files
  22. texture.generateMipmaps = false;
  23. if ( url instanceof Array ) {
  24. var loaded = 0;
  25. var loader = new THREE.XHRLoader();
  26. loader.setResponseType( 'arraybuffer' );
  27. var loadTexture = function ( i ) {
  28. loader.load( url[ i ], function ( buffer ) {
  29. var texDatas = scope._parser( buffer, true );
  30. images[ i ] = {
  31. width: texDatas.width,
  32. height: texDatas.height,
  33. format: texDatas.format,
  34. mipmaps: texDatas.mipmaps
  35. }
  36. loaded += 1;
  37. if ( loaded === 6 ) {
  38. texture.format = texDatas.format;
  39. texture.needsUpdate = true;
  40. if ( onLoad ) onLoad( texture );
  41. }
  42. } );
  43. }
  44. for ( var i = 0, il = url.length; i < il; ++ i ) {
  45. loadTexture( i );
  46. }
  47. } else {
  48. // compressed cubemap texture stored in a single DDS file
  49. var loader = new THREE.XHRLoader();
  50. loader.setResponseType( 'arraybuffer' );
  51. loader.load( url, function ( buffer ) {
  52. var texDatas = scope._parser( buffer, true );
  53. if ( texDatas.isCubemap ) {
  54. var faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  55. for ( var f = 0; f < faces; f ++ ) {
  56. images[ f ] = { mipmaps : [] };
  57. for ( var i = 0; i < texDatas.mipmapCount; i ++ ) {
  58. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  59. images[ f ].format = texDatas.format;
  60. images[ f ].width = texDatas.width;
  61. images[ f ].height = texDatas.height;
  62. }
  63. }
  64. } else {
  65. texture.image.width = texDatas.width;
  66. texture.image.height = texDatas.height;
  67. texture.mipmaps = texDatas.mipmaps;
  68. }
  69. texture.format = texDatas.format;
  70. texture.needsUpdate = true;
  71. if ( onLoad ) onLoad( texture );
  72. } );
  73. }
  74. return texture;
  75. }
  76. };