Hello welcome to this video especially to Owen Dorsey again thank you for commenting and asking any question so this video is how to use lerp a lerp function or linear interpolation to blend tiles that you’re making up a terrain with using Perlin noise together if they have very different amplitudes or likes heights Because you might have a an amplitude of say 24 on this tile and then at this part of the terrain you have an amplitude of 3 and when they meet you just get a sheer cliff between them so be nice they’re gonna blend those together so you get a smooth Continuation across your terrain so you could use your frequencies or different frequencies to have like a different kind of biome so this is another way to have biomes or an additional thing that you could you could use this was Owens idea to have different biomes and so this follows nicely from the previous Video where we had a look at biomes and so I’ve got a top-down view here and we can see that in the north we’ve got some like tundra or ice we’ve also got in the south we’ve got trees and things we can we can grow and we’ve also got like dark Green for like lowland grass and brown and I’ve got a Northwest file north east south south west south east and everything looks okay from this perspective but actually I’ve got it orthogonal so if I just look at the ordinary perspective you can see things are quite strange so what’s gone on is What I’ve just described is that over here we haven’t lurked between having a frequency in the North West we’ve got a frequency so an amplitude of 3 and in the Northeast we’ve got an amplitude of twin 84 in terms of height and so we create a Shear drift a sheer cliff looking to the south though I’ve switched learn upon so here Southwest again you’ve got an altitude of three and in the in the south east I’ve got amplitude of twenty-four but we’re using love to kind of stretch up those voxels those cubes so that they Match the kind of the target height of the neighboring terrain and I’ve used where are we I’ve got lurk switched on there and switched off there so what I’m going to show you in this video is how to add your own like lurk function to to match your terrains something we might Do though actually let’s unpause what i well i described this a little bit so you can see these are these are two very different biomes we’ve got a very very mountainous spiky biome and we’ve got a very very low land and very smooth terrain remember we can use frequency to Smooth our terrains we use very in our scheme a very low frequency number to have a very spiky terrain and in our scheme we use a very high frequency to have a nice smooth terrain I should really flip that it should be the other way around it doesn’t matter Too much closely we know that we’ve made that inversion and things are working so one downside is that if you want very very different amplitudes and frequencies neighboring each other even when you’ve got lerp switched on it means that your source terrain is kind of stretched up it keeps it it’s kind of Uniform purlins according to its own amplitude and its own frequency but if its neighboring something quite extreme it really is kind of stretch in extreme way so what’d I just do is reduce things a little bit reduce that difference so we can see the the effect Of the lab a bit more so we will switch on look everywhere and so in the West our left hand side let’s go up to eight so we have an amplitude of eight we’ll keep our smoothness there and in the east let’s go down to 12 and maybe we better Increase our spike eNOS as well for decrease our spike eNOS so increase our frequency that’s great about twelve okay now when we play we should have between east and west we should notice a difference yes you’ve kind of got a a uniformly stretched out terrain actually it’s Quite flat in pieces and then as we go over to the east we get a very spiky and a potentially much what a terrain that involves much higher peaks and more variation there we go so we’ve got it okay a mountainous treacherous biome and If we look back at the West much kind of smoother uniform terrain and if you imagine if we have more terrain to play with like if I add some more i’ve got turned around here there we go right there still turn around on it oh no I Looking at the east there we go right if we had some more terrain tiles going this way that that were the same frequency same amplitude as 8:24 here it wouldn’t be stretched me more there would be no stretching so you could like you could smooth out between biomes Even further sorry that’s all I was trying to say okay so how am I actually doing this so let’s jump into the code so this is my little love it’s my little er test first I’m gonna show you a basic version that I started out with just to give you the idea Sorry I just scrolled away there we go so when I have a look at this first of all as always let’s zoom in a little bit so you can see right so the lurk function is part of unities math library we just write math F dot look and it’s Gonna return a new value between the source value and what’s called a target value okay so it’s gonna return a value between those and how do you know like how far between these two so our source value is like the source terrain that we had in so as you’re looking at me I Should go over here in the West and our target is in the east and that was the higher terrain so you’ve got these two points and what love does is find the position in between those two points so the question is how where in between do you want to land Well I’m varying that as I go across from this side sorry from along the x-axis so along my grid of voxels and I’ve got 50 voxels there in the previous video I had a hundred by hundred over 50 voxels and so for the first voxel I just Want between the source and the target I just want it to be stretched up a little bit as I go along I want an Xbox would be stretched up a bit more and so on so now the neighboring voxels are kissing on meeting one another so you don’t want say this final parameter Which tells you how far between the source and the target you want her to go you don’t want that to be one because that would mean a hundred percent way across and that mean your your source terrain would just be lifted to the target terrains amplitude so what you Want to do is every time I’m positioning a block so here I’m inside the generate terrain nested loop so this is where we’re doing all the octave staff with our Perlin noise as in the previous video and then before doing the biome and stuff before doing the buyer and Stuff which we do here we’re changing the value of y that we’ve decided with the Perlin noise so we generate our terrains purlins and we generate the these or the the purlins next door I’ll go run this this way and then and then we say okay that match the sources Terrain to the targets and we need to do that after we’ve decided to basic purlins layout which we do here and we’ve got a sophisticated system of three purling octaves right so how I decided the target height all I’ve done here is hard-code a value I just thought I just thought looking at the target well let’s have a look I’ve changed these values around our target is in the east and it’s 12 that’s our amplitude so I could just put 12 F in here but the trouble is I’m adding on to the amplitude because I’m using 3 8 3 Purlins octaves so I’ve got 1 hunt cookie I’ve got 5 over here we could get the 35 and then another half so it’s about five and a half times whatever the amplitude was which was 12 so this is about what about 60 something like that About 60 and I’ve got 50 to go across so this is basically 1 divided by the amount of Colin’s that you’ve got so this would be a much better way to do this and I think I’ve done that down here yes 1 divided by columns and I made it That means to be a float this needs to be a float this needs to be a float and this needs to be a float if you get any problems with this not working it might be because you haven’t typed to a float I think I just want to making sure I get My brackets in the right place so let’s show you that working so once more lerp will return a new height between what why already was so that’s the height of each voxel and we doing one box at a time the target height which is 60 so Our source is about was 8 my target height and it’s going to be 8 times 3 which is what 24 or 24 and our target height is 60 so we’re gonna return a floating number between those two and as we get as we get across our terrain our source Terrain we’re going to travel along that distance more and more so that the the neighboring voxels between West and East are going to be about the same in terms of height right and that height is going to be 60 so our Y you can maximally only Return 60 so if I switch off the more sophisticated version but I haven’t talked about yet do that there there we go let’s have a look at what’s going on let’s see it working okay right so our source in the West I keep forgetting Western East our source in the West is Nice and flat it can be what was it eight eight high so eight points three high but it is being stretched it is being stretched up to the right-hand side unfortunately we’ve still got a cliff that’s because our source target isn’t high enough so I’ve done some bad Math so let’s correct that let’s try that so do I save this as well yes so let’s go 160 and let’s see what we get yeah that’s right I’m just making sure I’ve got nine yeah that’s fine oh my neck sure let’s F or maybe maybe it didn’t work because I didn’t float Floated by this let’s just see okay so that’s worked right so that third parameter is indeed very sensitive in terms of being a float and not an integer or something else again if you return if you use zero or one it’s gonna have no effect so it’s definitely Stretched the terrain now so we’re using lerp and it’s hopefully I haven’t over explained it too much but I have given you enough information that we’re going across or this way no it’s across me the terrain and for each column we’re interpolating or lurking more as we Approach the other side now here we go so this is column 50 and we reached a height of 60 there we go and so I don’t I don’t I think I’ve I still have done my math wrong I shouldn’t have gone to 60 so let’s just reduce that to half that about 28 Dramatic pause okay okay I haven’t quite gone up enough or have I so we’re in the West going over to the east and yes now we’ve kind of matched we’ve matched so our biomes are kind of blended together big problem though because each of our rows so going from along the z-axis they Should have a different target height because our neighboring biomes are very spiky and wavy so we can’t look – I’ve changed it from 60 to 28 we can’t just all up to 28 we want to lurk – what the individual heights of these voxels are So by row so what are those values well that worked out after we’ve done the the applied to three octaves so there’s two ways we could get hold of those values after our target terrain has worked out its purlins you could record each height for this neighboring row so this Neighboring : so column 0 or column 1 going all the way down all of the rows okay and then you could pass that to your script and that could be the target for each individual voxel so here you could have something like target Y and then this would be presumably an array And you would have to load that array into here and then you would get the individual targets so that’s one way to do it I didn’t do it that way I did it in a much more inefficient way I’ve done it in a non optimal way let’s just put This hard-coded value back in there we go so what I’ve done is think how do I get those bangles back I could just redo the pearl in three octave calculations here but make sure that I do the target frequency so what I’ve done is create a vector to target Position so I want a vector to is just a variable that holds two numbers so I’ve used those two spaces to hold the x position of the target voxel and the Z position of the target voxel and so really all I’m doing is finding the target Y according to these three Perlin Noise calculations here we go and for our x position in the Perlin noise equations you don’t want the x position of this voxel you want the the neighboring voxels position so I store that in this vector too so it’s just the seed I mean it’s almost the same as this It’s the seed plus my position plus X but I don’t want X I want you know I’ve done a slightly wrong on one oh no no I have done this right sorry I have done it right I don’t want I don’t want the position of each voxel Across the target domain I just want the the first column so what I’ve done is store the target terrain in a public game object and then I’ve got hold of the the transform position X because that will be the position of the the first line let me just illustrate one Earth I’m talking about by playing and pausing and having a look down there we go right so this is our source terrain and this is our target and so all we want is the positions in terms of height but also where they are X and where they Are Zed on our terrain so what I’ve done is for our lerp script I’ve got a target terrain public game object and I’ll show you that here so up at the top not yet here we go right well that terrain why can’t I see it okay right so I’ve got a Public seeding a farm so that’s the type I’m looking for a script I’m looking for this script because that’s gonna be the the terrain script with all the frequency and amplitude and position information and I’m calling it target terrain and because of the public it means I can just grab hold of the Eastern terrain I’ve dragged that into that target terrain public game object position I never know what to call these like variable variable value space container or something okay now that I’ve got hold of that I can refer to its target frequency target amplitude so in the Start menu all I’ve done start menu like Windows 95 in this start function I’ve said my target amplitude equals the target terranes dot amp because that just refers me to the seeding farm scripts frequency and amplitude there we go and really should I not hmm yep there we go right Once I talking about also yeah then because we’ve got our target terrain we can then get hold of the position of that neighboring column so target terrain transform position equals equals that that position there that column there okay so what’s going on so what we’re talking about is this Equation so what we’re gathering is remember the target height at each of those at each of those voxel positions on the target or neighboring terrain tile and so although we’re using the target position dot X the Z the position which counts for the rows that does want To change and that just wants to be the same as my position Z plus Z because that just goes down down the roads but so this because we’re running these equations for each voxel on our source or our western hemisphere then each row will get a different Height depending on the height of its neighbor so then we can use our love function after we’ve found what height our neighbor is at so our target y has generated generated its value from calculating what the height will be at the neighboring terrain and I think I’ve explained everything there I feel like I’ve really over explained that it’s probably because I’m not coding it from scratch in front of your eyes so I don’t know I might post this because at least you can see the the code and I’ve explained how it works and I’ll do a link I’ll make a link to The this code so you don’t have to type it out but maybe you’re maybe you are using your own code that’s kind of different so this only works specifically for what I’ve coded in these in these tutorial videos so I’m sorry Owen that felt like a bit messy it Feels like I’ve taken a long time to explain something but there you go but definitely works oh so it’s not working here why because i’m not i’ve not applied the new lab so what we were running there is the basic version of the lab which only took one line of code All you have to change actually to do it in the better version that I kind of talked about you just have to change this value for each of the rows because they were kind of like different so as long as you can get that in there and I suggest using an array be Was the neighbor the target terrain has already calculated done that calculation so you might as well just store the heights and pass it over to this script in a you know array somehow yeah I wish I I would have done that and then you don’t need to do all of this because This looks really messy and it took me a long time to explain there so we’ve got the new version here working let’s press play and thank goodness everything’s lined up okay so we do it we’re using the same lurk function but this time a target is derive from the Perlin noise Three octaves again so that means everything is stitched up nicely and blended wonderfully so so we’ve got lots of ways that we’ve calculated biomes or created biomes we’ve used Perlin noise again without an amplitude as it were we’ve divided our our terrain not Oh I’ve added some slight differences there Which I don’t think I should talk about on this video you can see the changes in there in the code if you if you go there it’s things like using randomness to kind of blur the edges things like that okay thanks for watching apologies for my explanation that felt Rubbish do ask many questions if you need a name or demand that I take this video down if it’s just so bad okay goodbye Video Information
This video, titled ‘Minecraft Tutorial in Unity: lerp between biomes’, was uploaded by Red Hen dev on 2018-05-25 20:36:50. It has garnered 2441 views and 42 likes. The duration of the video is 00:27:58 or 1678 seconds.
How to use the lerp function to blend biomes or Perlin Noise terrains of different amplitudes and frequencies, so that these terrains match up seamlessly at their edges.
Thanks again to Owen Dorsey for the brilliant suggestion 🙂
Link to first video in series: https://youtu.be/r77vL_bmg-o Link to code: https://github.com/RedHenDev/Unity_scripts/blob/master/simpleVoxels_expanded_tuts/Assets/simpleVoxels/seeding/lerpTerrain.cs
Thanks for watching!