GLSL Syntax
Further information for people interesting in writing GLSL
Additional shader example for understanding syntax
#ifdef GL_ES
precision highp float;
#endif
IN vec2 texCoord;
// Uniform variables:
// bridge between CPU - GPU that allows mapping external information
// For example: time
uniform float uTime;
{{MODULES_HEAD}}
void main()
{
/* Declaration of variables
type name = value; */
// Real numbers
float number_0 = 1.0; // with dot
int number_1 = 1;
// Vectors
// 2 dimensions [_ ,_ ]
vec2 v2_0 = vec2(0.,1.);
vec2 v2_1 = vec2(number_0,number_1);
//3 dimensions [_ ,_ ,_ ]
vec3 v3_0 = vec3(0.,0.5,1.);
vec3 v3_1 = vec3(v2_0,1.);
vec3 v3_2 = vec3(v2_0,number_0);
//4 dimensions [_ ,_ ,_ ,_ ]
vec4 v4_0 = vec4(0.,0.25,0.5,1.);
vec4 v4_1 = vec4(v2_0,v2_1);
vec4 v4_2 = vec4(v3_0,0.);
// Referencing vectors information
// [r, g, b, a] || [x , y , z , w]
float numero_2 = v4_0.r; // is equivalent to v4_0.x
vec2 v2_2 = v4_0.rg; // is equivalent to v4_0.xy
vec3 v3_3 = v4_0.gbr; // is equivalent to v4_0.yzx
gl_FragColor = vec4(0.5); // [ (r, g ,b , a) ] with values in between 0. y 1.
}
Functions to explore
Complete list of functions:
FUNCTIONS
Trigonometry
cos(x)
sin(x)
tan(x)
Power pow(x, 0.5) == sqrt(x)
pow(x, 1.)
pow(x, 2.)
pow(x, n) for any n:=number
sqrt(x): square root
inversesqrt(x) 1./sqrt(x)
Exponentials
exp(x) : base 10
exp2(x): base 2
Logarithms
log(x) : base 10
log2(x) : base 2
Geometry
length(x): length of a vector in the Euclidean norm.
distance(x,y): distance between x and y
dot(x,y): inner product between x and y (scalar product)
Other
abs(x) : absolute value of x. E.g. abs(1)=1, abs(-1)=1
sign(x) : sign of x. E.g. sign(1)=1 sign(-1)=0
floor(x) : the nearest whole number that is less than x. Ex: floor(3.14) = 3
ceiling(x):the nearest integer that is greater than x. Example: ceiling(3.14) = 4
fract(x): the fractional part of x. Example: fract(3.14)= .14;
min(a,b): minimum value between a and b
max(a,b): minimum value between a and b
clamp(x, a, b): if x
is less than a
returns a, if x
is greater than b
returns b
, otherwise returns x
.
mod(x, y): returns the remainder of the division of x divided by
y`
mix(x, y, a): x*(1.-a)+ya
step(x, a): returns 0 if x <= a, and returns 1 if x > a.
smoothstep(x,a,b): returns 0 if x<a, interpolates between 0 and 1 if a<x<=b, returns 1 if x > b.
Example for use in function explorer:
float y = cos(cos(cos(x
x+u_time*.1),1.5)*10.-u_time);
Last updated