glsl-transpiler 
Transforms glsl source to optimized js code. It converts vectors and matrices to arrays, expands swizzles, applies expressions optimizations and provides stdlib for environment compatibility.
Usage

import GLSL from 'glsl-transpiler'
var compile = GLSL({
uniform: function (name) {
return `uniforms.${name}`
},
attribute: function (name) {
return `attributes.${name}`
}
})
compile(`
precision mediump float
attribute vec2 uv
attribute vec4 color
varying vec4 fColor
uniform vec2 uScreenSize
void main (void) {
fColor = color
vec2 position = vec2(uv.x, -uv.y) * 1.0
position.x *= uScreenSize.y / uScreenSize.x
gl_Position = vec4(position, 0, 1)
}
`)
`
var uv = attributes.uv
var color = attributes.color
var fColor = new Float32Array([0, 0, 0, 0])
var uScreenSize = uniforms.uScreenSize
function main () {
fColor = color
var position = new Float32Array([uv[0], -uv[1]])
position[0] *= uScreenSize[1] / uScreenSize[0]
gl_Position = new Float32Array([position[0], position[1], 0, 1])
}
`
API
glsl-transpiler
To apply compilation to glsl AST or string, require glsl-transpiler
:
import GLSL from 'glsl-transpiler'
let compile = GLSL({
optimize: true,
preprocess: true,
uniform: false,
attribute: false,
varying: false,
version: '100 es',
includes: true,
debug: false
})
let result = compile('...source.glsl')
let {
attributes,
uniforms,
varyings,
structs,
functions,
scopes
} = compile.compiler
compiler.reset()
Note that texture2D
function expects whether ndarray instance or defined width
and height
parameters on passed array.
glsl-transpiler/stream
glsl-transpiler can also be used as a stream. For each node from the glsl-parser it will return compiled js chunk:
import compile from 'glsl-transpiler/stream.js'
import parse from 'glsl-parser/stream.js'
import tokenize from 'glsl-tokenizer/stream.js'
fs.createReadStream('./source.glsl')
.pipe(tokenize())
.pipe(parse())
.pipe(compile(options?))
.once('end', function () {
console.log(this.source)
})
Dependencies
Used by
- nogl-shader-output — evaluate fragment shader on rectangular vertex input, gl-less.
- GLSLRun – debug shader via adding
print()
function.
Similar