Use size from dna on the tree

This commit is contained in:
Nikola Petrov 2025-01-02 15:31:51 +01:00
parent 5670183dc0
commit f60b63e082
3 changed files with 59 additions and 17 deletions

View File

@ -9,9 +9,10 @@ struct DrawArgs
{
Vector2 start;
float angleDeg;
float lenghth;
float length;
int dep;
Color parent;
int size;
};
class Tree
@ -28,13 +29,15 @@ private:
Dna *m_dna;
uint128 branchSeed;
int size = 0;
int canvasSize = 0;
Vector2 start = {0};
std::list<DrawArgs> draw_calls;
void drawBranch();
inline uint8_t get_num_of_branches(uint8_t dep);
inline int get_num_of_branches(int dep);
inline Color get_start_color(DrawArgs &arg);
inline Color get_end_color(uint8_t dep, Color &start);
inline Color get_end_color(int dep, Color &start);
inline int get_start_size(DrawArgs &arg);
inline int get_end_size(DrawArgs &arg, int start);
};

View File

@ -36,6 +36,7 @@ struct Branch
uint8_t size;
uint8_t size_parent;
uint8_t size_level;
uint8_t size_change;
uint8_t size_var;
uint8_t length;

View File

@ -15,10 +15,18 @@ constexpr int max_color_change = 15;
constexpr int min_color_change = -15;
constexpr float color_parent_mix = 0.6f;
constexpr int max_size = 20;
constexpr int min_size = 2;
constexpr int max_size_var = 5;
constexpr int min_size_var = -5;
constexpr int max_size_chnage = 5;
constexpr int min_size_change = -5;
constexpr int sizes[] = {2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
// Public
void Tree::init(int size)
{
this->size = size;
this->canvasSize = size;
start.x = size / 2;
start.y = size;
}
@ -29,7 +37,7 @@ void Tree::draw(Dna *dna)
m_dna = dna;
branchSeed = dna->branchSeed;
draw_calls.push_back({start, 0, (float)size / 4, 0});
draw_calls.push_back({start, 180.0f, (float)canvasSize / 4, 0});
tick();
}
@ -54,23 +62,24 @@ void Tree::drawBranch()
{
DrawArgs arg = draw_calls.front();
float angle = ((arg.angleDeg + 180.0f) * PI) / 180.0f;
float nx = arg.lenghth * std::sin(angle);
float ny = arg.lenghth * std::cos(angle);
float angle = ((arg.angleDeg) * PI) / 180.0f;
float nx = arg.length * std::sin(angle);
float ny = arg.length * std::cos(angle);
Vector2 end = {arg.start.x + nx, arg.start.y + ny};
float thick = 2.0;
float fstep = 1.0 / ((arg.lenghth / thick) * 1.5);
int size_start = get_start_size(arg);
int size_end = get_end_size(arg, size_start);
float fstep = 1.0 / ((arg.length / size_start) * 2.0f);
Color colorStart = get_start_color(arg);
Color colorEnd = get_end_color(arg.dep, colorStart);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(colorStart, colorEnd, i);
DrawCircleV(point, thick, color); // Fester on the phone to call DrawCircle insted of the Circle shader
int size = Lerp(size_start, size_end, i);
DrawCircleV(point, size, color); // Fester on the phone to call DrawCircle insted of the Circle shader
// Circle::setColor(color);
// Circle::draw(point.x, point.y, thick); // TODO Change to BeginShaderMode and EndShaderMode only onece
@ -90,11 +99,11 @@ void Tree::drawBranch()
for (size_t i = 0; i < get_num_of_branches(arg.dep); i++)
{
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
draw_calls.push_back({end, newAngle, arg.lenghth * next_len, arg.dep + 1, colorEnd});
draw_calls.push_back({end, newAngle, arg.length * next_len, arg.dep + 1, colorEnd, size_end});
}
}
inline uint8_t Tree::get_num_of_branches(uint8_t dep)
inline int Tree::get_num_of_branches(int dep)
{
if (m_dna->branches[dep].branch_count < 128)
return 2;
@ -123,11 +132,40 @@ inline Color Tree::get_start_color(DrawArgs &arg)
return ret;
}
inline Color Tree::get_end_color(uint8_t dep, Color &start)
inline Color Tree::get_end_color(int dep, Color &start)
{
return {
start.r + m_dna->branches[dep].colorR_change,
start.g + m_dna->branches[dep].colorG_change,
start.b + m_dna->branches[dep].colorB_change,
255};
}
}
inline int Tree::get_start_size(DrawArgs &arg)
{
int size = Remap(m_dna->branches[arg.dep].size, 0, 255, min_size, max_size);
size += Remap(m_dna->branches[arg.dep].size_var, 0, 255, min_size_var, max_size_var) * mrand::getFloat(&branchSeed);
if (arg.dep > 0)
{
float size_parent = m_dna->branches[arg.dep].size_parent / 255.0f;
size = std::lerp(size, arg.size, size_parent);
}
float mix_level = m_dna->branches[arg.dep].size_level / 255.0f;
size = std::lerp(size, sizes[MAX_DEPTH - arg.dep - 1], mix_level);
if (size < 1)
size = 1;
return size;
}
inline int Tree::get_end_size(DrawArgs &arg, int start)
{
int size = Remap(m_dna->branches[arg.dep].size_change, 0, 255, min_size_change, max_size_chnage);
size += start;
if (size < 1)
size = 1;
return size;
}