Skip to content

Commit

Permalink
Merge branch 'main' into feat/mat-x-vec-mul
Browse files Browse the repository at this point in the history
  • Loading branch information
reczkok committed Feb 14, 2025
2 parents 51bb92b + b6185dd commit 90f7673
Show file tree
Hide file tree
Showing 7 changed files with 762 additions and 551 deletions.
40 changes: 40 additions & 0 deletions packages/typegpu/scripts/generateSwizzleFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Prints the swizzling getters to be manually added to the VecBase abstract class.
*/
printSwizzlingFor('xyzw');

/**
* Yields combinations of letters from `components` of given `length`.
*
* @example
* vectorComponentCombinations('xyz', 2) // xx, xy, xz, yx, yy ...
*/
function* vectorComponentCombinations(
components: string,
length: number,
): Generator<string, undefined, undefined> {
if (length > 1) {
for (const str of vectorComponentCombinations(components, length - 1)) {
for (const component of components) {
yield str + component;
}
}
} else {
yield* components;
}
}

function printSwizzlingFor(components: string) {
const componentIndex: Record<string, number> = { x: 0, y: 1, z: 2, w: 3 };
for (const count of [2, 3, 4] as const) {
const vecClassName = `_Vec${count}`;
for (const swizzle of vectorComponentCombinations(components, count)) {
const implementation = ` get ${swizzle}() { return new this.${vecClassName}(${[
...swizzle,
]
.map((s) => `this[${componentIndex[s]}]`)
.join(', ')}); }`;
console.log(implementation);
}
}
}
3 changes: 2 additions & 1 deletion packages/typegpu/src/data/matrix.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { inGPUMode } from '../gpuMode';
import type { SelfResolvable } from '../types';
import { type VecKind, vec2f, vec3f, vec4f } from './vector';
import { vec2f, vec3f, vec4f } from './vector';
import type {
Mat2x2f,
Mat3x3f,
Mat4x4f,
VecKind,
m2x2f,
m3x3f,
m4x4f,
Expand Down
Loading

0 comments on commit 90f7673

Please sign in to comment.