Drawing patterns with glsl shaders on modern browsers.
npm install gl-rendererA lightweight webgl renderer.
The underlying Library of glsl-doodle.
In browser:
``html`
With NPM:
`bash`
npm install gl-renderer
index.frag
`glsl
#ifdef GL_ES
precision mediump float;
#endif
uniform vec3 color;
void main() {
gl_FragColor.rgb = color;
gl_FragColor.a = 1.0;
}
`
index.html
`html`
You will see a canvas 512 pixels wide and 512 pixels high in red.
Create renderer options with specified canvas element and options.
The options:
- autoUpdate: Force renderer to update when uniforms or meshdata changes. Default value is true.
- vertexPosition: Attribute name of position in vertex shader. Default value is 'a_vertexPosition'.
- vertexTextureCoord: Attribute name of texture coordinate in vertext shader. Default value is 'a_vertexTextureCoord'.
- Other webgl context options: See MDN.
- webgl2: Use webgl2 context. Default value is false.
Create a program with specified fragment shader and vertex shader.
`js
const fragmentShader =
#ifdef GL_ES
precision mediump float;
#endif
void main() {
gl_FragColor = vec4(1, 0, 0, 1);
};
const program = renderer.createProgram(fragmentShader);
render.useProgram(program);
`
If you don't specified vertex shader, a default vertex shader will be loaded.
`glsl
attribute vec4 a_vertexPosition;
void main() {
gl_PointSize = 1.0;
gl_Position = a_vertexPosition;
}
`
or
`glsl
attribute vec4 a_vertexPosition;
attribute vec2 a_vertexTextureCoord;
varying vec2 vTextureCoord;
void main() {
gl_PointSize = 1.0;
gl_Position = a_vertexPosition;
vTextureCoord = a_vertexTextureCoord;
}
`
It depends whether you use texture samplers in your fragment shader or not.
gl-render allows you to use #pragma include statment to load and include other shaders.
base.glsl
`glsl`
highp float random(vec2 co) {
highp float a = 12.9898;
highp float b = 78.233;
highp float c = 43758.5453;
highp float dt= dot(co.xy ,vec2(a,b));
highp float sn= mod(dt,3.14);
return fract(sin(sn) * c);
}
`js
const fragmentShader =
#ifdef GL_ES
precision mediump float;
#endif
#pragma include "./base.glsl"
void main() {
gl_FragColor = random(gl_FragCoord.xy) * vec4(1, 0, 0, 1);
};
const program = await render.compile(fragmentShader);
renderer.useProgram(program);
`
Load fragment shader and vertex shader from url.
`js
const program = await renderer.load('./index.glsl');
renderer.useProgram(program);
`
Sets the specified Program as part of the current rendering state.
Wrap a image object (or canvas) to a webgl texture.
`js
function loadImage(src) {
const img = new Image();
img.crossOrigin = 'anonymous';
return new Promise((resolve) => {
img.onload = function () {
resolve(img);
};
img.src = src;
});
}
(async function () {
const glCanvas = document.getElementById('gl-canvas');
const renderer = new GlRenderer(glCanvas);
const image = await loadImage('http://path.to.image/image.png');
const texture = renderer.createTexture(image);
const program = await renderer.load('./index.frag');
renderer.useProgram(program);
// bind texture to samplerMyTex
renderer.uniforms.samplerMyTex = texture;
renderer.render();
}());
`
Load image from source url and create a texture.
`js
(async function () {
const glCanvas = document.getElementById('gl-canvas');
const renderer = new GlRenderer(glCanvas);
const texture = await renderer.loadTexture('http://path.to.image/image.png');
const program = await renderer.load('./index.frag');
renderer.useProgram(program);
// bind texture to samplerMyTex
renderer.uniforms.samplerMyTex = texture;
renderer.render();
}());
`
Set a list of meshData.
`js`
const meshData = [mesh0, mesh1, mesh2...];
A mesh object contains the following properties:
- positions : _Required_. The vertex positions of the geometry.
- cells: _Required_. The indices of the vertexes.
- textureCoord: The texture coordinates.
- attributes: The attributes passed into the shaders.
- uniforms: The changed uniform values.
- instanceCount: Set instanceCount for webgl2 context to draw instanced arrays.
index.vert
`glsl
attribute vec3 a_vertexPosition;
attribute vec3 a_color;
varying vec3 vColor;
void main() {
gl_PointSize = 1.0;
gl_Position.xyz = a_vertexPosition;
gl_Position.w = 1.0;
vColor = a_color;
}
`
index.frag
`glsl
#ifdef GL_ES
precision mediump float;
#endif
varying vec3 vColor;
void main() {
gl_FragColor = vec4(vColor, 1.0);
}
`
`js
(async function () {
const glCanvas = document.getElementById('gl-canvas');
const renderer = new GlRenderer(glCanvas);
const program = await renderer.load('./index.frag', './index.vert');
renderer.useProgram(program, {
a_color: {
type: 'UNSIGNED_BYTE',
normalize: true,
},
});
const vertexColors = [
[255, 0, 0],
[255, 0, 0],
[255, 255, 0],
];
renderer.setMeshData([
{
positions: [[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [1.0, 1.0, 0.0]],
cells: [[0, 1, 2]],
attributes: {
a_color: vertexColors,
},
},
{
positions: [[0.5, 0.5, 0], [-0.5, 0.8, 0], [1, -1, 0]],
cells: [[0, 1, 2]],
attributes: {
a_color: vertexColors,
},
},
]);
renderer.render();
}());
`
Clear and re-draw canvas. If clearBuffer set to true(default is true), renderer will automately clear color buffer before render.
The uniform declarations in the fragment shader will be automatically bind to renderer.uniforms.
index.frag
`glsl
#ifdef GL_ES
precision mediump float;
#endif
uniform float u_time;
void main() {
gl_FragColor = 0.5 (1.0 + sin(0.00314 u_time)) * vec4(1, 0, 0, 1);
}
`
`js
(async function () {
const glCanvas = document.getElementById('gl-canvas');
const renderer = new GlRenderer(glCanvas);
const program = await renderer.load('./index.frag');
renderer.useProgram(program);
const startTime = Date.now();
renderer.uniforms.u_time = 0;
requestAnimationFrame(function update() {
renderer.uniforms.u_time = Date.now() - startTime;
requestAnimationFrame(update);
});
renderer.render();
}());
``
Cause canvas to clear and re-draw in next frame. Change uniforms or set meshData will force renderer update automatically.