Browse Source

added polygon and polyline nodes

Jonathan Brandel 7 years ago
parent
commit
55475648f4
1 changed files with 66 additions and 0 deletions
  1. 66 0
      examples/js/loaders/SVGLoader.js

+ 66 - 0
examples/js/loaders/SVGLoader.js

@@ -58,6 +58,14 @@ THREE.SVGLoader.prototype = {
 					paths.push( parseRectNode( node ) );
 					break;
 
+				case 'polygon':
+					paths.push( parsePolygonNode( node ) );
+					break;
+
+				case 'polyline':
+					paths.push( parsePolylineNode( node ) );
+					break;
+
 				default:
 					console.log( node );
 
@@ -319,6 +327,64 @@ THREE.SVGLoader.prototype = {
 
 		}
 
+		function parsePolygonNode( node ) {
+
+			function iterator( match, a, b ) {
+
+				var x = parseFloat( a );
+				var y = parseFloat( b );
+
+				if ( index === 0 ) {
+					path.moveTo( x, y );
+				} else {
+					path.lineTo( x, y );
+				}
+
+				index++;
+
+			}
+
+			var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
+			var path = new THREE.ShapePath();
+			var index = 0;
+
+			node.getAttribute( 'points' ).replace(regex, iterator);
+
+			path.currentPath.autoClose = true;
+
+			return path;
+
+		}
+
+		function parsePolylineNode( node ) {
+
+			function iterator( match, a, b ) {
+
+				var x = parseFloat( a );
+				var y = parseFloat( b );
+
+				if ( index === 0 ) {
+					path.moveTo( x, y );
+				} else {
+					path.lineTo( x, y );
+				}
+
+				index++;
+
+			}
+
+			var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
+			var path = new THREE.ShapePath();
+			var index = 0;
+
+			node.getAttribute( 'points' ).replace(regex, iterator);
+
+			path.currentPath.autoClose = false;
+
+			return path;
+
+		}
+
 		// http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
 
 		function getReflection( a, b ) {