提交 b971e3c2 编写于 作者: M Mr.doob

AMFLoader: Clean up.

上级 af0b4346
......@@ -48,448 +48,457 @@ THREE.AMFLoader.prototype = {
parse: function ( data ) {
var amfName = "";
var amfAuthor = "";
var amfScale = 1.0;
var amfMaterials = {};
var amfObjects = {};
function loadDocument( data ) {
var xmldata = this._loadDocument( data );
var view = new DataView( data );
amfScale = this._loadDocumentScale( xmldata );
var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) );
var documentchildren = xmldata.documentElement.children;
if ( magic === "PK" ) {
for ( var i = 0; i < documentchildren.length; i ++ ) {
console.log( "Loading Zip" );
var zip = null;
var file = null;
if ( documentchildren[ i ].nodeName === 'metadata' ) {
try {
if ( documentchildren[ i ].attributes[ 'type' ] !== undefined ) {
zip = new JSZip( data );
if ( documentchildren[ i ].attributes[ 'type' ].value === 'name' ) {
} catch ( e ) {
amfName = documentchildren[ i ].textContent;
if ( e instanceof ReferenceError ) {
} else if ( documentchildren[ i ].attributes[ 'type' ].value === 'author' ) {
amfAuthor = documentchildren[ i ].textContent;
console.log( " jszip missing and file is compressed." );
return null;
}
}
} else if ( documentchildren[ i ].nodeName === 'material' ) {
var loadedmaterial = this._loadMaterials( documentchildren[ i ] );
amfMaterials[ loadedmaterial.id ] = loadedmaterial.material;
} else if ( documentchildren[ i ].nodeName === 'object' ) {
for ( file in zip.files ) {
var loadedobject = this._loadObject( documentchildren[ i ] );
amfObjects[ loadedobject.id ] = loadedobject.obj;
}
}
if ( file.toLowerCase().endsWith( ".amf" ) ) {
var sceneobject = new THREE.Object3D();
break;
sceneobject.name = amfName;
sceneobject.userData.author = amfAuthor;
sceneobject.userData.loader = "AMF";
}
var defaultmaterial = new THREE.MeshPhongMaterial( { shading: THREE.FlatShading, color: 0xaaaaff } );
}
for ( var objid in amfObjects ) {
console.log( " Trying to load file asset: " + file );
view = new DataView( zip.file( file ).asArrayBuffer() );
var newobject = new THREE.Object3D();
}
for ( var meshi = 0; meshi < amfObjects[ objid ].meshes.length; meshi ++ ) {
if ( TextDecoder === undefined ) {
var meshvertices = Float32Array.from( amfObjects[ objid ].meshes[ meshi ].vertices );
var vertices = new THREE.BufferAttribute( Float32Array.from( meshvertices ), 3 );
var objdefaultmaterial = defaultmaterial;
console.log( " TextDecoder not present. Please use TextDecoder polyfill." );
return null;
if ( amfObjects[ objid ].meshes[ meshi ].color ) {
}
var color = amfObjects[ objid ].meshes[ meshi ].color;
objdefaultmaterial = defaultmaterial.clone();
objdefaultmaterial.color = new THREE.Color( color.r, color.g, color.b );
var filetext = new TextDecoder( 'utf-8' ).decode( view );
if ( color.a != 1.0 ) {
var xmldata = new DOMParser().parseFromString( filetext, 'application/xml' );
objdefaultmaterial.transparent = true;
objdefaultmaterial.opacity = color.a;
if ( xmldata.documentElement.nodeName.toLowerCase() !== "amf" ) {
}
console.log( " Error loading AMF - no AMF document found." );
return null;
}
}
for ( var voli = 0; voli < amfObjects[ objid ].meshes[ meshi ].volumes.length; voli ++ ) {
return xmldata;
var currvolume = amfObjects[ objid ].meshes[ meshi ].volumes[ voli ];
var newgeometry = new THREE.BufferGeometry();
var indexes = Uint32Array.from( currvolume.triangles );
var normals = new Uint32Array( vertices.array.length );
}
var material = objdefaultmaterial;
function loadDocumentScale( xmldata ) {
newgeometry.setIndex( new THREE.BufferAttribute( indexes, 1 ) );
newgeometry.addAttribute( 'position', vertices.clone() );
var scale = 1.0;
if ( amfMaterials[ currvolume.materialid ] !== undefined ) {
var unit = 'millimeter';
material = amfMaterials[ currvolume.materialid ];
if ( xmldata.documentElement.attributes[ 'unit' ] !== undefined ) {
}
unit = xmldata.documentElement.attributes[ 'unit' ].value.toLowerCase();
newgeometry.scale( amfScale, amfScale, amfScale );
}
var newmesh = new THREE.Mesh( newgeometry, material.clone() );
var scale_units = {
'millimeter': 1.0,
'inch': 25.4,
'feet': 304.8,
'meter': 1000.0,
'micron': 0.001
};
newobject.add( newmesh );
if ( scale_units[ unit ] !== undefined ) {
}
scale = scale_units[ unit ];
}
sceneobject.add( newobject );
}
console.log( " Unit scale: " + scale );
return scale;
return sceneobject;
}
},
function loadMaterials( node ) {
_loadDocument: function ( data ) {
var mat = node;
var view = new DataView( data );
var loadedmaterial = null;
var matname = "AMF Material";
var matid = mat.attributes[ 'id' ].textContent;
var color;
var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) );
for ( var i = 0; i < mat.children.length; i ++ ) {
if ( magic === "PK" ) {
var matchildel = mat.children[ i ];
console.log( "Loading Zip" );
var zip = null;
var file = null;
if ( matchildel.nodeName === "metadata" && matchildel.attributes[ 'type' ] !== undefined ) {
try {
if ( matchildel.attributes[ 'type' ].value === 'name' ) {
zip = new JSZip( data );
matname = matchildel.textContent;
} catch ( e ) {
}
if ( e instanceof ReferenceError ) {
} else if ( matchildel.nodeName === 'color' ) {
console.log( " jszip missing and file is compressed." );
return null;
color = loadColor( matchildel );
}
}
for ( file in zip.files ) {
loadedmaterial = new THREE.MeshPhongMaterial( {
shading: THREE.FlatShading,
color: new THREE.Color( color.r, color.g, color.b ),
name: matname } );
if ( file.toLowerCase().endsWith( ".amf" ) ) {
if ( color.opacity !== 1.0 ) {
break;
}
loadedmaterial.transparent = true;
loadedmaterial.opacity = color.opacity;
}
console.log( " Trying to load file asset: " + file );
view = new DataView( zip.file( file ).asArrayBuffer() );
return { 'id': matid, 'material': loadedmaterial };
}
if ( TextDecoder === undefined ) {
function loadColor( node ) {
console.log( " TextDecoder not present. Please use TextDecoder polyfill." );
return null;
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 filetext = new TextDecoder( 'utf-8' ).decode( view );
var matcolor = node.children[ i ];
var xmldata = new DOMParser().parseFromString( filetext, 'application/xml' );
if ( matcolor.nodeName === 'r' ) {
if ( xmldata.documentElement.nodeName.toLowerCase() !== "amf" ) {
color.r = matcolor.textContent;
console.log( " Error loading AMF - no AMF document found." );
return null;
} else if ( matcolor.nodeName === 'g' ) {
}
color.g = matcolor.textContent;
return xmldata;
} else if ( matcolor.nodeName === 'b' ) {
},
color.b = matcolor.textContent;
_loadDocumentScale: function ( xmldata ) {
} else if ( matcolor.nodeName === 'a' ) {
var scale = 1.0;
color.opacity = matcolor.textContent;
var unit = 'millimeter';
}
if ( xmldata.documentElement.attributes[ 'unit' ] !== undefined ) {
}
unit = xmldata.documentElement.attributes[ 'unit' ].value.toLowerCase();
return color;
}
var scale_units = {
'millimeter': 1.0,
'inch': 25.4,
'feet': 304.8,
'meter': 1000.0,
'micron': 0.001
};
function loadMeshVolume( node ) {
if ( scale_units[ unit ] !== undefined ) {
var volume = { "name": "", "triangles": [], "materialid": null };
scale = scale_units[ unit ];
var currvolumenode = node.firstElementChild;
}
if ( node.attributes[ 'materialid' ] !== undefined ) {
console.log( " Unit scale: " + scale );
return scale;
volume.materialid = node.attributes[ 'materialid' ].nodeValue;
},
}
_loadMaterials: function ( node ) {
while ( currvolumenode ) {
var mat = node;
if ( currvolumenode.nodeName === "metadata" ) {
var loadedmaterial = null;
var matname = "AMF Material";
var matid = mat.attributes[ 'id' ].textContent;
var color;
if ( currvolumenode.attributes[ 'type' ] !== undefined ) {
for ( var i = 0; i < mat.children.length; i ++ ) {
if ( currvolumenode.attributes[ 'type' ].value === 'name' ) {
var matchildel = mat.children[ i ];
volume.name = currvolumenode.textContent;
if ( matchildel.nodeName === "metadata" && matchildel.attributes[ 'type' ] !== undefined ) {
}
}
if ( matchildel.attributes[ 'type' ].value === 'name' ) {
} else if ( currvolumenode.nodeName === "triangle" ) {
matname = matchildel.textContent;
var trianglenode = currvolumenode.firstElementChild;
}
while ( trianglenode ) {
} else if ( matchildel.nodeName === 'color' ) {
if ( trianglenode.nodeName === "v1" ||
trianglenode.nodeName === "v2" ||
trianglenode.nodeName === "v3" ) {
volume.triangles.push( trianglenode.textContent );
}
color = this._loadColor( matchildel );
trianglenode = trianglenode.nextElementSibling;
}
}
currvolumenode = currvolumenode.nextElementSibling;
}
return volume;
}
loadedmaterial = new THREE.MeshPhongMaterial( {
shading: THREE.FlatShading,
color: new THREE.Color( color.r, color.g, color.b ),
name: matname } );
function loadMeshVertices( node ) {
if ( color.opacity !== 1.0 ) {
var vert_array = [];
loadedmaterial.transparent = true;
loadedmaterial.opacity = color.opacity;
var currverticesnode = node.firstElementChild;
}
while ( currverticesnode ) {
return { 'id': matid, 'material': loadedmaterial };
if ( currverticesnode.nodeName === "vertex" ) {
},
var vnode = currverticesnode.firstElementChild;
_loadColor: function ( node ) {
while ( vnode ) {
var color = { 'r': 1.0, 'g': 1.0, 'b': 1.0, 'a': 1.0, opacity: 1.0 };
if ( vnode.nodeName === "coordinates" ) {
for ( var i = 0; i < node.children.length; i ++ ) {
var coordnode = vnode.firstElementChild;
var matcolor = node.children[ i ];
while ( coordnode ) {
if ( matcolor.nodeName === 'r' ) {
if ( coordnode.nodeName === "x" ||
coordnode.nodeName === "y" ||
coordnode.nodeName === "z" ) {
color.r = matcolor.textContent;
vert_array.push( coordnode.textContent );
} else if ( matcolor.nodeName === 'g' ) {
}
color.g = matcolor.textContent;
coordnode = coordnode.nextElementSibling;
} else if ( matcolor.nodeName === 'b' ) {
}
color.b = matcolor.textContent;
}
vnode = vnode.nextElementSibling;
} else if ( matcolor.nodeName === 'a' ) {
}
color.opacity = matcolor.textContent;
}
currverticesnode = currverticesnode.nextElementSibling;
}
return vert_array;
}
return color;
function loadObject( node ) {
},
"use strict";
_loadMeshVolume: function( node ) {
var objid = node.attributes[ 'id' ].textContent;
var loadedobject = { "name": "amfobject", "meshes": [] };
var volume = { "name": "", "triangles": [], "materialid": null };
var currcolor = null;
var currvolumenode = node.firstElementChild;
var currobjnode = node.firstElementChild;
if ( node.attributes[ 'materialid' ] !== undefined ) {
while ( currobjnode ) {
volume.materialid = node.attributes[ 'materialid' ].nodeValue;
if ( currobjnode.nodeName === "metadata" ) {
}
if ( currobjnode.attributes[ 'type' ] !== undefined ) {
while ( currvolumenode ) {
if ( currobjnode.attributes[ 'type' ].value === 'name' ) {
if ( currvolumenode.nodeName === "metadata" ) {
loadedobject.name = currobjnode.textContent;
if ( currvolumenode.attributes[ 'type' ] !== undefined ) {
}
if ( currvolumenode.attributes[ 'type' ].value === 'name' ) {
}
volume.name = currvolumenode.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 ) {
} else if ( currvolumenode.nodeName === "triangle" ) {
if ( currmeshnode.nodeName === "vertices" ) {
var trianglenode = currvolumenode.firstElementChild;
mesh.vertices = mesh.vertices.concat( loadMeshVertices( currmeshnode ) );
while ( trianglenode ) {
} else if ( currmeshnode.nodeName === "volume" ) {
if ( trianglenode.nodeName === "v1" ||
trianglenode.nodeName === "v2" ||
trianglenode.nodeName === "v3" ) {
mesh.volumes.push( loadMeshVolume( currmeshnode ) );
volume.triangles.push( trianglenode.textContent );
}
currmeshnode = currmeshnode.nextElementSibling;
}
trianglenode = trianglenode.nextElementSibling;
loadedobject.meshes.push( mesh );
}
currobjnode = currobjnode.nextElementSibling;
}
currvolumenode = currvolumenode.nextElementSibling;
}
return { 'id': objid, 'obj': loadedobject };
return volume;
}
},
//
_loadMeshVertices: function( node ) {
var xmldata = loadDocument( data );
var vert_array = [];
var amfName = "";
var amfAuthor = "";
var amfScale = loadDocumentScale( xmldata );
var amfMaterials = {};
var amfObjects = {};
var currverticesnode = node.firstElementChild;
var children = xmldata.documentElement.children;
while ( currverticesnode ) {
for ( var i = 0; i < children.length; i ++ ) {
if ( currverticesnode.nodeName === "vertex" ) {
var child = children[ i ];
var vnode = currverticesnode.firstElementChild;
if ( child.nodeName === 'metadata' ) {
while ( vnode ) {
if ( child.attributes[ 'type' ] !== undefined ) {
if ( vnode.nodeName === "coordinates" ) {
if ( child.attributes[ 'type' ].value === 'name' ) {
var coordnode = vnode.firstElementChild;
amfName = child.textContent;
while ( coordnode ) {
} else if ( child.attributes[ 'type' ].value === 'author' ) {
if ( coordnode.nodeName === "x" ||
coordnode.nodeName === "y" ||
coordnode.nodeName === "z" ) {
amfAuthor = child.textContent;
vert_array.push( coordnode.textContent );
}
}
}
coordnode = coordnode.nextElementSibling;
} else if ( child.nodeName === 'material' ) {
}
var loadedmaterial = loadMaterials( child );
amfMaterials[ loadedmaterial.id ] = loadedmaterial.material;
}
vnode = vnode.nextElementSibling;
} else if ( child.nodeName === 'object' ) {
}
var loadedobject = loadObject( child );
amfObjects[ loadedobject.id ] = loadedobject.obj;
}
currverticesnode = currverticesnode.nextElementSibling;
}
return vert_array;
var sceneobject = new THREE.Group();
},
sceneobject.name = amfName;
sceneobject.userData.author = amfAuthor;
sceneobject.userData.loader = "AMF";
_loadObject: function ( node ) {
var defaultmaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaff, shading: THREE.FlatShading } );
"use strict";
for ( var id in amfObjects ) {
var objid = node.attributes[ 'id' ].textContent;
var loadedobject = { "name": "amfobject", "meshes": [] };
var meshes = amfObjects[ id ].meshes;
var currcolor = null;
var newobject = new THREE.Group();
var currobjnode = node.firstElementChild;
for ( var i = 0; i < meshes.length; i ++ ) {
while ( currobjnode ) {
var mesh = meshes[ i ];
if ( currobjnode.nodeName === "metadata" ) {
var meshvertices = Float32Array.from( mesh.vertices );
var vertices = new THREE.BufferAttribute( Float32Array.from( meshvertices ), 3 );
var objdefaultmaterial = defaultmaterial;
if ( mesh.color ) {
if ( currobjnode.attributes[ 'type' ] !== undefined ) {
var color = mesh.color;
objdefaultmaterial = defaultmaterial.clone();
objdefaultmaterial.color = new THREE.Color( color.r, color.g, color.b );
if ( currobjnode.attributes[ 'type' ].value === 'name' ) {
if ( color.a !== 1.0 ) {
loadedobject.name = currobjnode.textContent;
objdefaultmaterial.transparent = true;
objdefaultmaterial.opacity = color.a;
}
}
} else if ( currobjnode.nodeName === "color" ) {
currcolor = this._loadColor( currobjnode );
var volumes = mesh.volumes;
} else if ( currobjnode.nodeName === "mesh" ) {
for ( var j = 0; j < volumes.length; j ++ ) {
var currmeshnode = currobjnode.firstElementChild;
var mesh = { "vertices": [], "volumes": [], "color": currcolor };
var volume = volumes[ j ];
while ( currmeshnode ) {
var newgeometry = new THREE.BufferGeometry();
var indexes = Uint32Array.from( volume.triangles );
var normals = new Uint32Array( vertices.array.length );
if ( currmeshnode.nodeName === "vertices" ) {
var material = objdefaultmaterial;
mesh.vertices = mesh.vertices.concat( this._loadMeshVertices( currmeshnode ) );
newgeometry.setIndex( new THREE.BufferAttribute( indexes, 1 ) );
newgeometry.addAttribute( 'position', vertices.clone() );
} else if ( currmeshnode.nodeName === "volume" ) {
if ( amfMaterials[ volume.materialid ] !== undefined ) {
mesh.volumes.push( this._loadMeshVolume( currmeshnode ) );
material = amfMaterials[ volume.materialid ];
}
currmeshnode = currmeshnode.nextElementSibling;
newgeometry.scale( amfScale, amfScale, amfScale );
newobject.add( new THREE.Mesh( newgeometry, material.clone() ) );
}
loadedobject.meshes.push( mesh );
}
currobjnode = currobjnode.nextElementSibling;
sceneobject.add( newobject );
}
return { 'id': objid, 'obj': loadedobject };
return sceneobject;
}
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册