CompressedTextureLoader.js 2.2 KB

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