FastLED 3.9.15
Loading...
Searching...
No Matches

◆ snoise16() [2/4]

uint16_t snoise16 ( uint32_t x,
uint32_t y )

Definition at line 129 of file simplex.cpp.

129 {
130 const uint64_t F2 = 1572067135; // .32: F2 = 0.5*(sqrt(3.0)-1.0)
131 const uint64_t G2 = 907633384; // .32: G2 = (3.0-Math.sqrt(3.0))/6.0
132
133 // Skew the input space to determine which simplex cell we're in
134 uint32_t s = (((uint64_t)x + (uint64_t)y) * F2) >> 32; // (.12 + .12) * .32 = .12: Hairy factor for 2D
135 uint32_t i = ((x>>1) + (s>>1)) >> 11; // .0
136 uint32_t j = ((y>>1) + (s>>1)) >> 11; // .0
137
138 uint64_t t = ((uint64_t)i + (uint64_t)j) * G2; // .32
139 uint64_t X0 = ((uint64_t)i<<32) - t; // .32: Unskew the cell origin back to (x,y) space
140 uint64_t Y0 = ((uint64_t)j<<32) - t; // .32
141 int32_t x0 = ((uint64_t)x<<2) - (X0>>18); // .14: The x,y distances from the cell origin
142 int32_t y0 = ((uint64_t)y<<2) - (Y0>>18); // .14
143
144 // For the 2D case, the simplex shape is an equilateral triangle.
145 // Determine which simplex we are in.
146 uint32_t i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
147 if (x0 > y0) {
148 i1 = 1;
149 j1 = 0; // lower triangle, XY order: (0,0)->(1,0)->(1,1)
150 } else {
151 i1 = 0;
152 j1 = 1;
153 } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
154
155 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
156 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
157 // c = (3-sqrt(3))/6
158
159 int32_t x1 = x0 - ((int32_t)i1<<14) + (int32_t)(G2>>18); // .14: Offsets for middle corner in (x,y) unskewed coords
160 int32_t y1 = y0 - ((int32_t)j1<<14) + (int32_t)(G2>>18); // .14
161 int32_t x2 = x0 - (1 << 14) + ((int32_t)(2*G2)>>18); // .14: Offsets for last corner in (x,y) unskewed coords
162 int32_t y2 = y0 - (1 << 14) + ((int32_t)(2*G2)>>18); // .14
163
164 int32_t n0 = 0, n1 = 0, n2 = 0; // Noise contributions from the three corners
165
166 // Calculate the contribution from the three corners
167 int32_t t0 = (((int32_t)1 << 27) - x0*x0 - y0*y0) >> 12; // .16
168 if (t0 > 0) {
169 t0 = (t0 * t0) >> 16; // .16
170 t0 = (t0 * t0) >> 16; // .16
171 n0 = t0 * grad(SIMPLEX_P((i+(uint32_t)(SIMPLEX_P(j&0xff)))&0xff), x0, y0); // .16 * .14 = .30
172 }
173
174 int32_t t1 = (((int32_t)1 << 27) - x1*x1 - y1*y1) >> 12; // .16
175 if (t1 > 0) {
176 t1 = (t1 * t1) >> 16; // .16
177 t1 = (t1 * t1) >> 16; // .16
178 n1 = t1 * grad(SIMPLEX_P((i+i1+(uint32_t)(SIMPLEX_P((j+j1)&0xff)))&0xff), x1, y1); // .16 * .14 = .30
179 }
180
181 int32_t t2 = (((int32_t)1 << 27) - x2*x2 - y2*y2) >> 12; // .16
182 if (t2 > 0) {
183 t2 = (t2 * t2) >> 16; // .16
184 t2 = (t2 * t2) >> 16; // .16
185 n2 = t2 * grad(SIMPLEX_P((i+1+(uint32_t)(SIMPLEX_P((j+1)&0xff)))&0xff), x2, y2); // .16 * .14 = .30
186 }
187
188 // Add contributions from each corner to get the final noise value.
189 // The result is scaled to return values in the interval [-1,1].
190 int32_t n = n0 + n1 + n2; // .30
191 n = ((n >> 8) * 23163) >> 16; // fix scale to fit exactly in an int16
192 return (uint16_t)n + 0x8000;
193}
int y
Definition simple.h:93
int x
Definition simple.h:92
static uint32_t t
Definition Luminova.h:54
static int32_t grad(uint8_t hash, int32_t x)
Definition simplex.cpp:74
#define SIMPLEX_P(x)
Definition simplex.cpp:35

References grad(), SIMPLEX_P, t, x, and y.

+ Here is the call graph for this function: