introduction

3 - Basic gradients

Basic examples of gradients

Hello, creative coders! 🎨💻 In this virtual work, we're going to dive into the world of GLSL (OpenGL Shading Language) and explore how to create stunning gradients using this powerful language. Get ready to add some vibrant and dynamic flair to your shader art!

Before we get started, let's quickly review what GLSL is all about. GLSL is a high-level shading language used for programming graphics processors (GPUs). It allows you to write shaders, which are small programs that run on the GPU and determine how pixels are rendered on the screen. Shaders are essential for creating real-time graphics, visual effects, and procedural art.

Now, let's talk about gradients. Gradients are smooth transitions between two or more colors, and they can add depth, atmosphere, and visual interest to your shader art. In GLSL, we can create gradients by leveraging the built-in functions and variables that control color and position.

Let's focus on the linear gradient for now: a linear gradient is a smooth transition between two colors along a straight line. To create a linear gradient, we can use the built-in GLSL variable gl_FragCoord.xy, which represents the current pixel's coordinates.

Here's an example of a GLSL shader that creates a linear gradient:

             #ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution; // Resolution of the canvas

void main() {
    vec2 v = gl_FragCoord.xy/u_resolution.xy; // Normalized pixel coordinates
    vec3 color = vec3(v.x, 1.0 - v.x, 0.0); // Horizontal gradient from green to red
    gl_FragColor = vec4(color, 1.0); // Set the output color
}

In this shader, we first normalize the pixel coordinates gl_FragCoord.xy by dividing them by the canvas resolution u_resolution. This gives us a value between 0 and 1 for both the x and y coordinates. Then, we use the normalized x coordinate v.x to create a linear gradient that transitions from green (0.0, 1.0, 0.0) to red (1.0, 0.0, 0.0) as we move from left to right.

Of course, this is just the beginning! You can experiment with different color combinations, gradient directions, and even combine multiple gradients to create more complex patterns. The possibilities are endless! 🌈

For instance, to create a vertical gradient from yellow to magenta instead of the horizontal one just showed, we can use the normalized y coordinate v.y instead of v.x. Here's the updated code:

             void main() {
    vec2 v = gl_FragCoord.xy/u_resolution.xy; // Normalized pixel coordinates
    vec3 color = vec3(v.x, 1.0 - v.x, 0.0); // Horizontal gradient from green to red
    vec3 color = vec3(1.0, v.y, 1.0 - v.y); // Vertical gradient from yellow to magenta
    gl_FragColor = vec4(color, 1.0); // Set the output color
}

In this version, we're using the normalized y coordinate v.y to create a gradient that transitions from yellow (1.0, 1.0, 0.0) at the top of the canvas to magenta (1.0, 0.0, 1.0) at the bottom.

You can also adjust the color values to create different gradient combinations. For example, if you want a gradient from cyan to magenta, you can use vec3(1.0 - v.y, v.y, 1.0) instead.

And now, are you ready to take your gradient game to the next level? Let's combine the power of horizontal and vertical gradients to create some truly mind-bending color combinations! 🌀

Here's how we can achieve this feat of chromatic brilliance in GLSL:

             
void main() {
    vec2 v = gl_FragCoord.xy/u_resolution.xy; // Normalized pixel coordinates
    vec3 color = vec3(1.0, v.y, 1.0 - v.y); // Vertical gradient from yellow to magenta
    vec2 v = gl_FragCoord.xy / u_resolution.xy; // Normalized pixel coordinates
    vec3 color = vec3(v.x, v.y, (1.0 - v.x) * (1.0 - v.y)); // Horizontal and vertical gradient
    gl_FragColor = vec4(color, 1.0); // Set the output color
}

In this shader, we're using both v.x and v.y to create a gradient that transitions horizontally from red to green and vertically from blue to yellow. The result? A mesmerizing kaleidoscope of colors that will make your eyes dance with delight! 👀🕺

But wait, there's more! We can also play with different color combinations and gradients to create truly unique and whimsical effects. How about a gradient that transitions from a fiery orange to a cool, refreshing mint? Or perhaps a gradient inspired by a tropical sunset, with hues of pink, purple, and golden yellow blending seamlessly together? 🌅

Try color = vec3(1.0 - v.x*v.y, v.y, v.x * (1.0 - v.y)); and watch your jaw drop to the floor like a colorblind chameleon trying to blend in with a bag of Skittles! 😮🦎🌈

The possibilities are endless, limited only by your imagination (and your understanding of vector math, of course! 😉). So go forth, my friends, and experiment with this legendary gradient incantation. Summon forth a kaleidoscope of colors that would make a rainbow jealous, and bask in the radiant glory of your shader masterpiece! Just don't forget to pick your jaw up off the floor when you're done. We wouldn't want you drooling all over your keyboard now, would we? 😉👅

Happy coding, my fellow shader artists! 💻💖


If you enjoyed this virtual work, please consider dropping me a line or making a donation to support my work. Your feedback and support mean a lot and help keep this project going. Thank you!

Reach out back

Featured

June 2024

Basic gradients

Basic examples of gradients

Hello, creative coders! 🎨💻 In this virtual work, we're going to dive into the world of GLSL (OpenGL Shading Language) and explore how to create stunning gradients using this powerful language. Get ready to add some vibrant and dynamic flair to your shader art!

Before we get started, let's quickly review what GLSL is all about. GLSL is a high-level shading language used for programming graphics processors (GPUs). It allows you to write shaders, which are small programs that run on the GPU and determine how pixels are rendered on the screen. Shaders are essential for creating real-time graphics, visual effects, and procedural art.