diff --git a/include/voroce.h b/include/voroce.h index f519702..797121c 100644 --- a/include/voroce.h +++ b/include/voroce.h @@ -17,7 +17,6 @@ namespace voroce static const auto LCG = 48271; - // These are not recommended because of artifacts especially for hexgrid static int32_t Hash2DLowQuality(const glm::ivec2& p) { return ((p.x * PrimeU) ^ (p.y * PrimeV)) * LCG; @@ -33,22 +32,6 @@ namespace voroce return ((p.x * PrimeU) ^ (p.y * PrimeV) ^ (p.z * PrimeW) ^ (p.w * PrimeT)) * LCG; } - // These are expensive but behave way better - static int32_t Hash2D(const glm::ivec2& p) - { - return std::hash()(std::hash()(p.x) + p.y); - } - - static int32_t Hash3D(const glm::ivec3& p) - { - return std::hash()(std::hash()(std::hash()(p.x) + p.y) + p.z); - } - - static int32_t Hash4D(const glm::ivec4& p) - { - return std::hash()(std::hash()(std::hash()(std::hash()(p.x) + p.y) + p.z) + p.w); - } - // minstd_rand (TODO: use better hash, do something beter here, fast but a bit ugly...) static auto OffsetX(const int32_t seed) @@ -91,6 +74,10 @@ namespace voroce return ((((seed * LCG) * LCG) * LCG) / float(0xFFFFFFFF)) * jitter + 0.5f; } + // constants + static const float sqrt3; + static const float one_over_sqrt3; + // traversal order static const int32_t us2[4][13]; static const int32_t vs2[4][13]; diff --git a/src/voroce.cpp b/src/voroce.cpp index 999f8b9..517bf4f 100644 --- a/src/voroce.cpp +++ b/src/voroce.cpp @@ -7,6 +7,9 @@ using namespace voroce; +const float Voronoi::sqrt3 = std::sqrt(3.0f); +const float Voronoi::one_over_sqrt3 = 1.0f / std::sqrt(3.0f); + // naive implementation std::tuple Voronoi::Evaluate2DRef(const glm::vec2& source, int32_t (*my_hash)(const glm::ivec2 &p), const float jitter) { @@ -1024,8 +1027,7 @@ std::tuple Voronoi::Evaluate2DTri(const glm::vec2& so { assert(0.0f <= jitter && jitter <= 1.0f); - const auto one_3 = 1.0f / std::sqrt(3.0f); - const auto local = glm::vec2(glm::dot(glm::vec2(1.0f, -one_3), source), glm::dot(glm::vec2(0.0f, 2.0f * one_3), source)); + const auto local = glm::vec2(glm::dot(glm::vec2(1.0f, -one_over_sqrt3), source), glm::dot(glm::vec2(0.0f, 2.0f * one_over_sqrt3), source)); const auto origin = glm::vec2(std::floor(local.x), std::floor(local.y)); const auto quantized = glm::ivec2(int32_t(origin.x), int32_t(origin.y)); @@ -1121,20 +1123,18 @@ std::tuple Voronoi::Evaluate2DHex(const glm::vec2& so { assert(0.0f <= jitter && jitter <= 1.0f); - static const auto sqrt_3 = std::sqrt(3.0f); - static const std::array basis = { - glm::vec2(-sqrt_3 * 0.5f, -0.5f), - glm::vec2( sqrt_3 * 0.5f, -0.5f), + glm::vec2(-sqrt3 * 0.5f, -0.5f), + glm::vec2( sqrt3 * 0.5f, -0.5f), glm::vec2(0, 1) }; static const std::array ortho = { - glm::vec2(-1 / sqrt_3, -1), - glm::vec2( 1 / sqrt_3, -1), - glm::vec2( 2 / sqrt_3, 0) + glm::vec2( -one_over_sqrt3, -1), + glm::vec2( one_over_sqrt3, -1), + glm::vec2(2 * one_over_sqrt3, 0) }; const std::array dots =