FastLED 3.10.8
Loading...
Searching...
No Matches

◆ snoise16() [2/4]

fl::u16 snoise16 ( fl::u32 x,
fl::u32 y )

Definition at line 130 of file simplex.cpp.hpp.

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

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

+ Here is the call graph for this function: