瀏覽代碼

remove duplicate start end points

Andreas Plesch 8 年之前
父節點
當前提交
d324144d5e
共有 1 個文件被更改,包括 36 次插入20 次删除
  1. 36 20
      examples/webgl_geometry_text_earcut.html

+ 36 - 20
examples/webgl_geometry_text_earcut.html

@@ -47,38 +47,54 @@
 		<!-- replace built-in triangulation with Earcut -->
 		<script src="js/libs/earcut.js"></script>
 		<script>
+			
+			function removeDupEndPts(points) {
+				
+				var l = points.length;
+				if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) {
+					points.pop();
+				}
+			}
+			
 			function addContour( vertices, contour ) {
-			    for ( var i = 0; i < contour.length; i++ ) {
-			        vertices.push( contour[i].x );
-			        vertices.push( contour[i].y );
-			    }
+				
+				for ( var i = 0; i < contour.length; i++ ) {
+					vertices.push( contour[i].x );
+					vertices.push( contour[i].y );
+				}
 			}
 
 			THREE.ShapeUtils.triangulateShape = function ( contour, holes ) {
-			    var vertices = [];
+				
+				removeDupEndPts( contour );
+				holes.forEach( removeDupEndPts );
 
-			    addContour( vertices, contour );
+				var vertices = [];
 
-			    var holeIndices = [];
-			    var holeIndex = contour.length;
+				addContour( vertices, contour );
 
-			    for ( i = 0; i < holes.length; i++ ) {
-			        holeIndices.push( holeIndex );
-			        holeIndex += holes[i].length;
-			        addContour( vertices, holes[i] );
-			    }
+				var holeIndices = [];
+				var holeIndex = contour.length;
 
-			    var result = earcut( vertices, holeIndices, 2 );
+				for ( i = 0; i < holes.length; i++ ) {
+					holeIndices.push( holeIndex );
+					holeIndex += holes[i].length;
+					addContour( vertices, holes[i] );
+				}
 
-			    var grouped = [];
-			    for ( var i = 0; i < result.length; i += 3 ) {
-			        grouped.push( result.slice( i, i + 3 ) );
-			    }
-			    return grouped;
+				var result = earcut( vertices, holeIndices, 2 );
+
+				var grouped = [];
+				for ( var i = 0; i < result.length; i += 3 ) {
+					grouped.push( result.slice( i, i + 3 ) );
+				}
+				
+				return grouped;
+				
 			};
+			
 		</script>
 
-
 		<script>
 
 			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();