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. geometry.addAttribute( 'position', Float32Array, 2 * numEdges, 3 );
  32. var coords = geometry.attributes.position.array;
  33. var index = 0;
  34. for ( var key in hash ) {
  35. var h = hash[ key ];
  36. if ( h.face2 === undefined || faces[ h.face1 ].normal.dot( faces[ h.face2 ].normal ) < 0.9999 ) { // hardwired const OK
  37. var vertex = vertices[ h.vert1 ];
  38. coords[ index ++ ] = vertex.x;
  39. coords[ index ++ ] = vertex.y;
  40. coords[ index ++ ] = vertex.z;
  41. vertex = vertices[ h.vert2 ];
  42. coords[ index ++ ] = vertex.x;
  43. coords[ index ++ ] = vertex.y;
  44. coords[ index ++ ] = vertex.z;
  45. }
  46. }
  47. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color } ), THREE.LinePieces );
  48. this.matrixAutoUpdate = false;
  49. this.matrixWorld = object.matrixWorld;
  50. };
  51. THREE.EdgesHelper.prototype = Object.create( THREE.Line.prototype );