Vector Swizzling in JavaScript: Best Methods

477 Views Asked by At

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;
1

There are 1 best solutions below

0
On

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