Skip to content

BufferAttribute setVector & setColor convenience methods#33348

Open
Kodub wants to merge 1 commit intomrdoob:devfrom
Kodub:feature/buffer-attribute-set-vector
Open

BufferAttribute setVector & setColor convenience methods#33348
Kodub wants to merge 1 commit intomrdoob:devfrom
Kodub:feature/buffer-attribute-set-vector

Conversation

@Kodub
Copy link
Copy Markdown
Contributor

@Kodub Kodub commented Apr 7, 2026

Description

This PR adds new convenience methods to BufferAttribute & InterleavedBufferAttribute.

  • setVector2()
  • setVector3()
  • setVector4()
  • setColor()

These helps simplify setting vectors and colors which seem to be a quite common pattern as most instances of setXYZ are possible to change into these new convenience methods.

Before:

const v = new THREE.Vector3(1, 2, 3);
attribute.setXYZ(0, v.x, v.y, v.z);

const c = new THREE.Color(0.1, 0.2, 0.3);
attribute.setXYZ(1, c.r, c.g, c.b);

After:

const v = new THREE.Vector3(1, 2, 3);
attribute.setVector3(0, v);

const c = new THREE.Color(0.1, 0.2, 0.3);
attribute.setColor(1, c);

Advantages

  • Shorter code
  • Helps prevent hard to spot mistakes. For example: attribute.setXYZ(0, v.x, v.x, v.z);

Discussion points

  • Other possible names are setXYZFromVector3 and setXYZFromColorRGB. Would these names be preferred to make it clearer that these are convenience methods for setXYZ?
  • Right now these methods simply call their respective setXYZ methods. The extra function call might introduce a small performance overhead.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 7, 2026

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 361.21
85.78
361.21
85.78
+0 B
+0 B
WebGPU 637.46
177.05
637.46
177.05
+0 B
+0 B
WebGPU Nodes 635.58
176.76
635.58
176.76
+0 B
+0 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 493.71
120.45
493.89
120.46
+184 B
+13 B
WebGPU 709.2
191.87
709.59
191.88
+386 B
+12 B
WebGPU Nodes 658.42
179.16
658.81
179.16
+386 B
+0 B

@Kodub Kodub force-pushed the feature/buffer-attribute-set-vector branch from 231773f to ba530c3 Compare April 7, 2026 14:55
@gkjohnson
Copy link
Copy Markdown
Collaborator

If convenience of setting BufferAttribute values here is the primary goal of this change I'll point out that using the spread operator serves this use quite well, in my opinion:

const pos = new Vector3();
posAttr.setXYZ( i, ...pos );

@WestLangley
Copy link
Copy Markdown
Collaborator

Note that we have similar convenience methods. See #25637. I'd follow the same naming conventions for consistency.

@Mugen87
Copy link
Copy Markdown
Collaborator

Mugen87 commented Apr 13, 2026

TBH, I'm not in favor of this PR since it bloats the API for no adequately good reason. The spread operator wasn't available in earlier days of three.js but now it's safe to use it and can avoid such verbose APIs.

Regarding the math classes, there is e.g. no need for setFromColor() since you can now do this:

vector.set( ... color );

https://jsfiddle.net/0hdq9sLr/

@Kodub
Copy link
Copy Markdown
Contributor Author

Kodub commented Apr 14, 2026

Note that the spread operator syntax does not work in this case when using TypeScript, since it can't guarantee that the iterator will return enough elements.
image
From my understanding this can't be fixed on the TS side either, because TS does not currently support tuple iterators.
See: microsoft/TypeScript#42033

This syntax works though:
attribute.setXYZ(0, ...v.toArray());

Either way, I think it's not entirely clear that the spread operator can be used like this, since the user would have to know that the Vector implements an iterator. But maybe it shouldn't be up to the library to educate users.

@Kodub Kodub force-pushed the feature/buffer-attribute-set-vector branch from ba530c3 to bbf484d Compare April 15, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants