@thi.ng/shader-ast
Version:
DSL to define shader code in TypeScript and cross-compile to GLSL, JS and other targets
31 lines (30 loc) • 731 B
JavaScript
import { bool, float, int, uint, vec2, vec3, vec4 } from "./lit.js";
const itemType = (type) => type.replace("[]", "");
const numberWithMatchingType = (t, x) => {
const id = t.type[0];
return id === "i" ? int(x) : id === "u" ? uint(x) : id === "b" ? bool(x) : float(x);
};
const matchingPrimFor = (t, x) => {
const ctor = { vec2, vec3, vec4 }[t.type];
return ctor ? ctor(x) : x;
};
const matchingBoolType = (t) => ({
float: "bool",
int: "bool",
uint: "bool",
vec2: "bvec2",
ivec2: "bvec2",
uvec2: "bvec2",
vec3: "bvec3",
ivec3: "bvec3",
uvec3: "bvec3",
vec4: "bvec4",
ivec4: "bvec4",
uvec4: "bvec4"
})[t.type];
export {
itemType,
matchingBoolType,
matchingPrimFor,
numberWithMatchingType
};