This random noise won an Oscar.
Stand-up Maths
0:00 I'm in the City of Leeds, at their Light Night Festival.
0:03 And behind me is a building which is
0:07 currently got all sorts of very loud laser effects.
0:11 That's because, actually if we swing around over there,
0:14 there's a bank of incredibly powerful lasers.
0:17 And my friend, Seb Lee-Delisle, is currently up in the control room somewhere.
0:21 Their computer is controlling these to do those effects.
0:26 And I'm here because the mathematics behind how
0:30 you get realistic lightning bolts is really interesting.
0:34 'Cos they got to move randomly, but it's got to be natural randomness.
0:39 And just your off-the-shelf random noise will not cut the mustard.
0:43 And thankfully earlier on today,
0:46 the mathematics of this was all explained by me...
0:52 me.
0:53 Thanks me, It's me, I'm here earlier in the day.
0:56 The building is behind me waiting to be lasered by Seb.
0:59 And we're going to have a look at why
1:02 this random noise is not good enough for Seb.
1:05 What I've got here is a grid 100 by 100 squares.
1:09 And every single square I've given a random value between
1:13 0 and 1 to go from black through to white.
1:16 And this is the standard issue, random noise we all know and love.
1:19 This is Perlin Noise, this is what Seb is actually using in the lightning bolts.
1:25 And as you can see, it looks very different.
1:27 It's got kind of a landscapy, continuous feel because yes,
1:32 while nature can be random, nature is also continuous.
1:36 And if we wanted a bit of, like, continuous one-dimensional noise,
1:41 we could draw a line across the proper random noise and then plot those values.
1:47 But as you can see, it's super jerky.
1:49 If we do the same thing with the Perlin Noise, we get a nice continuous line.
1:55 And that's what Seb's using for the lightning.
1:58 And that's only half of it.
2:00 Not only is this noise more continuous and natural looking,
2:04 it's also more predictable.
2:06 It's technically pseudo-random-noise.
2:10 So if we move around in the true random noise,
2:13 you can see it's just glitching all over the place.
2:16 Like, randomness has no memory.
2:18 Whereas Perlin Noise, if we move around,
2:21 we just get more of the same random landscape.
2:25 Technically, there's an infinite amount of continuous,
2:30 smooth-randomness noise available using Perlin Noise.
2:34 And it will always constantly flow perfectly from one bit to the next.
2:39 And the maths behind this is super interesting.
2:42 So the problem we have to solve is,
2:45 instead of assigning a random value to every pixel in a 2D image.
2:49 Instead, how do we get a field of values across that whole image,
2:54 which flow continuously but are still random.
2:57 Thankfully maths comes to the rescue.
3:00 You start with a grid and these are like integer coordinates.
3:03 So 0, 1, 2, 3, on the horizontal axis and then likewise going up the vertical.
3:09 You then assign a random unit vector
3:13 to every single integer coordinate on the grid.
3:18 So let me just do that quickly.
3:20 Now I'm a human, so the human random in my example.
3:25 In reality you would use like a hashing function or something deterministic.
3:30 So you take the coordinates as an input
3:34 and that would give you a random vector in some direction.
3:38 And the original version of this, which a guy called
3:41 Ken Perlin came up with, the vectors were completely random.
3:44 In newer versions he just had, like,
3:47 a set of 12 vectors and you'd pick one of those at random.
3:53 And he had clever computational ways of, like,
3:55 permutating them all around that's kind of your seed.
3:58 But somehow you end up with a bunch of vectors, one per unit coordinate,
4:05 and then you get the point where you want to calculate the noise for.
4:08 So let's say we've got a point, I don't know, right here.
4:12 There's the point, we want to know what is the noise at that point?
4:15 Ok, so if I zoom in on that one square,
4:20 you then calculate the vectors from every corner to that point.
4:25 You then take each of those four vectors and you
4:30 calculate its dot-product with all the other random vectors.
4:35 So for this top right corner,
4:37 the dot-product between the difference vector coming out here.
4:41 And our random vector from before.
4:44 Well, the dot-product is just the angle between the two vectors.
4:49 Whatever this angle in here is.
4:51 And then, obviously, it's scaled based on the size of the vectors.
4:55 You calculate that dot-product for all four corners and then you, somehow,
5:02 average them together and that gives you your random noise at that point.
5:07 I mean, there are a few other extra steps because you
5:10 want to make sure this is nice and smooth and continuous.
5:14 So you've got some normalising and some waiting
5:17 to get a nice smooth average as you move around.
5:21 Specifically in the updated version of the original Perlin Noise,
5:26 Ken Perlin used the formula of: 6t^5- 10t^4+ 15t^3 [note:
5:32 transcription of formula in line with verbal error by Matt] where
5:37 "t" is just the distance that you're putting this envelope across.
5:41 And you think, "what a weird formula." And I plotted it
5:44 and it's a very nice smooth curve going from 0 to 1;
5:47 fading in, fading out, pretty linear in the middle.
5:51 It's good, but you think, what an odd formula.
5:53 Well, partly it's deliberately picked because 6- 10+ 15 equals 1.
5:58 So, you end up with 1 at the end.
6:01 And because we got sequential powers,
6:04 it's actually computationally quite nice to work out because you can just
6:09 gradually multiply by extra values of t as you're putting in those constants.
6:13 So, like everything involving computers,
6:15 I tend to focus on the maths of how it works.
6:20 So all my talking about the vectors
6:22 and whatnot is just the mathematical logic behind it.
6:25 But the people who originally developed it
6:28 were also thinking about what is computationally clever,
6:32 like what can we implement in hardware and software
6:35 that can be done in the most efficient way possible.
6:37 Which actually brings us to the direct
6:40 descendant of Perlin Noise, Simplex Noise.
6:43 The reason an upgrade was needed was because,
6:46 while every pixel in 2D just requires those four vector calculations.
6:51 For higher dimensions things get much worse.
6:55 When you go to 3D, well now all your points are somewhere inside a cube
7:00 and suddenly you've got eight vectors that you've
7:03 got to do the dot-product for each one,
7:04 you've got to do all the weighting and the averaging.
7:07 Computationally it doubles each dimension.
7:10 So, if we go up into a hyper cube, uh, okay, I'm going to draw it.
7:14 If we go up to a hyper cube, suddenly you've got 16 corners,
7:18 which means you've got 16; oh, you get the idea.
7:21 You got 16 of everything.
7:24 And when you go up to 5D, you've got 32.
7:26 And Ken Perlin originally did the whole
7:29 thing based on hyper cubes, and it worked, but computationally got a bit heavy,
7:34 and people ended up using the higher dimensional versions of this.
7:38 So then they come up with a new version.
7:40 And they went, "Forget hyper cubes.
7:43 We're going to use simplexes." And "simplex"
7:45 is just the fancy word for triangle, but in arbitrary dimensions.
7:50 So once again, you can start with a line segment.
7:52 There's my 1D triangle, there.
7:54 In 2D, you've then got an equilateral triangle.
7:57 They tile the 2D plane quite nicely.
8:00 In 3D, you've got a tetrahedron.
8:03 So, you put an extra vertex on and then
8:06 you join that up to all of the other vertices.
8:09 And now you work out where each of your points
8:12 are in terms of which simplex or which tetrahedron they're in.
8:16 And then you've only got to do in this case now four, four calculations.
8:21 So what's called simplex noise because
8:24 you're using simplexes instead of hyper cubes.
8:28 You, the same computational cost what used to have to be
8:32 done in 2D with four points you can now do 3D.
8:36 And when you go up to 4D, I'll add an extra point in there,
8:39 join that all up; you've only got, what's that now,
8:42 five points so you're going up it's linear.
8:45 The computational complexity is linear with dimension, which is way better.
8:50 So I'm not going to be very careful about
8:53 saying the correct type of Perlin Noise or simplex noise.
8:55 I'm just going to say Perlin Noise
8:58 and I mean the entire family of Perlin Noises,
9:02 be they hyper cube based or simplex.
9:05 The fact that Perlin Noise is defined by vectors at the integer
9:09 coordinates means that it has a certain scale to it,
9:12 and that means you can both zoom in too far,
9:15 if you go all the way down you hit nothingness.
9:18 In fact, when I was first messing around
9:20 with Perlin Noise in Python, I rendered this.
9:23 And I thought I'd made a mistake,
9:25 but I just rendered the 0 to 1 origin square, which is a square of nothingness.
9:31 And you can also zoom too far out, 'cos if you go to the point where
9:35 the noise fluctuations are smaller than your pixel size,
9:38 you're just back to regular old noise.
9:40 But the fact that Perlin Noise changes with scale and that you
9:45 can get it wrong is a feature, not a bug.
9:48 It means you can layer up several scales to get fractal noise.
9:52 So we can take this region of noise.
9:55 We can then take a region which is twice as big,
9:58 but scale it by a factor of a half and then add them together.
10:02 We can get a region which is four times as big,
10:05 scale it by a quarter, add that as well.
10:07 We can stack layers of noise at different
10:11 scales and that's nature for you, right?
10:14 Fractal noise.
10:15 A little aside though,
10:16 when I was messing around with this in my code, I was using red,
10:20 green and blue values for different scales of noise so I could
10:24 keep track of what was happening when I was debugging the code.
10:27 And I had these weird, lava-lamp like images I'm going to render a unique one
10:32 of these for every Patreon supporter and email them out.
10:35 I'm also going to do a limited run of postcards, I think.
10:39 Details at the end of the video.
10:41 Anyway, in the noise business,
10:43 these different scales of noise are called octaves,
10:46 which is a great name because the wavelength
10:49 of the noise is getting half as big each time.
10:52 So, the frequency is actually getting twice as much, just like an octave.
10:56 And these octaves are exactly what Seb was using for his lightning.
11:01 For the record, Seb using Perlin Noise
11:04 in visual effects is not unusual or indeed uncommon.
11:08 It's the very point of Perlin Noise.
11:11 Ken Perlin is indeed a professor of computer science.
11:13 But they were inspired to create their noise
11:16 when they were working on the 1982 film,
11:19 Tron, and were dissatisfied with the random noise
11:22 that was currently available to put into visual effects.
11:25 They later developed the original Perlin Noise and it was a huge success.
11:30 It would take clinical looking VFX and turn it into realistic,
11:36 natural textured surfaces.
11:38 And not only is it now ubiquitous across the VFX industry.
11:42 But in 1997, random noise, Ken's Perlin Noise, won an Academy Award.
11:49 It is hard to understate how useful Ken's noise is in visual effects.
11:53 And to take a closer look at how it's implemented,
11:56 Seb assures me he has a version of his lightning that's safe to use indoors.
12:00 [Matt] So, here's, like, a mini laser; this is, like, a tiny little laser.
12:04 And you're projecting a frozen bolt onto the wall here for us.
12:09 [Seb] Yeah, that's right, so...
12:10 [Matt] And this is like one frame of your lightning effect.
12:12 [Seb] Yeah, exactly.
12:13 It's one frame of lightning.
12:14 It's a simplex noise.
12:15 It's one dimension from top to bottom
12:18 with the noise value adjusting the horizontal.
12:21 [Matt] All right.
12:22 So, if you turned all the noise off, it would just be a straight line.
12:24 [Seb] Oh, yeah.
12:25 [Matt] Actually, can you turn all.
12:26 [Seb] I can change the width, so it's like.
12:28 [Matt] Oh, there it is.
12:29 [Seb] I suppose I could turn all the octaves.
12:31 [Matt] Yeah, turn the octaves off.
12:32 [Seb] There you go.
12:33 [Matt] There you are, nothing.
12:34 Turn on the zeroth octave.
12:36 [Seb] Yeah.
12:36 The zeroth octave obviously start at zero.
12:38 [Matt] So, this is just a line through some Perlin noise.
12:41 [Seb] Exactly.
12:42 [Matt] Backwards and forwards.
12:43 [Seb] Yeah.
12:43 [Seb] Simplex noise.
12:44 This is octave one.
12:45 [Matt] Yeah.
12:46 [Seb] And then we can see octave two.
12:47 Each time the noise gets not only, like,
12:50 wigglier but actually narrower as well because we don't
12:54 add the same amplitude of noise for each octave.
12:57 [Matt] So you decay the amplitude for each one, gotcha, [Seb] Yeah,
13:00 you see the finest octave you can barely even see it, right?
13:04 [Matt] But now if you combine them.
13:06 [Seb] Yeah.
13:07 [Matt] Actually what would octave zero and like octave four together look like?
13:12 So that's zero.
13:13 Oh yeah, hah!
13:14 Yeah.
13:14 [Seb] Just makes it a little wiggly.
13:16 [Matt] Love it.
13:17 So you can if we switch them all
13:19 on then you've got your your basic lightning effect.
13:21 [Seb] There you go.
13:22 [Matt] Look at that.
13:23 [Seb] But we can also take that and add another dimension, right?
13:26 So we can move through the second dimension in time.
13:29 And that just makes this vary over time in the similar
13:33 smooth way as, as we're moving down in that first dimension.
13:36 [Matt] So you've got a single line through the 2D noise.
13:38 [Seb] Yeah.
13:39 [Matt] But now you're going to move that line.
13:40 [Seb] Move it along in the next dimension.
13:42 Let's turn that on.
13:43 So we can do that with time.
13:45 Keep it quite slow.
13:46 You can just see that motion.
13:48 [Matt] Look at that.
13:50 [Seb] And we're close to, like, my electric bolt effect, right?
13:53 But the the main problem I've got here is that the top
13:56 and the bottom also oscillate with all that other noise.
13:59 Um, and I need the start and the end to be in a fixed position.
14:04 So what I do is I take a sine curve,
14:06 you know, from zero to one and then back to zero again.
14:09 And I multiply that to the amplitude from the top to the end of the line.
14:13 [Matt] So you're just using half a sine wave
14:16 as a nice envelope from zero to a lot to zero.
14:20 [Seb] Yeah, exactly.
14:21 It's a nice way to pinch the ends.
14:22 If I turn that on, you'll see it there.
14:24 [Matt] Oh, nice.
14:24 [Seb] So now the...
14:26 [Matt] something going wrong somewhere.
14:27 [Seb] It's all fine.
14:28 [Matt] It's like your solution was just to turn off the radio, fixed.
14:31 [Seb] Yeah, that's my solution to all the crew, crew problems.
14:33 It's like, "la la la la" it's all fine.
14:37 [Matt] It's either you turn that off or you hit this giant.
14:39 This button also fixes a lot of problems.
14:42 [Seb] Yeah, that's the big emergency button.
14:44 Uh, anyway, where were we?
14:45 [Matt] Yeah.
14:46 So, okay.
14:46 So, you fixed the top and bottom.
14:47 [Seb] Fixed the top and the bottom.
14:48 That's basically my electricity effect, but it's running a bit.
14:51 I mean, it's very pretty.
14:52 It's not very dramatic.
14:54 [Matt] No.
14:54 [Seb] So, let's turn the speed up.
14:56 [Matt] And there it is.
14:57 [Seb] There you go.
14:59 That's my electricity.
14:59 [Matt] I mean, that's lightning.
15:00 Seb has also very helpfully inadvertently answered
15:03 a very common question about Perlin Noise.
15:06 And that is what would you possibly want with 4D noise or 5D noise.
15:13 Like, what practical application can there possibly be for the fact
15:16 that Perlin Noise can be rendered easily in four dimensions.
15:19 And it's because often if you want noise of a certain dimension,
15:23 you will take it as, like, a slice or a section of higher dimensional
15:28 noise because that gives you more room to move.
15:31 So Seb was using 1D noise, represented by this pen,
15:34 and Seb then moved it through a 2D, like, flat surface of noise.
15:39 So he got it changing over time, but you could start with 3D noise.
15:43 So imagine I'm surrounded by 3D noise.
15:45 There's my 1D line I'm taking.
15:48 And if I move my 1D section through the 3D noise and back to where I started,
15:53 I will have a perfect loop of noise
15:56 because the beginning and the end are identical.
15:59 It's not like we had to go and then reverse it.
16:01 We have completely different unique frames organically changing smoothly
16:06 from one to the other right back to where we started.
16:08 And we could do a different path.
16:09 We could come down this way and back up again.
16:11 We could have as many different looping bits of organic
16:15 noise all with the same start and finish frames.
16:19 I've actually done the same thing a dimension higher.
16:22 So here is some 2D Perlin Noise, but it's a slice through 4D noise.
16:30 And here's the same slice.
16:31 I then worked out two different paths through the four-dimensional noise
16:36 space that don't intersect and both end up where they started.
16:41 So now I can have this initial frame I can loop through
16:45 perfectly organic continuous noise and get right back to where I started.
16:50 It's so good, I mean Perlin Noise is just amazing.
16:54 Just so amazing.
16:55 In fact, it is my favourite random noise.
16:59 And that is quite an accolade.
17:01 I know it's already won an Academy Award, but just below that is the award.
17:07 And yes, I got a trophy made.
17:09 It's to Perlin, winner of Matt Parker's favourite random noise.
17:15 That is now two major awards that Perlin Noise has won.
17:20 Apart from thanks to Ken Perlin, for coming up with the noise.
17:24 I also want to thank Seb Lee-Delisle
17:26 for getting me along for this fantastic installation.
17:28 You must check out Seb's YouTube channel.
17:31 On there, he gave me a guided tour of all the tech.
17:35 I mean, we literally followed the network cable.
17:37 So, if you want to see how this was put together,
17:40 how it all works, head on over to Seb's channel.
17:43 And as I mentioned earlier on, if you're one of my Patreon supporters,
17:47 I am going to email a unique frame of that RGB Perlin overlays noise.
17:54 So, you'll have one each.
17:56 It's yours to have.
17:57 It's like an NFT but less naff.
18:00 It's all yours.
18:01 And if you support me at the "Statistically Significant" level or higher.
18:06 [Glitching noises] [Future Matt] Psst.
18:09 Over here!
18:09 Hey, it's me, Future Matt.
18:11 I don't know if you could tell from the somewhat dated NFT joke.
18:14 I filmed that back in 2021 and then never finished the edit to release it.
18:20 'Cos I take on too many projects.
18:22 Which is going to be a theme of this update.
18:24 But I've come back to Leeds to first of all say:
18:27 if you want this year's Patreon Christmas card,
18:29 which will be Perlin Noise based,
18:32 you have until the end of November 2025 to sign up and get that.
18:37 Oh, it's raining, great...
18:38 Um, this is what I get for filming on location again.
18:41 Um, I'm also here to apologise for my previous Patreon supporters,
18:47 who will have noticed they probably got their birthday
18:49 card late if they were entitled to it last year.
18:52 And they didn't get a Christmas card at all.
18:56 And that's because last year I took on too much stuff, which is on me.
19:00 What was I going to do, not calculate pi on the moon?
19:04 So, I did.
19:05 I over-committed.
19:05 I didn't burn out, um,
19:07 because I have a release valve where I just stopped doing some
19:10 things and I figured Patreon supporters
19:12 wouldn't mind if everything was super late.
19:15 So, I meant to put the laser video with Seb,
19:17 I thought it was late at the end of 2024.
19:19 Never got it out.
19:20 Never got your Christmas cards.
19:22 So, if you were a Patreon supporter before,
19:24 you will get the extra RGB postcard as well as your Christmas card this year.
19:30 And if you're new, you just get the Christmas card.
19:32 Oh, and if you were signed up and now you're not,
19:35 I'll just send you the RGB Christmas card.
19:36 I will get it to you.
19:37 So, thank you so much to your support everyone.
19:39 I'm going to make a dash for it now.
19:40 Um, I really appreciate you supporting everything I do.
19:44 And oh, I'm here in Leeds, by the way,
19:47 because I'm doing my show, "Getting Triggy With It" tonight.
19:49 Of course, I'm on tour.
19:50 That's one ex-.
19:51 It's been such a year.
19:53 But I've got another date coming up on the 1st of December,
19:56 which is the day after the deadline for joining
19:59 Patreon in time to get the Christmas card.
20:02 But if you want to come to the it's on the set of, um,
20:07 Matilda in London, it's going to be amazing.
20:09 I'd love to see loads of you there.
20:11 Um, yeah, that's then we're on tour first next year.
20:14 Um, thanks for your support.
20:15 Oh, I meant to, I meant to hand you back to 2021 Matt.
20:19 Ah, he's probably still blah-blah-blah'ing.
20:24 [Glitch noise] [Past Matt] So, look forward to that.
20:26 So, anyway, thank you so much everyone for watching this video.
20:29 Check out said video and support me on Patreon to get your own,
20:32 free noise by the end of the month.