CompressedTextureLoader.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. texture.format = texDatas.format;
  33. texture.needsUpdate = true;
  34. if ( onLoad ) onLoad( texture );
  35. }
  36. } );
  37. }
  38. for ( var i = 0, il = url.length; i < il; ++ i ) {
  39. loadTexture( i );
  40. }
  41. } else {
  42. // compressed cubemap texture stored in a single DDS file
  43. loader.load( url, function ( buffer ) {
  44. var texDatas = scope._parser( buffer, true );
  45. if ( texDatas.isCubemap ) {
  46. var faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  47. for ( var f = 0; f < faces; f ++ ) {
  48. images[ f ] = { mipmaps : [] };
  49. for ( var i = 0; i < texDatas.mipmapCount; i ++ ) {
  50. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  51. images[ f ].format = texDatas.format;
  52. images[ f ].width = texDatas.width;
  53. images[ f ].height = texDatas.height;
  54. }
  55. }
  56. } else {
  57. texture.image.width = texDatas.width;
  58. texture.image.height = texDatas.height;
  59. texture.mipmaps = texDatas.mipmaps;
  60. }
  61. texture.format = texDatas.format;
  62. texture.needsUpdate = true;
  63. if ( onLoad ) onLoad( texture );
  64. } );
  65. }
  66. return texture;
  67. }
  68. };