Shader - Simple Deform (Waving Flag) - [Tutorial][C#]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys welcome back to another shader tutorial today we are going to be looking at deforming or mesh previous episode we change the texture on top of it this time we're actually going to play with the geometry so if we have a quick look at this while it's playing I'm going to remove the maximise on play we have an object that just that waves like a flag when you click on it you see the outline is actually matching to the deformation but this thing is calculated on the GPU only and it's really important to know that this means that your game logic and your game physics are not going to take care or I should care about this total if you just like take a sphere right here I'm going to put a rigidbody on it it doesn't care about the deformation this is calculated only on the GPU and it doesn't really move as you can tell so whatever you let's get right into making this thing I'll be deleting what I currently have and we'll be making it from scratch we have a typical unity plane right here and this is more than for vertex so of course there's one right here right there right there and right here but there is also other vertex in the middle it looks a little bit like this if you can see the collider alright so without further ado let's just jump into making this thing I'll be creating a new material this material is going to be a simple deformation and again new unlit share this is going to be a simple deformation same name I'll just be keeping that convention and let's drag and drop our symbol information right on top of our plane now we're going to double click on symbol information the shader and change the name at the very top so m3k simple information we're now ready to go back in the game and apply this to our shader and here we go and today we're also going to be playing around with some built-in values just like last time we play around with the sin that sin time today we're going to be playing with object to world and let me explain to you something real quickly when we actually go through the vertex trader we don't receive the 3d position of the object in the world we actually receive it in a object format so it's like a whole a whole new coordinate system so receive it in what we call the object format then if we use object to world on those coordinates we're going to be able to receive the 3d position of those vertex and the world so in the game scene this is what we're gonna be using today to just make a little bit of deformation we didn't have to use this I just want to introduce it and I felt like this could be a good opportunity all right so let's open up these simple information we don't even have to worry about any of that we know that we're doing the formation we're modifying the geometries so we want to be right here in the vertex shader now this is going to look like super easy and it's actually his but let's have a look at what we have here the first line we get right here is oh the vertex so the position of our vertex is going to equal unity object so that's our object space - clip position and this is basically just a macro to say take the coordinates we have in object space that's what we get at first and then match it to my screen pixel so I can actually see it at the proper placement so we go from the unity object coordinate to the actual screen space now I'm going to go just in between them and actually declare myself a float 3 this will 3 is going to be for the world position now to get the world position what we have to do is to actually multiply our current view metric so the one we receive in object space and we need to actually multiply it with the object to world Metris so we're going to do multiply so molé molé stands for multiply there is a lot of other keywords like this I just I'm discovering them at the same time as you so right here I would type in the object to world that we saw earlier so object world like this but since the million D came out while not really new but a little while ago they replays the object to world by a unity object to world just like this and this change is is actually done automatically so if you have a shader in the paths that use the object to well the previous version is going to upgrade and you know change your code for the unity object to world and then I'm going to be using the vertex we receive in parameter so V dot vertex just like this and explain why we want to be using this one and not that one that one already got modified this one is already in screen space it's already you know it's ready to be rendered on your players screen that's not the actual matrix well that's not the object matter except like the screen space that's the proper placement at the end however the one right here I want to receive a parameter still has the original values from the object metrics and that's why we're going to be using this one so I can say X Y Z because I only want to affect the XYZ component since we're a float 3 this thing would return you by default it would return you a X Y Z W so like a vector 4 but we only need 3 floats for the world position so now we have our world position what could we do with it there is a lot of things we could do with it and we're going to do a lot of things in the future shader but today we are only going to be using it in some kind of formula to create a wave we're going to be using this to modify a formula in which we deform the mesh but before we go any further let's just say that let's just say this thing isn't here I'd like to show you something real quick so we said that this right here is Z is like almost ready to be render it's actually ready to be rendered and we have the proper position what if we do o dot vertex dot Y so we only modify the Y and we do say plus equal 1 what's going to happen I don't know if you saw it but our mesh went up or it went down actually did it went up or down feels like it went down so we'd have to be - equal Y for it to go up as you can tell we're actually modifying this thing more unifying the position of the vertex all of them right now that's what we're doing and if you have a look at the pivot the object is still a little bit below that I mean it's right here and the mesh is a little bit above so if you have any mechanic if you have any objects on top of this they're going to be like going through the floor basically now if we go a little bit further what if we use the same time like we did yesterday with a pixel shader we get a nice floating platform and like I said earlier if you want to create a sphere and put it say here it is not going to care about the fire it's moving or not so it's just going to do its thing and the collider is going to stay at the same place but hey we get a nice movement right here all right so let's discard this let's actually get our position back and create that wave so what we could do to create that wave is only play with the the Y component again so all those vertex at Y and we could say plus equal sin again we're using a sine operation but this time is the one that takes parameter so you can put it into parenthesis like this now what could we do here we could say well position X so we're making a we're actually getting that value base of the world position X if we go over here our mesh is actually deformed based on the position the X in the world so if we just move it like this you're going to see that since we're moving this thing it actually deforms itself so this is only based on the position of the world now to make this move automatically we could actually add one more parameter that changes every frame and that is something like sin time or time dot W Tondo W is is something that increments it's like tons of time and c-sharp unity basically and just like this we recreated the nice little flag affect the formation effect call it whatever you want we're playing around with the vertex and we're modifying them so that is fairly cool if you ask me all right guys this is actually going to be everything for today thank you so much for watching and I will see you in the next one
Info
Channel: N3K EN
Views: 25,335
Rating: undefined out of 5
Keywords: unity tutorial, unity, unity 5, unity 3d, 3d unity, unity 5 tutorial, unity mobile game tutorial, unity android, N3K, N3K EN, N3K tutorial, waving flag, deformation, shader, deformation shader, vert, vertex, vert shader, frag, fragment, unity 2018, unity 2018 shader, unity 2018 vert shader, appdata, pixel, pixel shader, shaderlab, unity shader, c# shader, hlsl, cg, cg program, math, matrices, matrice, unity 5 shader, unity shader program, unity post effect, unity post effect tutorial
Id: l5dbSS9BcqY
Channel Id: undefined
Length: 8min 34sec (514 seconds)
Published: Thu May 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.