KMZLoader.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.KMZLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. };
  7. THREE.KMZLoader.prototype = {
  8. constructor: THREE.KMZLoader,
  9. load: function ( url, onLoad, onProgress, onError ) {
  10. var scope = this;
  11. var loader = new THREE.FileLoader( scope.manager );
  12. loader.setResponseType( 'arraybuffer' );
  13. loader.load( url, function ( text ) {
  14. onLoad( scope.parse( text ) );
  15. }, onProgress, onError );
  16. },
  17. parse: function ( data ) {
  18. var zip = new JSZip( data );
  19. // console.log( zip );
  20. // var xml = new DOMParser().parseFromString( zip.file( 'doc.kml' ).asText(), 'application/xml' );
  21. function loadImage( image ) {
  22. var path = decodeURI( image.init_from );
  23. // Hack to support relative paths
  24. path = path.replace( '../', '' );
  25. var regex = new RegExp( path + '$' );
  26. var files = zip.file( regex );
  27. // console.log( image, files );
  28. if ( files.length ) {
  29. var file = files[ 0 ];
  30. var blob = new Blob( [ file.asArrayBuffer() ], { type: 'application/octet-binary' } );
  31. image.build.src = URL.createObjectURL( blob );
  32. }
  33. }
  34. // load collada
  35. var files = zip.file( /dae$/i );
  36. if ( files.length ) {
  37. var file = files[ 0 ];
  38. var collada = new THREE.ColladaLoader().parse( file.asText() );
  39. // fix images
  40. var images = collada.library.images;
  41. for ( var name in images ) {
  42. loadImage( images[ name ] );
  43. }
  44. return collada;
  45. }
  46. console.error( 'KMZLoader: Couldn\'t find .dae file.' );
  47. return {
  48. scene: new THREE.Group()
  49. };
  50. }
  51. };