#include #include "canvas/Tree.hpp" #include "canvas/Circle.hpp" #include #include #define ITER_PER_FRAME 5000 constexpr int maxColorChange = 15; constexpr int minColorChange = -15; constexpr float colorParentMix = 0.6f; constexpr int maxSize = 20; constexpr int minSize = 2; constexpr int maxSizeVar = 5; constexpr int minSizeVar = -5; constexpr int maxSizeChange = 5; constexpr int MinSizeChange = -5; constexpr int sizes[] = {2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; static_assert(sizeof(sizes) / sizeof(int) == MAX_POSIBLE_DEPTH); float lengths[MAX_DEPTH]; constexpr float maxAngles[] = {5.0f, 5.0f, 5.0f, 10.0f, 10.0f, 10.0f, 15.0f, 15.0f, 20.0f, 20.0f, 20.0f}; static_assert(sizeof(maxAngles) / sizeof(float) == MAX_POSIBLE_DEPTH); void calculateLevels(int canvasSize) { lengths[0] = canvasSize / 4.0f; for (size_t i = 1; i < MAX_DEPTH; i++) { lengths[i] = lengths[i - 1] * 0.7f; } } // Public void Tree::init(int size) { this->canvasSize = size; start.x = size / 2; start.y = size; calculateLevels(size); } void Tree::draw(Dna *dna) { Circle::setSoftEdge(false); m_dna = dna; branchSeed = dna->branchSeed; drawCalls.push_back({start, 180.0f, 0}); tick(); } bool Tree::tick() { size_t i = 0; while (!drawCalls.empty()) { drawBranch(); drawCalls.pop_front(); i++; if (i >= ITER_PER_FRAME) break; } return drawCalls.empty(); } // Private void Tree::drawBranch() { DrawArgs arg = drawCalls.front(); if (arg.dep == MAX_DEPTH) return; float angleVar = getAngleVar(arg); float angle = ((arg.angleDeg + angleVar) * PI) / 180.0f; float length = getLength(arg); float nx = length * std::sin(angle); float ny = length * std::cos(angle); Vector2 end = {arg.start.x + nx, arg.start.y + ny}; int sizeStart = getStartSize(arg); int sizeEnd = getEndSize(arg, sizeStart); float fstep = 1.0 / ((length / sizeStart) * 2.0f); Color colorStart = getStartColor(arg); Color colorEnd = getEndColor(arg.dep, colorStart); for (float i = 0; i < 1; i += fstep) { Vector2 point = Vector2Lerp(arg.start, end, i); Color color = ColorLerp(colorStart, colorEnd, i); int size = Lerp(sizeStart, sizeEnd, 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 // use // DrawRectangleGradientEx } // add more branches to draw if (arg.dep + 1 >= MAX_DEPTH) return; float sectors = getNumOfBranches(arg.dep) + 1; float degres = 180.0f / sectors; for (size_t i = 0; i < getNumOfBranches(arg.dep); i++) { float newAngle = arg.angleDeg - 90 + (degres * (i + 1)); drawCalls.push_back({end, newAngle, arg.dep + 1, colorEnd, sizeEnd}); } } inline size_t Tree::getNumOfBranches(int dep) { if (m_dna->branches[dep].branchCount < 128) return 2; else return 3; } inline Color Tree::getStartColor(DrawArgs &arg) { Color ret = { m_dna->branches[arg.dep].colorR, m_dna->branches[arg.dep].colorG, m_dna->branches[arg.dep].colorB, 255}; if (arg.dep > 0) { ret = ColorLerp(ret, arg.parent, colorParentMix); } int colorVar = Remap(m_dna->branches[arg.dep].colorVar, 0, 255, minColorChange, maxColorChange); ret.r += colorVar * mrand::getFloat(&branchSeed); ret.g += colorVar * mrand::getFloat(&branchSeed); ret.b += colorVar * mrand::getFloat(&branchSeed); return ret; } inline Color Tree::getEndColor(int dep, Color &start) { uint8_t r = start.r + m_dna->branches[dep].colorR_change; uint8_t g = start.g + m_dna->branches[dep].colorG_change; uint8_t b = start.b + m_dna->branches[dep].colorB_change; return {r, g, b, 255}; } inline int Tree::getStartSize(DrawArgs &arg) { int size = Remap(m_dna->branches[arg.dep].size, 0, 255, minSize, maxSize); size += Remap(m_dna->branches[arg.dep].sizeVar, 0, 255, minSizeVar, maxSizeVar) * mrand::getFloat(&branchSeed); if (arg.dep > 0) { float sizeParent = m_dna->branches[arg.dep].sizeParent / 255.0f; size = std::lerp(size, arg.size, sizeParent); } float mixLevel = m_dna->branches[arg.dep].sizeLevel / 255.0f; size = std::lerp(size, sizes[MAX_DEPTH - arg.dep - 1], mixLevel); if (size < 1) size = 1; return size; } inline int Tree::getEndSize(DrawArgs &arg, int start) { int size = Remap(m_dna->branches[arg.dep].sizeChange, 0, 255, MinSizeChange, maxSizeChange); size += start; if (size < 1) size = 1; return size; } inline float Tree::getLength(DrawArgs &arg) { float lenght = lengths[arg.dep]; float lenghtRatio = Remap(m_dna->branches[arg.dep].length, 0, 255, 0.5f, 1.3f); lenght *= lenghtRatio; float lenghtVar = Remap(m_dna->branches[arg.dep].lengthVar, 0, 255, -0.15f, 0.15f); lenght += lenght * lenghtVar * mrand::getFloat(&branchSeed); if (lenght < 1) lenght = 1; return lenght; } inline float Tree::getAngleVar(DrawArgs &arg) { float angleVar = Remap(m_dna->branches[arg.dep].branchAngleVar, 0, 255, 0.0f, maxAngles[arg.dep]); angleVar = Lerp(angleVar, -angleVar, mrand::getFloat(&branchSeed)); return angleVar; }