Simple Snow Tracks Shader with Unity Shader Graph

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
everyone welcome to another video in this one we're gonna talk about a snow shader with unity shader graph unlike the earlier tutorials we're gonna introduce a little new concept which is custom functions which I'll come in a moment but before that I'd like to talk about what we're gonna do with the snow shader so unlike true planar shavers which like show a snow effect on normal materials this shader is responsible for deforming the actual ground or let's say the terrain weltering is not particularly supported to keep that video simple but you can use custom meshes the only constraint is you need to have very dense mesh what I mean by that is you need to subdivide the mesh and have enough vertices in order to perform a smooth deformation otherwise the deformation might look not appropriate enough so to do that I'll provide a sample mesh to you which is a plain mesh I subdivide it in blender but you can do that with your custom meshes or your custom exported terrain meshes too it works right off the bat and along with that we we're going to introduce render textures in this video so we're going to use render textures blitt them with our deformation trails and then feed it into our shader and our shader will be responsible from getting that pixels this deformation trail pixels and deform the vertices according to the mapping which we will use custom function in that point anyway without further ado let's jump to the tutorial and see how it works we can start by creating a new PBR graph and name it as snow shader now first things first we need to have a trail texture to do so we can create the new expose property and name it as trail map as follows after dragging that property into canvas we need to create a custom function this will be the first time that we gonna use a custom function in this channel so better be explaining what it does and why we need it for so a custom function is a code snippet so it is it is capable of performing shader code operations without writing the entire shader by yourself we need to write a piece of code to make things easier for us it is also supported by other shade their visual editors like in Unreal blueprints or I believe amplifies supports it I'm not quite sure but we have it in the shader graph so we are capable of not using it but if we don't use it we're going to need to create at least 80 nodes to perform that operation instead of using a for loop so we're going to use this custom function write a bit of code which I will explain what it does and how it performs the operation and there the reason that we're gonna need that code is to sample our deformation map and then use it to deform the vertices all around the target vertex what I mean by that is you're gonna have a vertex here and you want to blur the deformation too not to just deform this one and prove like create some sort of weird art effect so you're going to want to blur it at all the neighbor vertices and it is possible to do that with just you know getting the getting a for loop and accessing to the earlier vertex and the following vertex and then distorting them you know having a curve kind of a distortion to make it more smooth so that's what we're gonna do with this shader snippet so let's go back and see how gonna do that back to the canvas we need to set up a few parameters using input and output fields of this custom shader code function first I foremost we need to provide our trail map into here then we're gonna pass UV coordinates and call it as texture chord is the third we need to have a vector one called blue size which I'll explain in a moment then as fourth and fifth parameters we need a sampler state and a height multiplier vector one variables and as the output we need to spit out two modified height value I'll call it as height Pass then I'll convert code type to string and name this function as calculate height as I mentioned before this is actually a shader code snippet which will make things easier for us while coding we're gonna define a float parameter as mean height then in a nested for loop from minus 1 to positive 1 we will add up to this mean height so to actually add a value we're gonna call sample texture 2d LOD macro and pass trail map as the first parameter to sample our map then we're gonna pass sampler state as the second parameter as the third parameter we need to pass UV chords as a float to in there we're gonna pass text chord X plus I times blur size in order to blur that coordinate in trail map with our smoothing radius as the Y value we're gonna do the same thing with Y value and J variable as the fourth variable we're gonna pass an LOD value although we don't need any level of detail so I'll just pass one as a sidenote in that point this macro will return a four-dimensional value but we need only the red channel of the texture so I'll get rid of the other channels and only use the red Channel with typing dot and so as the final thing we're gonna need to return our mean height into height pause variable with some adjustment but before that it seems like our sampler video will cause some trouble so I'll simply rename it as SS then back to our height pause equation I need to divide mean height by total sample count which is 9 after doing that we need to clamp that mean value between 0 and 1 to have deterministic results so unity has a special shader function called saturate to clamp your value between 0 and 1 then I'll multiply this decimal number via our height multiplier to control the total height cruel that's about it this is all the shader code that we gotta write rest of it will be good old shader graph fun okay now we can pass inputs as nodes first of all as the texture cord I'll pass UV into it then as the blur size I need to create a new exposed property and name it as trail radius after that I'll divide this vector one by hundred and connected into the blue side next we'll pass sampler States to input as s and finally I'll expose another variable for height multiplier but before connecting it we're going to divide this by a thousand reason of these division operation is to have none exaggerated deformation visuals otherwise deform surface would look way too off great now this function will give us a distortion on y-axis of a vertex for deforming the ground by snow but we can't directly use this variable because we just have an absolute height value it has nothing to do with the actual vertex position to do so I'll use a position node with an object space then I'll split this value into its vector components and combine it back but before combining the g-value back I'll subtract our deformation height from Y position of that vertex rest of the elements of that vertex can remain as it was since we're only interested with distorting our snow vertically finally if we connect this combined node to vertex position input off PB our master node we should be good to go now let me quickly assign this shader into a material and attach my test trail texture into it and drag the material to my plane finally I'll tweak radius and height multiply our values to observe the distortion as you can see we have a smooth distortion now but also our plane texturing looks pretty nasty to use actual snow textures and normal Maps we're going to go back into shader graph now we need to create a trail texture in the main texture in order to have two separate texture for deformed and non deformed snow surfaces after exposing texture properties I sample them as follows additionally I have a trail color tint value to slightly tint the trail color from outside of the shader graph then I just learnt main texture with trail texture by the way we also need to sample our trail map too as you can see it is pretty much the same thing except I discard alpha value Y a combined node since we don't need alpha value then I'll add a clamp node to control texture blending and use our blending factor as the T input also I'm gonna have a trail blending gap property and hope into max input off clamp in order to actually control the blending from outside now we can connect this to albedo off PBR and go back to our scene so I'll pass the necessary snow texture into my material and tweak color and blending cap values as you can see we have a better result in hand now but that's not enough if you want to render snow you should better be using normal maps and make it look cool to do so back in our shader graph I'll add two more texture for our normal maps and sample them too additionally since these are normal maps I can't use them like a color value I need to unpack normals with normal unpack node after that I'll add a tiling variable to control normal map tiling and assign this into a tiling and offset node then pass it into my samplers finally alert them together as I did for main textures then connected this to the normally put off the PBR back in the scene if I pass these normal maps into my material boom now we're talking but we can do better right let's quickly add a gloss variable to make it literally shine then alert them the same way and connect this into smoothness input off PBR now we can control its reflection glyphs in its rate as the follow up I'll adjust my scene lighting a bit and we have a pretty cool looking deformed snow shader now the question is how are we going to distort this in runtime to do so we're going to play with some smoke and magic I'll create a particle system at the bottom of my deformation cube and start configuring the particle system so if you're going to deform something with your objects you're probably going to need to create separate particle systems or use some sort of mesh emission to actually spawn particles now while I'm configuring the particle system you can find necessary variables on the screen one thing that we need to do now is creating a smooth fade-out particle material we can create a new material and set its either to legacy particle additive soft then pass built in unity particle texture into it after assigning this material into our render material slot I'll tweak min and Max particle sizes as 0.3 you can increase this value by 0.5 depending on your object size whatever now if you do that and if you do it right you will see that our particle system leave trail behind it great now to actually use this trail information we're gonna write it into a render texture but to do that we're gonna need a second camera okay we need this camera to only render our particle system so that it doesn't freak out and draw irrelevant trails everywhere to do that I'll go ahead and create a layer and set this layer to calling mask of my camera then I'll change camera projection to orthographic and tweak the size and clipping plane values as follows then I've repositioned the camera to let it see the particle system properly now we can hook a render texture into our second camera let's create a new render texture set size as 1024 then since we just use the red channel I'll change its color format to our 16s float it is for 16-bit red channel assigned float but you know we call it like this then attach the render text to camera and also put the render texture into your material scene instance after doing that you can see a dynamic deformation also since our particle dissolved by time our snow trails fill up too if you don't want them to fill up by the time you can play with the particle systems lifetime to remove this fill-up effect with that we have successfully created a snow deformation shader congratulations all right that's all for this video I hope you liked it and I hope you can achieve great distortions snowy environments let me know what you're gonna do with this shader and if you liked this video you can press the like button and if you want to share this video with your friends on your discs or channels now you're free to do that also please leave a comment in the comment section below to let me know about what you do with this one how it helps so I'm glad you guys are making awesome comments and you know watching the videos it is really a good motivation for me and until next time see you
Info
Channel: Mert Kirimgeri
Views: 41,582
Rating: undefined out of 5
Keywords:
Id: ThlqTMBzyjI
Channel Id: undefined
Length: 15min 51sec (951 seconds)
Published: Thu May 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.