Create a Procedural Hatching Shader in Blender Cycles

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi there this is manuel with entigma today looking at more procedural techniques inside of blender one procedural area of blender is a shader editor you can build procedural shaders for cycles and eevee and today i want to build a procedural hatching shade of cycles that comes in handy whenever you want to do some npr rendering so let's dive in and get started for this project i downloaded this boss from a site full of free 3d scans you can find the link in the description and i brought it into blender to apply my shader to it to create some 3d procedural hatching we first will need some lines so let's first apply a shader this is boston call it hatching and then let's go in and delete the principle bsdf as we won't need it for now at least now how can we create some procedural lines let's first create a texture coordinates node if i connect this up you see this is a generated coordinates the model does not have any uv coordinates but we won't need them anyways because we want to create the lines and camera space so we want to use this camera output this camera output gives us the position of the camera so we will need a camera let's create a camera quickly camera and let's use it here in this viewport to make this a little smaller like so and maybe let's go in and make this square like so okay and you can see these coordinates are zero here in the middle of the camera because it is using the camera coordinate space first we want to decide in which direction we want to create the lines and i want to create them in the local y direction so put down a separate x y z converter separate x y z and connect it here let's use the y-coordinates but because the zero point of the camera is in the middle of the frame that won't work so we want to move the entire coordinate space down to do that we want to use a math node and connect it after the separate set it to add and add a value of 10 to this that moves all the coordinates down so now we are entirely in the positive coordinate space and now we want to use a modulo function to turn this smooth value range into a step one we want to turn a function that looks like this into a function that looks like this so like a sawtooth pattern and this can be done using a modulo let me get rid of this annotation quickly so let's duplicate this node and put it here and switch it over to modulo and you can see that we get this fold here because the modular function just gives us a remainder of the division of the incoming value by this second value that is currently set to 10 that means that we have a smooth function that raises up to 10 and then falls down to zero again but we do not want to use 10 instead we want to use one and now you get this alternating pattern but it is still very large so let's use a multiply function before the modulo multiply you just multiply the value range up a little and that gives quite some lines maybe only 20 for now such we can see something here like so so now we have lines if i quickly switch back to one you can see that these values here start at zero and then go up to one and then fall down to zero again that is nice but it is a sawtooth pattern and what we want is spikes so we want lines that are one in the middle and then fall down to black and then alternating up to one that can be done by first transforming this value range into the negatives and then folding the entire function over using an absolute value so let's first convert this to a value range of minus one to one by multiplying everything with two and then subtracting one subtract one so now you can see by this blackness here by this darkness that the values are negative in parts and now if we use an absolute value absolute it folds over the negative part so what happens is that we have this sawtooth pattern like so and then we convert it into the negative meaning that it goes like this and then we use this absolute which will fold everything over that is in the negatives so it will look like this but then this part here will be positive again so it will go down like this and then up like this and then down like this so we end up with this pattern that is only positive okay time to increase the multiplier again from 1 to say 20. and here we have our lines on our object in camera space as you can see if i navigate the camera these lines won't move because they are created in camera space fantastic so we can use these lines for our hatching shader but now think about what we need as a second ingredient and that is the actual shading of this bust so if we quickly create ourselves a principled bsdf and connect it to the output you will see that we have some shading here at the moment it only comes from this hdr in the background so let's create a light source light point light let's quickly switch over to 3d viewport to be able to position this light a little easier let's put it over there and then go to the light source and give it some more power now you can see what's happening here what we want to do is to take the effect of the shader so this shading of the bust and then use this shading as color information to combine it with the lines to create a hatched area where these shading values are grey so where it goes from black to white we want to have lines and these lines should increase and decrease in thickness so the big problem is how can we read this shading as color information inside of the shader and unfortunately we cannot so what we have to do is we have to build it ourselves the 3d shading you see here is called dot product lighting at least it is similar and how it works is the following imagine you have a polygon in 3d and i draw it here in 2d only and then you have a normal vector like so then in the scene there is a light source if the renderer wants to determine the shading of the polygon it creates a vector pointing from the midpoint of this polygon to the light and then it compares both vectors and finds the angle between them it's easy to see if the angle is zero or very low then the polygon gets all the light but if the angle is bigger than 90 degrees this polygon will not get any light to recreate the shading entirely inside of the shader we want to find the vector to the light and then calculate the dot product between the normal and this light vector so let's get started the normal we already have here now we need the light so let's duplicate the texture coordinates and by specifying which object to use in this field down here in our case the .001 light source we get information about the shading point in the space of the light so if we look at the object output that is the position of the shading point in the space of the light to clearly see that we can quickly take this light and move it over here and you see there is the zero point so these colors resemble the vector from the shading point to the light that is pretty much what we need it's just that these values are inverted we have to first invert these values to do that let's create a vector mass node vector mass and let's set it to multiply and multiply everything by -1 to invert the vectors like so then we want to normalize them because the dot product only gives reliable results if both vectors are normalized to normalize these duplicate the vector math and set this to normalize and normalize these vectors now we have the normals at the shading point and we have the vectors to the light source exactly what we need so we now can just use another vector mass to calculate the dot product and here we have it that was the wrong connection we want to compare the normal and these vectors and see what that gives remember that node wrangler is giving me an emission shader for previewing nevertheless i get 3d shading in the viewport because i calculated myself one last thing is that these values might exceed the value range 0 to 1 and that is not a good thing so let's create another math node convert the math let's switch this to maximum and let's use the maximum of this and zero this way we won't get any negative values so now we have 3d shading as color information inside of our shader and we have lines time to combine both first we want to take care about this transition area and you can interpret these colors on the surface of the model as a function so again you have increasing and decreasing values and to control the slope of this function we can just multiply the result so let's duplicate one of these math nodes and switch it over to multiply and connect this here if we multiply this up you see that the result gets harder because the slope of the function gets steeper now we want to transpose this function or at least we want to be able to transpose the function so we duplicate this node again and put it here and set it to add and now we can move the entire function up and down so where you can decide where the 0.5 point is so what this is is just the line equation basically if you have a function y is mx plus t that means this is the slope this m here and the t transpose of the function up and down think of it as having a line and if you increase m this will be steeper and with t you can move it down and you can set the inception point that is what i do here now with this out of the way let's introduce the lines to this shading by subtracting them duplicate this node set it to subtract and subtract the lines from this and you see immediately you get some lines because now you take the shading as a function and you subtract either zero or one or the values in between from it and that gives these alternating lines let's go down again to one here and to zero here and you see now you have these lines and you can control the hardness of the basic shading here transpose it here and then subtract the lines okay that is great see i can just make this harder or less hard but it would be great if i could control the hardness of the final result so nothing easier than that just duplicate this math node and multiply everything by another math node and that is now the hardness of the result so now you have full control over the shader first you can say how hard the initial shading function is and here you can say how hard the result should be the gradation basically can be controlled fantastic and that is basically our line shader it's just that the lines are all horizontal and that is not necessarily pretty why don't we just rotate the vectors that are used to create the lines in the first place we can go up here before the separate xyz and introduce a vector rotate vector rotate and let's put it here between the camera vector and this vector and this node now can be used to rotate these vectors just by using this angle down here and you can see that i now can angle my lines of course because i'm rotating the input vectors so to make this a little more pretty let's go to 1.4 here and to 1.5 here and that gives some decent lines maybe we want even more lines say 30 and here you have your hatching the last part is to make this look a little nicer for this we will need this principle bsdf because i'm building this in cycle for a reason because i want this npr hatching combined with realistic shading of the surfaces but before we set up the shader let's quickly go to the 3d viewport and create a background so let's introduce a cube here and quickly delete these two points so this is now let's bevel this edge like so give this a some subdivisions and set everything to shade smooth and then we want to move it up like so item let's move it up to zero zero one and then i can say object set origin origin to 3d cursor of course the object should be selected origin to 3d cursor like so and now i can just scale this up to give myself a background like so still the bust is flying a little higher now i have this background what about the light let's see position it over there to get some nice shading well back to the shader and now let's use these black and white lines as an input to this principle bsdf but before we do that let's create a color ramp color ramp and let's pipe this result through this color ramp get rid of this n panel through the color ramp and then to the principle bsdf and make it visible and you see the principle bsdf now creates shading and takes a line as an input so we get all the shadows now we can just set the colors to something a little bit more pretty i want to use a red here orangey red and on this side some sort of yellow like so and now to give it more of an npr look i want to color the shadows how can that be done well for the shade arrays i just want to use a different shader so duplicate this principle bsdf and set it to something ridiculous right now like green and now i want to combine these two shaders by using a mix shader node so combine both of them and you see that it's just a mixture now with this vector you can mix between the green one and the one with the lines but we do not want to control this factor manually instead we want to use a light path node for this so look for a light path node and this light path node tells us the type of ray that the renderer is currently shooting and we want to connect the camera array to this is camera array and of course we have to switch the shaders like so and now you see if it is a camera array we get our hatching shader but if it's an indirect ray the green shader is used and that gives green shadows and i found this to be quite nice last but not least the background needs a shader too like so and let's just create one of these orangey colors for the background too like so and now it's probably time to increase the power of the light by a good amount again and that concludes the hatred shader now you have a hatching shader that is combined with cycles a realistic rendering goodness that reacts to the light because we build the shading inside of the shader and use the same light that cycles uses for rendering i hope you enjoyed this tutorial and happy rendering if you like what we are doing please consider becoming a patreon for supporting us and for access to more in-depth courses on topics like volume techniques or pdg or vellum and more to everybody who is already supporting us thank you so much without your continuous support entanglement would not be possible
Info
Channel: Entagma
Views: 32,554
Rating: undefined out of 5
Keywords:
Id: KXD2eQe3F8Q
Channel Id: undefined
Length: 16min 44sec (1004 seconds)
Published: Mon Apr 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.