CompressedTextureLoader.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.setPath( this.path );
  21. loader.setResponseType( 'arraybuffer' );
  22. function loadTexture( i ) {
  23. loader.load( url[ i ], function ( buffer ) {
  24. var texDatas = scope._parser( buffer, true );
  25. images[ i ] = {
  26. width: texDatas.width,
  27. height: texDatas.height,
  28. format: texDatas.format,
  29. mipmaps: texDatas.mipmaps
  30. };
  31. loaded += 1;
  32. if ( loaded === 6 ) {
  33. if ( texDatas.mipmapCount === 1 )
  34. texture.minFilter = THREE.LinearFilter;
  35. texture.format = texDatas.format;
  36. texture.needsUpdate = true;
  37. if ( onLoad ) onLoad( texture );
  38. }
  39. }, onProgress, onError );
  40. }
  41. if ( Array.isArray( url ) ) {
  42. var loaded = 0;
  43. for ( var i = 0, il = url.length; i < il; ++ i ) {
  44. loadTexture( i );
  45. }
  46. } else {
  47. // compressed cubemap texture stored in a single DDS file
  48. loader.load( url, function ( buffer ) {
  49. var texDatas = scope._parser( buffer, true );
  50. if ( texDatas.isCubemap ) {
  51. var faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  52. for ( var f = 0; f < faces; f ++ ) {
  53. images[ f ] = { mipmaps : [] };
  54. for ( var i = 0; i < texDatas.mipmapCount; i ++ ) {
  55. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  56. images[ f ].format = texDatas.format;
  57. images[ f ].width = texDatas.width;
  58. images[ f ].height = texDatas.height;
  59. }
  60. }
  61. } else {
  62. texture.image.width = texDatas.width;
  63. texture.image.height = texDatas.height;
  64. texture.mipmaps = texDatas.mipmaps;
  65. }
  66. if ( texDatas.mipmapCount === 1 ) {
  67. texture.minFilter = THREE.LinearFilter;
  68. }
  69. texture.format = texDatas.format;
  70. texture.needsUpdate = true;
  71. if ( onLoad ) onLoad( texture );
  72. }, onProgress, onError );
  73. }
  74. return texture;
  75. },
  76. setCrossOrigin: function ( value ) {
  77. this.crossOrigin = value;
  78. },
  79. setPath: function ( value ) {
  80. this.path = value;
  81. }
  82. };
粤ICP备19079148号