Created
March 23, 2016 17:51
-
-
Save larsberg/d86c2620db241ea30db5 to your computer and use it in GitHub Desktop.
Revisions
-
larsberg created this gist
Mar 23, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,84 @@ 'use strict' var getWingedEdges = function( g ){ var edgeMap = {}; // find the edges function getEdgeKey( i0, i1 ){ return Math.min(i0, i1) + '_' + Math.max(i0, i1); } function addEdge( i0, i1, faceIndex ){ var edgeKey = getEdgeKey( i0, i1 ); if(edgeMap[ edgeKey ] !== undefined){ edgeMap[ edgeKey ].f.push( faceIndex ); } else { edgeMap[ edgeKey ] = { v: [i0, i1], f: [faceIndex], normal: new THREE.Vector3() } } return edgeKey; } g.faces.forEach( function( f, i ){ addEdge(f.a, f.b, i); addEdge(f.b, f.c, i); addEdge(f.c, f.a, i); }); // compute edge normals g.computeFaceNormals(); function normalFromTwoVector( v0, v1 ){ return v0.cross( v1 ).normalize(); } var v = g.vertices; for(var i in edgeMap){ var e = edgeMap[i]; if(e.f.length > 1){ // average the face normals var n = e.normal.set(0,0,0); e.f.forEach( function( i ){ n.add( g.faces[i].normal ); }); n.divideScalar( e.f.length ); } else { var f = g.faces[ e.f[0] ]; var v0 = f.normal.normalize().clone(); var v1 = v[ e.v[0] ].clone().sub( v[ e.v[1] ] ).normalize(); e.normal.copy( normalFromTwoVector( v0, v1 ) ); } } return edgeMap } module.exports = getWingedEdges;