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

◆ snoise16() [2/4]

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

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

129 {
130 const u64 F2 = 1572067135; // .32: F2 = 0.5*(sqrt(3.0)-1.0)
131 const u64 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 fl::u32 s = (((u64)x + (u64)y) * F2) >> 32; // (.12 + .12) * .32 = .12: Hairy factor for 2D
135 fl::u32 i = ((x>>1) + (s>>1)) >> 11; // .0
136 fl::u32 j = ((y>>1) + (s>>1)) >> 11; // .0
137
138 u64 t = ((u64)i + (u64)j) * G2; // .32
139 u64 X0 = ((u64)i<<32) - t; // .32: Unskew the cell origin back to (x,y) space
140 u64 Y0 = ((u64)j<<32) - t; // .32
141 fl::i32 x0 = ((u64)x<<2) - (X0>>18); // .14: The x,y distances from the cell origin
142 fl::i32 y0 = ((u64)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 fl::u32 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 fl::i32 x1 = x0 - ((fl::i32)i1<<14) + (fl::i32)(G2>>18); // .14: Offsets for middle corner in (x,y) unskewed coords
160 fl::i32 y1 = y0 - ((fl::i32)j1<<14) + (fl::i32)(G2>>18); // .14
161 fl::i32 x2 = x0 - (1 << 14) + ((fl::i32)(2*G2)>>18); // .14: Offsets for last corner in (x,y) unskewed coords
162 fl::i32 y2 = y0 - (1 << 14) + ((fl::i32)(2*G2)>>18); // .14
163
164 fl::i32 n0 = 0, n1 = 0, n2 = 0; // Noise contributions from the three corners
165
166 // Calculate the contribution from the three corners
167 fl::i32 t0 = (((fl::i32)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+(fl::u32)(SIMPLEX_P(j&0xff)))&0xff), x0, y0); // .16 * .14 = .30
172 }
173
174 fl::i32 t1 = (((fl::i32)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+(fl::u32)(SIMPLEX_P((j+j1)&0xff)))&0xff), x1, y1); // .16 * .14 = .30
179 }
180
181 fl::i32 t2 = (((fl::i32)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+(fl::u32)(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 fl::i32 n = n0 + n1 + n2; // .30
191 n = ((n >> 8) * 23163) >> 16; // fix scale to fit exactly in an int16
192 return (fl::u16)n + 0x8000;
193}
int y
Definition simple.h:93
int x
Definition simple.h:92
static uint32_t t
Definition Luminova.h:55
fl::u64 u64
Definition s16x16x4.h:221
#define SIMPLEX_P(x)
static fl::i32 grad(fl::u8 hash, fl::i32 x)

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

+ Here is the call graph for this function: