From: Daniel Karbach Date: Tue, 28 Nov 2017 16:45:07 +0000 (+0100) Subject: generate base color for creature X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=5b2ab168a9b3c05082bd9cd924f548bc90fc8efa;p=blobs.git generate base color for creature --- diff --git a/src/creature/Genome.hpp b/src/creature/Genome.hpp index 882a8a2..a7be0e1 100644 --- a/src/creature/Genome.hpp +++ b/src/creature/Genome.hpp @@ -57,6 +57,10 @@ struct Genome { }; std::vector composition; + math::Distribution base_hue; + math::Distribution base_saturation; + math::Distribution base_lightness; + void Configure(Creature &) const; static PropertySet Instantiate( diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index 6c9fb85..1196a3c 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -322,6 +322,9 @@ void Spawn(Creature &c, world::Planet &p) { genome.properties.Death().mass = { 0.9, 0.05 }; genome.properties.Death().fertility = { 0.0, 0.0 }; + glm::dvec3 color_avg(0.0); + double color_divisor = 0.0; + if (p.HasAtmosphere()) { genome.composition.push_back({ p.Atmosphere(), // resource @@ -330,6 +333,8 @@ void Spawn(Creature &c, world::Planet &p) { { 0.1, 0.0005 }, // penalty { 0.0, 0.0 }, // growth }); + color_avg += c.GetSimulation().Resources()[p.Atmosphere()].base_color; + color_divisor += 0.1; } if (liquid > -1) { genome.composition.push_back({ @@ -339,17 +344,28 @@ void Spawn(Creature &c, world::Planet &p) { { 0.01, 0.002 }, // penalty { 0.1, 0.0 }, // growth }); + color_avg += c.GetSimulation().Resources()[liquid].base_color; + color_divisor += 0.5; } if (solid > -1) { genome.composition.push_back({ solid, // resource { 0.4, 0.01 }, // mass - //{ 0.1, 0.001 }, // intake { 0.4, 0.001 }, // intake { 0.001, 0.0001 }, // penalty { 10.0, 0.002 }, // growth }); + color_avg += c.GetSimulation().Resources()[solid].base_color; + color_divisor += 1.0; + } + + if (color_divisor > 0.001) { + color_avg /= color_divisor; } + glm::dvec3 hsl = rgb2hsl(color_avg); + genome.base_hue = { hsl.x, 0.01 }; + genome.base_saturation = { hsl.y, 0.01 }; + genome.base_lightness = { hsl.z, 0.01 }; genome.Configure(c); } diff --git a/src/math/glm.hpp b/src/math/glm.hpp index 4b1bf33..46843db 100644 --- a/src/math/glm.hpp +++ b/src/math/glm.hpp @@ -5,6 +5,7 @@ # define GLM_FORCE_RADIANS 1 #endif +#include #include #include #include @@ -35,5 +36,29 @@ inline bool anynan(const T &v) noexcept { return glm::any(glm::isnan(v)); } +template +inline T rgb2hsl(const T &rgb) { + using Vec4 = glm::tvec4; + const Vec4 K(0.0, -1.0/3.0, 2.0/3.0, -1.0); + const Vec4 p(glm::mix(Vec4(rgb.z, rgb.y, K.w, K.z), Vec4(rgb.y, rgb.z, K.x, K.y), rgb.y < rgb.z ? 0.0 : 1.0)); + const Vec4 q(glm::mix(Vec4(p.x, p.y, p.w, rgb.x), Vec4(rgb.x, p.y, p.z, p.x), rgb.x < p.x ? 0.0 : 1.0)); + const typename T::value_type d = q.x - std::min(q.w, q.y); + const typename T::value_type e = 1.0e-10; + T hsl = rgb; + hsl.x = std::abs(q.z + (q.w - q.y) / (6.0 * d + e)); + hsl.y = d / (q.x + e); + hsl.z = q.x; + return hsl; +} + +template +inline T hsl2rgb(const T &hsl) { + using Vec3 = glm::tvec3; + using Vec4 = glm::tvec4; + const Vec4 K(0.0, -1.0/3.0, 2.0/3.0, -1.0); + const Vec3 p(glm::abs(glm::fract(Vec3(hsl.h) + Vec3(K)) * 6.0 - Vec3(K.w))); + T rgb = hsl.z * glm::mix(Vec3(K.x), glm::clamp(p - Vec3(K.x), 0.0, 1.0), hsl.y); + return rgb; +} #endif