Skip to content

ObeidaZakzak/Houdini-VEX-Butterfly-Curve

Repository files navigation

Houdini-VEX-Butterfly-Curve

alt text

Check it on ArtStation

Here's a tutorial on how to make a butterfly pattern with VEX. Everything is done inside of one AttributeWrangler node. The following code is available inside of vex_snippet.c source file. You can also download the .hipnc file where everything is already done.

The Butterfly Curve equation was discovered by Temple Fay, (Wikipedia)

We will use the parametric equations describing this pattern to build points with VEX :

alt text

alt text

alt text

Getting started

All you need is to create a Geometry node and dive inside then create a AttributeWrangler node.

Then set the Run Over mode of the wrangler on Detail (once only).

User controls

Let's start by adding an integer channel to control the number of points to build :

int nbPoints = max(1,chi('points_count'));

Using the max function helps to avoid division by zero as we are using nbPoints value to calculate our parameter step (that involves a division by zero).

(We will come back to add another control later for another parameter in the bonus section)

Parameter for the parametric equations

Now let's define our variable t representing the parameter going from 0 to 12*PI. The value of dt represents the step to add to t at each iteration of the for loop (that we will implement together).

float t;
float dt = 12*PI / nbPoints;

Useful variables for points creation

We still need to define some variables to store the point coordinates at each iteration :

float x, y, z; // point coordinates
float factor;  // a value that we will calculate at each iteration
vector pts;    // the calculated point at each iteration
int points[];  // a list to store the points as we build them, so we can build edges later

Calculating the coordinates

We are ready to start our for loop and calculate all the points of the butterfly curve. first, let's calculate the coordinates (by applying the two formulas in the beginning of the tutorial) :

for (int i = 0; i < nbPoints; ++i)
{
    t = i * dt;
    
    factor = exp(cos(t)) - 2*cos(4*t) - pow(sin(t/12),5);
    
    x = sin(t) * factor;
    y = cos(t) * factor;
    z = 0;
}

We can now create points at each iteration :

    pts = set(x, y, z);
    points[i] = addpoint(0, pts);

And when we have at least one point, we can create edges :

    if (i>0) {
        addprim(0, "polyline", points[i-1], points[i]);
    }

We are done ! Now we have a plane butterfly curve !

alt text

Bonus : bending the butterfly wings

A simple bend can be done by giving the z value something based on the x value. For example, the absolute value divided by a float.

Let's add another parameter channel in the user controls section :

float r = max(1/1000,ch('bend'));

And now let's calculate the value of z based on x inside of the for loop:

  z = abs(x) / r;

Now we can bend the wings of our butterfly curve by modifing the value of bend channel.

alt text

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages