A Procedural Polka Dot Shader with Analytic Normals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi there this is manuel with intagma today i return with a blender tutorial and today it will be about shaders i intend to build a procedural shader today i want to create a dot pattern i think it is called a poll cut dot pattern but i don't want to stop at the color component of the shader instead i want to create the color and the normal map for this shader i intend to put little hemispheres on the surface and create the normals for them let's dive into blender and start by deleting the default cube and replacing it with a plane a plane is always good for shader stuff and let's delete the camera but let's keep the light this time because i intend to work with normals and a light in the scene makes it easy to see the normal maps so let's create a new editor and switch it over to be the shader editor like so select the plane and then create a new shader and let's call this dot pattern normals like so let's get rid of the end panel over here i will switch to ev shading to make sure to see something so whenever we want to create a procedure shader we start with the texture coordinate node because this gives us access to the uv map as you can see here let me zoom in a little and you see we have the uv space here and what i intend to do is now i want to use the uv vectors as an input and then derive a z component from them a spherical z component to end up with a normal for the dense well for this i want to use the implicit equation of a sphere and that goes like this x squared plus y squared plus z squared should be r squared so what is this well if you take just any point in 3d space and then you take the components of the point square them and add them up you get the length squared of the vector and this length squared of the vector should equal the radius squared so if you have a sphere and you have any vector from somewhere the length of this squared should equal the radius of the sphere squared this is only true for the points that lie on the surface of a sphere so we can use this equation to calculate the z components of the vectors if we have the x and y components and in our case these x and y components just will be the u and v coordinates here on our surface so let's transform this equation because we are interested in z so z squared will be r squared minus x squared plus y squared like so that is of course a squared quantity so z will be the square root of all of this business here x squared plus y squared brackets like this very nice so by using this formula we can create the hypothetical v coordinate of our sphere but we can make our life even easier as i said if you take a vector and you square its components and then add them up you get the length squared and in this case here we have two dimensional vectors because this will be u and v so x squared plus y squared or u squared plus v squared is the length squared of the uv vectors so this here is the uv vectors and then the lengths squared there is a different approach on calculating the length squared of a vector if you have a vector say a and then you create the dot product of the vector with itself this yields the length of a squared so instead of calculating this we can use the dot product and because x and y is in fact the uv vectors we can rewrite this as the z component that we are after will be the square root of the radius squared and then minus the uv vectors dotted with themselves like so and then the square root of all of this so this is our formula we take the radius squared and then we subtract the dot product of the uv coordinates with themselves and that gives the z coordinate now with the math out of the way let's start to implement this let me quickly get rid of the annotations and let's start but before we implement the z coordinate we want to move the uv space at the moment the origin of the uv space is down here in the bottom left corner and i want to move it over to be here in the middle so the left side here of my plane should be negative and i can do that by creating a vector math vector math and then we take the uv coordinates put them in here and multiply them by two so now this will not go between 0 and 1 but between 0 and 2 in both directions and we only want to do this in the first two components because it's a two-dimensional vector now let's duplicate this vector math and let's subtract one from this and again only the first two components and then if i make this visible you see that i moved over the origin of the uv space to the middle of the square here because i now have negatives and now with these vectors i can start to implement what i just showed you so let's first create another vector math node and switch it over to dot product like so and now let's get the dot product of the uv vectors with themselves this is the dot product now i want to use the radius squared so let's create a new value for the radius and let's put in the radius here and let's subtract subtract the dot product from the radius so remember what we wrote down for our formula now we have the dot product and we subtract it from the radius and now we have to take the square root of all of this so let's duplicate this note and switch it over to square root and let's take the square root of all of this so now let's see what we get here so at the moment we are looking at this subtraction here and we have moved the origin of the ue space over if we now take the dot product we are getting this this is looking very similar to the distance now the difference being that this is a length squared so it is 0 in the middle where we have the origin of the ue space and then the values get higher and higher the further we get away from this zero zero point and now if we subtract this radius here in our case it's 0.5 we end up with this and then we take the square root and that is our z components so now let's create three dimensional vectors out of all of this so let's move this over and to do that we first have to take the first two components from our uv vectors because these will serve as x and y components in our three dimensional vectors and then we want to combine this with the calculated z component so let's create a converter separate x y z to split this vector up into its components and then a converter combine x y d we want to use x and y from the original uv components and the z that we calculated and that gives this sphere here this is the vectors or the normal vectors of a sphere and we now can directly use this inside of our principled bsdf shader by just connecting it to the normal and then looking at it you can see it sort of works i have something that resembles a sphere if i move the light around you can see the normals are sort of working but we have two problems here the first problem is that of course the values for the normal outside of the sphere are wrong outside of the sphere the normal should just point upward and the second problem is this only works because this plane here sits in global world space at the origin as soon as we start to have a three-dimensional object this will fail let's first tackle the second problem by converting these normals that we calculated here into tangent space and then let's just use a normal map node to calculate the correct normals taking the surface orientation into account transforming these world space normals into tangent space is not very hard the main difference is that in tangent space we are limited to the zero to one range so in theory we just do the opposite of what we've done to the uv space earlier we converted from the range minus one to one to the range 0 to 1 and for that we need a vector math because it is vectors vector math let's cut these connections for now quickly and let's get rid of the viewer and let's use this vector here first multiply these vectors by 0.5 and then let's duplicate this vector math mode and let's connect this up let's add 0.5 like so as soon as we do that you see the colors are now this typical purplish yellowish color that you see in tangent space normal maps because we converted the range and now we have a tangent space normal to get our world space normal back we now just need a normal mode so let's create a normal map node and let's connect this tangent space normal map that we created to the color and if we make it visible you see we are back to our normals but that is only the case because the plane has the same orientation as well space this will now work with all orientations at the moment this and that is the same so that was problem number one tangent space normals and now for the second problem the second problem is that outside of our sphere the norms are wrong we want normals that go in this direction we want to retain the normals of the original surface so we need a mask we need a mask that we can use to mix the normals the mask should just be this circle here so the perimeter of the sphere so to speak if you just look at this square root here the z component that we calculated maybe you get an idea how to calculate a mask the z component is only positive for points that are part of the sphere and outside of the sphere the z component is zero so we could use another math node to calculate the mask so let's duplicate the square root and let's connect it here and make it visible and now let's switch this to greater than greater than and down here to zero you see now we get a black and white mask that is one inside of the sphere and black zero outside of the sphere because the z component is only greater than 0 where the points are part of the sphere now this mask can be used to blend between the normals so to blend colors we can just use a mix node mix rgb and we want to blend these colors here with normals that just point in the z direction and to create normals that just point in the z direction and tangent space let's just duplicate this combine x y z and define a normal in the z direction by just putting in zero zero one that is the normal and now we duplicate these two nodes to convert this normal into tangent space now you get this typical purple here now we can use the mix node to mix the spherical normals and the plane normals and now we get this a mixture of both and now we want to connect this mask that we created here at the factor and that gives a mix of both but of course at the moment it's a wrong way around so connect the spherical normals to the second port and play normal to the first port and now you see now we have a spherical normals here inside of a sphere and outside we have the same normals as before and now we can take all of this and connect it to the normal map and put it here in the normal of the principle bsdf and make this visible and after ev updating you see now we have a perfect spherical normal and if i quickly switch over to the 3d viewport here i can move my light around and you see it is working now we have a spherical normal map as you can see so now the next step is to start tiling this to create a dot pattern to tile the uv space we want to use the standard approach of uv space tiling so let's go to the very beginning of our graph before we convert the uv space here and let's first multiply the uv space for that let's create another vector math vector math let's set it to multiply and let's multiply the uvs with a value so let's create another value node and let's say 30 for now and let's multiply the uv space so at the moment this is a uv space and we want to multiply it up now it is very bright because it is multiplied by 30. and now to tile it we want to use the modulo so let's duplicate this vector math and set it to modulo and the modular operation will give you the remainder of a division so if we take these values and they go up to 30 at the moment and we calculate the modulo with one what you get is a lot of tiles 30 tiles at the moment because whenever the value exceeds one it drops down to zero and then it raises again to one and then it drops down to zero because the modular always gives you the remainder by a division with one so now we tiled the uv space and if we now use the tile uv space here instead of the original uv space like so let's see what this gives make our principal bsdf visible and you see here we have our spherical dot pattern and this is procedural now because we can just increase or decrease the tiling here with this value but one problem remains because at the moment this is just a square tiling here we have these dots in rows and it's all square and what i want to do is to move every second row a little bit over to have a pattern that looks more like this where every second row has the dots offset so how can we do this that's a little tricky let's think about it somehow i want to add to the dv space if i take this uv space and before creating the modulo adding to the ue space what i can do is i can move the uv space around as you can see if i add the first component i move in u and if i add the second component i move in v that is just a translation we want to translate every second row by a value of 0.5 to move it over so we have to create a pattern that gives us a value of 0.5 for every second row so how can we create this pattern well we want to do it in the v direction only the very first thing is that we need another separate xyz and we connect the uvs here and you can see the second port outputs just the v values and now we want to multiply these v values and then eventually use a modular on them to create stripes but we don't want to multiply them the same mount as the v's themselves because we only need a stripe for every second row let's create a math node switch it to multiply just like down here connect this here and now we want to multiply it not by this value but by exactly half of it we want to divide this value by 2 and then multiply this uv space here or at least the v component by half of this value so let's duplicate this math node and down here let's connect this and let's multiply this value by 0.5 so that is now half of this value if i put 20 here that will be 10. so now i multiply this here by 10 and then use a modulo but this time a float based modulo by just duplicating this math node and setting it to modulo like so now switch the modulo too use one and you get stripes twice as large as the tiles that we have in our uv space we have stripes that span two rows of our uv space now we can split these stripes in two parts by using a greater than so duplicate the math note once again and use a greater than and then a 0.5 down here what that will give is now the stripes split in half half of it will be zero and the other half will be one because the greater than decide that half of the stripe will be zero so now we have stripes that are zero for the first row of our uv tiles and one for the second row of reveal tiles we could in theory use these to offset our uvs but remember we don't want to offset by a value of one and at the moment we have a value of one here so we have to tone down this value by using a multiply so duplicate this math node and set it to multiply connect it here and just multiply everything by 0.5 that brings down the values that used to be 1 to 0.5 and that is exactly what we need to offset our vectors down here let me make the modulo visible we want to use this offset here but only on every second row so the value that goes in here should be 0.5 for every second row and we have that by using the output of this node here but this is a vector math node so we have to create a vector out of this so let's use another combine x y z to transform this stuff here into a vector where the first component is stripes alternating between 0 and 0.5 and the other two components are just zero because we don't want to add anything to the other two components and now we can use this information output by this combine xyv node to add down here to our uv space and if you now look at the modulo look what happened every second row is offset by a value of 0.5 and that is exactly what we wanted from the get go so let's go back there to our principal bsdf and now you see now you have what we intended now we have dots that are offset and form a nice polka dot pattern but with normals with correct spherical normals and let's quickly give it a different color now you have a procedural polka dot shader where you can here decide how many dots you get and over here you can decide about the radius of the delts remember this is a radius and over there where you create the mask you can even just use a subset of the sphere so you can make the mask smaller and then just use a portion of this sphere so as you can see you have a very flexible shader that reacts to light and it is built completely procedurally which gives you a lot flexibility and that concludes the tutorial i hope you learned something and see you next time 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 a pdg or vellum and more to everybody who is already supporting us thank you so much without your continuous support and tagwa would not be possible
Info
Channel: Entagma
Views: 18,832
Rating: undefined out of 5
Keywords:
Id: 3Z9gvtf8rTQ
Channel Id: undefined
Length: 21min 4sec (1264 seconds)
Published: Mon Jun 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.