GLSL Overview
by Lea Rosema
- semicolons are required
- float numbers must contain a
.
(valid floats are1.0
,.5
,5.
) - no implicit conversion between
float
andint
- built-in vector/matrix arithmetics
Built-in data types
anchor- primitives (
bool
,int
,float
,double
) - vectors (
vec2
,vec3
,vec4
) - matrices (
mat2
,mat3
,mat4
) - texture data (
sampler2D
)
Vector components
anchor- Vectors can be accessed by its properties, named
x
,y
,z
,w
. - Because the vector data type is also used for colors, the components can also be accessed via
r
,g
,b
,a
. - For accessing texture coordinates, there are also the aliases
s
,t
,p
,q
Vector swizzling
anchorYou create a new vector by "swizzling" the vector's components. Any combination of x
, y
, z
, w
is allowed.
vec3 a = vec3(1., 2., 3.);
vec2 b = a.yx; // vec2(2., 1.);
vec4 c = a.zxyx; // vec4(3., 1., 2., 1.);
Type conversion
anchor// int to float and back
int i = 3;
float f = float(i);
int j = int(f);
// vector initialization
vec3 v1 = vec3(1.); // vec3(1., 1., 1.)
vec4 v2 = vec4(v1, 2.); // vec4(1., 1., 1., 2.)
// vec2 to vec4
vec2 a = vec2(1.,2.);
vec4 b = vec4(a, 3., 4.); // vec4(1., 2., 3., 4.)
Special variable types
anchorattribute
– vertex data from WebGL buffers, only accessible in the vertex shader.uniform
– like global variables you pass in from the JavaScript side before executing the programvarying
– passing attributes from the vertex shader to the fragment shader and interpolate in between coordinates
Built-in inputs and outputs
anchorVertex Shader Inputs
anchorvec4 gl_Position
– output: transformed vertex position (unit: clip coordinates)float gl_PointSize
– output: transformed point size; only with drawMode POINTS (unit: pixels)
Fragment shader Inputs
anchorvec4 gl_FragCoord
– input: current pixel coordinate, available in the pixel shader.vec2 gl_PointCoord
contains the 2D coordinate from within a point from 0.0 to 1.0 (drawMode POINTS only)vec4 gl_FragColor
– output: current pixel color (unit: RGBA color)vec4 gl_FragData[n]
– output: fragment color for color attachment n, used with WEBGL_draw_buffers
Layout of a WebGL program
anchorVertex Shader
anchorprecision highp float;
attribute vec4 position;
varying vec4 vPos;
void main() {
vPos = position;
gl_Position = position;
}
Fragment Shader (or Pixel Shader)
anchorprecision highp float;
varying vec4 vPos;
void main() {
float red = 1.0 - length(vPos);
float green = 0.0;
float blue = 0.0;
float alpha = 1.0;
gl_FragColor = vec4(red, green, blue, alpha);
}
Useful built-in functions
anchorlength(x)
– return length of afloat
,vec2
,vec3
orvec4
distance(a, b)
– return distance between twofloat
,vec2
,vec3
orvec4
valuesstep(edge, x)
– return 0 ifx < edge
, otherwise 1smoothstep(edge0, edge1, x)
– likestep
but with smooth hermite interpolation betweenedge0
andedge1
.mix(x, y, a)
- linear interpolation betweenx
andy
witha = 0 .. 1
min(x, y)
– return the lesser valuemax(x, y)
– return the greater valueclamp(x, a, b)
– clamp the value between[a .. b]
- Trigonometric functions, eg.
sin(x)
,cos(x)
,atan(x)