diff --git a/wled00/util.cpp b/wled00/util.cpp index e7dbbca23..c04915031 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -653,34 +653,27 @@ static inline __attribute__((always_inline)) int32_t gradient2D(uint32_t x0, int } static inline __attribute__((always_inline)) int32_t gradient3D(uint32_t x0, int32_t dx, uint32_t y0, int32_t dy, uint32_t z0, int32_t dz) { - //uint32_t hash = perlinHash(x0 ^ perlinHash(y0 ^ perlinHash(z0))); - - // fast and good entropy hash from corner coordinates - uint32_t hash = x0 * 0x68E31DA4 + y0 * 0xB5297A4D + z0 * 0x1B56C4E9; - hash ^= hash >> 8; - hash += hash << 3; - hash ^= hash >> 16; - // calculate gradients for each corner from hash value - //int32_t gradx = (hash & 0x07) - 4; // +3 to -4 - //int32_t grady = ((hash>>3) & 0x07) - 4; - //int32_t gradz = ((hash>>6) & 0x07) - 4; + // fast and good entropy hash from corner coordinates + uint32_t h = (x0 * 0x68E31DA4) ^ (y0 * 0xB5297A4D) ^ (z0 * 0x1B56C4E9); + h ^= h >> 15; + h = h * 0x92C3412B + (h >> 13); - int32_t gradx = (hash & 0xFF) - 128; // +127 to -128 - int32_t grady = ((hash>>7) & 0xFF) - 128; - int32_t gradz = ((hash>>14) & 0xFF) - 128; - return (gradx * dx + grady * dy + gradz * dz) >> 10; + int32_t gradx = (h & 0xFF) - 128; // +127 to -128 + int32_t grady = ((h>>7) & 0xFF) - 128; + int32_t gradz = ((h>>14) & 0xFF) - 128; + return (gradx * dx + grady * dy + gradz * dz) >> 8; // 25bit >> 8bit -> result is signed 17bit max } -// fast cubic smoothstep: t*(3 - 2t²), optimized for fixed point +// fast cubic smoothstep: t*(3 - 2t²), optimized for fixed point, scaled to avoid overflows 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) >> 16; + return (t_squared * factor) >> 19; } // simple linear interpolation for fixed-point values, scaled for perlin noise use static inline int32_t lerpPerlin(int32_t a, int32_t b, int32_t t) { - return a + (((b - a) * t) >> 16); + return a + (((b - a) * t) >> 13); } // 1D Perlin noise function that returns a value in range of approximately -32768 to +32768 @@ -786,5 +779,5 @@ uint16_t perlin16(uint32_t x, uint32_t y) { } uint16_t perlin16(uint32_t x, uint32_t y, uint32_t z) { - return (perlin3D_raw(x, y, z)>>1) + 0x7FFF; + return perlin3D_raw(x, y, z) + 0x7FFF; // scale to signed 16bit range and offset } \ No newline at end of file diff --git a/wled00/wled.cpp b/wled00/wled.cpp index de36908ca..639b7fe0c 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -345,18 +345,18 @@ void WLED::setup() uint32_t end; uint32_t time; uint8_t offset = hw_random(); -/* - for(int i = 0; i < 0xFFFFF; i+=800) { + + for(int i = 0; i < 0xFFFFF; i+=500) { Serial.print(inoise16(i, offset, (offset >> 3))); Serial.print(","); //x Serial.print(inoise16(offset, i, (offset >> 3))); Serial.print(","); //y Serial.print(inoise16(offset, (offset >> 3), i)); Serial.print(","); //z Serial.print(perlin16(i, offset, (offset >> 3))); Serial.print(","); //x Serial.print(perlin16(offset, i, (offset >> 3))); Serial.print(","); //y Serial.print(perlin16(offset, (offset >> 3), i)); Serial.print(","); //z - Serial.print(inoise16(i, offset+i/4, i*2 + (offset >> 3))); Serial.print(","); //mixed mode - Serial.print(perlin16(i, offset+i/4, i*2 + (offset >> 3))); Serial.print(","); + Serial.print(inoise16(i, offset+i/2, i + (offset >> 3))); Serial.print(","); //mixed mode + Serial.print(perlin16(i, offset+i/2, i + (offset >> 3))); Serial.print(","); Serial.println(perlin3D_raw(i, offset+i/4, i*2 + (offset >> 3))); //raw - }*/ + } /* for(int i = 0; i < 0x2FFFF; i+=100) { @@ -419,7 +419,7 @@ void WLED::setup() minval=0xFFFFF; maxval=0; start = micros(); - for(int i = 0; i < 0xFFFFFFF; i+=5) { + for(int i = 0; i < 0xFFFFFFF; i+=50) { uint32_t pos = i + offset; //int32_t noiseval = perlin16(hw_random()); int32_t noiseval = perlin1D_raw(hw_random()); @@ -433,7 +433,7 @@ Serial.print(" perlin1D raw min: "); Serial.print(minval); Serial.print(" max: " minval=0xFFFFF; maxval=0; start = micros(); - for(int i = 0; i < 0xFFFFFFF; i+=5) { + for(int i = 0; i < 0xFFFFFFF; i+=50) { uint32_t pos = i + offset; //int32_t noiseval = perlin16( hw_random(), hw_random()); int32_t noiseval = perlin2D_raw( hw_random(), hw_random()); @@ -448,7 +448,7 @@ Serial.print(" perlin1D raw min: "); Serial.print(minval); Serial.print(" max: " minval=0xFFFFF; maxval=0; - for(int i = 0; i < 0xFFFFFFF; i+=5) { + for(int i = 0; i < 0xFFFFFFF; i+=50) { uint32_t pos = i + offset; //int32_t noiseval = perlin3D_raw(pos, pos+46845, pos+654684); //int32_t noiseval = perlin3D_raw(hw_random(), hw_random(), hw_random()); @@ -463,7 +463,7 @@ Serial.print(" perlin1D raw min: "); Serial.print(minval); Serial.print(" max: " minval=0xFFFFF; maxval=0; - for(int i = 0; i < 0xFFFFFFF; i+=5) { + for(int i = 0; i < 0xFFFFFFF; i+=50) { uint32_t pos = i + offset; //int32_t noiseval = perlin3D_raw(pos, pos+46845, pos+654684); int32_t noiseval = perlin3D_raw(hw_random(), hw_random(), hw_random());