EdgesHelper.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @author WestLangley / http://github.com/WestLangley
  3. */
  4. THREE.EdgesHelper = function ( object, hex ) {
  5. var color = ( hex !== undefined ) ? hex : 0xffffff;
  6. var edge = [ 0, 0 ], hash = {};
  7. var sortFunction = function ( a, b ) { return a - b };
  8. var keys = [ 'a', 'b', 'c' ];
  9. var geometry = new THREE.BufferGeometry();
  10. var geometry2 = object.geometry.clone();
  11. geometry2.mergeVertices();
  12. geometry2.computeFaceNormals();
  13. var vertices = geometry2.vertices;
  14. var faces = geometry2.faces;
  15. var numEdges = 0;
  16. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  17. var face = faces[ i ];
  18. for ( var j = 0; j < 3; j ++ ) {
  19. edge[ 0 ] = face[ keys[ j ] ];
  20. edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ];
  21. edge.sort( sortFunction );
  22. var key = edge.toString();
  23. if ( hash[ key ] === undefined ) {
  24. hash[ key ] = { vert1: edge[ 0 ], vert2: edge[ 1 ], face1: i, face2: undefined };
  25. numEdges ++;
  26. } else {
  27. hash[ key ].face2 = i;
  28. }
  29. }
  30. }
  31. var coords = new Float32Array( numEdges * 2 * 3 );
  32. var index = 0;
  33. for ( var key in hash ) {
  34. var h = hash[ key ];
  35. if ( h.face2 === undefined || faces[ h.face1 ].normal.dot( faces[ h.face2 ].normal ) < 0.9999 ) { // hardwired const OK
  36. var vertex = vertices[ h.vert1 ];
  37. coords[ index ++ ] = vertex.x;
  38. coords[ index ++ ] = vertex.y;
  39. coords[ index ++ ] = vertex.z;
  40. vertex = vertices[ h.vert2 ];
  41. coords[ index ++ ] = vertex.x;
  42. coords[ index ++ ] = vertex.y;
  43. coords[ index ++ ] = vertex.z;
  44. }
  45. }
  46. geometry.addAttribute( 'position', new THREE.BufferAttribute( coords, 3 ) );
  47. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color } ), THREE.LinePieces );
  48. this.matrix = object.matrixWorld;
  49. this.matrixAutoUpdate = false;
  50. };
  51. THREE.EdgesHelper.prototype = Object.create( THREE.Line.prototype );