thebookofshaders/03
2016-03-02 11:19:12 -03:00
..
08.png first coomit 2015-03-15 11:21:47 -04:00
index.php adding files and main README-JP.md 2015-08-05 08:39:27 -03:00
README-ch.md fix typo 2015-12-31 23:26:30 +08:00
README-es.md Translated the first 7 chapters to spanish 2016-03-02 11:19:12 -03:00
README-jp.md minor tweaks to enforce writing conventions 2015-12-27 15:35:04 -08:00
README-kr.md chapter 3 and 4 korean translation 2016-02-05 20:07:20 -08:00
README.md relative paths as variables, glossary querry, code style 2015-07-17 16:24:23 -04:00
space.frag first coomit 2015-03-15 11:21:47 -04:00
time.frag Fixed some spelling and syntax. 2015-03-25 21:58:31 +01:00
TODO.md notes 2015-06-20 07:31:09 -04:00

Uniforms

So far we have seen how the GPU manages large numbers of parallel threads, each one responsible for assigning the color to a fraction of the total image. Although each parallel thread is blind to the others, we need to be able to send some inputs from the CPU to all the threads. Because of the architecture of the graphic card those inputs are going to be equal (uniform) to all the threads and necessarily set as read only. In other words, each thread receives the same data which it can read but cannot change.

These inputs are call uniform and come in most of the supported types: float, vec2, vec3, vec4, mat2, mat3, mat4, sampler2D and samplerCube. Uniforms are defined with the corresponding type at the top of the shader right after assigning the default floating point precision.

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution; // Canvas size (width,height)
uniform vec2 u_mouse;      // mouse position in screen pixels
uniform float u_time;	  // Time in seconds since load 

You can picture the uniforms like little bridges between the CPU and the GPU. The names will vary from implementation to implementation but in this series of examples Im always passing: u_time (time in seconds since the shader started), u_resolution (billboard size where the shader is being drawn) and u_mouse (mouse position inside the billboard in pixels). Im following the convention of putting u_ before the uniform name to be explicit about the nature of this variable but you will find all kinds of names for uniforms. For example ShaderToy.com uses the same uniforms but with the following names:

uniform vec3 iResolution;   // viewport resolution (in pixels)
uniform vec4 iMouse;        // mouse pixel coords. xy: current, zw: click
uniform float iGlobalTime;  // shader playback time (in seconds)

Enough talking, let's see the uniforms in action. In the following code we use u_time - the number of seconds since the shader started running - together with a sine function to animate the transition of the amount of red in the billboard.

As you can see GLSL has more surprises. The GPU has hardware accelerated angle, trigonometric and exponential functions. Some of those functions are: sin(), cos(), tan(), asin(), acos(), atan(), pow(), exp(), log(), sqrt(), abs(), sign(), floor(), ceil(), fract(), mod(), min(), max() and clamp().

Now it is time again to play with the above code.

  • Slow down the frequency until the color change becomes almost imperceptible.

  • Speed it up until you see a single color without flickering.

  • Play with the three channels (RGB) in different frequencies to get interesting patterns and behaviors.

gl_FragCoord

In the same way GLSL gives us a default output, vec4 gl_FragColor, it also gives us a default input, vec4 gl_FragCoord, which holds the screen coordinates of the pixel or screen fragment that the active thread is working on. With vec4 gl_FragCoord, we know where a thread is working inside the billboard. In this case we don't call it uniform because it will be different from thread to thread, instead gl_FragCoord is called a varying.

In the above code we normalize the coordinate of the fragment by dividing it by the total resolution of the billboard. By doing this the values will go between 0.0 and 1.0, which makes it easy to map the X and Y values to the RED and GREEN channel.

In shader-land we dont have too many resources for debugging besides assigning strong colors to variables and trying to make sense of them. You will discover that sometimes coding in GLSL is very similar to putting ships inside bottles. Is equally hard, beautiful and gratifying.

Now it is time to try and challenge our understanding of this code.

  • Can you tell where the coordinate (0.0,0.0) is in our canvas?

  • What about (1.0,0.0), (0.0,1.0), (0.5,0.5) and (1.0,1.0)?

  • Can you figure out how to use u_mouse knowing that the values are in pixels and NOT normalized values? Can you use it to move colors around?

  • Can you imagine an interesting way of changing this color pattern using u_time and u_mouse coordinates?

After doing these exercises you might wonder where else you can try your new shader-powers. In the following chapter we will see how to make your own shader tools in three.js, Processing, and openFrameworks.