Hi, welcome to this new video. In this lesson, we're going to continue learning about Perlin noise and how we can actually use Perlin noise a bit more precisely to design with it. So let's look at the function that we're going to write today. It's actually a rather simple function. We are going to be able to clamp the noise, meaning that we're going to just differentiate between two conditions, black or white. So as we know, the noise is a value that kind of gradiently moves between these peaks and valleys, let's say the -1 and 1. And it creates gradients in between, right? So it would always be like a gray condition in between. But we could create an arbitrary threshold, a threshold that would determine if a pixel is white or black. We're going to just do it for a segment of those two conditions. But you're free to consider maybe you want to create ranges and maybe between this and these values of the noise, it's a color, or maybe between these other values of the noise, it's a different color or pattern, right? So segmenting this Perlin noise is actually achieved rather simply with, as we're going to see, with an if statement. So let's look at the code example that we have here on the left. We're going to be writing a function that we're going to call threshold. What is the cutoff point for this value of the noise to either be white or be black, right? We're going to see how to write that in a very kind of short if statement. So let's jump into processing and see what you can see this interactively as well. So we're going to continue writing based on the example from last video, right? Let me just remind you where we at. We have this noise pattern that we can actually manipulate with the x coordinate, right? So that gives us a sense of the scale. So let's just write our function. Let's make sure that our indentation is right all the way to the back here. And we're going to write the DEF threshold. It's going to require an argument of a value and a cutoff, right? The value is going to be, which is the noise value that you're providing the function. And the cutoff is the arbitrary point in which we're going to cut it, right? Knowing that the value needs to be coming from between. If it's directly coming from the noise, it's going to be between -1 and 1. But if we have remapped that value, it might be in a different range, right? So let's assume that it's going to be between -1 and 1. Well, here's where I want to bring attention to some of the math that we're doing. Everything that we're doing within the noise function will remain still between -1 and 1, right? And this noise function cannot return anything other than that, right? But once we multiply it times 200, we are changing the result to something different, right? So instead of doing this 200 here, we're going to use the mapping operation once more. But let's just do first the threshold function, so let's return. So once we call this threshold function with a value, let's imagine that we provide a value of, let's say, -0.5. We will return, We're going to return 255. That's going to be our default. Return 255 is going to be our white condition. But if the value, It's bigger or equal than the cutoff, Else zero, right? So this is a way of saying, look, this is going to be our condition. The condition is going to be 255. If the value remains above the cutoff else, it's going to be 0, right? And we can actually include all these into our return function. Once the function gets executed, it will return this equation, right, which in fact is an if statement, right? So kind of a simple function. It's just a one line function. And let's figure out here where to use it, right? Because we will actually have to define the cutoff and the value, right? So first, I would like to have control. Let's just remove this 200 now. And we know that our noise, right, I'm going to call it n_mapped just to differentiate it from the noise itself. It's going to be a map version of n. So we're going to take the noise value that goes between -1 and 1, right? And we are going to map it to 0, 255, right? And this is better than multiplying by 200 because what we were trying to do is kind of convert it into something that would be recognizable as a color, right? So we were actually missing out on some of the upper ranges of the color, right? So let's just say that the fill is going to be the mapped version of the noise, right? Let's just see how that works. We're getting it quite bright. Maybe I actually got this incorrectly. Maybe the value of the noise goes between 0 and 1 as opposed to -1. Let's double check that. Yeah, so that's more correct. I think that this is a kind of a small mistake. I had the impression that the noise was actually operating between -1 and one is actually between 0 and 1, right? So that's clear now, by defining the parameters, the domain of the value, right? So the noise operation, it's returning something between 0 and 1. And we are remapping it to a value between 0 and 255, right? And we get kind of a nice crisp range between blacks and whites, right, at different scales, that's perfect. But we're not using the threshold value yet, right? So let's incorporate this. If we're going to incorporate the value of threshold somewhere here, now we know that the range before the mapped function is between 0 and 1. Let's just include it here. So we could say threshold, the threshold of n or n. Now it's going to be the threshold of n, let's just do 0.5. So 0.5 is going to be halfway through 0 and 1. So we're going to be saying, now n can only be two things. Could be white or black if it's closer to below 0.5, or if it's above 0.5. And then we can do again the remapping to a value that we can see in the screen. And as you can see, the same principle of the noise now applies. But actually, we're just clamping the noise value, right? So just to conclude, and just to give you a sense of the dynamism that we could actually use this for, a lot of people use this sometimes in procedurally generated cave formations or like terrains. You could use that when we eventually learned 3D, we could actually see it how we could affect the height field of a surface, right? But let's just do the same operation that we did up here with the mouse, right? So we can use the second axis of the mouse to affect the cutoff, right? Right now, we don't have a variable for the cutoff, but let's create one. So let's say cutoff. It's going to be a map version of the mouse in y, knowing that the mouse will go between 0 and 600. Let's say the cutoff, we now know that we want to go between 0 and 1. So let's do 0, 1, right? So 0.5 would be somewhere in the middle of the screen, up would be 0, lower would be 1, right? So we could actually see how we're kind of slicing through the noise as we move with the mouse y-axis, right? So falloff now, this variable, we can replace it for this 0.5 here, which was an arbitrary cutoff value and see how we can actually achieve an interactive version. So we still have in this axis the scale, but now we have, if we move up, we could actually get, you see, and you could imagine something like a simulation of a water level rising or something along those lines. We can create a lot of control over the result of this variable just by moving the mouse up or down, right? So obviously, some of the most interesting moments are maybe somewhere in between. But what I would invite you to try is potentially print the value of the mouse at a position that you like. So you create this little interactive condition, right? And then you think, well, maybe I like this moment, right? And that's a very calibrated moment. And if you would have the console printing the mouse position x and y, you would know what are the values, right? Or you can be printing out directly the cutoff value and the scale value, which are the two variables that are being manipulated to get this result, right? So again, it's a very powerful tool. You'll see a lot of different designers using Perlin noise somewhere, not very visible, but sometimes in between the code, allowing for some of this noise to really create some gradient differentiation in the composition, right? So this is kind of a big upgrade of just using random, which doesn't really create any kind of pattern, right? We could actually start using a kind of form of noise that does create this kind of pattern. So we'll continue talking about pixel arrays in this fashion in the next video. So I'll see you then.