Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
14 changes: 12 additions & 2 deletions src/generic_vector.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading