
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
gl-shader-core
Advanced tools
The core of gl-shader, without the parser. It can be used to compile shaders without including the (relatively large) glsl-parser dependencies, or invoked directly by libraries which use a transform.
npm install gl-shader-core
var shader = require("gl-shader-core")(gl, vertexSource, fragmentSource, uniforms, attributes)
Constructs a packaged gl-shader object with shims for all of the uniforms and attributes in the program.
gl
is the webgl context in which the program will be createdvertexSource
is the source code for the vertex shaderfragmentSource
is the source code for the fragment shaderuniforms
is a list of all uniforms exported by the shader programattributes
is a list of all attributes exported by the shader programThe uniform and attributes variables have output which is consistent with glsl-extract
Returns A compiled shader object.
shader.bind()
Binds the shader for rendering
shader.dispose()
Deletes the shader program and associated resources.
gl
The WebGL context associated to the shader
handle
A handle to the underlying WebGL program object
vertShader
A handle to the underlying WebGL fragment shader object
fragShader
A handle to the underlying WebGL vertex shader object
The uniforms for the shader program are packaged up as properties in the shader.uniforms
object. For example, to update a scalar uniform you can just assign to it:
shader.uniforms.scalar = 1.0
While you can update vector uniforms by writing an array to them:
shader.uniforms.vector = [1,0,1,0]
Matrix uniforms must have their arrays flattened first:
shader.uniforms.matrix = [ 1, 0, 1, 0,
0, 1, 0, 0,
0, 0, 1, 1,
0, 0, 0, 1 ]
You can also read the value of uniform too if the underlying shader is currently bound. For example,
console.log(shader.uniforms.scalar)
console.log(shader.uniforms.vector)
console.log(shader.uniforms.matrix)
Struct uniforms can also be accessed using the normal dot property syntax. For example,
shader.uniforms.light[0].color = [1, 0, 0, 1]
The 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.
attrib.set()
For 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:
shader.attributes.color = [1, 0, 0, 1]
This internally uses gl.vertexAttribnf
. You can also assign to the attribute by calling:
shader.attributes.color.set(1, 0, 0, 1)
//Or:
shader.attributes.color.set([1, 0, 0, 1])
attrib.location
This 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:
attrib.location = 0
Or you can read the currently bound location back by just accessing it:
console.log(attrib.location)
Internally, these methods just call gl.bindAttribLocation
and access the stored location.
WARNING Changing the attribute location requires recompiling the program. Do not dynamically modify this variable in your render loop.
attrib.pointer([type, normalized, stride, offset])
A shortcut for gl.vertexAttribPointer
. See the OpenGL man page for details on how this works. The main difference here is that the WebGL context, size and index are known and so these parameters are bound.
type
is the type of the pointer (default gl.FLOAT
)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
)stride
the byte offset between consecutive generic vertex attributes. (Default: 0
)offset
offset of the first element of the array in bytes. (Default 0
)attrib.enable()
This enables the vertex attribute pointer. It is a short cut for:
gl.enableVertexAttribArray(attrib.location)
attrib.disable()
This disables the vertex attribute pointer. It is a short cut for:
gl.disableVertexAttribArray(attrib.location)
Finally, the library supports some reflection capabilities. The set of all uniforms and data types are stored in the "type" property of the shader object,
console.log(shader.types)
This reflects the uniform and attribute parameters that were passed to the shader constructor.
(c) 2013 Mikola Lysenko. MIT License
FAQs
Core implementation of gl-shader without parser dependencies
The npm package gl-shader-core receives a total of 782 weekly downloads. As such, gl-shader-core popularity was classified as not popular.
We found that gl-shader-core demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.