Просмотр исходного кода

BufferGeometry: Check for existing attribute in `setFromPoints()`. (#29696)

* BufferGeometry: Check for existing attribute in `setFromPoints()`.

* BufferGeometry: Add warning to `setFromPoints()`.

* Update BufferGeometry.js

Fix typo.

* Docs: Improve `BufferGeometry.setFromPoints()`.
Michael Herzog 1 год назад
Родитель
Сommit
3102d6b289
2 измененных файлов с 35 добавлено и 7 удалено
  1. 6 1
      docs/api/en/core/BufferGeometry.html
  2. 29 6
      src/core/BufferGeometry.js

+ 6 - 1
docs/api/en/core/BufferGeometry.html

@@ -335,7 +335,12 @@
 		</p>
 
 		<h3>[method:this setFromPoints] ( [param:Array points] )</h3>
-		<p>Sets the attributes for this BufferGeometry from an array of points.</p>
+		<p>
+			Defines a geometry by creating a `position` attribute based on the given array of points. The array can hold 
+			instances of [page:Vector2] or [page:Vector3]. When using two-dimensional data, the `z` coordinate for all vertices is set to `0`.<br />
+			If the method is used with an existing `position` attribute, the vertex data are overwritten with the data from the array. The length of the 
+			array must match the vertex count.
+		</p>
 
 		<h3>[method:this setIndex] ( [param:BufferAttribute index] )</h3>
 		<p>Set the [page:.index] buffer.</p>

+ 29 - 6
src/core/BufferGeometry.js

@@ -287,16 +287,39 @@ class BufferGeometry extends EventDispatcher {
 
 	setFromPoints( points ) {
 
-		const position = [];
+		const positionAttribute = this.getAttribute( 'position' );
 
-		for ( let i = 0, l = points.length; i < l; i ++ ) {
+		if ( positionAttribute === undefined ) {
 
-			const point = points[ i ];
-			position.push( point.x, point.y, point.z || 0 );
+			const position = [];
 
-		}
+			for ( let i = 0, l = points.length; i < l; i ++ ) {
+
+				const point = points[ i ];
+				position.push( point.x, point.y, point.z || 0 );
+
+			}
+
+			this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
+
+		} else {
+
+			for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
 
-		this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
+				const point = points[ i ];
+				positionAttribute.setXYZ( i, point.x, point.y, point.z || 0 );
+
+			}
+
+			if ( points.length > positionAttribute.count ) {
+
+				console.warn( 'THREE.BufferGeometry: Buffer size too small for points data. Use .dispose() and create a new geometry.' );
+
+			}
+
+			positionAttribute.needsUpdate = true;
+
+		}
 
 		return this;
 

粤ICP备19079148号