npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gl-basic-shader

v1.3.0

Published

generates a basic shader with vertex attributes

Downloads

50

Readme

browser support

gl-basic-shader

experimental

Generates and compiles a basic shader with some common attributes and uniforms and a Model, View, Projection vertex transform. Also includes a command-line interface

var createShader = require('gl-basic-shader')
var shader = createShader(gl, {
	texcoord: true,    //vertex texcoords
	normal: false,     //vertex normals
	color: true,       //vertex colors
	tint: [1, 0, 0, 1] //uniform color tint
})

//do some stuff with it
shader.bind()
shader.uniforms.texture0 = 0
shader.attribuets.texcoord0.pointer() 
shader.attributes.color.pointer()

The resulting shader will be compiled with the generated source:

// vertex shader
attribute vec4 position;
attribute vec4 color;
attribute vec2 texcoord0;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
varying vec4 v_col;
varying vec2 v_tex0;
void main() {
   gl_Position = projection * view * model * position;
   v_col = color;
   v_tex0 = texcoord0;
   gl_PointSize = 1.0;
}

//fragment shader 
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_col;
varying vec2 v_tex0;
uniform sampler2D texture0;
uniform vec4 tint;
void main() {
   gl_FragColor = v_col *  texture2D(texture0,  v_tex0) * tint;
}

Usage

NPM

shader = createShader(gl[, options])

Creates a basic shader with the given options:

  • texcoord whether to generate texcoord0 attribute vec2 and texture0 sampler2D. If this is a number, then N texcoords and samplers will be used: texture0, texture1, etc.
  • normal wherther to generate a normal attribute vec3
  • color whether to generate a color attribute vec4
  • tint the default value bound to the tint uniform vec4, if not specified then white is used
  • pointSize the gl_PointSize for the vertex shader, defualt 1.0

If the texcoord, normal or color options are false (or texcoord is 0) then that attribute will not be included in the shader.

The projection, model, and view matrices are set to an identity matrix initially.

createShader.generate([options])

This function is exposed to provide the generated shader source, useful for testing and other purposes where GL shader compilation may not be desirable. The options are the same as the constructor.

var source = require('gl-basic-shader').generate(options)
console.log(source.vertex, source.fragment, source.attributes, source.uniforms)

attributes

The attributes. Some may not be present if not specified at construction. They use a fixed location for easier VAO bindings.

  • vec4 position (location=0)
  • vec3 normal (location=1)
  • vec4 color (location=2)
  • vec2 texcoord0, texcoord1, etc.. (location=3..N)

uniforms

The uniforms.

  • mat4 model
  • mat4 view
  • mat4 projection
  • vec4 tint
  • sampler2D texture0, texture1, etc

cli

You can also use the command line tool to print a generic shader.

Examples:

# print the vertex and fragment shader with the given options
gl-basic-shader --color --normal --texcoord=2

# print a new vertex shader to a file
gl-basic-shader --vert > vert.glsl

# pretty-print the JSON with extracted uniform/attribute info
# could then be fed easily into gl-shader-core
gl-basic-shader -jpe > shader.json

Full options:

Generate a basic shader.
Usage: gl-basic-shader [opts]

Options:
  --help, -h      show help message                   
  --color, -c     insert color vertex attribute       
  --texcoord, -t  insert texcoord vertex attribute(s) 
  --normal, -n    insert normal vertex attribute      
  --json, -j      json output                         
  --pretty, -p    pretty print json                   
  --extract, -e   extract attribs/uniforms (json only)
  --frag, -f      only show fragment shader           
  --vert, -v      only show vertex shader  

License

MIT, see LICENSE.md for details.