Skip to content

Commit

Permalink
BufferGeometry: Check for existing attribute in setFromPoints(). (#…
Browse files Browse the repository at this point in the history
…29696)

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

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

* Update BufferGeometry.js

Fix typo.

* Docs: Improve `BufferGeometry.setFromPoints()`.
  • Loading branch information
Mugen87 authored Oct 20, 2024
1 parent 04ddcb9 commit 3102d6b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
7 changes: 6 additions & 1 deletion docs/api/en/core/BufferGeometry.html
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,12 @@ <h3>[method:undefined setDrawRange] ( [param:Integer start], [param:Integer coun
</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>
Expand Down
35 changes: 29 additions & 6 deletions src/core/BufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 3102d6b

Please sign in to comment.