Last active
August 29, 2015 14:00
-
-
Save itayadler/11369191 to your computer and use it in GitHub Desktop.
Override THREE JS triangulateShape to use poly2tri.js
This file contains 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 characters
//three.js: https://github.com/mrdoob/three.js/ | |
//poly2tri.js: https://code.google.com/p/poly2tri/ | |
//Copied from: https://github.com/jahting/three.js/commit/b9774b00ca6a4f65deab2100d13a788802275a32 - triangulate2 function | |
THREE.Shape.Utils.triangulatePoly2Tri = function( pts, holes ) { | |
// For use with Poly2Tri.js | |
var allpts = pts.concat(); | |
var shape = []; | |
for (var p in pts) { | |
shape.push(new js.poly2tri.Point(pts[p].x, pts[p].y)); | |
} | |
var swctx = new js.poly2tri.SweepContext(shape); | |
for (var h in holes) { | |
var aHole = holes[h]; | |
var newHole = [] | |
for (i in aHole) { | |
newHole.push(new js.poly2tri.Point(aHole[i].x, aHole[i].y)); | |
allpts.push(aHole[i]); | |
} | |
swctx.AddHole(newHole); | |
} | |
var find; | |
var findIndexForPt = function (pt) { | |
find = new THREE.Vector2(pt.x, pt.y); | |
var p; | |
for (p=0, pl = allpts.length; p<pl; p++) { | |
if (allpts[p].equals(find)) return p; | |
} | |
return -1; | |
}; | |
// triangulate | |
js.poly2tri.sweep.Triangulate(swctx); | |
var triangles = swctx.GetTriangles(); | |
var tr ; | |
var facesPts = []; | |
for (var t in triangles) { | |
tr = triangles[t]; | |
facesPts.push([ | |
findIndexForPt(tr.GetPoint(0)), | |
findIndexForPt(tr.GetPoint(1)), | |
findIndexForPt(tr.GetPoint(2)) | |
]); | |
} | |
// Returns array of faces with 3 element each | |
return facesPts; | |
}; | |
THREE.Shape.Utils.triangulateShape = function( pts, holes ) { | |
return THREE.Shape.Utils.triangulatePoly2Tri(pts, holes); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment