Macro godot::prelude::swizzle

macro_rules! swizzle {
    ($vec:expr => $a:ident, $b:ident) => { ... };
    ($vec:expr => $a:ident, $b:ident, $c:ident) => { ... };
    ($vec:expr => $a:ident, $b:ident, $c:ident, $d:ident) => { ... };
}
Expand description

Access vector components in different order.

Allows to rearrange components, as well as to create higher- or lower-order vectors.

This macro supports all vector types (2D, 3D, 4D; both integer and float). The resulting vector type is deduced from the number and types of components.

To repeat a single component, check out the splat method on specific vector types.

ยงExamples

Reorder or duplicate fields:

use godot::prelude::*;

let vec3 = Vector3i::new(1, 2, 3);
let xzx = swizzle!(vec3 => x, z, x); // Vector3i

assert_eq!(xzx, Vector3i::new(1, 3, 1));

Create lower-order vector:

let vec4 = Vector4::new(1.0, 2.0, 3.0, 4.0);
let yw = swizzle!(vec4 => y, w); // Vector2

assert_eq!(yw, Vector2::new(2.0, 4.0));

Create higher-order vector:

let vec3 = Vector3i::new(1, 2, 3);
let xyyz = swizzle!(vec3 => x, y, y, z); // Vector4i

assert_eq!(xyyz, Vector4i::new(1, 2, 2, 3));