In the last class, we hinted briefly at how we can manipulate the texture coordinates in the fragment shader when we looked at textureWrap() options. Remember that texture coordinates are just two numbers, a vec2, so there is lots we can do with them in the fragment shader to get interesting effects, sometimes even without using an actual texture to sample.
Quantization
We can use the webcam as our input texture to add a bit of motion to our sketch. This is as simple as setting up the webcam with createCapture(), and passing the webcam object as a uniform sampler2D to our shader. Note that we are not passing the entire webcam object into the shader; p5.js automatically extracts the current frame of video and passes it as a texture to the shader.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
/* eslint-disable no-undef, no-unused-vars */
let mapShader;
let webcam;
function preload() {
mapShader = loadShader(shaderUrl(sketchFile0), shaderUrl(sketchFile1));
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
webcam = createCapture(VIDEO);
webcam.size(1280, 720);
webcam.hide();
}
function draw() {
background("#292f33");
noStroke();
mapShader.setUniform("uResolution", [width, height]);
mapShader.setUniform("uTexMap", webcam);
shader(mapShader);
beginShape(TRIANGLE_STRIP);
vertex(0, 0, 0, 0, 0);
vertex(width, 0, 0, 1, 0);
vertex(0, height, 0, 0, 1);
vertex(width, height, 0, 1, 1);
endShape();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
import "p5";
import sketchFile0 from "./map.vert";
import sketchFile1 from "./map.frag";
function shaderUrl(source) {
return "data:text/plain;charset=utf-8," + encodeURIComponent(source);
}
Object.assign(window, { preload, setup, draw, windowResized });
|
The first effect we will try is to pixelate the image. There are a couple of ways to do this:
- The simple way is to sample the same point in the video for many neighboring points on screen.
- The harder way is to average many neighboring points in the video, and set the average color to the corresponding points on screen.
Let’s attempt the simple version. This concept of limiting values to a smaller defined set is called quantizing. If we think about this in pseudo-code, we want to map a cluster of texture coordinates to one value, and then sample the video texture using this single texture coordinate. One way to do this is to take away some precision in the texture coordinates, so that a sequence of them end up being equal. Take a look at the following example, where we will only look at 1 decimal place and discard everything smaller.
0.0134 => 0.0
0.0232 => 0.0
0.0754 => 0.0
0.1000 => 0.1
0.1564 => 0.1
0.2999 => 0.2
0.3119 => 0.3
0.3487 => 0.3
If we use the 1 decimal place version as our texture coordinate, we will end up with the same value for all coordinates 0.0 to 0.1, 0.1 to 0.2, 0.2 to 0.3, etc.
In GLSL, the mod() operator can help us with this calculation. mod() is similar to the modulo % operator, except it also works with floats (not just integers)! mod() will give us the decimal places we want to ignore. We just need to subtract those from the original texture coordinate to get our modified version.
mod(0.0134, 0.1) = 0.0134
mod(0.0232, 0.1) = 0.0232
mod(0.0754, 0.1) = 0.0754
mod(0.1000, 0.1) = 0.0000
mod(0.1564, 0.1) = 0.0564
mod(0.2999, 0.1) = 0.0999
mod(0.3119, 0.1) = 0.0119
mod(0.3487, 0.1) = 0.0487
//
0.0134 - 0.0134 = 0.0
0.0232 - 0.0232 = 0.0
0.0754 - 0.0754 = 0.0
0.1000 - 0.0000 = 0.1
0.1564 - 0.0564 = 0.1
0.2999 - 0.0999 = 0.2
0.3119 - 0.0119 = 0.3
0.3487 - 0.0487 = 0.3
Here it is in action in the fragment shader. This sketch uses createSlider() to add an HTML slider to the page, used to tweak the size of the pixel in the program. Note that our texture coordinates are normalized, so this value must be in the range [0.0, 1.0].
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
// Custom uniforms.
uniform sampler2D uTexMap;
uniform vec2 uDimensions;
uniform float uPixelate;
void main()
{
vec2 texCoord;
if (uPixelate > 0.0)
{
// Sample the middle of the quantized area
texCoord = vTexCoord - mod(vTexCoord, uPixelate) + uPixelate * 0.5;
}
else
{
texCoord = vTexCoord;
}
vec4 texColor = texture2D(uTexMap, texCoord);
gl_FragColor = texColor;
}
|
We can selectively pixelate parts of the image by calculating different pixelate values for different fragments. To do this, we can generate a mask over the image and use the mask to determine how much to pixelate a fragment: a black 0.0 value means don’t pixelate at all, a white 1.0 value means pixelate at the maximum level, and anything in between is some value in between, relative to the gray level of the mask.
The following sketch uses the mouse position to set the center of the mask, and an HTML slider to set its size. These parameters are converted from p5.js space to normalized space in the vertex shader, then passed to the fragment shader using a varying variable. This calculation could have been made in the fragment shader, but the shader will run faster if it’s done in the vertex shader because there are a lot less vertices than fragments to process.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
varying vec3 vLightParams;
// Custom uniforms.
uniform sampler2D uTexMap;
void main()
{
// Get the distance between the pixel and the light position.
float lightDist = distance(vTexCoord, vLightParams.xy);
// Convert the distance to a [0,1] gradient.
float luminance = 1.0 - smoothstep(0.0, vLightParams.z, lightDist);
vec4 texColor = texture2D(uTexMap, vTexCoord);
gl_FragColor = vec4(texColor.rgb * luminance, texColor.a);
}
|
The fragment shader uses the distance() function to calculate the distance between the mask center and the texture coordinate, and the smoothstep() function to get a gradient value between 0.0 and 1.0.
0.0 means the texture coordinate is right on the mask center point.
- This value increases until it reaches
1.0 on the edge of the mask.
- A value above
1.0 means the fragment is out of bounds, which we don’t need to worry about for this particular program.
- We want the closest point to have the effect at its fullest, so we invert the mask with
1.0 - val.
If we make the radius small enough and move the mouse to the corners, you will notice that the mask center does not quite correspond to the mouse cursor. We normalized the mouse coordinates using uResolution as the range, but this is incorrect. uResolution corresponds to the entire sketch window, not just the rectangle we are rendering using the shader. An easy fix for this is to remap the mouse coordinates when we pass them in as a uniform.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
/* eslint-disable no-undef, no-unused-vars */
let webcam;
let xformShader;
let fxSlider;
function preload() {
xformShader = loadShader(shaderUrl(sketchFile0), shaderUrl(sketchFile1));
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
webcam = createCapture(VIDEO);
webcam.size(1280, 720);
webcam.hide();
fxSlider = createSlider(0, 100, 50);
fxSlider.position(10, 10);
fxSlider.style("width", width - 40 + "px");
}
function draw() {
background("#292f33");
noStroke();
const paddingX = width * 0.1;
const paddingY = height * 0.1;
const left = paddingX;
const top = paddingY;
const right = width - paddingX;
const bottom = height - paddingY;
xformShader.setUniform("uResolution", [width, height]);
xformShader.setUniform("uTexMap", webcam);
xformShader.setUniform("uDimensions", [webcam.width, webcam.height]);
xformShader.setUniform("uLightPos", [
map(mouseX, left, right, 0, width),
map(mouseY, top, bottom, 0, height),
]);
xformShader.setUniform("uLightRadius", fxSlider.value() / 100.0);
shader(xformShader);
beginShape(TRIANGLE_STRIP);
vertex(left, top, 0, 0, 0);
vertex(right, top, 0, 1, 0);
vertex(left, bottom, 0, 0, 1);
vertex(right, bottom, 0, 1, 1);
endShape();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
fxSlider.style("width", width - 20 + "px");
}
import "p5";
import sketchFile0 from "./xform.vert";
import sketchFile1 from "./xform.frag";
function shaderUrl(source) {
return "data:text/plain;charset=utf-8," + encodeURIComponent(source);
}
Object.assign(window, { preload, setup, draw, windowResized });
|
Now that we have an accurate mask, we can use its value to set a pixelation value between 0.0 and uPixelate. An easy way to do this is to use a similar algorithm to the map() function in p5.js. However, there is no equivalent built into GLSL, so we need to write it ourselves.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
varying vec3 vLightParams;
// Custom uniforms.
uniform sampler2D uTexMap;
uniform float uPixelate;
float map(float inVal, float inMin, float inMax, float outMin, float outMax)
{
return ((inVal - inMin) / (inMax - inMin) * (outMax - outMin) + outMin);
}
void main()
{
// Get the distance between the fragment and the light position.
float lightDist = distance(vTexCoord, vLightParams.xy);
// Convert the distance to a [0,1] gradient.
float luminance = 1.0 - smoothstep(0.0, vLightParams.z, lightDist);
// Map the pixelation value using the distance gradient.
float pixelate = map(luminance, 0.0, 1.0, 0.0, uPixelate);
vec2 pixCoord;
if (pixelate > 0.0)
{
// Sample the texture at the pixelated texture coord.
pixCoord = vTexCoord - mod(vTexCoord, pixelate);
}
else
{
// Sample the texture at the original texture coord.
pixCoord = vTexCoord;
}
vec4 texColor = texture2D(uTexMap, pixCoord);
gl_FragColor = texColor;
}
|
While this looks interesting, it’s not exactly what we were aiming for. It looks like the pixelation is following a circular pattern, which makes sense considering our distance calculations and mask are both circular in shape as well.
The trick here is to use the quantized texture coordinate in the distance calculation. This will make our distance values also increment in steps and our pixelated blocks will be squares.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
varying vec3 vLightParams;
// Custom uniforms.
uniform sampler2D uTexMap;
uniform float uPixelate;
float map(float inVal, float inMin, float inMax, float outMin, float outMax)
{
return ((inVal - inMin) / (inMax - inMin) * (outMax - outMin) + outMin);
}
void main()
{
// Get the distance between the pixel and the light position.
vec2 lightCoord = vTexCoord - mod(vTexCoord, uPixelate);
float lightDist = distance(lightCoord, vLightParams.xy);
// Convert the distance to a [0,1] gradient.
float luminance = smoothstep(0.0, vLightParams.z, lightDist);
// Map the pixelation value using the distance gradient.
float pixelate = map(luminance, 1.0, 0.0, 0.0, uPixelate);
vec2 pixCoord;
if (pixelate > 0.0)
{
// Sample the texture at the pixelated texture coord.
pixCoord = vTexCoord - mod(vTexCoord, pixelate);
}
else
{
// Sample the texture at the original texture coord.
pixCoord = vTexCoord;
}
vec4 texColor = texture2D(uTexMap, pixCoord);
gl_FragColor = texColor;
// Uncomment the following line to debug the pixelation amount.
//gl_FragColor = vec4(lightCoord, luminance, 1.0);
}
|
Randomness
When used purposefully, adding randomness to a sketch can lead to very interesting results. While some form of random() function exists in most programming languages, there is no implementation in GLSL. However, a quick Google search will return many results with the following:
1
2
3
4
|
float random(vec2 st)
{
return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43758.5453123);
}
|
This is a standard implementation that is used in many GLSL programs. This function and the numbers used are well explained and visualized in the Book of Shaders, but what is essentially happening is that we are taking a cyclical value (a sin() wave), multiplying it by a large value to get more varied results, and then keeping only the decimal part of the large number using fract().
If we just draw this value out in our fragment, our image looks like static.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
float random(vec2 st)
{
return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43758.5453123);
}
void main()
{
float randVal = random(vTexCoord);
gl_FragColor = vec4(vec3(randVal), 1.0);
}
|
We can use our quantizing mod() trick here to enlarge our cells. This reveals that each call to random() returns a gray value between 0.0 black and 1.0 white. Note also that calling random() with the same parameter (in this case our quantized texture coordinate) returns the same value. This is why this is called a pseudo-random function; it looks like a random number generator but it is actually deterministic (predictable).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
float random(vec2 st)
{
return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43758.5453123);
}
void main()
{
vec2 texCoord = vTexCoord - mod(vTexCoord, 0.05);
float randVal = random(texCoord);
gl_FragColor = vec4(vec3(randVal), 1.0);
}
|
Let’s add an elapsed time uniform to the shader to bring in animation. We will use the time to change the random() parameter for each frame rendered. Using the time value directly is too chaotic and fast, it needs to be slowed down to give us a good looking result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
// Custom uniforms.
uniform float uTime;
float random(vec2 st)
{
return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43758.5453123);
}
void main()
{
vec2 texCoord = vTexCoord - mod(vTexCoord + uTime * 0.000000001, 0.1);
float randVal = random(texCoord);
gl_FragColor = vec4(vec3(randVal), 1.0);
}
|
This value can then be used like a regular input in our shader. Here is a simple example where the random value is used as a threshold to draw a checkerboard video.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
// Custom uniforms.
uniform sampler2D uTexMap;
uniform float uTime;
float random(vec2 st)
{
return fract(sin(dot(st, vec2(12.9898, 78.233))) * 43758.5453123);
}
void main()
{
vec2 rCoord = vTexCoord - mod(vTexCoord + uTime * 0.000000001, 0.1);
float randVal = random(rCoord);
if (randVal > 0.5)
{
vec4 texCol = texture2D(uTexMap, vTexCoord);
gl_FragColor = texCol;
}
else
{
vec2 pCoord = vTexCoord - mod(vTexCoord, 0.1);
vec4 texCol = texture2D(uTexMap, pCoord);
gl_FragColor = texCol;
}
}
|
Other useful random number generators are noise() functions. Unlike random(), noise generators are continuous over the domain. This means that if we call a noise() function with arguments that are close to each other, the results will also be close to each other. This is well illustrated by this openFrameworks example:
There are many different types of noise generators, with the most famous one being Perlin noise. Once again, this is well explained and illustrated in the Book of Shaders and is well worth a read.
Although some noise() functions are implemented in OpenGL Core, they are not available in OpenGL ES or WebGL, so we must include our own custom versions.
We can add a simplex noise implementation to our fragment shader and draw the value out to see what it looks like. We don’t really need to worry about how the noise function works, just remember that we call it with snoise() and pass in a vec2 as a parameter. The result will be a number between -1 and 1.
In this example, we are using two uniform variables to scale our result:
uNoiseScale modifies how big of a step we take when sampling the noise function. A bigger step will give us more variance (more ups and downs), and a smaller step will give us more resolution.
uOffsetScale modifies the result of the noise sampling. A bigger value will give us more variance between the peaks and troughs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
// Custom uniforms.
uniform float uNoiseScale;
uniform float uOffsetScale;
// Some useful functions
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec2 mod289(vec2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec3 permute(vec3 x) { return mod289(((x*34.0)+1.0)*x); }
//
// Description : GLSL 2D simplex noise function
// Author : Ian McEwan, Ashima Arts
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License :
// Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
float snoise(vec2 v) {
// Precompute values for skewed triangular grid
const vec4 C = vec4(0.211324865405187,
// (3.0-sqrt(3.0))/6.0
0.366025403784439,
// 0.5*(sqrt(3.0)-1.0)
-0.577350269189626,
// -1.0 + 2.0 * C.x
0.024390243902439);
// 1.0 / 41.0
// First corner (x0)
vec2 i = floor(v + dot(v, C.yy));
vec2 x0 = v - i + dot(i, C.xx);
// Other two corners (x1, x2)
vec2 i1 = vec2(0.0);
i1 = (x0.x > x0.y)? vec2(1.0, 0.0):vec2(0.0, 1.0);
vec2 x1 = x0.xy + C.xx - i1;
vec2 x2 = x0.xy + C.zz;
// Do some permutations to avoid
// truncation effects in permutation
i = mod289(i);
vec3 p = permute(
permute( i.y + vec3(0.0, i1.y, 1.0))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(
dot(x0,x0),
dot(x1,x1),
dot(x2,x2)
), 0.0);
m = m*m ;
m = m*m ;
// Gradients:
// 41 pts uniformly over a line, mapped onto a diamond
// The ring size 17*17 = 289 is close to a multiple
// of 41 (41*7 = 287)
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt(a0*a0 + h*h);
m *= 1.79284291400159 - 0.85373472095314 * (a0*a0+h*h);
// Compute final noise value at P
vec3 g = vec3(0.0);
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * vec2(x1.x,x2.x) + h.yz * vec2(x1.y,x2.y);
return 130.0 * dot(m, g);
}
void main()
{
// Calculate noise value.
float noiseVal = snoise(vTexCoord * uNoiseScale);
float offsetVal = noiseVal * uOffsetScale;
gl_FragColor = vec4(vec3(offsetVal), 1.0);
}
|
Let’s bring the webcam texture back in, and use the noise to offset our texture coordinate when sampling the video. Note that we have greatly reduced the range of uNoiseScale in this example to avoid getting too much tiling of the image.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
// Custom uniforms.
uniform sampler2D uTexMap;
uniform float uNoiseScale;
uniform float uOffsetScale;
// Some useful functions
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec2 mod289(vec2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec3 permute(vec3 x) { return mod289(((x*34.0)+1.0)*x); }
//
// Description : GLSL 2D simplex noise function
// Author : Ian McEwan, Ashima Arts
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License :
// Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
float snoise(vec2 v) {
// Precompute values for skewed triangular grid
const vec4 C = vec4(0.211324865405187,
// (3.0-sqrt(3.0))/6.0
0.366025403784439,
// 0.5*(sqrt(3.0)-1.0)
-0.577350269189626,
// -1.0 + 2.0 * C.x
0.024390243902439);
// 1.0 / 41.0
// First corner (x0)
vec2 i = floor(v + dot(v, C.yy));
vec2 x0 = v - i + dot(i, C.xx);
// Other two corners (x1, x2)
vec2 i1 = vec2(0.0);
i1 = (x0.x > x0.y)? vec2(1.0, 0.0):vec2(0.0, 1.0);
vec2 x1 = x0.xy + C.xx - i1;
vec2 x2 = x0.xy + C.zz;
// Do some permutations to avoid
// truncation effects in permutation
i = mod289(i);
vec3 p = permute(
permute( i.y + vec3(0.0, i1.y, 1.0))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(
dot(x0,x0),
dot(x1,x1),
dot(x2,x2)
), 0.0);
m = m*m ;
m = m*m ;
// Gradients:
// 41 pts uniformly over a line, mapped onto a diamond
// The ring size 17*17 = 289 is close to a multiple
// of 41 (41*7 = 287)
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt(a0*a0 + h*h);
m *= 1.79284291400159 - 0.85373472095314 * (a0*a0+h*h);
// Compute final noise value at P
vec3 g = vec3(0.0);
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * vec2(x1.x,x2.x) + h.yz * vec2(x1.y,x2.y);
return 130.0 * dot(m, g);
}
void main()
{
// Calculate texture coord offset.
float noiseVal = snoise(vTexCoord + uNoiseScale * 10.0);
float offsetVal = noiseVal * uOffsetScale;
vec2 texCoord = vTexCoord + vec2(offsetVal);
vec4 texColor = texture2D(uTexMap, texCoord);
gl_FragColor = texColor;
// Uncomment the line below to debug the generated noise.
//gl_FragColor = vec4(offsetVal * 10.0, 0.0, 0.0, 1.0);
}
|
Here again we can bring in the time element to animate the effect. Adding the time to the snoise() offset results in a wobbly video effect. If we uncomment the last line of the fragment shader, we can see how all the parameters affect the sampled noise value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#ifdef GL_ES
precision mediump float;
#endif
// Passed attributes.
varying vec2 vTexCoord;
// Custom uniforms.
uniform sampler2D uTexMap;
uniform float uNoiseScale;
uniform float uOffsetScale;
uniform float uTime;
// Some useful functions
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec2 mod289(vec2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec3 permute(vec3 x) { return mod289(((x*34.0)+1.0)*x); }
//
// Description : GLSL 2D simplex noise function
// Author : Ian McEwan, Ashima Arts
// Maintainer : ijm
// Lastmod : 20110822 (ijm)
// License :
// Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
float snoise(vec2 v) {
// Precompute values for skewed triangular grid
const vec4 C = vec4(0.211324865405187,
// (3.0-sqrt(3.0))/6.0
0.366025403784439,
// 0.5*(sqrt(3.0)-1.0)
-0.577350269189626,
// -1.0 + 2.0 * C.x
0.024390243902439);
// 1.0 / 41.0
// First corner (x0)
vec2 i = floor(v + dot(v, C.yy));
vec2 x0 = v - i + dot(i, C.xx);
// Other two corners (x1, x2)
vec2 i1 = vec2(0.0);
i1 = (x0.x > x0.y)? vec2(1.0, 0.0):vec2(0.0, 1.0);
vec2 x1 = x0.xy + C.xx - i1;
vec2 x2 = x0.xy + C.zz;
// Do some permutations to avoid
// truncation effects in permutation
i = mod289(i);
vec3 p = permute(
permute( i.y + vec3(0.0, i1.y, 1.0))
+ i.x + vec3(0.0, i1.x, 1.0 ));
vec3 m = max(0.5 - vec3(
dot(x0,x0),
dot(x1,x1),
dot(x2,x2)
), 0.0);
m = m*m ;
m = m*m ;
// Gradients:
// 41 pts uniformly over a line, mapped onto a diamond
// The ring size 17*17 = 289 is close to a multiple
// of 41 (41*7 = 287)
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
// Normalise gradients implicitly by scaling m
// Approximation of: m *= inversesqrt(a0*a0 + h*h);
m *= 1.79284291400159 - 0.85373472095314 * (a0*a0+h*h);
// Compute final noise value at P
vec3 g = vec3(0.0);
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * vec2(x1.x,x2.x) + h.yz * vec2(x1.y,x2.y);
return 130.0 * dot(m, g);
}
void main()
{
// Calculate texture coord offset.
float noiseVal = snoise(vTexCoord + uTime * uNoiseScale);
float offsetVal = noiseVal * uOffsetScale;
vec2 texCoord = vTexCoord + vec2(offsetVal);
vec4 texColor = texture2D(uTexMap, texCoord);
gl_FragColor = texColor;
// Uncomment the line below to debug the generated noise.
//gl_FragColor = vec4(vec3(offsetVal * 10.0), 1.0);
}
|