Browse Source

BufferGeometry/Geometry: Introduce .setFromPoints()

Mugen87 7 years ago
parent
commit
d38d3cce02

+ 3 - 0
docs/api/core/BufferGeometry.html

@@ -316,6 +316,9 @@
 		<h3>[method:BufferGeometry setFromObject] ( [page:Object3D object] )</h3>
 		<div>Sets the attributes for this BufferGeometry from an [page:Object3D].</div>
 
+		<h3>[method:BufferGeometry setFromPoints] ( [page:Array points] )</h3>
+		<div>Sets the attributes for this BufferGeometry from an array of points.</div>
+
 		<h3>[method:Object toJSON]()</h3>
 		<div>Returns a JSON object representation of the BufferGeometry.</div>
 

+ 3 - 0
docs/api/core/Geometry.html

@@ -315,6 +315,9 @@
 	        Use [page:Object3D.rotation] for typical real-time mesh rotation.
 		</div>
 
+		<h3>[method:Geometry setFromPoints] ( [page:Array points] )</h3>
+		<div>Sets the vertices for this Geometry from an array of points.</div>
+
 		<h3>[method:null sortFacesByMaterialIndex] (  )</h3>
 		<div>
 		Sorts the faces array according to material index. For complex geometries with several materials,

+ 2 - 2
docs/api/extras/curves/CatmullRomCurve3.html

@@ -27,8 +27,8 @@ var curve = new THREE.CatmullRomCurve3( [
 	new THREE.Vector3( 10, 0, 10 )
 ] );
 
-var geometry = new THREE.Geometry();
-geometry.vertices = curve.getPoints( 50 );
+var points = curve.getPoints( 50 );
+var geometry = new THREE.BufferGeometry().setFromPoints( points );
 
 var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
 

+ 1 - 10
docs/api/extras/curves/CubicBezierCurve.html

@@ -28,17 +28,8 @@ var curve = new THREE.CubicBezierCurve(
 	new THREE.Vector2( 10, 0, 0 )
 );
 
-var path = new THREE.Path( curve.getPoints( 50 ) );
-
-var geometry = new THREE.Geometry();
 var points = curve.getPoints( 50 );
-
-for ( var i = 0, l = points.length; i < l; i ++ ) {
-
-	var point = points[ i ];
-	geometry.vertices.push( new THREE.Vector3( point.x, point.y, 0 ) );
-
-}
+var geometry = new THREE.BufferGeometry().setFromPoints( points );
 
 var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
 

+ 2 - 2
docs/api/extras/curves/CubicBezierCurve3.html

@@ -28,8 +28,8 @@ var curve = new THREE.CubicBezierCurve3(
 	new THREE.Vector3( 10, 0, 0 )
 );
 
-var geometry = new THREE.Geometry();
-geometry.vertices = curve.getPoints( 50 );
+var points = curve.getPoints( 50 );
+var geometry = new THREE.BufferGeometry().setFromPoints( points );
 
 var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
 

+ 1 - 7
docs/api/extras/curves/EllipseCurve.html

@@ -28,15 +28,9 @@ var curve = new THREE.EllipseCurve(
 	0                 // aRotation
 );
 
-var geometry = new THREE.Geometry();
 var points = curve.getPoints( 50 );
+var geometry = new THREE.BufferGeometry().setFromPoints( points );
 
-for ( var i = 0, l = points.length; i < l; i ++ ) {
-
-	var point = points[ i ];
-	geometry.vertices.push( new THREE.Vector3( point.x, point.y, 0 ) );
-
-}
 var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
 
 // Create the final object to add to the scene

+ 1 - 7
docs/api/extras/curves/QuadraticBezierCurve.html

@@ -27,15 +27,9 @@ var curve = new THREE.QuadraticBezierCurve(
 	new THREE.Vector2( 10, 0 )
 );
 
-var geometry = new THREE.Geometry();
 var points = curve.getPoints( 50 );
+var geometry = new THREE.BufferGeometry().setFromPoints( points );
 
-for ( var i = 0, l = points.length; i < l; i ++ ) {
-
-	var point = points[ i ];
-	geometry.vertices.push( new THREE.Vector3( point.x, point.y, 0 ) );
-
-}
 var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
 
 //Create the final object to add to the scene

+ 2 - 2
docs/api/extras/curves/QuadraticBezierCurve3.html

@@ -27,8 +27,8 @@ var curve = new THREE.QuadraticBezierCurve3(
 	new THREE.Vector3( 10, 0, 0 )
 );
 
-var geometry = new THREE.Geometry();
-geometry.vertices = curve.getPoints( 50 );
+var points = curve.getPoints( 50 );
+var geometry = new THREE.BufferGeometry().setFromPoints( points );
 
 var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
 

+ 1 - 7
docs/api/extras/curves/SplineCurve.html

@@ -29,15 +29,9 @@ var curve = new THREE.SplineCurve( [
 	new THREE.Vector2( 10, 0 )
 ] );
 
-var geometry = new THREE.Geometry();
 var points = curve.getPoints( 50 );
+var geometry = new THREE.BufferGeometry().setFromPoints( points );
 
-for ( var i = 0, l = points.length; i < l; i ++ ) {
-
-	var point = points[ i ];
-	geometry.vertices.push( new THREE.Vector3( point.x, point.y, 0 ) );
-
-}
 var material = new THREE.LineBasicMaterial( { color : 0xff0000 } );
 
 // Create the final object to add to the scene

+ 1 - 8
examples/canvas_geometry_shapes.html

@@ -82,15 +82,8 @@
 
 					// line
 
-					var geometry = new THREE.Geometry();
 					var points = shape.getPoints();
-
-					for ( var i = 0, l = points.length; i < l; i ++ ) {
-
-						var point = points[ i ];
-						geometry.vertices.push( new THREE.Vector3( point.x, point.y, 0 ) );
-
-					}
+					var geometry = new THREE.Geometry().setFromPoints( points );
 
 					var material = new THREE.LineBasicMaterial( { linewidth: 10, color: 0x333333, transparent: true } );
 

+ 6 - 28
examples/webgl_geometry_shapes.html

@@ -111,41 +111,19 @@
 
 				function addLineShape( shape, color, x, y, z, rx, ry, rz, s ) {
 
-					var geometryPoints = new THREE.BufferGeometry();
-					var geometrySpacedPoints = new THREE.BufferGeometry();
-
 					// lines
 
 					shape.autoClose = true;
+
 					var points = shape.getPoints();
 					var spacedPoints = shape.getSpacedPoints( 50 );
 
-					// points
-
-					var position = [];
-
-					for ( var i = 0, l = points.length; i < l; i ++ ) {
-
-						var point = points[ i ];
-						position.push( point.x, point.y, 0 );
-
-					}
-
-					geometryPoints.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
-
-					// spaced points
-
-					position = [];
-
-					for ( var i = 0, l = spacedPoints.length; i < l; i ++ ) {
-
-						var point = spacedPoints[ i ];
-						position.push( point.x, point.y, 0 );
-
-					}
-
-					geometrySpacedPoints.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
+					var geometryPoints = new THREE.BufferGeometry();
+					geometryPoints.setFromPoints( points );
 
+					var geometrySpacedPoints = new THREE.BufferGeometry();
+					geometrySpacedPoints.setFromPoints( spacedPoints );
+					
 					// solid line
 
 					var line = new THREE.Line( geometryPoints, new THREE.LineBasicMaterial( { color: color, linewidth: 3 } ) );

+ 2 - 11
examples/webgl_geometry_text_shapes.html

@@ -115,20 +115,11 @@
 					for ( var i = 0; i < shapes.length; i ++ ) {
 
 						var shape = shapes[ i ];
-						var points = shape.getPoints();
 
+						var points = shape.getPoints();
 						var geometry = new THREE.BufferGeometry();
-						var position = [];
-
-						for ( var j = 0; j < points.length; j ++ ) {
-
-							var point = points[ j ];
-							position.push( point.x, point.y, 0 );
-
-						}
 
-						geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
-						geometry.translate( xMid, 0, 0 );
+						geometry.setFromPoints( points ).translate( xMid, 0, 0 );
 
 						var lineMesh = new THREE.Line( geometry, matDark );
 						lineText.add( lineMesh );

+ 17 - 0
src/core/BufferGeometry.js

@@ -334,6 +334,23 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 	},
 
+	setFromPoints: function ( points ) {
+
+		var position = [];
+
+		for ( var i = 0, l = points.length; i < l; i ++ ) {
+
+			var point = points[ i ];
+			position.push( point.x, point.y, 0 );
+
+		}
+
+		this.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
+
+		return this;
+
+	},
+
 	updateFromObject: function ( object ) {
 
 		var geometry = object.geometry;

+ 15 - 0
src/core/Geometry.js

@@ -921,6 +921,21 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 	},
 
+	setFromPoints: function ( points ) {
+
+		this.vertices = [];
+
+		for ( var i = 0, l = points.length; i < l; i ++ ) {
+
+			var point = points[ i ];
+			this.vertices.push( new Vector3( point.x, point.y, point.z || 0 ) );
+
+		}
+
+		return this;
+
+	},
+
 	sortFacesByMaterialIndex: function () {
 
 		var faces = this.faces;