Check out a free preview of the full Advanced Creative Coding with WebGL & Shaders course

The "Color Interpolation with mix" Lesson is part of the full, Advanced Creative Coding with WebGL & Shaders course featured in this preview video. Here's what you'd learn in this lesson:

Matt adds color to a shader by using a built-in function called mix.


Transcript from the "Color Interpolation with mix" Lesson

>> Okay, so this is not exactly but we're learning a little bit more about some of these circles and how to draw circles in a shader. One other thing we'd like to do here is maybe instead of using black and white, we would like to introduce some color.

And in order to do that, we can use another built in function called mix. So, what we're gonna do is we already have a color uniform here. We're gonna say we want it to be the red color, or the pink color, whatever the color is of our cube that we wanna define down in JavaScript.

We're gonna use that color, where all the black is, and then we're gonna modulate the color towards some other color where all the weight is. So, down here once we define our mask, we're going to create a new vec three. Let's call it fragColor just like this. We're going to say the fragColor is equal to mix.

Whatever color you want to replace the black with, so let's say we're going to use color, which is the uniform. And then the next one is whatever the circle color should be, let's say, just leave it as white for now. And then we're going to use the mask value to interpolate from the first parameter to the second parameter.

And I'll explain that in just a second. But for now we can try putting this value in to the frag color, the final frag color. And one of the things you might see is just pure white. And that's probably because the color in JavaScript might just be pure white.

So I'm gonna change this to a different color, tomato, or pink or red. And so what this is doing is it's saying I want to mix from this first parameter, which is the the color in JavaScript. And I want to interpolate or I wanna switch the value towards this second parameter which is white, based on this value here.

So, when this value is zero, it's gonna use the first parameter. When this value is 1, it's gonna use the second parameter which is white. That's why I can't see anything. When this value is exactly in between 0 and 1, it's going to use some sort of blended version between the color in between pure white.

And so this is called interpolation, or linear interpolation actually is what it's called. So we're interpolating from A to B using the mask value. Because the mask value is also between zero and the mask value is zero. When we're outside or inside of the circle depending on whether we have it inverted or not.

And then it's one, on the other side.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now