updated scaling

This commit is contained in:
Damian Schneider
2025-03-12 06:56:33 +01:00
parent 4ecc531998
commit 95dcb03f6d
3 changed files with 23 additions and 236 deletions

View File

@@ -631,7 +631,7 @@ static inline __attribute__((always_inline)) int32_t hashToGradient(uint32_t h)
//return (h & 0xFF) - 128; // use PERLIN_SHIFT 7
//return (h & 0x0F) - 8; // use PERLIN_SHIFT 3
//return (h & 0x07) - 4; // use PERLIN_SHIFT 2
return (h & 0x03) - 2; // use PERLIN_SHIFT 1
return (h & 0x03) - 2; // use PERLIN_SHIFT 1 -> closest to original fastled version
}
// Gradient functions for 1D, 2D and 3D Perlin noise note: forcing inline produces smaller code and makes it 3x faster!
@@ -658,6 +658,17 @@ static inline __attribute__((always_inline)) int32_t gradient3D(uint32_t x0, int
h ^= h >> 15;
h *= 0x92C3412B;
h ^= h >> 13;
/*
// fastled version: 25% slower but gives original "look"
h = h&15;
int32_t u = h<8?dx:dy;
int32_t v = h<4?dy:h==12||h==14?dx:dz;
if(h&1) { u = -u; }
if(h&2) { v = -v; }
return (u >> 1) + (v >> 1) + (u & 0x1);
*/
// closer to actual perlin version
return ((hashToGradient(h) * dx + hashToGradient(h>>(1+PERLIN_SHIFT)) * dy + hashToGradient(h>>(1 + 2*PERLIN_SHIFT)) * dz) * 85) >> (8 + PERLIN_SHIFT); // scale to 16bit, x*85 >> 8 = x/3
}
@@ -665,7 +676,7 @@ static inline __attribute__((always_inline)) int32_t gradient3D(uint32_t x0, int
static uint32_t smoothstep(const uint32_t t) {
uint32_t t_squared = (t * t) >> 16;
uint32_t factor = (3 << 16) - ((t << 1));
return (t_squared * factor) >> 18; // scale to avoid overflows
return (t_squared * factor) >> 18; // scale to avoid overflows and give best resolution
}
// simple linear interpolation for fixed-point values, scaled for perlin noise use
@@ -771,25 +782,26 @@ int32_t perlin3D_raw(uint32_t x, uint32_t y, uint32_t z, bool is16bit) {
uint16_t perlin16(uint32_t x) {
//return ((perlin1D_raw(x) * 1168) >> 10) + 0x7FFF; //scale to 16bit and offset (full range)
return ((perlin1D_raw(x) * 895) >> 10) + 34616; //scale to 16bit and offset (fastled range)
//return ((perlin1D_raw(x) * 895) >> 10) + 34616; //scale to 16bit and offset (fastled range) -> 8 steps
return ((perlin1D_raw(x) * 1159) >> 10) + 32803; //scale to 16bit and offset (fastled range) -> 8 steps
}
uint16_t perlin16(uint32_t x, uint32_t y) {
return ((perlin2D_raw(x, y) * 1359) >> 10) + 31508; //scale to 16bit and offset (empirical values with some overflow safety margin)
return ((perlin2D_raw(x, y) * 1537) >> 10) + 32725; //scale to 16bit and offset (empirical values with some overflow safety margin)
}
uint16_t perlin16(uint32_t x, uint32_t y, uint32_t z) {
return ((perlin3D_raw(x, y, z) * 1923) >> 10) + 31290; //scale to 16bit and offset (empirical values with some overflow safety margin)
return ((perlin3D_raw(x, y, z) * 1731) >> 10) + 33147; //scale to 16bit and offset (empirical values with some overflow safety margin)
}
uint8_t perlin8(uint16_t x) {
return (((perlin1D_raw((uint32_t)x << 8, true) * 1168) >> 10) + 0x7FFF) >> 8;
return (((perlin1D_raw((uint32_t)x << 8, true) * 1353) >> 10) + 32769) >> 8;
}
uint8_t perlin8(uint16_t x, uint16_t y) {
return (((perlin2D_raw((uint32_t)x << 8, (uint32_t)y << 8, true) * 1359) >> 10) + 31508) >> 8;
return (((perlin2D_raw((uint32_t)x << 8, (uint32_t)y << 8, true) * 1620) >> 10) + 32771) >> 8;
}
uint8_t perlin8(uint16_t x, uint16_t y, uint16_t z) {
return (((perlin3D_raw((uint32_t)x << 8, (uint32_t)y << 8, (uint32_t)z << 8, true) * 1923) >> 10) + 31290) >> 8; //scale to 8bit
return (((perlin3D_raw((uint32_t)x << 8, (uint32_t)y << 8, (uint32_t)z << 8, true) * 2015) >> 10) + 33168) >> 8; //scale to 8bit
}