浏览代码

Merge pull request #10495 from andreasplesch/dev

text earcut example: remove duplicate start end points
Mr.doob 8 年之前
父节点
当前提交
66100dd054
共有 1 个文件被更改,包括 44 次插入23 次删除
  1. 44 23
      examples/webgl_geometry_text_earcut.html

+ 44 - 23
examples/webgl_geometry_text_earcut.html

@@ -47,38 +47,59 @@
 		<!-- replace built-in triangulation with Earcut -->
 		<script src="js/libs/earcut.js"></script>
 		<script>
-			function addContour( vertices, contour ) {
-			    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 = [];
 
-			    addContour( vertices, contour );
+				function removeDupEndPts( points ) {
+
+					var l = points.length;
+					if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) {
+
+						points.pop();
+
+					}
+
+				}
+
+				function addContour( vertices, contour ) {
 
-			    var holeIndices = [];
-			    var holeIndex = contour.length;
+					for ( var i = 0; i < contour.length; i ++ ) {
 
-			    for ( i = 0; i < holes.length; i++ ) {
-			        holeIndices.push( holeIndex );
-			        holeIndex += holes[i].length;
-			        addContour( vertices, holes[i] );
-			    }
+						vertices.push( contour[ i ].x );
+						vertices.push( contour[ i ].y );
 
-			    var result = earcut( vertices, holeIndices, 2 );
+					}
+
+				}
+
+				removeDupEndPts( contour );
+				holes.forEach( removeDupEndPts );
+
+				var vertices = [];
+				addContour( vertices, contour );
+				var holeIndices = [];
+				var holeIndex = contour.length;
+				for ( i = 0; i < holes.length; i ++ ) {
+
+					holeIndices.push( holeIndex );
+					holeIndex += holes[ i ].length;
+					addContour( vertices, holes[ i ] );
+
+				}
+
+				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;
 
-			    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();