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

Moved geometry exporter into its own class.

Naming suggestions welcome...
Maybe *Importer and *Exporter instead of *Loader and *Saver?
上级 cb0cfc45
......@@ -25,6 +25,7 @@
<script src="../examples/js/loaders/STLLoader.js"></script>
<script src="../examples/js/loaders/UTF8Loader.js"></script>
<script src="../examples/js/loaders/VTKLoader.js"></script>
<script src="../examples/js/savers/GeometrySaver.js"></script>
<script src="js/libs/signals.min.js"></script>
......
......@@ -110,176 +110,12 @@ Sidebar.Properties.Geometry = function ( signals ) {
function exportGeometry() {
var geometry = selected;
var output = new THREE.GeometrySaver().save( selected );
var vertices = [];
for ( var i = 0; i < geometry.vertices.length; i ++ ) {
var vertex = geometry.vertices[ i ];
vertices.push( vertex.x, vertex.y, vertex.z );
}
var faces = [];
var uvs = [[]];
var normals = [];
var normalsHash = {};
for ( var i = 0; i < geometry.faces.length; i ++ ) {
var face = geometry.faces[ i ];
var isTriangle = face instanceof THREE.Face3;
var hasMaterial = face.materialIndex !== undefined;
var hasFaceUv = geometry.faceUvs[ 0 ][ i ] !== undefined;
var hasFaceVertexUv = geometry.faceVertexUvs[ 0 ][ i ] !== undefined;
var hasFaceNormal = face.normal.length() > 0;
var hasFaceVertexNormal = face.vertexNormals[ 0 ] !== undefined;
var hasFaceColor = face.color;
var hasFaceVertexColor = face.vertexColors[ 0 ] !== undefined;
var faceType = 0;
faceType = setBit( faceType, 0, ! isTriangle );
// faceType = setBit( faceType, 1, hasMaterial );
// faceType = setBit( faceType, 2, hasFaceUv );
// faceType = setBit( faceType, 3, hasFaceVertexUv );
faceType = setBit( faceType, 4, hasFaceNormal );
faceType = setBit( faceType, 5, hasFaceVertexNormal );
// faceType = setBit( faceType, 6, hasFaceColor );
// faceType = setBit( faceType, 7, hasFaceVertexColor );
faces.push( faceType );
if ( isTriangle ) {
faces.push( face.a, face.b, face.c );
} else {
faces.push( face.a, face.b, face.c, face.d );
}
if ( hasMaterial ) {
faces.push( face.materialIndex );
}
if ( hasFaceUv ) {
/*
var uv = geometry.faceUvs[ 0 ][ i ];
uvs[ 0 ].push( uv.u, uv.v );
*/
}
if ( hasFaceVertexUv ) {
/*
var uvs = geometry.faceVertexUvs[ 0 ][ i ];
if ( isTriangle ) {
faces.push(
uvs[ 0 ].u, uvs[ 0 ].v,
uvs[ 1 ].u, uvs[ 1 ].v,
uvs[ 2 ].u, uvs[ 2 ].v
);
} else {
faces.push(
uvs[ 0 ].u, uvs[ 0 ].v,
uvs[ 1 ].u, uvs[ 1 ].v,
uvs[ 2 ].u, uvs[ 2 ].v,
uvs[ 3 ].u, uvs[ 3 ].v
);
}
*/
}
if ( hasFaceNormal ) {
var faceNormal = face.normal;
faces.push( getNormalIndex( faceNormal.x, faceNormal.y, faceNormal.z ) );
}
if ( hasFaceVertexNormal ) {
var vertexNormals = face.vertexNormals;
if ( isTriangle ) {
faces.push(
getNormalIndex( vertexNormals[ 0 ].x, vertexNormals[ 0 ].y, vertexNormals[ 0 ].z ),
getNormalIndex( vertexNormals[ 1 ].x, vertexNormals[ 1 ].y, vertexNormals[ 1 ].z ),
getNormalIndex( vertexNormals[ 2 ].x, vertexNormals[ 2 ].y, vertexNormals[ 2 ].z )
);
} else {
faces.push(
getNormalIndex( vertexNormals[ 0 ].x, vertexNormals[ 0 ].y, vertexNormals[ 0 ].z ),
getNormalIndex( vertexNormals[ 1 ].x, vertexNormals[ 1 ].y, vertexNormals[ 1 ].z ),
getNormalIndex( vertexNormals[ 2 ].x, vertexNormals[ 2 ].y, vertexNormals[ 2 ].z ),
getNormalIndex( vertexNormals[ 3 ].x, vertexNormals[ 3 ].y, vertexNormals[ 3 ].z )
);
}
}
}
function setBit( value, position, enabled ) {
return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
}
function getNormalIndex( x, y, z ) {
var hash = x.toString() + y.toString() + z.toString();
if ( normalsHash[ hash ] !== undefined ) {
return normalsHash[ hash ];
}
normalsHash[ hash ] = normals.length / 3;
normals.push( x, y, z );
return normalsHash[ hash ];
}
//
var output = [
'{',
' "metadata": {',
' "formatVersion" : 3',
' },',
' "vertices": ' + JSON.stringify( vertices ) + ',',
' "normals": ' + JSON.stringify( normals ) + ',',
' "uvs": ' + JSON.stringify( uvs ) + ',',
' "faces": ' + JSON.stringify( faces ),
'}'
].join( '\n' );
var file = new BlobBuilder();
file.append( output );
var objectURL = URL.createObjectURL( file.getBlob( 'text/json' ) );
var blob = new Blob( [ output ], { type: 'text/json' } );
var objectURL = URL.createObjectURL( blob );
/*
var clickEvent = document.createEvent( 'MouseEvent' );
clickEvent.initMouseEvent( 'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null );
......@@ -289,6 +125,7 @@ Sidebar.Properties.Geometry = function ( signals ) {
download.dispatchEvent( clickEvent );
URL.revokeObjectURL( objectURL );
*/
}
......
/**
* @author mrdoob / http://mrdoob.com/
*/
THREE.GeometrySaver = function () {};
THREE.GeometrySaver.prototype = {
constructor: THREE.GeometrySaver,
save: function ( geometry ) {
var vertices = [];
for ( var i = 0; i < geometry.vertices.length; i ++ ) {
var vertex = geometry.vertices[ i ];
vertices.push( vertex.x, vertex.y, vertex.z );
}
var faces = [];
var uvs = [[]];
var normals = [];
var normalsHash = {};
for ( var i = 0; i < geometry.faces.length; i ++ ) {
var face = geometry.faces[ i ];
var isTriangle = face instanceof THREE.Face3;
var hasMaterial = face.materialIndex !== undefined;
var hasFaceUv = geometry.faceUvs[ 0 ][ i ] !== undefined;
var hasFaceVertexUv = geometry.faceVertexUvs[ 0 ][ i ] !== undefined;
var hasFaceNormal = face.normal.length() > 0;
var hasFaceVertexNormal = face.vertexNormals[ 0 ] !== undefined;
var hasFaceColor = face.color;
var hasFaceVertexColor = face.vertexColors[ 0 ] !== undefined;
var faceType = 0;
faceType = setBit( faceType, 0, ! isTriangle );
// faceType = setBit( faceType, 1, hasMaterial );
// faceType = setBit( faceType, 2, hasFaceUv );
// faceType = setBit( faceType, 3, hasFaceVertexUv );
faceType = setBit( faceType, 4, hasFaceNormal );
faceType = setBit( faceType, 5, hasFaceVertexNormal );
// faceType = setBit( faceType, 6, hasFaceColor );
// faceType = setBit( faceType, 7, hasFaceVertexColor );
faces.push( faceType );
if ( isTriangle ) {
faces.push( face.a, face.b, face.c );
} else {
faces.push( face.a, face.b, face.c, face.d );
}
if ( hasMaterial ) {
faces.push( face.materialIndex );
}
if ( hasFaceUv ) {
/*
var uv = geometry.faceUvs[ 0 ][ i ];
uvs[ 0 ].push( uv.u, uv.v );
*/
}
if ( hasFaceVertexUv ) {
/*
var uvs = geometry.faceVertexUvs[ 0 ][ i ];
if ( isTriangle ) {
faces.push(
uvs[ 0 ].u, uvs[ 0 ].v,
uvs[ 1 ].u, uvs[ 1 ].v,
uvs[ 2 ].u, uvs[ 2 ].v
);
} else {
faces.push(
uvs[ 0 ].u, uvs[ 0 ].v,
uvs[ 1 ].u, uvs[ 1 ].v,
uvs[ 2 ].u, uvs[ 2 ].v,
uvs[ 3 ].u, uvs[ 3 ].v
);
}
*/
}
if ( hasFaceNormal ) {
var faceNormal = face.normal;
faces.push( getNormalIndex( faceNormal.x, faceNormal.y, faceNormal.z ) );
}
if ( hasFaceVertexNormal ) {
var vertexNormals = face.vertexNormals;
if ( isTriangle ) {
faces.push(
getNormalIndex( vertexNormals[ 0 ].x, vertexNormals[ 0 ].y, vertexNormals[ 0 ].z ),
getNormalIndex( vertexNormals[ 1 ].x, vertexNormals[ 1 ].y, vertexNormals[ 1 ].z ),
getNormalIndex( vertexNormals[ 2 ].x, vertexNormals[ 2 ].y, vertexNormals[ 2 ].z )
);
} else {
faces.push(
getNormalIndex( vertexNormals[ 0 ].x, vertexNormals[ 0 ].y, vertexNormals[ 0 ].z ),
getNormalIndex( vertexNormals[ 1 ].x, vertexNormals[ 1 ].y, vertexNormals[ 1 ].z ),
getNormalIndex( vertexNormals[ 2 ].x, vertexNormals[ 2 ].y, vertexNormals[ 2 ].z ),
getNormalIndex( vertexNormals[ 3 ].x, vertexNormals[ 3 ].y, vertexNormals[ 3 ].z )
);
}
}
}
function setBit( value, position, enabled ) {
return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
}
function getNormalIndex( x, y, z ) {
var hash = x.toString() + y.toString() + z.toString();
if ( normalsHash[ hash ] !== undefined ) {
return normalsHash[ hash ];
}
normalsHash[ hash ] = normals.length / 3;
normals.push( x, y, z );
return normalsHash[ hash ];
}
//
var output = [
'{',
' "metadata": {',
' "formatVersion" : 3',
' },',
' "vertices": ' + JSON.stringify( vertices ) + ',',
' "normals": ' + JSON.stringify( normals ) + ',',
' "uvs": ' + JSON.stringify( uvs ) + ',',
' "faces": ' + JSON.stringify( faces ),
'}'
].join( '\n' );
return output;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册