1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
float perlinSound(float x, float y, float res, int[] perm){
x /= res;
y /= res;
int x0 = (int)x;
int y0 = (int)y;
float unit = 1.0f/(float)Math.sqrt(2);
float vecteurs[][] = {{unit,unit},{-unit,unit},{unit,-unit},{-unit,-unit},
{1,0} ,{-1,0} ,{0,1} ,{0,-1}};
int gradiant1 = perm[((x0 &255) + perm[y0&255])&255]%8; // 1---2
int gradiant2 = perm[((x0+1 )& 255 + perm[y0&255])&255]%8; // | |
int gradiant3 = perm[((x0 & 255) + perm[(y0+1)&255])&255]%8;// 3---4
int gradiant4 = perm[((x0+1) & 255 + perm[(y0+1)&255])&255]%8;
float distX = x-x0;
float distY = y-y0;
float vecteur1 = (vecteurs[gradiant1][0] * distX + vecteurs[gradiant1][1] * distY);
distX = x- (x0+1 );
distY = y- (y0);
float vecteur2 = (vecteurs[gradiant2][0] * distX + vecteurs[gradiant2][1] * distY);
distX = x- x0;
distY = y- (y0+1);
float vecteur3 = (vecteurs[gradiant3][0] * distX + vecteurs[gradiant3][1] * distY);
distX = x- (x0+1 );
distY = y- (y0+1);
float vecteur4 = (vecteurs[gradiant4][0] * distX + vecteurs[gradiant4][1] * distY);
distX = x- x0;
distY = y - y0;
float eqnX = distX*distX*distX*(distX*(distX*6.0f-15.0f)+10.0f);
float lisX1 = vecteur1+ eqnX *(vecteur2 - vecteur1);
float lisX2 = vecteur3 + eqnX *(vecteur4 - vecteur3);
float eqnY = distY*distY*distY*(distY*(distY*6.0f-15.0f)+10.0f);
float lisY = lisX1 + eqnY * (lisX2 - lisX1);
return lisY; |
Partager