The Switch Node | 5-Minute Materials [UE5]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to 5 minute materials the show where we demystify I've forgotten how the punch line goes but today we're going to be looking at the switch node uh now this is one that actually went under my radar for quite a while why is my screen hold now this is one that went under my radar for a little while uh I believe it was introduced somewhere in unreal 5 and if we just take a quick look at it I'll show you what it does do so if we search switch uh now this is different to static switch so this switch node you'll see it has a switch value it has a a default value and then it's got just an empty an empty boy however if we take a look over here you can see that when I click on this node we have the ability to create an input and another input and another input and another input and another another input and we are also able to name these pins so this one can be I don't know Linguini now you're probably wondering Aron Chini isn't actually a form of pasta but we're going to ignore that for now so if we Chuck this into our base color we get an error straight away cuz we're not putting anything in so something has to go into to all of these values uh and let's just put uh let's put some colors in and let's just go and randomize all these colors the default is zero the current value is also zero so what happens when we change this to I don't know 0.5 well nothing happens what about 0.9 also nothing happens but as soon as this cracks past number one we are now using the the different switch switchy thing so you can see we can cycle through these whatever whatever very cool uh if we go outside of our value um I believe it uses the yeah the default so if the value isn't present uh or we put in like a negative number or something then it will use this thing this switch here is a some sort of turnery operation uh it isn't a true branching switch like the static switches are so static switches can only be changed in a material instance and when you do actually click you know these switches um it will actually recompile the Shader which is why they can't be done at runtime so you can create like a master material and then you can have a switch that says you know use uh detail texture or something and it might be like an expensive operation and you don't want it to be running in like the background of anything that inherits from that M material so you know that's a way that you can like turn on and off features per material however this switch on the other hand isn't recompiling the Shader every time the value changes um so all of the you know you could call them branches um all of the selectors are running over the top of each other even if they're not visible you know on the Shader um so if you've got you know 100 different things going into this switch all of them are going to be running in the Shader now gpus are like Giga fast at doing that kind of thing it's usually much more expensive to actually do a branching operation so for a GPU to evaluate a branch and then not execute you know anything past that point is usually a lot more expensive than just doing all of it um even though only some of it is actually visible you know on the screen however one thing to keep in mind um if this is a static value I'm pretty sure it actually does only include you know the the one that it's set to I don't know when that would really come in handy I guess in like a material function where you've got like a bunch of I don't know different effects or something um and you just chucking them all into here and in your master material you're like with which you know effect do I want to use oh we want to use number two in this one anyway moving on to some actual use cases now if you've been following my channel for a little while uh and you've seen either my video about reducing draw calls getting rid of material slots by doing like a big Giga blend uh material function or the video about like my character Mega material setup which uses functions that look like like uh this so as a little refresher all of the props in my project have a like a secondary UV Channel where all of their UVS are like collapsed down into little groups and put in kind of columns and then we use those you know the values so like 1 2 3 4 etc etc we use them to separate you know the colors the textures and the effects and all that kind of stuff which eliminates the use of extra material slots which reduces the the draw calls that we're you know that we're shooting from the CPU to GPU so if we take a little look inside here you can see that there is uh a little bit of math going on it's very light math it looks like a lot it's also got like little elimination switches some people set up this using like a like lur like a chain of lur slurps or a chain of if nodes which aren't true branching if nodes they're just turner you know it evaluates both sides this whole setup here could be simplified by just doing our switch node get the text coordinates uh we want to just get the r Channel like the the horizontal axis uh we multiply that by however many segments we want so let's just do 10 for now and plug that into the switch value now what that's doing if you're having trouble visualizing it is our UV space uh is usually 0 to one and 0 to one in this direction but we don't care about that for now so let's imagine this is zero uh and we have like a slight upwards to one uh what we're saying is multiply all of these values by 10 so the line now looks like this this where this is 10 and you know if this was 0.9 it will now be just 9 the the integer 9 and 0.8 is now 8 etc etc so if we plug all those colors back in you can see that we instantly have our 10 little segments and they all just get spat out here uh obviously I don't have anything in the extra segments now if if you wanted to do a grid so like horizontal and vertical you could simply just get the G channel the number of uh columns and then you would simply just chain these together so you would have one of these going into there one of these going into there one into there this would go into all of these this is going to take me a few minutes so I'm probably just going to skip ahead and then I just have to hook up the colors so I'll do do the colors so that they're in like a kind of crisscross pattern all right and now when I hit compile uh what the huh ah I didn't plug this in haha there we go okay we have our our grid pattern um so you could use this to separate UV Islands you could use this to I don't know if you just wanted to make a funny pattern then good for you another thing you could use this for is just switching between textures so maybe you've got like a a really high resolution like facial uh texture uh you know like like kind of cartoony style where they just switch between different face textures but you don't want to pack them all into a you know like a 16k texture Atlas and use like an actual flip B you just want to be like okay here's this character's happy texture their sad texture their whatever texture you whack them all into here and then you just have a parameter that you can change on the fly so you just be like okay when the character's sad we want to change this to one and when they're happy we want to change it to three and that would just you know update the uh that would do all the the kind of switching between the textures for you all right so it's time to set up our our kind of test scenario to see uh if either is cheaper than the other so our test conditions are going to be uh we're going to get I don't know 20 of these oh maybe we'll just do 10 or I think what's my material props Atlas go to it goes to 16 so we'll do 16 and we're going to hook up all of these things so we now have our 16 little segments uh so we're going to get one and then we're just going to duplicate it and then we're going to duplicate that 10 of them okay there we go okay now we're starting to uh to dip under my frame rate cap so our test is thus we have 200 translucent planes over the top of one another so in theory we are running the pixel Shader 200 times per pixel we're going to keep the camera in the the same spot if we just put in just a static value into the base color our Baseline is just around 90 FPS if we put in a texture we go to about 85 86 87 Now using this switch uh we're at about 80 87 88 89 cheaper than a texture at least relatively speaking uh then if we use my old uh atlasing function 88 87 so maybe one frame lower than uh than this method I'd say this has a a slight Edge which is great because you know it's uh it's much simpler and cleaner uh now in terms of instruction count cuz I know everyone you know cares about their instruction count and how you know how green their materials look in the Shader complexity model my old Atlas uh 179 instructions and the switch has 186 instructions so like according to you know Shader complexity enthusiasts my method would you know oh it's it's cheaper it's cheaper uh but in reality it's actually very slightly slower according to our extremely scientific testing that we're doing right now yeah yeah well there you go so that's going to conclude it for today's video I hope that you know having this node brought to your attention has opened up some new new avenues for your uh your Shader Journey as always if you want to support monetarily the best way to do so is through the patreon for as little as $1 per month thanks to all of the existing patrons you guys are spectacular uh if you want to see me do live development I'm streaming on Twitch most days of the week and with that we say goodbye [Music] goodbye [Music] [Music] n
Info
Channel: PrismaticaDev
Views: 14,440
Rating: undefined out of 5
Keywords: unreal engine, unreal engine 5, ue5, ue5 shaders, ue5 materials, ue5 effects, unreal engine materials tutorial, unreal engine shaders, unreal engine nanite, unreal engine lumen, ue5 graphics, ue5 material functions, material function, ue5 environment, ue5 characters, prismaticdev, prismatica dev, 5 minute materials, ue5 switch node, ue5 material atlas, ue5 draw calls, ue5 reduce draw calls, ue5 uv atlas
Id: 9GhOr33Uvxo
Channel Id: undefined
Length: 13min 0sec (780 seconds)
Published: Fri Oct 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.