glsl-extract-reflect

1.0.1 • Public • Published

glsl-extract-reflect

stable

Takes the type data from glsl-extract and transforms the names into a JSON structure where each leaf node is the return value of the given map function.

This is useful to create wrappers for uniform and attribute structs/arrays.

var reflect = require('glsl-extract-reflect')
 
var uniforms = [
  { name: 'lights[0].position', type: 'vec4' },
  { name: 'projection', type: 'mat4' },
  { name: 'color', type: 'vec3' }
]
 
var locations = reflect(uniforms, function (uniform) {
  return {
    type: uniform.type,
    location: gl.getUniformLocation(program, uniform.path)
  }
})

The locations result looks like this:

{
  "lights": [
    {
      "position": {
        "type": "vec4",
        "location": WebGLUniformLocation
      }
    }
  ],
  "projection": {
    "type": "mat4",
    "location": WebGLUniformLocation
  },
  "color": {
    "type": "vec3",
    "location": WebGLUniformLocation
  }
}

Credit goes to @mikolalysenko; this was pulled out of gl-shader and made a little more generic.

Usage

NPM

result = reflect(array, map)

Takes an array of uniforms or attributes with { name, type } values, and returns the "unflattened" object for all structs and arrays. Each leaf node is the result of calling map(data, index, array), where data has the following:

{
  name: 'radius'           // the standalone uniform name
  path: 'lights[0].radius' // the qualified name used by WebGL
  type: 'float'            // the original type
  ...
}

Other values (like location) are also copied into the data object.

See Also

License

MIT, see LICENSE.md for details.

Package Sidebar

Install

npm i glsl-extract-reflect

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • mattdesl