# Perlin Noise Implementation in VEX

## Recommended Posts

Hello,

For the sake of understanding perlin noise (and due to a little downtime during the holidays) I tried to implement a very simple approach to the perlin noise purely in VEX. I'm just a beginner in coding, so maybe this won't work at all, but for the life of me I can't seem to find a solution.

Here's my code:

float perlin(vector pos;vector freq)
{

float xi = floor((pos.x/freq.x));
float xmingridpos = xi*freq.x;
float xmaxgridpos = xmingridpos+freq.x;                               //define a grid in x-direction

float zi = floor((pos.z/freq.z));
float zmingridpos = zi*freq.z;
float zmaxgridpos = zmingridpos+freq.z;                               //same in z-direction

vector randvec1 = normalize(set(random(xmingridpos,40),0,random(zmingridpos,400)))*freq.x;                               //assign each corner of the cell a random vector
vector randvec2 = normalize(set(random(xmingridpos,80),0,random(zmaxgridpos,800)))*freq.x;
vector randvec3 = normalize(set(random(xmaxgridpos,120),0,random(zmingridpos,1200)))*freq.x;
vector randvec4 = normalize(set(random(xmaxgridpos,160),0,random(zmaxgridpos,1600)))*freq.x;

vector g1 = pos-set(xmingridpos,0,zmingridpos);                               //calculate the distance between the point in question and the grid corner
vector g2 = pos-set(xmingridpos,0,zmaxgridpos);
vector g3 = pos-set(xmaxgridpos,0,zmingridpos);
vector g4 = pos-set(xmaxgridpos,0,zmaxgridpos);

float res1 = dot(g1,randvec1);                                                        //dot product between the distance vector and the random vector
float res2 = dot(g2,randvec2);
float res3 = dot(g3,randvec3);
float res4 = dot(g4,randvec4);

float u=fit(pos.x%freq.x,0,freq.x,0,1);                                            //where the point is in relation to the corners of the grid
float v=fit(pos.z%freq.z,0,freq.z,0,1);

float temp1 = lerp(res2 ,res1,v);                                                  // interpolate the values
float temp2 = lerp(res4 ,res3,v);

float result =  lerp(temp2,temp1,u);

return result;

}

I just wanted the x and z coordinates for simplicity. I'm reading through some tutorials (http://flafla2.github.io/2014/08/09/perlinnoise.html), but he implements a variation (i believe the simplex noise). I just tried to copy what is said in the introduction in the article above.

As I have zero clue about what to do, and if anyone is interested, I would like some insight to understand the inner workings of the perlin noise

Kind regards,

Bajt

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×