This article has participated in the Denver Nuggets Creators Camp 3 “More Productive writing” track, see details: Digg project | creators Camp 3 ongoing, “write” personal impact.

Chestnut 7 — Shake line

“Smooth, beautiful Lines” is one of LNP’s (Lazy Nezumi Pro) signature lines that many users find when looking for a line stabilizer. But when used properly, messy, shaky lines can look great in the right context. Let’s write a script that gives our lines some tasteful imperfection!

The first thing we need to do is add some line thickness to make it look like we are drawing with an ink-flow pen. If you look closely at the function reference, you will see that there is a RAND function that returns a random value in the range [0..1]. Let’s try multiplying the input pressure by a random quantity and see what happens.

op = p ? p * rand() : 0;
Copy the code

It’s interesting, but it’s not what we’re looking for. A little dirty, and instead of scaling our input pressure, let’s try adding a small random quantity. We will use the mix function to remap random values between minNoise and maxNoise.

op = p ? p + mix(minNoise, maxNoise, rand()) : 0;
Copy the code

Here’s what minNoise looks like at -0.2 and maxNoise at 0:

It looks better that way, but it would be better if we could control the imperfect length. The rand function returns a different value each time, so we have to try something else. If you look at the function reference, you will find a wave function called noise. This is a chart preview.

This is a non-repeating continuous function, which is good because it looks random, but we can still control its scaling, just like any other wave function. Try this script:

n = 0.5 + noise(d/period) / 2; 
op = p ? p + mix(minNoise, maxNoise, n) : 0;
Copy the code

The noise function returns a range of [-1..1], so we remap it to [0..1], so we can use it to mix minNoise and maxNoise, similar to the previous script.

This is what it looks like when period is 30, minNoise is -0.3, maxNoise is 0:

Now we’re making progress! The noise looks more subtle than the previous script, which looked like we were scribbling with a pen.

Being able to control the width of the noise pattern with the period parameter is really nice.

But now if you look closely at the bottom three straight lines, you might notice something. Even if the pressure applied to each line is different, you can still see that the noise pattern is exactly the same each time. This is useful in some cases, but it would be nice if we could change the pattern. We can easily do this by adding a fixed offset to the distance value passed to the noise function. Let’s name it seed and set its scope to [0..10000]:

n = 0.5 + noise((seed+d)/period) / 2; 
op = p ? p + mix(minNoise, maxNoise, n) : 0;
Copy the code

We can now change the seed parameter each time we need a different pattern. That’s great if we want to be able to copy a particular pattern that we like, but what if we want each line to have a different pattern without having to change the parameters every time? In this case, we can use the RAND function to automatically initialize our seed at the beginning of the line

seed = seed ? seed : rand() * 10000; 
n = 0.5 + noise((seed+d)/period) / 2; 
op = p ? p + mix(minNoise, maxNoise, n) : 0;
Copy the code

Since the script variable is initialized to zero before drawing begins, we can reinitialize the seed value by giving it a new value only when the seed value is zero. That’s what the first line does with conditional assignment. Now the noise pattern on each line looks different!

Let’s complete the script by adding dither to the pen position. We will use the same normal vector permutation offset technique as in the previous example. But this time, the displacement offset will be replaced by the result of calling the noise function, multiplied by a new posNoiseAmount argument:

seed = seed ? seed : rand() * 10000; 
n = 0.5 + noise((seed+d)/period) / 2; 
op = p ? p + mix(minNoise, maxNoise, n) : 0; 
pn = posNoiseAmount * noise((seed+d)/posNoisePeriod); 
ox = x + nx * pn; oy = y + ny * pn;
Copy the code

This is what posNoisePeriod looks like with 30 and posNoiseAmount with 3. This is actually a pretty good water reflection effect!

Chestnut 8 — City scenery

Next time

Chestnut 9 — Lightning

Next time

Chestnut 10 – Signal misidentification

Next time

Chestnut 11 — Debugging

Next time