{"id":13991869,"url":"https://github.com/stackgl/gl-shader","last_synced_at":"2025-04-13T05:00:23.405Z","repository":{"id":8921550,"uuid":"10649621","full_name":"stackgl/gl-shader","owner":"stackgl","description":"🎁 WebGL shader wrapper","archived":false,"fork":false,"pushed_at":"2022-09-09T07:57:36.000Z","size":208,"stargazers_count":124,"open_issues_count":5,"forks_count":23,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-13T04:59:58.718Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://stackgl.github.io/gl-shader/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stackgl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-12T18:52:02.000Z","updated_at":"2025-04-01T02:43:45.000Z","dependencies_parsed_at":"2022-08-07T05:00:17.946Z","dependency_job_id":null,"html_url":"https://github.com/stackgl/gl-shader","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackgl%2Fgl-shader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackgl%2Fgl-shader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackgl%2Fgl-shader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stackgl%2Fgl-shader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stackgl","download_url":"https://codeload.github.com/stackgl/gl-shader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665756,"owners_count":21142123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-09T14:01:38.619Z","updated_at":"2025-04-13T05:00:23.326Z","avatar_url":"https://github.com/stackgl.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"gl-shader\n=========\nA wrapper for WebGL shaders.  Part of [stack.gl](http://stack.gl)\n\n# Example\n\nTry it out now in your browser:  [http://stackgl.github.io/gl-shader/](http://stackgl.github.io/gl-shader/)\n\n```javascript\nvar shell = require('gl-now')()\nvar createShader = require('gl-shader')\nvar shader, buffer\n\nshell.on('gl-init', function() {\n  var gl = shell.gl\n\n  //Create shader\n  shader = createShader(gl,\n    'attribute vec3 position;\\\n    varying vec2 uv;\\\n    void main() {\\\n      gl_Position = vec4(position, 1.0);\\\n      uv = position.xy;\\\n    }',\n    'precision highp float;\\\n    uniform float t;\\\n    varying vec2 uv;\\\n    void main() {\\\n      gl_FragColor = vec4(0.5*(uv+1.0), 0.5*(cos(t)+1.0), 1.0);\\\n    }')\n\n  //Create vertex buffer\n  buffer = gl.createBuffer()\n  gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([\n    -1, 0, 0,\n    0, -1, 0,\n    1, 1, 0\n  ]), gl.STATIC_DRAW)\n})\n\nshell.on('gl-render', function(t) {\n  var gl = shell.gl\n\n  //Bind shader\n  shader.bind()\n  \n  //Set attributes\n  gl.bindBuffer(gl.ARRAY_BUFFER, buffer)\n  shader.attributes.position.pointer()\n\n  //Set uniforms\n  shader.uniforms.t += 0.01\n\n  //Draw\n  gl.drawArrays(gl.TRIANGLES, 0, 3)\n})\n```\n\nHere is the result:\n\n\u003cimg src=\"https://raw.github.com/stackgl/gl-shader/master/screenshot.png\"\u003e\n\n# Install\n\n    npm install gl-shader\n\n# API\n\n```javascript\nvar createShader = require('gl-shader')\n```\n\n\n### Constructor\n\nThere are two main usages for the constructor.  First,\n\n#### `var shader = createShader(gl, vertexSource, fragmentSource[, uniforms, attributes])`\n\nConstructs a wrapped shader object with shims for all of the uniforms and attributes in the program.\n\n* `gl` is the webgl context in which the program will be created\n* `vertexSource` is the source code for the vertex shader\n* `fragmentSource` is the source code for the fragment shader\n* `uniforms` is an (optional) list of all uniforms exported by the shader program\n* `attributes` is an (optional) list of all attributes exported by the shader program\n\nThe optional `uniforms` and `attributes` arrays have the following format. This will be extracted at run-time from the shader, so you can typically omit the `uniforms` and `attributes` arguments.\n\n```js\n{\n  uniforms: [\n    { type: 'mat4', name: 'projection' },\n    { type: 'sampler2D', name: 'texture0' }\n  ],\n  attributes: [\n    { type: 'vec3', name: 'position' }\n  ]\n}\n```\n\nYou can specify a default `location` number for each attribute, otherwise WebGL will bind it automatically. \n\n**Returns** A compiled shader object.\n\n#### `var shader = createShader(gl, opt)`\n\nThe same as above, but takes an object instead of a parameter list.\n\n* `gl` is a WebGL context\n* `opt.vertex` a vertex shader source\n* `opt.fragment` a fragment shader source\n* `opt.uniforms` (optional) a list of uniforms\n* `opt.attributes` (optional) a list of attributes\n\n**Returns** A wrapped shader object\n\n### Methods\n\n#### `shader.bind()`\nBinds the shader for rendering\n\n#### `shader.update(vertSource,fragSource[,uniforms,attributes])`\nRebuilds the shader object with new vertex and fragment shaders (same behavior as constructor)\n\n#### `shader.update(opt)`\nRebuilds the shader object with new vertex and fragment shaders (same behavior as constructor)\n\n#### `shader.dispose()`\nDeletes the shader program and associated resources.\n\n### Properties\n\n#### `gl`\nThe WebGL context associated to the shader\n\n#### `program`\nA reference to the underlying program object in the WebGL context\n\n#### `vertShader`\nA reference to the underlying vertex shader object\n\n#### `fragShader`\nA reference to the underlying fragment shader object\n\n### Uniforms\nThe uniforms for the shader program are packaged up as properties in the `shader.uniforms` object.  The shader must be bound before the uniforms are assigned. For example, to update a scalar uniform you can just assign to it:\n\n```javascript\nshader.bind()\nshader.uniforms.scalar = 1.0\n```\n\nWhile you can update vector uniforms by writing an array to them:\n\n```javascript\nshader.uniforms.vector = [1,0,1,0]\n```\n\nMatrix uniforms must have their arrays flattened first:\n\n```javascript\nshader.uniforms.matrix = [ 1, 0, 1, 0,\n                           0, 1, 0, 0,\n                           0, 0, 1, 1,\n                           0, 0, 0, 1 ]\n```\n\nYou can read the value of uniform too if the underlying shader is currently bound.  For example,\n\n```javascript\nshader.bind()\nconsole.log(shader.uniforms.scalar)\nconsole.log(shader.uniforms.vector)\nconsole.log(shader.uniforms.matrix)\n```\n\nStruct uniforms can also be accessed using the normal dot property syntax:\n\n```javascript\nshader.uniforms.light[0].color = [1, 0, 0, 1]\n```\n\nIt is also possible to initialize uniforms in bulk by assigning an object:\n\n```javascript\nshader.uniforms = {\n  model:  [1, 0, 0, 0,\n           0, 1, 0, 0,\n           0, 0, 1, 0,\n           0, 0, 0, 1],\n  color:  [1, 0, 1, 1]\n}\n```\n\nThe contents of uniform values are lost when a shader is unbound.\n\n### Attributes\n\nThe basic idea behind the attribute interface is similar to that for uniforms, however because attributes can be either a constant value or get values from a vertex array they have a slightly more complicated interface.  All of the attributes are stored in the `shader.attributes` property.\n\n#### `attrib = constant`\nFor non-array attributes you can set the constant value to be broadcast across all vertices.  For example, to set the vertex color of a shader to a constant you could do:\n\n```javascript\nshader.attributes.color = [1, 0, 0, 1]\n```\n\nThis internally uses [`gl.vertexAttribnf`](http://www.khronos.org/opengles/sdk/docs/man/xhtml/glVertexAttrib.xml). Setting the attribute will also call `gl.disableVertexAttribArray` on the attribute's location.\n\n#### `attrib.location`\nThis property accesses the location of the attribute.  You can assign/read from it to modify the location of the attribute.  For example, you can update the location by doing:\n\n```javascript\nattrib.location = 0\n```\n\nOr you can read the currently bound location back by just accessing it:\n\n```javascript\nconsole.log(attrib.location)\n```\n\n**WARNING** Changing the attribute location requires recompiling the program. This recompilation is deferred until the next call to `.bind()`\n\n#### `attrib.pointer([type, normalized, stride, offset])`\nA shortcut for `gl.vertexAttribPointer`/`gl.enableVertexAttribArray`.  See the [OpenGL man page for details on how this works](http://www.khronos.org/opengles/sdk/docs/man/xhtml/glVertexAttribPointer.xml).  The main difference here is that the WebGL context, size and index are known and so these parameters are bound.\n\n* `type` is the type of the pointer (default `gl.FLOAT`)\n* `normalized` specifies whether fixed-point data values should be normalized (`true`) or converted directly as fixed-point values (`false`) when they are accessed.  (Default `false`)\n* `stride` the byte offset between consecutive generic vertex attributes.  (Default: `0`)\n* `offset` offset of the first element of the array in bytes. (Default `0`)\n\n#### Matrix attributes\n\nMatrix attributes are also supported, however there are a few subtle difference.  Due to WebGL limitations, d-dimensional matrix attributes require d separate attribute locations.  If `matrix` is a matrix attribute, then the rows of the matrix can be accessed independently using:\n\n```javascript\n//First row of matrix\nshader.attributes.matrix[0]\n\n//Second row\nshader.attributes.matrix[1]\n\n// ... etc.\n```\n\nThe interface for these attributes is identical to the above interfaces for vector attributes (support constant setters, `.pointer()`, and `.location`).\n\nThere is also a bulk interface which simplifies working with the matrix as a whole unit.  For example, it is possible to update the location of each row of the matrix simultaneously by assigning it a vector value:\n\n```javascript\nshader.attributes.matrix.location = [1, 2, 3, 4]\n```\n\nSimilarly, if the matrix attribute is stored as a contiguous range in memory, the pointer for each row can be set using `.pointer()`.  For example, if `matrix` is a 4x4 matrix attribute then,\n\n```javascript\nshader.attributes.matrix.pointer(gl.FLOAT, false, 16, 0)\n```\n\nis equivalent to,\n\n```javascript\nshader.attributes.matrix[0].pointer(gl.FLOAT, false, 16, 0)\nshader.attributes.matrix[0].pointer(gl.FLOAT, false, 16, 4)\nshader.attributes.matrix[0].pointer(gl.FLOAT, false, 16, 8)\nshader.attributes.matrix[0].pointer(gl.FLOAT, false, 16, 12)\n```\n\n### Reflection\n\nFinally, the library supports some reflection capabilities.  The set of all uniforms and data types are stored in the \"type\" property of the shader object,\n\n```javascript\nconsole.log(shader.types)\n```\n\nThis reflects the uniform and attribute parameters that were passed to the shader constructor.\n\n## Acknowledgements\n\n(c) 2013-2015 Mikola Lysenko.  MIT License\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstackgl%2Fgl-shader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstackgl%2Fgl-shader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstackgl%2Fgl-shader/lists"}