MCPcopy Create free account
hub / github.com/aardappel/treesheets / SimplexRawNoise

Function SimplexRawNoise

lobster/src/simplex.cpp:84–141  ·  view source on GitHub ↗

2D raw Simplex noise

Source from the content-addressed store, hash-verified

82
83// 2D raw Simplex noise
84float SimplexRawNoise(const float x, const float y) {
85 // Noise contributions from the three corners
86 float n0, n1, n2;
87 // Skew the input space to determine which simplex cell we're in
88 float F2 = 0.5f * (sqrtf(3.0f) - 1.0f);
89 // Hairy factor for 2D
90 float s = (x + y) * F2;
91 int i = fastfloor( x + s );
92 int j = fastfloor( y + s );
93 float G2 = (3.0f - sqrtf(3.0f)) / 6.0f;
94 float t = (i + j) * G2;
95 // Unskew the cell origin back to (x,y) space
96 float X0 = i-t;
97 float Y0 = j-t;
98 // The x,y distances from the cell origin
99 float x0 = x-X0;
100 float y0 = y-Y0;
101 // For the 2D case, the simplex shape is an equilateral triangle.
102 // Determine which simplex we are in.
103 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
104 if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1)
105 else {i1=0; j1=1;} // upper triangle, YX order: (0,0)->(0,1)->(1,1)
106 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
107 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
108 // c = (3-sqrt(3))/6
109 float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
110 float y1 = y0 - j1 + G2;
111 float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
112 float y2 = y0 - 1.0f + 2.0f * G2;
113 // Work out the hashed gradient indices of the three simplex corners
114 int ii = i & 255;
115 int jj = j & 255;
116 int gi0 = perm[ii+perm[jj]] % 12;
117 int gi1 = perm[ii+i1+perm[jj+j1]] % 12;
118 int gi2 = perm[ii+1+perm[jj+1]] % 12;
119 // Calculate the contribution from the three corners
120 float t0 = 0.5f - x0*x0-y0*y0;
121 if(t0<0) n0 = 0.0f;
122 else {
123 t0 *= t0;
124 n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
125 }
126 float t1 = 0.5f - x1*x1-y1*y1;
127 if(t1<0) n1 = 0.0f;
128 else {
129 t1 *= t1;
130 n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
131 }
132 float t2 = 0.5f - x2*x2-y2*y2;
133 if(t2<0) n2 = 0.0f;
134 else {
135 t2 *= t2;
136 n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
137 }
138 // Add contributions from each corner to get the final noise value.
139 // The result is scaled to return values in the interval [-1,1].
140 return 70.0f * (n0 + n1 + n2);
141}

Callers 1

SimplexNoiseFunction · 0.85

Calls 2

fastfloorFunction · 0.85
dotFunction · 0.70

Tested by

no test coverage detected