When procedural modeling in Blender using Geometry Nodes, we often use various parameters of mesh geometry, such as the position of vertices in space, the length of edges, direction of the normals, etc. Also, quite often we need to know the angle between two adjacent mesh edges. Getting it using GN and mathematics is not difficult.
First, add a plane to the scene (Shift + a – Mesh – Plane) and make a triangle by selecting and collapsing two vertices in edit mode (m – Collapse). Move the vertices a bit so that the triangle is not isosceles.
Add a Geometry Nodes modifier and initiate a new node tree by clicking the New button.
To calculate the angle between any two mesh edges, we need to get two vectors lying along these edges and coming out of their common point. And for vectors, using mathematics, we can easily find the angle between them.
Let’s start with vertices, since each vertex is connected to exactly two edges, the angle between which we want to get.
Add the Index node (shift + a – Geometry – Read – Index).
From its Index output, we get a list of indices of all mesh points. In our case:
1 |
[0, 1, 2] |
Having a vertex index, we can get the indices of both edges adjacent to it using the Edges of Vertex node.
This node, based on the vertex index, gets a list of indices of all edges adjacent to it. For example, for a vertex with index 0, edges with indices 0 and 2 will be obtained inside the Edges of Vertex node.
But from the Edge Index output, this node returns only the first index in the list of all the edge indices obtained.
To get the index of the second edge adjacent to the vertex, we can shift the sorting in the list of edge indices of the Edges of Vertex node, using its “Sort Index” field. If we leave the value 0 in it, we will get the index of the first edge adjacent to the vertex, and if we set it to 1, we will get the index of the second edge adjacent to the vertex, due to the fact that the last index is moved to the first place when shifted.
So, add two Edge of Vertex nodes (Mesh – Topology – Edge of Vertex) and in one of them specify the value 1 in the “Sort Index” field. Feed them with the vertex indices from the Index node.
Now for each vertex, we have two indices of the edges adjacent to it.
Now we need to get the coordinates in space by the edge index. To switch from the edge to the vector lying along the edge.
This can be done using the Sample Index and Position nodes.
Add a Position node (shift + a – Geometry – Read – Position). From it, we will get the coordinates for our edges.
For each edge index, add a Sample Index node (shift + a – Geometry – Sample – Sample Index). This node will allow us to get the edge position for the index we specified. Connect the nodes by feeding the geometry, position from the Position node, and index from the Edge of Vertex nodes to the input sockets.
For correct calculation, switch the domain of the Sample Index node to the “Edges” and the attribute to the “Vector” type.
Now, at the “Vector” output of each Sample Index node, we have received the coordinates for the edges relative to the reference vertex. Let’s recalculate them so that the reference point is at the origin.
This can be easily done by subtracting the current value of the vector from its position in space, which we have already received through the Position node.
Add two Vector Math nodes (shift + a – Utilities – Vector – Vector Math) and switch them to the “Subtract” mode.
We can use the Position node we’ve already added or add a copy of it again to make the node tree less confusing.
Connect the vector outputs of the Position and Sample Index nodes for each Vector Math node.
So, now for each mesh vertex, we have two abstract vectors that come out from the origin and are directed accordingly to the edges adjacent to this vertex.
We can now easily get the value of the angle between these vectors using mathematics. The angle between two vectors is defined as the arccosine of the dot product of these two vectors after normalization.
We need to normalize (reduce to unit length) the vectors to get rid of the influence of the lengths of these vectors during calculations.
Add two more Vector Math nodes, switch them to the “Normalize” mode and connect them to our vectors.
Now that our vectors have unit length, we can get a dot product for them.
Add another Vector Math node and switch it to the “Dot Product” mode. Feed it with our normalized vectors.
To get the angle value, take the arccosine of the resulting scalar product.
Add a Math node (shift + a – Utilities – Math – Math) and switch it to the “Arccosine” mode. The output of the node will be our angle.
Now for each mesh point we got the angle between two edges adjacent to it.
Add a Viewer node (shift + a – Output – Viewer), feed it with the obtained result and activate viewing by clicking on the “eye”. In the viewport Overlays, also activate the Attribute Text checkbox to see the output values in the viewport area.
Since the arccosine gives us values in radians, for ease of viewing we can convert them to degrees by adding another math node switched to the “To Degrees” mode before the Viewer node.
Now we can visually control the angle directly on the 3D viewport.