Simulating 100K Boids with Compute Shaders in Godot - Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello everybody Welcome Back To Part Two uh in this part of the tutorial we will be covering uh the ability to color the boids so we will'll show you how to set this up so you can set them to be whatever color you like uh as well as some more advanced modes like coloring them based on the direction they're flying they're heading and finally connecting them to color based on the amount of friends that they have sort of a heat mat View so if you're interested in this let's see how we do that going to start simple by allowing you to change the color of the Void so I'm in my particle Shader now make sure that you are in the particle Shader or you have the scene uh the void particle scene uh selected otherwise you won't be able to uh control the variables uh over here they'll be it'll be locked out unless you want to make make this unique so we're going to make a uniform back four discolored color you should see uh over here color pops up here and if we add a hit called a source color you could actually change this to uh be a color selection which is nice so we go in here let's say we want to make the boys red uh we can set that and then this is a built-in variable color like you see for transform above where we can actually control the color so let's set that to our variable and let's run it and see if these voids come up there you go you get your new color the other thing I'm going to do because I think it makes it more visible is change change the uh environment to be black that environment the clear color here I'm just going to make this black close that maybe these guys a little brighter let's run it again okay there we go so that's what we're going to go with for for S the color what I want to do is add it to that panel we had where we can control uh all of the other aspects of the Void so we're going to go to our main here and I got to use that okay so back in our main function here we want to add a variable so that we can actually control that color uh there's a cool thing you can do here click on the main scene to group these uh so let's say these are all the void settings uh I can do a uh export category and I can give this a name I call this like void settings and what you'll see happen over here is now this has a a little title uh for void settings and then if I want to have another category um or like rendering settings let's call it that uh I can do this and then I'm going to create a variable export and this will be a color I'm going to call this variable void color and I'm going to set it equal to a default color start with boring white and I'm going to use the setter and getter to set this I'm going to talk about some of the applications with doing this so set the new color that comes in uh void color color equal to new color but I need to do a check here because I want to set this on that particle node and the particle node won't be instantiated by the first time this gets hit because I made an export variable so when you make something in export variable it will call the setter and getter as this is coming up it'll call it before the unready function the tree won't be there this this will fail say it's call something on a no on a on a null so by doing this uh if is inside tree it will'll check to see if that's if if we're there yet if it's been instantiated if it hasn't it'll set it uh I'm going to set this on the void particles I need to get the processed material and then I need to set the Shader parameter that we called color and I'm going to set it with the void color now if you what did I do here should be color okay now if I run this one problem you'll see is they don't come up white because this hasn't been called because the first time it it went into this this was they were weren't inside the tree so it didn't call the satin now uh if I set this after the fact so let's I don't know let's pick a different color how about how about blue uh and then go look at that now you can see this set so if we want to fix that one thing that we can do is basic I don't know if this is the best way to do it but this is one of the solutions I have is we can go in here and our ready function and we can just set vo color equal to Vo color and that will cause that to trigger again once you get in on ready so now you'll see they starting color is blue they will come up blue if I change this to Orange now that should come up orange all right so we're going to be using this technique for some other variables so I just wanted to talk through that uh quickly before we we went further now that we have the basics for coloring voids let's do something a little more uh interesting so we want to color these voids based on the directions they're heading and the way we're going to do that is by mapping their heading to a texture map uh so on the right you can see a texture map it's important for this if you want this smooth flow of 360 that uh it sort of wraps so you can see I've got red at the top of the texture red at the bottom of the texture uh in text Maps they use what's called UV coordinates the top left is 0 0 the bottom right is one one and so we want to map uh we'll just use zero for U the X Direction but the V direction we're going to map The Heading by dividing it by 360 which is the uh the max heading here and you'll see for heading of zero we'll be at the top there we'll get uh red boy 0id 360 is0 uh and then you can step through the other values here just to get an example there's 90 180 would put us there 270 would put us there so let's see what that looks like uh to implement I'm going to use a tool called to create this uh texture map for the heading so file new I'm just going to make a one pixel wide by 32 pixel High image pretty small but that's all we need for this and then I'm going to use the full saturation Spectrum gradient to create this so if I just start at the top I can hold control and drag down to the bottom here I get a nice texture map that I can use to to map my headings so then I'm going to do a file export I'm going to export this as a PNG I'm going to put heading map and Export that all right now I'm back in uh gdo I'm going to go back to my particle Shader and bring that up and I'm going to add a new variable uh this is going to be uniform sampler 2D I'm going to call this uh heading map okay once I do that you should see this pop up over here here's heading map uh I'm going to get that heading map sorry that I created I'm going to drop that in the particle Shader folder so now I have that and I can set that uh over here so there's our our heading map is in there and now I'm going to set the color so let's just comment this out for a second show you how this works color equals texture and then I'm going to use the heading map and what we talked about earlier uh the U is the UV I'm going to give it zero uh for the X so we got one pixel Y and then for the heading I'm going to convert this two degrees I have a hard time thinking at radians and that information comes from that texture that has the boy data it is in the uh blue so red is X position green is y position and blue is the rotation so uh we've got our blue there and then we need to divide that by 360 like we talked about to map that uh between 0 0 and 1 for the UV coordinates so now if we run it you should see the boids flying uh getting colored base on the direction uh that they're they're flying and there we go this is a little chaotic with this this many voids I might turn that down let's adjust that really quickly just while working on this go back into Main and let's just try like 5,000 so we pop this up okay that's a that's a little better so there you can see now they're being uh colored by the direction that they're heading I would like some way to be able to basically toggle or pick which color mode I want so we're going to go through adding an enumeration to select the different color modes so uh let's put this in the same section that we had here so I'm my main and over here get this rendering section so as long as this is the active one this is where this the variables will that will go so I'm going to export a variable called uh void color mode and I'm going to make this and a n so let's create that an new right right above this here so a new void color mode and come up with some names for the solid and let's call this one heading so we've got two color modes here I'm going to use that set oh je set and then I'm going to set the void color mode new color mode and then the same thing I did before I need to check and see if we're inside the tree and now we are setting we're going to set a variable called color mode uh to the void color mode okay we haven't created this yet we're going to need to go back in and create this uh also we'll need to do that same thing that we did before that all right let's go back to our Shader and add the color mode so here we are the Shader We'll add uh this be a IGN in color mode and then down here I'm going to create a switch statement that's based on the color mode and unfortunately I can't use the en within the Shader so we just have to know that uh case Zero is solid color so if I'm in case Zero then I'm just going to set the color equal to the color bre on that and if I'm in case one then I'm in heading so and then I will use this new heading code that we just created clean this up and hopefully that's all good let's let's do that trick where we break this out and make this floating we'll run it and let's see uh let's see how this works now so I should be able to go in here I can change my color in real time make this brighter so you can see these but I can also go in here and select color by heading uh and so now you can bounce back and forth and they're all flying the direction it makes them all the same color so there you see okay we're going to add another color mode which colors the voids based on the number friends that they have so sort of like a heat map of you know who where where the most uh friends are within the the flock and so so same idea here we got texture map that goes from 0 0 to 1 one this time we're going to map the b value based on the number of friends divided by the maximum number of friends so you know you can have a sort of a different high high end of friends if you've got 100 boids vers say 10,000 boids so you may need to adjust that Max friends to to get the visualization that you want uh same idea here we check this boid it has zero friends so that would be colored blue uh at the top uh this boid has one friend so you can see as we walk down here how the different colors map based on the checks uh when these boids uh see how many friends they have you do that for all the boids you're going to end up with sort of this this cool heat map pattern so look at how to implement that next all right we're back in so we're going to create another image we just going to do 1X 30 2 again uh and this this texture map will be a little bit different I'm going to use the counterclockwise full saturation you'll see what we're going to do here so I'll start at the top bring this down here but in this case we don't want it to wrap we want we want the blue the least amount of friends to be uh at the zero end so I can take this I can drag this further out until I get uh just blue on that and maybe we go one more jeez okay now we've got the deep blue there we've got red at the other end uh same thing we'll export as we'll call this uh friend map export that export now we're going to connect the front map up to the Shader here so do uniform sampler Tod FR map yeah I'm also going to make a variable for the max friends that we talked about friend this default down to 10 and then what's nice is now that we have this case statement it's easy to add new color mode so case two we need color equals texture friend map to again zero for the U for the V it's going to be uh well we probably realize problem because we don't know we don't know the number of friends but what we could do is store that information uh in this void position rotation in the alpha Channel because we're not using that for anything uh right now for the data we've got XY rotation so we'll put it there uh and then we will divide that by the max friend and that will give us our uh UV uh but then I want to clamp this value between uh basically zero and one so we don't get out of balance with this okay we're good on that let's look at our Shader here STI back in the window here the inspector if we click on the void particles we've got place for our friend map now store out here put that there front map set that there okay now we've got our front map as well now when we' run this let's let's pop this out again and run it uh you'll see that now now these are being colored by the amount of friends they have so if you wanted to adjust uh the threshold to click on our main scene here this down you want to adjust the threshold you can turn down the number of friends they would need to have to go red so 50 is pretty high for this amount so let's try 25 uh let's try 10 so now we're starting to see some that are showing up red so anyone that's R has you know 10 friends or more so a cool way to sort of get a get an idea or a heat map of the amount of friends that these these voids have as they're they're blocking okay I hope you enjoyed this video we learned how to change the color of the boids uh color the boids by heading as you're seeing here and then also color them based on the number of friends that they have uh in the next video we'll move on to the spatial bidding uh and go after increasing performance
Info
Channel: niceeffort1
Views: 658
Rating: undefined out of 5
Keywords:
Id: GoIv5TlLh9M
Channel Id: undefined
Length: 21min 59sec (1319 seconds)
Published: Tue Jan 30 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.