| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import { BufferGeometry } from '../core/BufferGeometry';
- import { BufferAttribute } from '../core/BufferAttribute';
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- function WireframeGeometry( geometry ) {
- BufferGeometry.call( this );
- this.type = 'WireframeGeometry';
- var edge = [ 0, 0 ], hash = {};
- var keys = [ 'a', 'b', 'c' ];
- if ( geometry && geometry.isGeometry ) {
- var vertices = geometry.vertices;
- var faces = geometry.faces;
- var numEdges = 0;
- // allocate maximal size
- var edges = new Uint32Array( 6 * faces.length );
- for ( var i = 0, l = faces.length; i < l; i ++ ) {
- var face = faces[ i ];
- for ( var j = 0; j < 3; j ++ ) {
- edge[ 0 ] = face[ keys[ j ] ];
- edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ];
- edge.sort( sortFunction );
- var key = edge.toString();
- if ( hash[ key ] === undefined ) {
- edges[ 2 * numEdges ] = edge[ 0 ];
- edges[ 2 * numEdges + 1 ] = edge[ 1 ];
- hash[ key ] = true;
- numEdges ++;
- }
- }
- }
- var coords = new Float32Array( numEdges * 2 * 3 );
- for ( var i = 0, l = numEdges; i < l; i ++ ) {
- for ( var j = 0; j < 2; j ++ ) {
- var vertex = vertices[ edges [ 2 * i + j ] ];
- var index = 6 * i + 3 * j;
- coords[ index + 0 ] = vertex.x;
- coords[ index + 1 ] = vertex.y;
- coords[ index + 2 ] = vertex.z;
- }
- }
- this.addAttribute( 'position', new BufferAttribute( coords, 3 ) );
- } else if ( geometry && geometry.isBufferGeometry ) {
- if ( geometry.index !== null ) {
- // Indexed BufferGeometry
- var indices = geometry.index.array;
- var vertices = geometry.attributes.position;
- var groups = geometry.groups;
- var numEdges = 0;
- if ( groups.length === 0 ) {
- geometry.addGroup( 0, indices.length );
- }
- // allocate maximal size
- var edges = new Uint32Array( 2 * indices.length );
- for ( var o = 0, ol = groups.length; o < ol; ++ o ) {
- var group = groups[ o ];
- var start = group.start;
- var count = group.count;
- for ( var i = start, il = start + count; i < il; i += 3 ) {
- for ( var j = 0; j < 3; j ++ ) {
- edge[ 0 ] = indices[ i + j ];
- edge[ 1 ] = indices[ i + ( j + 1 ) % 3 ];
- edge.sort( sortFunction );
- var key = edge.toString();
- if ( hash[ key ] === undefined ) {
- edges[ 2 * numEdges ] = edge[ 0 ];
- edges[ 2 * numEdges + 1 ] = edge[ 1 ];
- hash[ key ] = true;
- numEdges ++;
- }
- }
- }
- }
- var coords = new Float32Array( numEdges * 2 * 3 );
- for ( var i = 0, l = numEdges; i < l; i ++ ) {
- for ( var j = 0; j < 2; j ++ ) {
- var index = 6 * i + 3 * j;
- var index2 = edges[ 2 * i + j ];
- coords[ index + 0 ] = vertices.getX( index2 );
- coords[ index + 1 ] = vertices.getY( index2 );
- coords[ index + 2 ] = vertices.getZ( index2 );
- }
- }
- this.addAttribute( 'position', new BufferAttribute( coords, 3 ) );
- } else {
- // non-indexed BufferGeometry
- var vertices = geometry.attributes.position.array;
- var numEdges = vertices.length / 3;
- var numTris = numEdges / 3;
- var coords = new Float32Array( numEdges * 2 * 3 );
- for ( var i = 0, l = numTris; i < l; i ++ ) {
- for ( var j = 0; j < 3; j ++ ) {
- var index = 18 * i + 6 * j;
- var index1 = 9 * i + 3 * j;
- coords[ index + 0 ] = vertices[ index1 ];
- coords[ index + 1 ] = vertices[ index1 + 1 ];
- coords[ index + 2 ] = vertices[ index1 + 2 ];
- var index2 = 9 * i + 3 * ( ( j + 1 ) % 3 );
- coords[ index + 3 ] = vertices[ index2 ];
- coords[ index + 4 ] = vertices[ index2 + 1 ];
- coords[ index + 5 ] = vertices[ index2 + 2 ];
- }
- }
- this.addAttribute( 'position', new BufferAttribute( coords, 3 ) );
- }
- }
- // custom array sort function
- function sortFunction( a, b ) {
- return a - b;
- }
- }
- WireframeGeometry.prototype = Object.create( BufferGeometry.prototype );
- WireframeGeometry.prototype.constructor = WireframeGeometry;
- export { WireframeGeometry };
|