From 8d4d3fc1a33e1f2a3ca8214f5423229ca960e983 Mon Sep 17 00:00:00 2001 From: Slashscreen Date: Mon, 10 Nov 2025 11:44:47 -0800 Subject: [PATCH 1/3] Improve swizzle function for vector components swizzle function supports swizzle with '0' or '1'. --- src/generic_vector.zig | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/generic_vector.zig b/src/generic_vector.zig index baabc70..6a21b91 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; } From 2da5f56fb653b347011a50aaf9c93914262e95e0 Mon Sep 17 00:00:00 2001 From: Slashscreen Date: Mon, 10 Nov 2025 11:45:35 -0800 Subject: [PATCH 2/3] Document 'swizzle' operation in README Added description for the 'swizzle' operation in README. --- README.md | 1 + 1 file changed, 1 insertion(+) 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 | From 315e23c545d2ecdfab79a1fb8601dcd8779202ab Mon Sep 17 00:00:00 2001 From: Slashscreen Date: Fri, 14 Nov 2025 01:06:29 -0800 Subject: [PATCH 3/3] Fix typo in swizzle function comment --- src/generic_vector.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generic_vector.zig b/src/generic_vector.zig index 6a21b91..c303293 100644 --- a/src/generic_vector.zig +++ b/src/generic_vector.zig @@ -404,7 +404,7 @@ pub fn GenericVector(comptime dimensions: comptime_int, comptime T: type) type { /// 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... + // Someone doing a single component swizzle with 0 or 1 is weird but... it's supported... if (comps.len == 1) { return switch (comps[0]) { '0' => 0,