CompressedTextureLoader.js 2.5 KB

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