One method is to create a volumetric cylinder and distort it by rotating it around the origin by an amount that varies based on the distance from the origin.
Note that this is a very much "brute force" method of generating a spiral - and it can only produce a spiral with two "arms" (unless you were to truncate the spiral at y = 0
- in which case it would have a single "arm"). There
Another approach is to generate the spiral directly - rather than distorting a bettercylinder - by calculating for each 'sample point' the closest point that lies on the centreline of the spiral. Calculating the distance to the centreline of the spiral allows us to determine whether that point lies within the spiral. This produces more versatile results and does not require manual adjusting of the thickness. It can also generate spirals with any number of 'arms'.
To achieve this we need to be able to calculate the closest point on the centreline of the spiral. For any point in the volume we can determine a number of things about this 'closest point'
- It lies on the 'plane' of the spiral - in our case we can use
z = 0
- The sample point in the volume is along the direction of the Normal of the centreline from the closest point - ie, it points away from or towards the origin.
- The centreline distance from the origin varies based on the angle that the spiral centreline makes in the plane of the spiral (and, therefore, also its normal).
This allows us to calculate the closest point of the centreline for any point in the volume. Once we know the 'closest point' we can simply calculate the distance between it and the sample point to determine whether that point in the volume lies inside or outside the spiral (more mathematicalbased on our desired "arm" radius) method.
Firstly, the angle to the origin - which I'llthis can be calculated using standard trigonometry using the arctangent, since tan(angle) = x / y
so atan(x / y) = angle
. This will only give a result over half the rotation before repeating for the other half, so we need to include additional nodes to add 180 degrees (or 'pi' radians) for the second half of the rotation - ie, when the sign of the y-coordinate flips between positive and negative. This yeilds the following nodes to determine the angle from the coordinates :
The distance from the origin can be determined simply using the Dot Product and a Power(0.5) maths node (again, passing the same vector into both inputs of the Dot Product calculates the square of the distance, the power(0.5) takes the square root, resulting in due coursethe actual length of the vector) :
The modulo function is used to determine which rotation around the spiral arm is the closest to this point and this, adjusted based on the angle (calculated as above) gives us the point on the spiral that is closest to this sample point in the volume.
We can add in some nodes to allow the results to be adjusted (eg, varying the thickness based on the distance so that it thins out towards the ends of the arms) and this produces the following material :
To make it easier to reuse, we can create a node group that takes the vector in the volume, the desired properties of the spiral (number of "arms", "twistedness") :
This can be used as follows :
Now you have a configurable volumetric spiral, the only remaining challenge is to use this to produce a believable galaxy :
To make each galaxy different we can use the Object Info 'Random' property to generate the spiral properties to produce the following result :