Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gl-util

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gl-util

Set of practical webgl utils

  • 1.1.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
179K
increased by1.02%
Maintainers
1
Weekly downloads
 
Created
Source

gl-util unstable

Set of practical functions for webgl.

npm install gl-util

const util = require('gl-util');

let gl = util.context({preserveDrawingBuffer: false})
document.body.appendChild(gl.canvas)

util.program(gl, `
	precision mediump float;

	attribute vec2 position;

	void main() {
		gl_Position = vec4(position * 2. - 1., 0, 1);
	}
`, `
	precision mediump float;

	uniform vec4 color;

	void main () {
		gl_FragColor = color;
	}
`);
util.attribute(gl, 'position', [0,0, 1,0, 0,1]);
util.uniform(gl, 'color', [1, .2, 0, 1.]);

gl.drawArrays(gl.TRIANGLES, 0, 3);

API

context(options)

Get context based off options. Basically an extension of webgl-context enabling float param and alpha blending function, as well as fixing defaults. Possible options:

NameDefaultMeaning
canvasAn existing canvas element to re-use rather than creating a new one.
widthIf specified, will set the canvas width.
heightIf specified, will set the canvas height.
antialiastrueEnable antialiasing.
alphatrueWhether canvas contains an alpha buffer, i. e. can be transparent. If false, an alpha blending function gl.blendEquation( gl.FUNC_ADD ); gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) will be enabled.
premultipliedAlphatruePage compositor will assume the drawing buffer contains colors with pre-multiplied alpha.
preserveDrawingBuffertrueDelegate clearing context to the author or clear every frame.
depthfalseEnable depth buffer.
stencilfalseEnable stencil buffer.
floattrueEnable OES_texture_float/OES_texture_float_linear or OES_texture_half_float/OES_texture_half_float_linear extensions.
failIfMajorPerformanceCaveatContext will be created if the system performance is low.
const getContext = require('gl-util/context')

let canvas = document.createElement('canvas')

let gl = getContext('webgl', {
	canvas: canvas,
	antialias: true
})

program(gl, program?)

program(gl, vertSource, fragSource)

Get/set active program or create new program from vertex and fragment sources. The WebGLProgram instance is returned.

const createProgram = require('gl-util/program')

let program = createProgram(gl, `
	precision mediump float;

	attribute vec2 position;

	void main() {
		gl_Position = vec4(position * 2. - 1., 0, 1);
	}
`, `
	precision mediump float;

	uniform sampler2D image;
	uniform vec2 shape;
	uniform float x;

	void main () {
		gl_FragColor = texture2D(image, gl_FragCoord.xy / shape);
	}
`)

uniform(gl, name?, data?, program?)

uniform(gl, {name: data, ...}, program?)

Get/set uniform or multiple uniforms. Returns object with uniform parameters: {name, location, data, type}. Uniforms are stored per-program instance, so to make sure right program is active before updating uniforms a program can be passed as the last argument.

const uniform = require('gl-util/uniform')

uniform(gl, 'color', [1, .2, 0, 1]);

texture(gl, name?, data|parameters?, program?)

texture(gl, {name: data|parameters, ...}, program?)

Set texture[s] data or parameters:

NameMeaning
dataData passed to texture. Can be array, typed array, image, canvas or string denoting the URL of image to load.
indexTexture unit number, if undefined - calculated automatically.
filterSets texture scaling for both min and mag. Can be defined as two separate properties minFilter and magFilter. By default gl.LINEAR.
wrapDefines texture tiling vertically and horizontally. Can be defined precisely as wrapS and wrapT. By default gl.CLAMP_TO_EDGE, can be gl.MIRRORED_REPEAT or gl..
widthIn pixels
heightIn pixels
formatgl.ALPHA, gl.RGB, gl.RGBA (default), gl.LUMINANCE, gl.LUMINANCE_ALPHA, gl.DEPTH_COMPONENT, gl.DEPTH_STENCIL, etc
typegl.UNSIGNED_BYTE, can be gl.FLOAT with proper extension enabled
level0, mipmap level.

Returns object with texture properties {data, index, location, minFilter, magFilter, wrapS, wrapT, width, height, format, type, texture}.

const texture = require('gl-util/texture')

texture(gl, 'image', './picture.gif');

attribute(gl, name?, data|parameters?, program?)

attribute(gl, {name: data|parameters, ...}, program?)

Set attribute[s] data or parameters:

NameDefaultMeaning
datanullData for the attribute, can be array or typed array
size2Number of data items per vertex
stride0Offset in bytes between the beginning of consecutive vertex attributes.
offset0Offset in bytes of the first component in the data. Must be a multiple of type.
typegl.FLOATData type of each component in the data array. Must be one of: gl.BYTE, gl.UNSIGNED_BYTE, gl.SHORT, gl.UNSIGNED_SHORT, gl.FLOAT.
usagegl.STATIC_DRAWMode of draw: gl.STATIC_DRAW (rare changes), gl.DYNAMIC_DRAW (frequent changes) or gl.STREAM_DRAW (frequent updates)
normalizedfalseIf fixed-point data values should be normalized or are to converted to fixed point values when accessed.
index0Attribute unit number, detected automatically if omitted.
targetgl.ARRAY_BUFFER
buffernullWebGLBuffer to use for attribute

Returns attribute properties {data, size, stride, offset, usage, type, normalized, index, target, buffer}.

const attribute = require('gl-util/attribute')

attribute(gl, 'position', [0,0,1,0,0,1]);

Motivation

There are regl and other stack.gl components like gl-texture, gl-shader etc, so why bother?

Because their API may give hard time remembering, same as pure webgl methods. Also gl-utils do not supersede webgl API, so that allows for debugging pure webgl for a moment if one need to. Also if one need minimalistic webgl setup it may be better to opt for a couple of functions over relatively massive stack.gl components.

gl-util is like functions from any webgl tutorial. Tiny, handy and already familiar, so.

Keywords

FAQs

Package last updated on 02 Jan 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc