Creating Procedural Hexagons with Blender's Shader Nodes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I recently learned how to create a procedural hexagonal texture in blender which I used as a base to create these other procedural materials that you see here and I wanted to show you guys how I made it and some of the things I learned along the way three quick things before we get started first I made this with the intent to explain why things work the way they do and to go over the thought process behind it so you can get insight into how to think about these problems as such I'm going to assume you're somewhat familiar with blender Shader nodes because covering the introductory mechanics too would be a lot to pack into a single tutorial secondly this particular tutorial involves a decent amount of math if you're not familiar with all the concepts then I'm hoping you'll at least get a high level understanding of what's Happening by having a visual walkth through and that you'll also be introduced to vocabulary that you can use to do further research if you want to dive deeper into any given topic third I wanted to give a huge shout out to Martine from the art of code his video on coding a hexagonal Shader and Shader toy was pivotal to me understanding the math behind everything thing and I highly recommend checking out his videos if you want to watch a wizard work some magic using the openg GL shading language with that out of the way let's get started by adding a plane and creating a new material for it since a hexagon is a 2d shape we'll be working with UV coordinates by default the origin of the UV coordinates is in the lower leftand corner meaning that we're only seeing the positive values of the UV space now why is this important well the hexagon is a shape with a lot of symmetry if I cut the shape in half from top to bottom we can see that these two sides are the same as each other just reflected from one side to the next same thing if I cut the shape from left to right the top is the same as the bottom just mirrored this means we can save ourselves a lot of effort by drawing one quadrant of the shape and then mirror it to the other three sides to start we'll Center the UV Space by subtracting 0.5 from both the X and Y AIS with a vector math node there are a few different ways we can Orient a hexagon the one we're drawing will have sides that are parallel to the Y AIS which is as good of a place as any to start to get a vertical line we can use a separate XYZ node and take a look at the X values if you've never used this before what we're currently looking at is every x value in this coordinate system represented in grayscale black is zero and below and one is white and above with gray being everything in between since we move the origin X is zero here at the center and it increases in value as we move to the right which is why we get this gradient we can convert this gradient into a defined line with a math node set to greater than using the threshold to adjust where this line sits this gets us a line on the right side to mirror it to the left we can take the absolute value to reflect it to the negative axis note that I'm using a vector math node for the absolute value and not a regular math node a regular math node would work work but we're going to need to reflect the Y values later on so we might as well apply the absolute values to everything at the start with the single node to get the slope sides we'll be using a do product node if you're unfamiliar with the dot product it's a calculation that gives a number that represents how much two vectors are pointing in the same direction if you don't know what vectors are then for the purposes of this tutorial just think of them as lines that have a length and point in a certain direction if they're facing the same Direction the dot product will be positive if they're facing away from each other the dot product will be negative and if they're perpendicular to each other the dot product will be zero for example consider the gradient produced by the dotproduct of our centered UV coordinates and the vector 1 one what we're seeing is the dotproduct calculation of every single Vector in this coordinate space with the vector 1 one so the vectors that point in the same direction are positive which are represented in white as we get to vectors that are Clos Clos and closer to being perpendicular to 1 one the dot product gets closer and closer to zero which results in this gradient once we go beyond perpendicular the numbers are negative which are represented in black with that in mind notice how when I scroll through the X and Y values of the vector we're calculating the dotproduct with we changed the slope of the gradient that means if we can figure out a vector that has a slope that is perpendicular to the slope of the side of our hexagon then we can use the dot product of to the two to draw the edge to get that Vector we'll have to use a little trigonometry let's start with what we know I'll draw a line that represents the vector I'm trying to calculate it'll cross through the origin and is perpendicular to the slope side of the hexagon any Vector that falls on this line will work so really what I want to know is the slope of the line in other words the rise over run because this line is perpendicular we know these are 90° angles by definition one of the properties of hexagons are the interior angles are 120° so the interior angle that is intersected by the y- axis is half of that at 60° since this line forms a triangle and we know the interior angle of all triangles add up to 180° then this remaining angle must be 30° now draw a vertical line from the perpendicular intersection to the xais this forms another triangle the angle near the origin must be 60° since the intersection of the axes total is 90 and we know the other triangle is using 30° of that corner the side where the vertical line intersects with the x-axis forms a 90° angle which means the remaining angle must be 30° it took a little bit of work but we now have a triangle that contains the correct ratios for the slope we want to calculate since we're trying to calculate the rise over run we can take the tangent of the 60° angle to calculate the ratio of the opposite over adjacent sides punching this into a calculator gives us 1732 or you can look up a tangent unit circle to find out that the ratio is the < TK of 3 over 1 to get this value into the dot product you can use a combined XYZ node with a square root or a tangent node be wary that the tangent node is expecting radians instead of degrees so you'll either have to add a two radians conversion or you can type in pi over 3 which is de and radians into the tangent node what you can't do is type the values directly into the dot product because we'll need to normalize these values first which I'll get to in a second for now plug the dot product into a greater than node to make the line discrete so we can see the edges easier and use the absolute value version of our coordinates to mirror the slope to the other quadrants we now have two shapes a diamond that contains the angle slope we want and a rectangle that contains the vertical sides we want to combine these we can use a maximum node to get the intersection of the two now depending on the thresholds you have set for your comparison nodes you might end up with a shape that doesn't look quite right instead of fiddling around with these thresholds to try to get the correct shape let's go back to our DOT product my understanding is that this problem is caused by the length of the vector we've used to calculate the angle of the slope which is affecting how the gradient produced by the dot product scales all the work we did earlier with the triangles gave us the ratio of the slope of our Vector but technically there are an infinite number of vectors that can fall on this line luckily for us we don't need to manually find the one with the specific length we need we can just use a normalize operation which will output a vector of length one which standardizes with the rest of our texture coordinates and this was the reason why we couldn't just directly type the values into the dot product from here just set the greater than threshold to the the same number for both nodes and you should now have a proportional image since the thresholds can be the same we can consolidate both of these into a single node placed in front of the maximum now that we've created the shape let's work on creating the UV grid to tile the shape across looking at this example we can see that the grid consists of two patterns one offset from the other let's start by setting up offset UVS without specifically thinking about hexagons we'll start with the scale node to increase the UV space so we can see more of whatever pattern develops to divide up the UV space and make it repeat we'll use a vector math node set to modulo if you're unfamiliar with the module operation what it does is return the value of the remainder after division so you get this repeating effect where as the value you're dividing increases the remainder continues to increase until you hit a number that is evenly divisible where the remainder drops to zero so as a simple example these are the X values module 1 which means every time we hit a number that is evenly divisible by one the result is zero which gives us black as we move away from that number to the right the remainder increases giving us this gradient until we hit the next number evenly divisible by one which drops us back down to zero with that in mind we're going to need two modular nodes one with the vector add before it so you can create an offset note that this needs to be an add node specifically because we need to module in the positive UV space blender does trunked modulo with the vector node which treats negative numbers differently so you'll get strange results we can use a subtract node after the modulos that Center the repeated UVS without a problem for now I'll arbitrarily add and subtract the X and Y values by 0.5 and modulo by one just so we have different patterns to look at and can see the effects of each node now that we've established these two different UV spaces the question then becomes how do we combine them one way to solve this problem is to create cells by dividing up the space by whichever UV origin they're closest to to do that we can use the vector length which returns a gradient that represents the distance to the origin and since the origins of one UV pattern set at the intersections of the other UV pattern if we combine these two lengths with the greater than node we get this cool diamond pattern which we can then use as a factor with the mixed color node to combine the UV spaces now that we've created offset UVS let's turn them into hexagons to calculate the specific offset we need let's think about the center points if I look at two adjacent hex's in the same row I can draw a line between these two Center points let's also arbitrarily give this length a distance of one notice that this length goes from Center to Center through a shared side that means that the distance from Center to Center for any other hexagon with the share side must also be the same length since these are all the same size size so if we go from the center of one hex in this row to the center of an adjacent hex in another row that distance must also be one using that information we can construct a triangle and using the Pythagorean theorem we can calculate the length of the last leg of the triangle this tells us that to go from one hexagon to the next in the same row we move one unit in the X direction to go from one hexagon to the next in the same pattern we move the square root of three in the y direction since the distances in the X and Y y directions are symmetrical we can conclude that the offset from one row to the next is half of these distances translating this into blender nodes we'll use a combined XYZ to First establish the ratio and plug that into the modulos so that the UV tiles are the correct length and width to fit a hexagon we'll then take half of that ratio and use it to offset one of the rows and to Center the individual UV Spaces by subtracting it after the module operations now that we have the shape we want to draw and a UV grid to tile it on creating the hexagonal texture is a simple as taking the UV grid coordinates and using that as the vector input of our shape note that the threshold for the greater than node needs to be less than 05 otherwise it will look like nothing is happening this is due to the dot product being calculated from the center so it only extends 0.5 in any direction before running into another cell while we're on this topic something cool we could do with the dot product gradient is use it to create a bevel mask which is useful when creating height maps to use with bumper displacement to give your materials more of a 3D feel this particular trick I learned from Arendale where you chain a color Burn mixed with black into a color Dodge mixed with white if you're unfamiliar with what this does the short explanation is that when you have a black and white gradient the factor of the color burn will push black from the Dark Side of the gradient while the color Dodge will push white from the Light Side to get this setup properly first I'm going to multiply the maximum output by two to standardize the range from 0 to 1 then because I want the bevel to be along the sides of the hexagon rather than the center I'm going to invert the color which then gets fed into the color burn but now we have a problem where the greater than threshold controls the size of the hex's as well as the factor of the color burn from a node group perspective you want to avoid having two inputs to control the same thing one way to fix this is to First change the greater than node into a less than so the colors for both of these masks are operating off the same basis then I'm going to multiply the threshold input by 0.5 because when I set up the node group I want the size input to range from 0 to one now if you didn't notice as the value of the threshold for the less than node gets bigger the hex's get bigger but for the color burn as the factor gets bigger the hex's actually get smaller these two are inverse to each other and they range from 0 to 1 so if I want to use a single number to control both I can have the color burn Factor be set to one minus whatever the threshold input is now we have a single input that will make the hex size match for both masks and we can use the factor of the color Dodge to control the size of the bevel itself one last thing I want to show you before we wrap things up is how to turn this tiled grid that repeats the same UV coordinates into an indexed one that assigns a different value to each cell all you have to do is take the base scaled UV and subtract it from the hexagonal UV grid we created this results in a hexagonal grid where every cell contains a single number or color this works because the base UV coordinates and the hexagonal UVS are scaled the same meaning that both will change by the same value when moving the same distance so subtraction at each cell will always result in the color of the base UV located at the origin of the hexagonal UV tile and with that you have the node set up to create four different outputs you have a shape mask a bevel mask a tiled UV grid and an index UV grid using those you can create all sorts of cool stuff limited only by your imagination if you do use this to make something interesting feel free to tag me in it my socials are below and I would love to see what you made
Info
Channel: Left Brain CG Art
Views: 757
Rating: undefined out of 5
Keywords: 3D, Blender, Guide, Hexagon, How To, Left Brain CG Art, Material, Nodes, Procedural, Shader, Texture, Tutorial
Id: PZqXoKPvZ2s
Channel Id: undefined
Length: 16min 32sec (992 seconds)
Published: Mon Apr 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.