1

If swizzling is possible in JavaScript, what would be the best way or a good start to vector swizzling? It would be similar to swizzling in other programming languages, for instance, glsl:

vec3 v = vec3(fragCoord.xy, 0);

Something like this should be achieved in JavaScript:

var v1 = vec(1, 2, 3);
var v2 = v1.zyx;
  • I've never heard of GLSL, and I don't think this is common in general purpose languages. It's not in PHP, Python, JavaScript. It seems like it's mainly in languages for graphics processing. – Barmar Dec 11 '20 at 00:42
  • You could easily write a function that takes an array of values (the vector) and an array of indexes (in place of `zyx`), and returns an array of the values at those indexes. – Barmar Dec 11 '20 at 00:44
  • 1
    `var v1 = [1, 2, 3]; var v2 = swizzle(v1, [2,1,0]);` – Barmar Dec 11 '20 at 00:45
  • 1
    What if you use a Proxy? – clickbait Jul 20 '22 at 01:42

1 Answers1

0

If you want something like that then I made the library

Please try

https://github.com/KukuhGit/SwizzVec

How to use

var v1 = vec(1, 2, 3);
var v2 = vec(v1.zyx, 7);
var v3 = vec(v1.zz, v2.xd);
var v4 = vec(2, 7, v3.zy);
v4.x = 6;
var v5 = vec(v4.xzyd);

Not yet supported

var V2 = V1.xy;
V2.xy = V1.xx;

And mathematical operations in it

var V8 = V4.zd + V2.yx;

But if you agree, I will update the code or you can recommend an update and help later