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

◆ snoise16() [2/4]

uint16_t snoise16 ( uint32_t x,
uint32_t y )

Definition at line 125 of file simplex.cpp.

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

References grad(), P, x, and y.

+ Here is the call graph for this function: