From 3ca3b1daa8cbd63d8ca51d16d90679dcc116f673 Mon Sep 17 00:00:00 2001 From: Lucian Plesea Date: Wed, 24 Jul 2024 10:47:48 -0700 Subject: [PATCH] Tuned hillshade formula a bit --- docs/lerc1dec.wasm | Bin 57586 -> 57618 bytes rasterfunk.cpp | 31 ++++++++++++++----------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/docs/lerc1dec.wasm b/docs/lerc1dec.wasm index 13d9058405ae6942426eb9e3852bb3081e3e0843..d7e1ba9756b442aba2a0c1ef6d13e567d43134f3 100644 GIT binary patch delta 156 zcmex#ka^M}<_*UgnHqm@KFK(vpQ%@Va`Hrr&2uMO^BFMJD@ZBGW+`!NFfk}`IdYUa z{%0)Q;lcn0Z|oOASq=^h6j*1@S7M#~XTGis7am1G1*((d7x2h)ISSx2Y;*mBVg~^D Ci7jOS delta 128 zcmbPqi22h&<_*UgnI8Vye3EfSKhs;e$;lHf80$CBnrOqPqQI)ax=4vtK}tb3ONm>9 zi9w0Wk)zD-NDR3#lbN)&mR>VfLHFk}ryK%xR@+BVN$Q0xEz D&-Ezo diff --git a/rasterfunk.cpp b/rasterfunk.cpp index 4931edc..7592833 100644 --- a/rasterfunk.cpp +++ b/rasterfunk.cpp @@ -56,7 +56,8 @@ int hillshade(float *data, size_t sz, double pixel_size, double sun_angle, uint3 constexpr int TILESIZE(256); constexpr int DATATILESIZE(257); constexpr uint32_t ALPHA(0xff000000); - + constexpr auto base(32.0); // baseline brightness + constexpr auto rescale((256 - base) / 256); // brightness rescale factor for (int y = 0; y < TILESIZE; y++) { for (int x = 0; x < TILESIZE; x++) @@ -66,24 +67,20 @@ int hillshade(float *data, size_t sz, double pixel_size, double sun_angle, uint3 double slope = (rightp - leftp) / 1.41 / pixel_size; double normal_angle = std::asin(slope) - sun_angle + M_PI_2; double factor = abs(std::cos(normal_angle)); // 0 to 1 - factor *= factor; // square - - // If the input is grayscale: - // uint32_t val = pixels[TILESIZE * y + x] & 0xff; // grayscale byte - // val = 64 + val * 3 / 4; // Keep a baseline brightness of 64 - // val = uint32_t(val * factor); - // pixels[TILESIZE * y + x] = (val * 0x10101) | ALPHA; + // negative power decrease the effect, positive increase it + // -2 is a good value for a subtle effect + factor = sqrt(factor); - uint32_t red = pixels[TILESIZE * y + x] & 0xff; - uint32_t green = (pixels[TILESIZE * y + x] >> 8) & 0xff; - uint32_t blue = (pixels[TILESIZE * y + x] >> 16) & 0xff; - uint32_t alpha = (pixels[TILESIZE * y + x] >> 24) & 0xff; - red = (64 + red * 3 / 4) * factor; - green = (64 + green * 3 / 4) * factor; - blue = (64 + blue * 3 / 4) * factor; - alpha = 255; + uint32_t val = pixels[TILESIZE * y + x]; + uint32_t red = val & 0xff; + uint32_t green = (val >> 8) & 0xff; + uint32_t blue = (val >> 16) & 0xff; + uint32_t alpha = (val >> 24) & 0xff; + red = (base + rescale * red) * factor; + green = (base + rescale * green) * factor; + blue = (base + rescale * blue) * factor; - pixels[TILESIZE * y + x] = (alpha << 24) | (blue << 16) | (green << 8) | red; + pixels[TILESIZE * y + x] = ALPHA | (blue << 16) | (green << 8) | red; } } return 1;