Adding GLSL style swizzling to vectors
See original GitHub issueI 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:
- Created 2 years ago
- Comments:12 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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:Can’t you do that already?