123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- THREE.GeometryExporter = function () {};
- THREE.GeometryExporter.prototype = {
- constructor: THREE.GeometryExporter,
- parse: function ( geometry ) {
- var output = {
- metadata: {
- version: 4.0,
- type: 'geometry',
- generator: 'GeometryExporter'
- }
- };
- 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 normals = [];
- var normalsHash = {};
- var colors = [];
- var colorsHash = {};
- var uvs = [];
- for ( var i = 0; i < geometry.faces.length; i ++ ) {
- var face = geometry.faces[ i ];
- var isTriangle = face instanceof THREE.Face3;
- var hasMaterial = false; // face.materialIndex !== undefined;
- var hasFaceUv = false; // geometry.faceUvs[ 0 ][ i ] !== undefined;
- var hasFaceVertexUv = false; // geometry.faceVertexUvs[ 0 ][ i ] !== undefined;
- var hasFaceNormal = face.normal.length() > 0;
- var hasFaceVertexNormal = face.vertexNormals.length > 0;
- var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
- var hasFaceVertexColor = face.vertexColors.length > 0;
- 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 ) {
- faces.push( getNormalIndex( face.normal ) );
- }
- if ( hasFaceVertexNormal ) {
- var vertexNormals = face.vertexNormals;
- if ( isTriangle ) {
- faces.push(
- getNormalIndex( vertexNormals[ 0 ] ),
- getNormalIndex( vertexNormals[ 1 ] ),
- getNormalIndex( vertexNormals[ 2 ] )
- );
- } else {
- faces.push(
- getNormalIndex( vertexNormals[ 0 ] ),
- getNormalIndex( vertexNormals[ 1 ] ),
- getNormalIndex( vertexNormals[ 2 ] ),
- getNormalIndex( vertexNormals[ 3 ] )
- );
- }
- }
- if ( hasFaceColor ) {
- faces.push( getColorIndex( face.color ) );
- }
- if ( hasFaceVertexColor ) {
- var vertexColors = face.vertexColors;
- if ( isTriangle ) {
- faces.push(
- getColorIndex( vertexColors[ 0 ] ),
- getColorIndex( vertexColors[ 1 ] ),
- getColorIndex( vertexColors[ 2 ] )
- );
- } else {
- faces.push(
- getColorIndex( vertexColors[ 0 ] ),
- getColorIndex( vertexColors[ 1 ] ),
- getColorIndex( vertexColors[ 2 ] ),
- getColorIndex( vertexColors[ 3 ] )
- );
- }
- }
- }
- function setBit( value, position, enabled ) {
- return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
- }
- function getNormalIndex( normal ) {
- var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
- if ( normalsHash[ hash ] !== undefined ) {
- return normalsHash[ hash ];
- }
- normalsHash[ hash ] = normals.length / 3;
- normals.push( normal.x, normal.y, normal.z );
- return normalsHash[ hash ];
- }
- function getColorIndex( color ) {
- var hash = color.r.toString() + color.g.toString() + color.b.toString();
- if ( colorsHash[ hash ] !== undefined ) {
- return colorsHash[ hash ];
- }
- colorsHash[ hash ] = colors.length;
- colors.push( color.getHex() );
- return colorsHash[ hash ];
- }
- output.vertices = vertices;
- output.normals = normals;
- if ( colors.length > 0 ) output.colors = colors;
- if ( uvs.length > 0 ) output.uvs = uvs;
- output.faces = faces;
- //
- return output;
- }
- };
|