- A browser feature that lets you execute code on the GPU
- The coolest thing to happen to the web since the GIF
(* in brief)
<canvas>
element and grab a WebGL context var gl = canvas.getContext("webgl");
(* in brief)
//gradient pixel shader glsl
varying vec2 uv;
void main(){
gl_FragColor = vec4(uv.x, uv.y, 0.0, 1.0);
}
var pixelShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(pixelShader, pixelShaderSrc);
gl.compileShader(pixelShader);
var program = gl.createProgram();
gl.attachShader(program, geometryShader);
gl.attachShader(program, pixelShader);
gl.linkProgram(program);
(* in brief)
var vertices = [
0.0, 1.0,
-1.0, -1.0,
1.0, -1.0
];
vertices.elementsPerVertex = 2;
var vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
//tell the GPU about the format of the vertices
var attribPositionLoc = gl.getAttribLocation(program, "position");
gl.enableVertexAttribArray(attribPositionLoc);
gl.vertexAttribPointer(attribPositionLoc, vertices.elementsPerVertex, gl.FLOAT, false, 0, 0);
(* in brief)
gl.drawArrays(gl.TRIANGLES, 0, vertices.length / vertices.elementsPerVertex);
(* in brief)
uniform float time;
uniform vec2 resolution;
varying vec2 uv;
void main(){
vec3 col = vec3(0);
float r = distance(uv, vec2(.5));
col.r = pow(sin(time + r), 4.);
col.g = cos(time + uv.y + r)*.5 + .5;
col.b = sin(time * 2.0)*.5 + .5;
gl_FragColor = vec4(col, 1.0);
}
edit me
(* in brief)
(* in brief)
(* in brief)
attribute vec2 position;
uniform float time;
uniform vec2 resolution;
varying vec2 uv;
void main(){
vec2 p = position.xy;
uv = (p + vec2(1.0))*.5; //converts from clip space to graph space
mat2 transformationMatrix = mat2(
cos(time), -sin(time),
sin(time), cos(time)
);
float invAspect = resolution.y / resolution.x;
vec2 scale = vec2(invAspect, 1.0) * .7;
p = transformationMatrix * p * scale;
gl_Position = vec4(p, 0.0, 1.0);
}
edit me
(* in brief)
With WebGL you can
#define t iGlobalTime
#define r iResolution.xy
void mainImage( out vec4 fragColor, in vec2 fragCoord ){
vec3 c;
float l,z=t;
for(int i=0;i<3;i++) {
vec2 uv,p=fragCoord.xy/r;
uv=p;
p-=.5;
p.x*=r.x/r.y;
z+=.07;
l=length(p);
uv+=p/l*(sin(z)+1.)*abs(sin(l*9.-z*2.));
c[i]=.01/length(abs(mod(uv,1.)-.5));
}
fragColor=vec4(c/l,t);
}
3D |
2D |