thebookofshaders/11/README.md

104 lines
5.1 KiB
Markdown
Raw Normal View History

2015-03-19 21:06:41 +00:00
![NASA / WMAP science team](mcb.jpg)
2015-03-15 15:35:14 +00:00
## Noise
2015-04-02 19:54:09 +00:00
Break time! We have been playing with all this random functions that looks like TV white noise, our head is still spinning around thinking on shaders, and our eyes are tired. Time to get out side and walk!
2015-03-19 21:06:41 +00:00
2015-04-02 19:54:09 +00:00
We feel the air in our face, the sun in our nose and chicks. The world is such a vivid and rich places. Colors, textures, sounds. While we walk we can avoid notice the surface of the roads, rocks, trees and clouds. Suddenly all seams random. But definitely not the type of random we were making. The real world is such a rich place. How we can approximate to this level of variety computationally?
2015-03-19 21:06:41 +00:00
2015-04-02 19:54:09 +00:00
We are on the same path of thoughts that Ken Perlin's walk through on 1982 when he was commissioned with the job of generating "more realistic" textures for a new disney movie call "Tron". In response to that he came up with an elegant *oscar winner* noise algorithm.
2015-03-19 21:06:41 +00:00
2015-04-02 19:54:09 +00:00
The following is not the Perlin noise algorithm, but is a good starting point to start getting out head around the idea of how to generate *noise* o *smooth random*.
2015-03-19 21:06:41 +00:00
2015-04-02 19:54:09 +00:00
Look the following graph, is in essence what we where doing on line 36 and 37 on the last example of the previus chapter. We are computing the ```rand()``` of the integers of `x` (the `i` variable).
2015-03-19 21:06:41 +00:00
<div class="simpleFunction" data="
float i = floor(x); // integer
float f = fract(x); // fraction
y = rand(i);
//y = mix(rand(i), rand(i + 1.0), f);
//y = mix(rand(i), rand(i + 1.0), smoothstep(0.,1.,f));
"></div>
2015-04-02 19:54:09 +00:00
By uncommenting the following line, you can make a linear interpolation between the random value of an integer to the next one.
2015-03-19 21:06:41 +00:00
```glsl
y = mix(rand(i), rand(i + 1.0), f);
```
2015-04-02 19:54:09 +00:00
At this point we have learn that we can do better than a linear interpolation. Right? By uncommenting the following line, we will use the native ```smoothstep()``` to make a *smooth* transition between the previous random values.
2015-03-19 21:06:41 +00:00
```glsl
y = mix(rand(i), rand(i + 1.0), smoothstep(0.,1.,f));
```
You will find in other implementations that people use cubic curves ( like the following formula) instead of using a ```smoothstep()```.
```glsl
float u = f * f * (3.0 - 2.0 * f );
y = mix(rand(i), rand(i + 1.0), u);
```
## 2D Noise
2015-04-02 19:54:09 +00:00
Now that we understand how noise is made in one demotion, is time to port it to two dimensions. Check how on the following code the interpolation (line 29) is made between the for corners of a square (lines 22-25).
2015-03-19 21:06:41 +00:00
<div class="codeAndCanvas" data="2d-noise.frag"></div>
2015-04-02 19:54:09 +00:00
At this point you can recognize what's going on at line 40. We are scaling and "moving" the space, right?
2015-03-19 21:06:41 +00:00
Try:
* Change the multiplier. Try to animate it.
2015-04-02 19:54:09 +00:00
2015-03-19 21:06:41 +00:00
* At what level of zoom the noise start looking like random again?
2015-04-02 19:54:09 +00:00
2015-03-19 21:06:41 +00:00
* At what zoom level the noise is imperceptible.
2015-04-02 19:54:09 +00:00
2015-03-19 21:06:41 +00:00
* Change the u_time by the normalize values of the mouse coordinates.
2015-04-02 19:54:09 +00:00
* Now that you achieve some control over noise & chaos, is time to mix it with previous knowledge. Making a composition of rectangles, colors and noise that resemble some of the complexity of the texture of the following painting made by [Mark Rothko](http://en.wikipedia.org/wiki/Mark_Rothko).
![Mark Rothko - Three (1950)](rothko.jpg)
2015-03-19 21:06:41 +00:00
## Using 2D Noise
2015-04-02 19:54:09 +00:00
As we saw, noise was designed to give a natural *je ne sais quoi* to digital textures, and could be use to make convincing generative textures. Lets refresh some of the previous knowledge and then jump forward learning how to mix all the knowledge we have learn so far.
In the following code you will find:
2015-03-19 21:06:41 +00:00
* Shaping functions to create: random (line 13), noise (line 32) and a line pattern (line 46).
* A color gradient (line 68).
* A matrix to rotate the line pattern (line 37-40 and 45)
* A 2d random function (line 12-16)
* A 2d noise function (line 20-35)
<div class="codeAndCanvas" data="wood.frag"></div>
By uncommenting line 60 you will see the line pattern we are talking about.
2015-04-02 19:54:09 +00:00
Revealing line 63 you can see how we can alternate this pattern in a "natural-like" way, to then finally, uncommenting line 66 we can stretch the noise pattern in *y* axis.
2015-03-19 21:06:41 +00:00
Wow! You really should be proud of your self. He have went from nothing to generating our own wood procedural texture!
Now is time for you to play with it:
* What do you thing it will happen if the patter looks like this:
```glsl
pattern = sin(lines(pos, noise(pos*vec2(2.,0.5)),0.5)*3.14);
```
Or like this
```glsl
pattern = fract(lines(pos, noise(pos*vec2(2.,0.5)),0.5)*2.0);
```
2015-04-02 19:54:09 +00:00
* What other generative pattern can you make? What about granite? marble? magma? water?
* What about noise apply to motion? Go back to the Matrix chapter and use the translation example that move a the "+" around to apply some *random* and *noise* movements to it.
2015-03-15 15:35:14 +00:00
2015-03-19 21:06:41 +00:00
Nothing like having some control over the chaos and chances. Right?
2015-04-02 19:54:09 +00:00
Noise is one of those subjects that you can dig and always find new exciting formulas. In fact, noise means different things for different people. Musicians will think in audio noise, communicators into interference, and astrophysics on cosmic microwave background. On the next chapter we will use some related concepts from sign and audio behavior to our noise function to explore more uses of noise.
2015-03-15 15:35:14 +00:00