diff --git a/README.md b/README.md index 1c7cf0e..c4ae9fd 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ pub fn main () void { | div | Construct new vector resulting from the division between two vectors | | mul | Construct new vector resulting from the multiplication between two vectors | | scale | Construct new vector after multiplying each components by a given scalar | +| swizzle | Comptime vector component swizzle. Accepts component names, 0, or 1 | | cross | Construct the cross product (as vector) from two vectors (only for vec3) | | dot | Return the dot product between two vectors | | lerp | Linear interpolation between two vectors | diff --git a/src/generic_vector.zig b/src/generic_vector.zig index baabc70..c303293 100644 --- a/src/generic_vector.zig +++ b/src/generic_vector.zig @@ -402,14 +402,24 @@ pub fn GenericVector(comptime dimensions: comptime_int, comptime T: type) type { pub const rotate = DimensionImpl.rotate; pub const cross = DimensionImpl.cross; + /// Comptime vector component swizzle. Accepts component names, 0, or 1. pub fn swizzle(self: Self, comptime comps: []const u8) SwizzleType(comps.len) { + // Someone doing a single component swizzle with 0 or 1 is weird but... it's supported... if (comps.len == 1) { - return self.data[@intFromEnum(@field(Component, &.{comps[0]}))]; + return switch (comps[0]) { + '0' => 0, + '1' => 1, + else => self.data[@intFromEnum(@field(Component, &.{comps[0]}))], + }; } var result = GenericVector(comps.len, T).zero(); inline for (comps, 0..) |comp, i| { - result.data[i] = self.data[@intFromEnum(@field(Component, &.{comp}))]; + switch (comp) { + '0' => result.data[i] = 0, + '1' => result.data[i] = 1, + else => result.data[i] = self.data[@intFromEnum(@field(Component, &.{comp}))], + } } return result; }