get-attributes-uniforms
v1.1.1
Published
Get the attributes and uniforms from a GLSL shader string
Downloads
21
Maintainers
Readme
get-attributes-uniforms
Get the uniforms and attributes from a GLSL shader string
Background / Initial Motivation
The initial motivation for get-attributes-uniforms
was to be used in the browser during runtime
when getting attribute and uniform locations from a shader
(i.e. calling getAttribLocation
& getUniformLocation
).
We want get-attributes-uniforms
to be tiny since it intends to run in the browser,
so instead of depending on a powerful but large glsl AST parser we use the
native string functions.
In short.. we just chop out the uniform and attribute names based on our knowledge of the GLSL syntax.
Notes
This API does not use an AST and could very well be overlooking an edge case. If you run into one, feel super free to open an issue or open a PR with a test case.
To Install
$ npm install --save get-attributes-uniforms
Changes to the demo
and src
files will now live reload in your browser.
Usage
var getAttributesUniformss = require('get-attributes-uniforms')
var fragmentShader = `
precision mediump float;
attribute mat4 someAttrib;
uniform vec4 color;
void main () {
gl_FragColor = color;
}
`
var vertexShader = `
precision mediump float;
attribute vec2 position;
attribute vec4 foobar ;
void main () {
gl_Position = vec4(position, 0, 1);
}
`
console.log(getAttributesUniforms(fragmentShader))
// { attributes: {someAttrib: 'mat4'}, uniforms: {color: 'vec4' } }
console.log(getAttributesUniforms(vertexShader))
// { attributes: {position: 'vec2', foobar: 'vec4'}, uniforms: {} }
API
getAttributesUniforms(shaderString)
-> Object
shaderString
Type: String
A string representing a vertex or fragment shader.
var shaderString = `
precision mediump float;
attribute vec3 position;
void main() {
// ...
}
`
TODO:
- [ ] Fix when shader code has comments above a uniform. Somehow got a uniform back called
uniform
See Also
License
MIT