Skip to content

Commit

Permalink
reifyOrigin3D
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Mar 17, 2022
1 parent 7492ae5 commit 0adaa5d
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/webgpu/util/unions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
/**
* Reifies a `GPUOrigin3D` into a `Required<GPUOrigin3DDict>`.
*/
export function reifyOrigin3D(
val: Readonly<GPUOrigin3DDict> | Iterable<number>
): Required<GPUOrigin3DDict> {
if (Symbol.iterator in val) {
const v = Array.from(val as Iterable<number>);
return {
x: v[0] ?? 0,
y: v[1] ?? 0,
z: v[2] ?? 0,
};
} else {
const v = val as Readonly<GPUOrigin3DDict>;
return {
x: v.x ?? 0,
y: v.y ?? 0,
z: v.z ?? 0,
};
}
}

/**
* Reifies a `GPUExtent3D` into a `Required<GPUExtent3DDict>`.
*/
export function reifyExtent3D(
val: Readonly<GPUExtent3DDict> | Iterable<number>
): Required<GPUExtent3DDict> {
// TypeScript doesn't seem to want to narrow the types here properly, so hack around it.
if (typeof (val as Iterable<number>)[Symbol.iterator] === 'function') {
if (Symbol.iterator in val) {
const v = Array.from(val as Iterable<number>);
return { width: v[0] ?? 1, height: v[1] ?? 1, depthOrArrayLayers: v[2] ?? 1 };
return {
width: v[0] ?? 1,
height: v[1] ?? 1,
depthOrArrayLayers: v[2] ?? 1,
};
} else {
const v = val as Readonly<GPUExtent3DDict>;
return {
Expand Down

0 comments on commit 0adaa5d

Please sign in to comment.