question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding GLSL style swizzling to vectors

See original GitHub issue

I love glsl swizzling, and I was wondering if we could get the same thing in threejs? I mean this sort of thing: u.set(new THREE.Vector3(v.x,v.x,v.y))

Describe the solution you’d like

Something that works the same as the following code but isn’t run afterwards added:

array = ["x","y","z"]
swizzle2d = array.map(e=>array.map(f=>[f,e])).flat(1)
swizzle3d = array.map(e=>array.map(f=>array.map(g=>[e,f,g]))).flat(2)
swizzle2d.forEach(e=>{
			Object.defineProperty(this, e[0]+e[1], {
				get: function() {
					return new THREE.Vector2(this[e[0]],this[e[1]]);
				},
				set: function(vec2) {
					this[e[0]] = vec2[e[0]];
					this[e[1]] = vec2[e[1]];
				}
			})
})

swizzle3d.forEach(e=>{
			Object.defineProperty(this, e[0]+e[1]+e[2], {
				get: function() {
					return new THREE.Vector3(this[e[0]],this[e[1]],this[e[2]]);
				},
				set: function(vec3) {
					this[e[0]] = vec3[e[0]];
					this[e[1]] = vec3[e[1]];
					this[e[2]] = vec3[e[2]];
				}
			})
})

Additional context

Will this cause performance issues? I am unclear on how getters and setters work.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
gkjohnsoncommented, Nov 24, 2021

Not in this specific context, but how do I make it so it doesn’t make new objects? Like I know the performance implications, i just don’t see any other way around them even in other contexts.

Most other math functions take a “target” object that is used to get the result of an operation which ultimately I think would defeat the simplicity of your swizzle functions. That target function can be global so it’s reused without having to be remade in any frequently evaluated contexts. Something like:

const target = new Vector3()
const v = new Vector3( 1, 2, 3 );
v.yyx( target );

console.log( target ); // 2, 2, 1

I agree these types of fields are convenient and it’s nice that they align with glsl behavior but unfortunately I think the nature of Javascript makes them difficult to implement elegantly.

Also FYI you don’t need to pass a Vector3 into the set function. You can pass in numbers directly which makes the set function a fairly readable one liner:

u.set( v.x, v.x, v.y );
2reactions
mrdoobcommented, Nov 16, 2021

I mean this sort of thing: u.set(new THREE.Vector3(v.x,v.x,v.y))

Can’t you do that already?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Type (GLSL) - OpenGL Wiki - Khronos Group
Swizzling. You can access the components of vectors using the following syntax: vec4 someVec; someVec.
Read more >
GLSL-style SSE vector swizzle intrinsics for C - Reddit
I've poked around some in GLM and it was pretty sweet for writing 3.0+ style GL. Not sure how optimized it is but...
Read more >
Support GLSL style initialization and functions for vectors #4822
Less important: Add a Vector4 type that aliases to color and can use xyz; Add swizzling to Vectors (marginal, probably not worth it)....
Read more >
Shaders - LearnOpenGL
The vector datatype allows for some interesting and flexible component selection called swizzling . Swizzling allows us to use syntax like this: vec2...
Read more >
How to achieve vector swizzling in C++? - Stack Overflow
struct vec2 // use C++ style struct declaration { // struct is public by default union { struct { float x, y; }...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found