123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986 |
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author kile / http://kile.stravaganza.org/
- * @author alteredq / http://alteredqualia.com/
- * @author mikael emtinger / http://gomo.se/
- * @author zz85 / http://www.lab4games.net/zz85/blog
- * @author bhouston / http://exocortex.com
- */
- THREE.Geometry = function () {
- this.id = THREE.GeometryIdCount ++;
- this.uuid = THREE.Math.generateUUID();
- this.name = '';
- this.type = 'Geometry';
- this.vertices = [];
- this.colors = []; // one-to-one vertex colors, used in Points and Line
- this.faces = [];
- this.faceVertexUvs = [ [] ];
- this.morphTargets = [];
- this.morphColors = [];
- this.morphNormals = [];
- this.skinWeights = [];
- this.skinIndices = [];
- this.lineDistances = [];
- this.boundingBox = null;
- this.boundingSphere = null;
- this.hasTangents = false;
- this.dynamic = true; // the intermediate typed arrays will be deleted when set to false
- // update flags
- this.verticesNeedUpdate = false;
- this.elementsNeedUpdate = false;
- this.uvsNeedUpdate = false;
- this.normalsNeedUpdate = false;
- this.tangentsNeedUpdate = false;
- this.colorsNeedUpdate = false;
- this.lineDistancesNeedUpdate = false;
- this.buffersNeedUpdate = false;
- this.groupsNeedUpdate = false;
- };
- THREE.Geometry.prototype = {
- constructor: THREE.Geometry,
- applyMatrix: function ( matrix ) {
- var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
- for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
- var vertex = this.vertices[ i ];
- vertex.applyMatrix4( matrix );
- }
- for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
- var face = this.faces[ i ];
- face.normal.applyMatrix3( normalMatrix ).normalize();
- for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
- face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
- }
- }
- if ( this.boundingBox instanceof THREE.Box3 ) {
- this.computeBoundingBox();
- }
- if ( this.boundingSphere instanceof THREE.Sphere ) {
- this.computeBoundingSphere();
- }
- },
- center: function () {
- this.computeBoundingBox();
- var offset = new THREE.Vector3();
- offset.addVectors( this.boundingBox.min, this.boundingBox.max );
- offset.multiplyScalar( - 0.5 );
- this.applyMatrix( new THREE.Matrix4().makeTranslation( offset.x, offset.y, offset.z ) );
- this.computeBoundingBox();
- return offset;
- },
- computeFaceNormals: function () {
- var cb = new THREE.Vector3(), ab = new THREE.Vector3();
- for ( var f = 0, fl = this.faces.length; f < fl; f ++ ) {
- var face = this.faces[ f ];
- var vA = this.vertices[ face.a ];
- var vB = this.vertices[ face.b ];
- var vC = this.vertices[ face.c ];
- cb.subVectors( vC, vB );
- ab.subVectors( vA, vB );
- cb.cross( ab );
- cb.normalize();
- face.normal.copy( cb );
- }
- },
- computeVertexNormals: function ( areaWeighted ) {
- var v, vl, f, fl, face, vertices;
- vertices = new Array( this.vertices.length );
- for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
- vertices[ v ] = new THREE.Vector3();
- }
- if ( areaWeighted ) {
- // vertex normals weighted by triangle areas
- // http://www.iquilezles.org/www/articles/normals/normals.htm
- var vA, vB, vC, vD;
- var cb = new THREE.Vector3(), ab = new THREE.Vector3(),
- db = new THREE.Vector3(), dc = new THREE.Vector3(), bc = new THREE.Vector3();
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- vA = this.vertices[ face.a ];
- vB = this.vertices[ face.b ];
- vC = this.vertices[ face.c ];
- cb.subVectors( vC, vB );
- ab.subVectors( vA, vB );
- cb.cross( ab );
- vertices[ face.a ].add( cb );
- vertices[ face.b ].add( cb );
- vertices[ face.c ].add( cb );
- }
- } else {
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- vertices[ face.a ].add( face.normal );
- vertices[ face.b ].add( face.normal );
- vertices[ face.c ].add( face.normal );
- }
- }
- for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
- vertices[ v ].normalize();
- }
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- face.vertexNormals[ 0 ] = vertices[ face.a ].clone();
- face.vertexNormals[ 1 ] = vertices[ face.b ].clone();
- face.vertexNormals[ 2 ] = vertices[ face.c ].clone();
- }
- },
- computeMorphNormals: function () {
- var i, il, f, fl, face;
- // save original normals
- // - create temp variables on first access
- // otherwise just copy (for faster repeated calls)
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- if ( ! face.__originalFaceNormal ) {
- face.__originalFaceNormal = face.normal.clone();
- } else {
- face.__originalFaceNormal.copy( face.normal );
- }
- if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
- for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
- if ( ! face.__originalVertexNormals[ i ] ) {
- face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
- } else {
- face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
- }
- }
- }
- // use temp geometry to compute face and vertex normals for each morph
- var tmpGeo = new THREE.Geometry();
- tmpGeo.faces = this.faces;
- for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
- // create on first access
- if ( ! this.morphNormals[ i ] ) {
- this.morphNormals[ i ] = {};
- this.morphNormals[ i ].faceNormals = [];
- this.morphNormals[ i ].vertexNormals = [];
- var dstNormalsFace = this.morphNormals[ i ].faceNormals;
- var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
- var faceNormal, vertexNormals;
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- faceNormal = new THREE.Vector3();
- vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
- dstNormalsFace.push( faceNormal );
- dstNormalsVertex.push( vertexNormals );
- }
- }
- var morphNormals = this.morphNormals[ i ];
- // set vertices to morph target
- tmpGeo.vertices = this.morphTargets[ i ].vertices;
- // compute morph normals
- tmpGeo.computeFaceNormals();
- tmpGeo.computeVertexNormals();
- // store morph normals
- var faceNormal, vertexNormals;
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- faceNormal = morphNormals.faceNormals[ f ];
- vertexNormals = morphNormals.vertexNormals[ f ];
- faceNormal.copy( face.normal );
- vertexNormals.a.copy( face.vertexNormals[ 0 ] );
- vertexNormals.b.copy( face.vertexNormals[ 1 ] );
- vertexNormals.c.copy( face.vertexNormals[ 2 ] );
- }
- }
- // restore original normals
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- face.normal = face.__originalFaceNormal;
- face.vertexNormals = face.__originalVertexNormals;
- }
- },
- computeTangents: function () {
- // based on http://www.terathon.com/code/tangent.html
- // tangents go to vertices
- var f, fl, v, vl, i, il, vertexIndex,
- face, uv, vA, vB, vC, uvA, uvB, uvC,
- x1, x2, y1, y2, z1, z2,
- s1, s2, t1, t2, r, t, test,
- tan1 = [], tan2 = [],
- sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
- tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
- n = new THREE.Vector3(), w;
- for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
- tan1[ v ] = new THREE.Vector3();
- tan2[ v ] = new THREE.Vector3();
- }
- function handleTriangle( context, a, b, c, ua, ub, uc ) {
- vA = context.vertices[ a ];
- vB = context.vertices[ b ];
- vC = context.vertices[ c ];
- uvA = uv[ ua ];
- uvB = uv[ ub ];
- uvC = uv[ uc ];
- x1 = vB.x - vA.x;
- x2 = vC.x - vA.x;
- y1 = vB.y - vA.y;
- y2 = vC.y - vA.y;
- z1 = vB.z - vA.z;
- z2 = vC.z - vA.z;
- s1 = uvB.x - uvA.x;
- s2 = uvC.x - uvA.x;
- t1 = uvB.y - uvA.y;
- t2 = uvC.y - uvA.y;
- r = 1.0 / ( s1 * t2 - s2 * t1 );
- sdir.set( ( t2 * x1 - t1 * x2 ) * r,
- ( t2 * y1 - t1 * y2 ) * r,
- ( t2 * z1 - t1 * z2 ) * r );
- tdir.set( ( s1 * x2 - s2 * x1 ) * r,
- ( s1 * y2 - s2 * y1 ) * r,
- ( s1 * z2 - s2 * z1 ) * r );
- tan1[ a ].add( sdir );
- tan1[ b ].add( sdir );
- tan1[ c ].add( sdir );
- tan2[ a ].add( tdir );
- tan2[ b ].add( tdir );
- tan2[ c ].add( tdir );
- }
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
- handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
- }
- var faceIndex = [ 'a', 'b', 'c', 'd' ];
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- for ( i = 0; i < Math.min( face.vertexNormals.length, 3 ); i ++ ) {
- n.copy( face.vertexNormals[ i ] );
- vertexIndex = face[ faceIndex[ i ] ];
- t = tan1[ vertexIndex ];
- // Gram-Schmidt orthogonalize
- tmp.copy( t );
- tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
- // Calculate handedness
- tmp2.crossVectors( face.vertexNormals[ i ], t );
- test = tmp2.dot( tan2[ vertexIndex ] );
- w = ( test < 0.0 ) ? - 1.0 : 1.0;
- face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
- }
- }
- this.hasTangents = true;
- },
- computeLineDistances: function () {
- var d = 0;
- var vertices = this.vertices;
- for ( var i = 0, il = vertices.length; i < il; i ++ ) {
- if ( i > 0 ) {
- d += vertices[ i ].distanceTo( vertices[ i - 1 ] );
- }
- this.lineDistances[ i ] = d;
- }
- },
- computeBoundingBox: function () {
- if ( this.boundingBox === null ) {
- this.boundingBox = new THREE.Box3();
- }
- this.boundingBox.setFromPoints( this.vertices );
- },
- computeBoundingSphere: function () {
- if ( this.boundingSphere === null ) {
- this.boundingSphere = new THREE.Sphere();
- }
- this.boundingSphere.setFromPoints( this.vertices );
- },
- merge: function ( geometry, matrix, materialIndexOffset ) {
- if ( geometry instanceof THREE.Geometry === false ) {
- console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
- return;
- }
- var normalMatrix,
- vertexOffset = this.vertices.length,
- uvPosition = this.faceVertexUvs[ 0 ].length,
- vertices1 = this.vertices,
- vertices2 = geometry.vertices,
- faces1 = this.faces,
- faces2 = geometry.faces,
- uvs1 = this.faceVertexUvs[ 0 ],
- uvs2 = geometry.faceVertexUvs[ 0 ];
- if ( materialIndexOffset === undefined ) materialIndexOffset = 0;
- if ( matrix !== undefined ) {
- normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
- }
- // vertices
- for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
- var vertex = vertices2[ i ];
- var vertexCopy = vertex.clone();
- if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
- vertices1.push( vertexCopy );
- }
- // faces
- for ( i = 0, il = faces2.length; i < il; i ++ ) {
- var face = faces2[ i ], faceCopy, normal, color,
- faceVertexNormals = face.vertexNormals,
- faceVertexColors = face.vertexColors;
- faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
- faceCopy.normal.copy( face.normal );
- if ( normalMatrix !== undefined ) {
- faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
- }
- for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
- normal = faceVertexNormals[ j ].clone();
- if ( normalMatrix !== undefined ) {
- normal.applyMatrix3( normalMatrix ).normalize();
- }
- faceCopy.vertexNormals.push( normal );
- }
- faceCopy.color.copy( face.color );
- for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
- color = faceVertexColors[ j ];
- faceCopy.vertexColors.push( color.clone() );
- }
- faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
- faces1.push( faceCopy );
- }
- // uvs
- for ( i = 0, il = uvs2.length; i < il; i ++ ) {
- var uv = uvs2[ i ], uvCopy = [];
- if ( uv === undefined ) {
- continue;
- }
- for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
- uvCopy.push( new THREE.Vector2( uv[ j ].x, uv[ j ].y ) );
- }
- uvs1.push( uvCopy );
- }
- },
- /*
- * Checks for duplicate vertices with hashmap.
- * Duplicated vertices are removed
- * and faces' vertices are updated.
- */
- mergeVertices: function () {
- var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
- var unique = [], changes = [];
- var v, key;
- var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
- var precision = Math.pow( 10, precisionPoints );
- var i,il, face;
- var indices, k, j, jl, u;
- for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
- v = this.vertices[ i ];
- key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
- if ( verticesMap[ key ] === undefined ) {
- verticesMap[ key ] = i;
- unique.push( this.vertices[ i ] );
- changes[ i ] = unique.length - 1;
- } else {
- //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
- changes[ i ] = changes[ verticesMap[ key ] ];
- }
- };
- // if faces are completely degenerate after merging vertices, we
- // have to remove them from the geometry.
- var faceIndicesToRemove = [];
- for ( i = 0, il = this.faces.length; i < il; i ++ ) {
- face = this.faces[ i ];
- face.a = changes[ face.a ];
- face.b = changes[ face.b ];
- face.c = changes[ face.c ];
- indices = [ face.a, face.b, face.c ];
- var dupIndex = - 1;
- // if any duplicate vertices are found in a Face3
- // we have to remove the face as nothing can be saved
- for ( var n = 0; n < 3; n ++ ) {
- if ( indices[ n ] == indices[ ( n + 1 ) % 3 ] ) {
- dupIndex = n;
- faceIndicesToRemove.push( i );
- break;
- }
- }
- }
- for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
- var idx = faceIndicesToRemove[ i ];
- this.faces.splice( idx, 1 );
- for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
- this.faceVertexUvs[ j ].splice( idx, 1 );
- }
- }
- // Use unique set of vertices
- var diff = this.vertices.length - unique.length;
- this.vertices = unique;
- return diff;
- },
- // Geometry splitting
- makeGroups: ( function () {
- var geometryGroupCounter = 0;
- return function ( usesFaceMaterial, maxVerticesInGroup ) {
- var f, fl, face, materialIndex,
- groupHash, hash_map = {},geometryGroup;
- var numMorphTargets = this.morphTargets.length;
- var numMorphNormals = this.morphNormals.length;
- this.geometryGroups = {};
- this.geometryGroupsList = [];
- for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
- face = this.faces[ f ];
- materialIndex = usesFaceMaterial ? face.materialIndex : 0;
- if ( ! ( materialIndex in hash_map ) ) {
- hash_map[ materialIndex ] = { 'hash': materialIndex, 'counter': 0 };
- }
- groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
- if ( ! ( groupHash in this.geometryGroups ) ) {
- geometryGroup = { 'id': geometryGroupCounter++, 'faces3': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
- this.geometryGroups[ groupHash ] = geometryGroup;
- this.geometryGroupsList.push(geometryGroup);
- }
- if ( this.geometryGroups[ groupHash ].vertices + 3 > maxVerticesInGroup ) {
- hash_map[ materialIndex ].counter += 1;
- groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
- if ( ! ( groupHash in this.geometryGroups ) ) {
- geometryGroup = { 'id': geometryGroupCounter++, 'faces3': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
- this.geometryGroups[ groupHash ] = geometryGroup;
- this.geometryGroupsList.push(geometryGroup);
-
- }
- }
- this.geometryGroups[ groupHash ].faces3.push( f );
- this.geometryGroups[ groupHash ].vertices += 3;
- }
- };
- } )(),
- toJSON: function () {
- var output = {
- metadata: {
- version: 4.0,
- type: 'BufferGeometry',
- generator: 'BufferGeometryExporter'
- },
- uuid: this.uuid,
- type: this.type
- };
- if ( this.name !== "" ) output.name = this.name;
- if ( this.parameters !== undefined ) {
- var parameters = this.parameters;
- for ( var key in parameters ) {
- if ( parameters[ key ] !== undefined ) output[ key ] = parameters[ key ];
- }
- return output;
- }
- var vertices = [];
- for ( var i = 0; i < this.vertices.length; i ++ ) {
- var vertex = this.vertices[ i ];
- vertices.push( vertex.x, vertex.y, vertex.z );
- }
- var faces = [];
- var normals = [];
- var normalsHash = {};
- var colors = [];
- var colorsHash = {};
- var uvs = [];
- var uvsHash = {};
- for ( var i = 0; i < this.faces.length; i ++ ) {
- var face = this.faces[ i ];
- var hasMaterial = false; // face.materialIndex !== undefined;
- var hasFaceUv = false; // deprecated
- var hasFaceVertexUv = this.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, 0 );
- 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 );
- faces.push( face.a, face.b, face.c );
- /*
- if ( hasMaterial ) {
- faces.push( face.materialIndex );
- }
- */
- if ( hasFaceVertexUv ) {
- var faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
- faces.push(
- getUvIndex( faceVertexUvs[ 0 ] ),
- getUvIndex( faceVertexUvs[ 1 ] ),
- getUvIndex( faceVertexUvs[ 2 ] )
- );
- }
- if ( hasFaceNormal ) {
- faces.push( getNormalIndex( face.normal ) );
- }
- if ( hasFaceVertexNormal ) {
- var vertexNormals = face.vertexNormals;
- faces.push(
- getNormalIndex( vertexNormals[ 0 ] ),
- getNormalIndex( vertexNormals[ 1 ] ),
- getNormalIndex( vertexNormals[ 2 ] )
- );
- }
- if ( hasFaceColor ) {
- faces.push( getColorIndex( face.color ) );
- }
- if ( hasFaceVertexColor ) {
- var vertexColors = face.vertexColors;
- faces.push(
- getColorIndex( vertexColors[ 0 ] ),
- getColorIndex( vertexColors[ 1 ] ),
- getColorIndex( vertexColors[ 2 ] )
- );
- }
- }
- 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 ];
- }
- function getUvIndex( uv ) {
- var hash = uv.x.toString() + uv.y.toString();
- if ( uvsHash[ hash ] !== undefined ) {
- return uvsHash[ hash ];
- }
- uvsHash[ hash ] = uvs.length / 2;
- uvs.push( uv.x, uv.y );
- return uvsHash[ hash ];
- }
- output.data = {};
- output.data.vertices = vertices;
- output.data.normals = normals;
- if ( colors.length > 0 ) output.data.colors = colors;
- if ( uvs.length > 0 ) output.data.uvs = [ uvs ]; // temporal backward compatibility
- output.data.faces = faces;
- //
- return output;
- },
- clone: function () {
- var geometry = new THREE.Geometry();
- var vertices = this.vertices;
- for ( var i = 0, il = vertices.length; i < il; i ++ ) {
- geometry.vertices.push( vertices[ i ].clone() );
- }
- var faces = this.faces;
- for ( var i = 0, il = faces.length; i < il; i ++ ) {
- geometry.faces.push( faces[ i ].clone() );
- }
- var uvs = this.faceVertexUvs[ 0 ];
- for ( var i = 0, il = uvs.length; i < il; i ++ ) {
- var uv = uvs[ i ], uvCopy = [];
- for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
- uvCopy.push( new THREE.Vector2( uv[ j ].x, uv[ j ].y ) );
- }
- geometry.faceVertexUvs[ 0 ].push( uvCopy );
- }
- return geometry;
- },
- dispose: function () {
- this.dispatchEvent( { type: 'dispose' } );
- }
- };
- THREE.EventDispatcher.prototype.apply( THREE.Geometry.prototype );
- THREE.GeometryIdCount = 0;
|