Colors
Position
//define vectors
vec2 position = vec2(0.5, 0.5);
vec3 color = vec3(0.0, 0.5, 1.0);
vec4 colorWithAlpha = vec4(0.0, 0.0, 1.0, 0.5);
// Swizzling: access numbers inside vectors
float red = color.r;
float green = color.g;
float blue = color.b;
Explanation of Swizzling from OpenGL wiki
Additionally, there are 3 sets of swizzle masks.
You can use xyzw, rgba (for colors), or stpq (for texture coordinates).
These three sets have no actual difference; they're just syntactic sugar.
You cannot combine names from different sets in a single swizzle operation.
So ".xrs" is not a valid swizzle mask.
const float PI = 3.141592653;
// or
#define PI 3.141592653;
// vertex data aPosition from p5 webgl
attribute vec3 aPosition;
void main() {
// copy the position data into a vec4
// we're using 1.0 as the w component (which controls scaling/normalization of the coordinates)
vec4 positionVec4 = vec4(aPosition, 1.0);
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
// send the vertex information on to the fragment shader
gl_Position = positionVec4;
}
Uniforms
are constant variables.Const
is constant per frame, so it can be updated between draw calls.Uniforms
can be accessed by all of the parallel threads in our GPU.Uniform
.uniform
to pass from p5 to shaders are time, resolution, mouse coordinates.u
or u_
.theShader.setUniform("u_resolution", [width, height]);
theShader.setUniform("u_time", millis() / 1000.0);
theShader.setUniform("u_mouse", [mouseX, map(mouseY, 0, height, height, 0)]); // flip Y to orient properly in shader
uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
Varying
is per-fragment / per-pixel parameter.Uniform
is same for all pixels.varying
to pass texture coordinates from p5 webgl (through vertex shader) to fragment shader.v
or v_
.vertex code
attribute vec2 aTexCoord; // texture coord from p5/webgl
varying vec2 vTexCoord; // create varying vector
void main() {
vTexCoord = aTexCoord;
}
fragement code
varying vec2 vTexCoord; // grab var from vertex shader
void main() {
vec2 uv = vTexCoord; // set texture coord to uv coord
}
What is TexCoord(texture coordinate), FragCoord(fragment coordinate)?
Think about your shader as a stretchy cloth and these coord variables as a series of pins that hold the corners tight.
Depending on the location and spacing of these pins your shader can be stretched, distorted, or duplicated in different ways.
TexCoord is calculated in the vertex and passed from the vertex to the fragment shader, and then the texture coordinates of our shader are set to be equal to TexCoord.
TexCoord in .vert file
attribute vec2 aTexCoord; // cpu/p5 to vert file
varying vec2 vTexCoord; // to move texcoords from vert to frag shader
void main() {
//copy the texture coordinates
vTexCoord = aTexCoord;
}
TexCoord in .frag file
// receive vTexCoord from vertex shader
// ranges from 0.0 to 1.0
varying vec2 vTexCoord;
// use a value to access every pixel on the screen
vec2 st = vTexCoord;
FragCoord in .frag file
void main (void) {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
}