Skip to content

v0.4.0

Choose a tag to compare

@CodyJasonBennett CodyJasonBennett released this 13 Feb 11:28
· 32 commits to main since this release

What's Changed

This release follows up v0.3.0 with a visitor API to recurse AST. Useful for reflecting or modifying a shader program.

import { type Program, parse, visit, generate } from 'shaderkit'

// https://github.com/CodyJasonBennett/shaderkit#ast
const program: Program = parse(glsl)

// optionally reflect program here (e.g. collect all global uniforms)
const uniforms: string[] = []
visit(program, {
  VariableDeclarator(node) {
    if (node.qualifiers.includes('uniform')) {
      uniforms.push(node.id.name)
    }
  },
})

// optionally modify program here (e.g. remove version header)
visit(program, {
  PreprocessorStatement(node, ancestors) {
    const parent = ancestors.at(-1)
    if (node.name === 'version' && parent?.type === 'Program') {
      parent.body.splice(parent.body.indexOf(node), 1)
    }
  },
})

// Compile back into GLSL
const output: string = generate(program, { target: 'GLSL' })

Full Changelog: v0.3.1...v0.4.0