123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- /*
- * @author tamarintech / https://tamarintech.com
- *
- * Description: Early release of an AMF Loader following the pattern of the
- * example loaders in the three.js project.
- *
- * More information about the AMF format: http://amf.wikispaces.com
- *
- * Usage:
- * var loader = new AMFLoader();
- * loader.load('/path/to/project.amf', function(objecttree) {
- * scene.add(objecttree);
- * });
- *
- * Materials now supported, material colors supported
- * Zip support, requires jszip
- * TextDecoder polyfill required by some browsers (particularly IE, Edge)
- * No constellation support (yet)!
- *
- */
- THREE.AMFLoader = function ( manager ) {
- this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
- };
- THREE.AMFLoader.prototype = {
- constructor: THREE.AMFLoader,
- load: function ( url, onLoad, onProgress, onError ) {
- var scope = this;
- var loader = new THREE.XHRLoader( scope.manager );
- loader.setCrossOrigin( this.crossOrigin );
- loader.setResponseType( 'arraybuffer' );
- loader.load( url, function( text ) {
- var amfObject = scope.parse( text );
- onLoad( amfObject );
- }, onProgress, onError );
- },
- parse: function ( data ) {
- function loadDocument( data ) {
- var view = new DataView( data );
- var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) );
- if ( magic === "PK" ) {
- console.log( "Loading Zip" );
- var zip = null;
- var file = null;
- try {
- zip = new JSZip( data );
- } catch ( e ) {
- if ( e instanceof ReferenceError ) {
- console.log( " jszip missing and file is compressed." );
- return null;
- }
- }
- for ( file in zip.files ) {
- if ( file.toLowerCase().endsWith( ".amf" ) ) {
- break;
- }
- }
- console.log( " Trying to load file asset: " + file );
- view = new DataView( zip.file( file ).asArrayBuffer() );
- }
- if ( TextDecoder === undefined ) {
- console.log( " TextDecoder not present. Please use TextDecoder polyfill." );
- return null;
- }
- var filetext = new TextDecoder( 'utf-8' ).decode( view );
- var xmldata = new DOMParser().parseFromString( filetext, 'application/xml' );
- if ( xmldata.documentElement.nodeName.toLowerCase() !== "amf" ) {
- console.log( " Error loading AMF - no AMF document found." );
- return null;
- }
- return xmldata;
- }
- function loadDocumentScale( xmldata ) {
- var scale = 1.0;
- var unit = 'millimeter';
- if ( xmldata.documentElement.attributes[ 'unit' ] !== undefined ) {
- unit = xmldata.documentElement.attributes[ 'unit' ].value.toLowerCase();
- }
- var scale_units = {
- 'millimeter': 1.0,
- 'inch': 25.4,
- 'feet': 304.8,
- 'meter': 1000.0,
- 'micron': 0.001
- };
- if ( scale_units[ unit ] !== undefined ) {
- scale = scale_units[ unit ];
- }
- console.log( " Unit scale: " + scale );
- return scale;
- }
- function loadMaterials( node ) {
- var mat = node;
- var loadedmaterial = null;
- var matname = "AMF Material";
- var matid = mat.attributes[ 'id' ].textContent;
- var color;
- for ( var i = 0; i < mat.children.length; i ++ ) {
- var matchildel = mat.children[ i ];
- if ( matchildel.nodeName === "metadata" && matchildel.attributes[ 'type' ] !== undefined ) {
- if ( matchildel.attributes[ 'type' ].value === 'name' ) {
- matname = matchildel.textContent;
- }
- } else if ( matchildel.nodeName === 'color' ) {
- color = loadColor( matchildel );
- }
- }
- loadedmaterial = new THREE.MeshPhongMaterial( {
- shading: THREE.FlatShading,
- color: new THREE.Color( color.r, color.g, color.b ),
- name: matname } );
- if ( color.opacity !== 1.0 ) {
- loadedmaterial.transparent = true;
- loadedmaterial.opacity = color.opacity;
- }
- return { 'id': matid, 'material': loadedmaterial };
- }
- function loadColor( node ) {
- var color = { 'r': 1.0, 'g': 1.0, 'b': 1.0, 'a': 1.0, opacity: 1.0 };
- for ( var i = 0; i < node.children.length; i ++ ) {
- var matcolor = node.children[ i ];
- if ( matcolor.nodeName === 'r' ) {
- color.r = matcolor.textContent;
- } else if ( matcolor.nodeName === 'g' ) {
- color.g = matcolor.textContent;
- } else if ( matcolor.nodeName === 'b' ) {
- color.b = matcolor.textContent;
- } else if ( matcolor.nodeName === 'a' ) {
- color.opacity = matcolor.textContent;
- }
- }
- return color;
- }
- function loadMeshVolume( node ) {
- var volume = { "name": "", "triangles": [], "materialid": null };
- var currvolumenode = node.firstElementChild;
- if ( node.attributes[ 'materialid' ] !== undefined ) {
- volume.materialid = node.attributes[ 'materialid' ].nodeValue;
- }
- while ( currvolumenode ) {
- if ( currvolumenode.nodeName === "metadata" ) {
- if ( currvolumenode.attributes[ 'type' ] !== undefined ) {
- if ( currvolumenode.attributes[ 'type' ].value === 'name' ) {
- volume.name = currvolumenode.textContent;
- }
- }
- } else if ( currvolumenode.nodeName === "triangle" ) {
- var trianglenode = currvolumenode.firstElementChild;
- while ( trianglenode ) {
- if ( trianglenode.nodeName === "v1" ||
- trianglenode.nodeName === "v2" ||
- trianglenode.nodeName === "v3" ) {
- volume.triangles.push( trianglenode.textContent );
- }
- trianglenode = trianglenode.nextElementSibling;
- }
- }
- currvolumenode = currvolumenode.nextElementSibling;
- }
- return volume;
- }
- function loadMeshVertices( node ) {
- var vert_array = [];
- var currverticesnode = node.firstElementChild;
- while ( currverticesnode ) {
- if ( currverticesnode.nodeName === "vertex" ) {
- var vnode = currverticesnode.firstElementChild;
- while ( vnode ) {
- if ( vnode.nodeName === "coordinates" ) {
- var coordnode = vnode.firstElementChild;
- while ( coordnode ) {
- if ( coordnode.nodeName === "x" ||
- coordnode.nodeName === "y" ||
- coordnode.nodeName === "z" ) {
- vert_array.push( coordnode.textContent );
- }
- coordnode = coordnode.nextElementSibling;
- }
- }
- vnode = vnode.nextElementSibling;
- }
- }
- currverticesnode = currverticesnode.nextElementSibling;
- }
- return vert_array;
- }
- function loadObject( node ) {
- "use strict";
- var objid = node.attributes[ 'id' ].textContent;
- var loadedobject = { "name": "amfobject", "meshes": [] };
- var currcolor = null;
- var currobjnode = node.firstElementChild;
- while ( currobjnode ) {
- if ( currobjnode.nodeName === "metadata" ) {
- if ( currobjnode.attributes[ 'type' ] !== undefined ) {
- if ( currobjnode.attributes[ 'type' ].value === 'name' ) {
- loadedobject.name = currobjnode.textContent;
- }
- }
- } else if ( currobjnode.nodeName === "color" ) {
- currcolor = loadColor( currobjnode );
- } else if ( currobjnode.nodeName === "mesh" ) {
- var currmeshnode = currobjnode.firstElementChild;
- var mesh = { "vertices": [], "volumes": [], "color": currcolor };
- while ( currmeshnode ) {
- if ( currmeshnode.nodeName === "vertices" ) {
- mesh.vertices = mesh.vertices.concat( loadMeshVertices( currmeshnode ) );
- } else if ( currmeshnode.nodeName === "volume" ) {
- mesh.volumes.push( loadMeshVolume( currmeshnode ) );
- }
- currmeshnode = currmeshnode.nextElementSibling;
- }
- loadedobject.meshes.push( mesh );
- }
- currobjnode = currobjnode.nextElementSibling;
- }
- return { 'id': objid, 'obj': loadedobject };
- }
- //
- var xmldata = loadDocument( data );
- var amfName = "";
- var amfAuthor = "";
- var amfScale = loadDocumentScale( xmldata );
- var amfMaterials = {};
- var amfObjects = {};
- var children = xmldata.documentElement.children;
- for ( var i = 0; i < children.length; i ++ ) {
- var child = children[ i ];
- if ( child.nodeName === 'metadata' ) {
- if ( child.attributes[ 'type' ] !== undefined ) {
- if ( child.attributes[ 'type' ].value === 'name' ) {
- amfName = child.textContent;
- } else if ( child.attributes[ 'type' ].value === 'author' ) {
- amfAuthor = child.textContent;
- }
- }
- } else if ( child.nodeName === 'material' ) {
- var loadedmaterial = loadMaterials( child );
- amfMaterials[ loadedmaterial.id ] = loadedmaterial.material;
- } else if ( child.nodeName === 'object' ) {
- var loadedobject = loadObject( child );
- amfObjects[ loadedobject.id ] = loadedobject.obj;
- }
- }
- var sceneobject = new THREE.Group();
- sceneobject.name = amfName;
- sceneobject.userData.author = amfAuthor;
- sceneobject.userData.loader = "AMF";
- var defaultmaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaff, shading: THREE.FlatShading } );
- for ( var id in amfObjects ) {
- var meshes = amfObjects[ id ].meshes;
- var newobject = new THREE.Group();
- for ( var i = 0; i < meshes.length; i ++ ) {
- var mesh = meshes[ i ];
- var meshvertices = Float32Array.from( mesh.vertices );
- var vertices = new THREE.BufferAttribute( Float32Array.from( meshvertices ), 3 );
- var objdefaultmaterial = defaultmaterial;
- if ( mesh.color ) {
- var color = mesh.color;
- objdefaultmaterial = defaultmaterial.clone();
- objdefaultmaterial.color = new THREE.Color( color.r, color.g, color.b );
- if ( color.a !== 1.0 ) {
- objdefaultmaterial.transparent = true;
- objdefaultmaterial.opacity = color.a;
- }
- }
- var volumes = mesh.volumes;
- for ( var j = 0; j < volumes.length; j ++ ) {
- var volume = volumes[ j ];
- var newgeometry = new THREE.BufferGeometry();
- var indexes = Uint32Array.from( volume.triangles );
- var normals = new Uint32Array( vertices.array.length );
- var material = objdefaultmaterial;
- newgeometry.setIndex( new THREE.BufferAttribute( indexes, 1 ) );
- newgeometry.addAttribute( 'position', vertices.clone() );
- if ( amfMaterials[ volume.materialid ] !== undefined ) {
- material = amfMaterials[ volume.materialid ];
- }
- newgeometry.scale( amfScale, amfScale, amfScale );
- newobject.add( new THREE.Mesh( newgeometry, material.clone() ) );
- }
- }
- sceneobject.add( newobject );
- }
- return sceneobject;
- }
- };
|