Learn how to add new WGSL functions and contribute to the library
The wgsl-fns library welcomes contributions of new WGSL functions! Whether you've created a useful mathematical function, noise algorithm, or shader utility, we'd love to include it in the library for others to use.
Start by forking the wgsl-fns repository on GitHub and cloning it to your local machine.
git clone https://github.com/koole/wgsl-fns.git cd wgsl-fns npm install
Install dependencies and start the development watcher:
npm install
npm run dev # Watch for changes during development
npm test # Run testsAdd your function to the appropriate category file:
src/math.ts - Mathematical utilities, easing functionssrc/noise.ts - Noise generation algorithmssrc/sdf.ts - Signed distance field functionssrc/color.ts - Color manipulation and conversionsrc/ for more categoriesEach function should follow this structure with complete JSDoc documentation:
/** * @wgsl * @name myFunction * @description Brief description of what the function does * @param {f32} x First parameter description * @param {f32} y Second parameter description * @returns {f32} Description of return value * @requires dependency1 dependency2 */ export const myFunction = `//! requires dependency1 dependency2 fn myFunction(x: f32, y: f32) -> f32 { return dependency1(x) + dependency2(y); }`;
@wgsl - Marks this as a WGSL function@name - Function name (should match export name)@description - Clear explanation of purpose and use case@param - Each parameter with type and description@returns - Return type and description@requires - Dependencies (if any)If your function calls other WGSL functions, declare dependencies using magic comments:
export const fbm = `//! requires noise2D hash22 fn fbm(p: vec2<f32>) -> f32 { var value = 0.0; var amplitude = 0.5; var frequency = 0.0; for (var i = 0; i < 4; i++) { value += amplitude * noise2D(p * frequency); frequency *= 2.0; amplitude *= 0.5; } return value; }`;
The build system uses these comments to automatically resolve dependencies when users call getFns().
Make sure your function is exported from the category file (e.g., src/math.ts).
Add your function to src/functions.ts:
// Import your function import { myFunction } from './math'; // Add to the functions object export const functions = { // ...existing functions... myFunction, } as const;
Before submitting your contribution, make sure all tests pass:
npm test # Run all tests npm run test:ci # Run tests with build (CI command)
getFns() functionality and error handlingOnce your function is ready and tests pass:
git add . git commit -m "Add myFunction to math utilities" git push origin your-branch-name
Then create a pull request on GitHub with a clear description of your function and its use case.
Here's a complete example of adding a new easing function:
// In src/math.ts /** * @wgsl * @name easeInOutCubic * @description Cubic ease-in-out function for smooth animations * @param {f32} t Progress value between 0.0 and 1.0 * @returns {f32} Eased value between 0.0 and 1.0 */ export const easeInOutCubic = `fn easeInOutCubic(t: f32) -> f32 { if (t < 0.5) { return 4.0 * t * t * t; } let p = 2.0 * t - 2.0; return 1.0 + p * p * p / 2.0; }`; // In src/functions.ts import { easeInOutCubic } from './math'; export const functions = { // ...existing functions... easeInOutCubic, } as const;
Once your pull request is merged, the maintainers will handle the release process:
Your contributions help make WebGPU shader development more accessible for everyone. We appreciate your effort in expanding the wgsl-fns library!