From 16ae6489c903936cda3f5efe1df6ee4542ac92ea Mon Sep 17 00:00:00 2001 From: Kelsi Date: Fri, 8 May 2026 23:15:03 -0700 Subject: [PATCH] feat(editor): add --gen-texture-bark tree-bark pattern Vertical streaks of varying brightness (per-column hash gives each streak a stable shade in 0.85..1.10) sway-warped per-row via a slow cosine offset, plus dark vertical cracks at random columns where bark splits as the trunk expands. Defaults: density=0.04 (~4% of columns become cracks), seed=1. Useful for tree-trunk textures, wooden palisades, bark-clad buildings. Brings the procedural texture pattern set to 24. --- tools/editor/cli_gen_texture.cpp | 113 +++++++++++++++++++++++++++++++ tools/editor/cli_help.cpp | 2 + tools/editor/main.cpp | 2 +- 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/tools/editor/cli_gen_texture.cpp b/tools/editor/cli_gen_texture.cpp index 7ec3338c..5d11e4f2 100644 --- a/tools/editor/cli_gen_texture.cpp +++ b/tools/editor/cli_gen_texture.cpp @@ -1882,6 +1882,116 @@ int handleTile(int& i, int argc, char** argv) { return 0; } +int handleBark(int& i, int argc, char** argv) { + // Tree bark: vertical wavy streaks (the trunk's growth lines) + // plus dark vertical cracks at random columns (where bark + // splits as the tree expands). Streaks waver per row via a + // smooth cosine offset so the texture doesn't look gridded. + std::string outPath = argv[++i]; + std::string baseHex = argv[++i]; + std::string crackHex = argv[++i]; + uint32_t seed = 1; + float density = 0.04f; // fraction of columns that become cracks + int W = 256, H = 256; + if (i + 1 < argc && argv[i + 1][0] != '-') { + try { seed = static_cast(std::stoul(argv[++i])); } catch (...) {} + } + if (i + 1 < argc && argv[i + 1][0] != '-') { + try { density = std::stof(argv[++i]); } catch (...) {} + } + if (i + 1 < argc && argv[i + 1][0] != '-') { + try { W = std::stoi(argv[++i]); } catch (...) {} + } + if (i + 1 < argc && argv[i + 1][0] != '-') { + try { H = std::stoi(argv[++i]); } catch (...) {} + } + if (W < 1 || H < 1 || W > 8192 || H > 8192 || + density < 0.0f || density > 0.5f) { + std::fprintf(stderr, + "gen-texture-bark: invalid dims (W/H 1..8192, density 0..0.5)\n"); + return 1; + } + uint8_t br, bg, bb_, cr, cg, cb; + if (!parseHex(baseHex, br, bg, bb_)) { + std::fprintf(stderr, + "gen-texture-bark: '%s' is not a valid hex color\n", + baseHex.c_str()); + return 1; + } + if (!parseHex(crackHex, cr, cg, cb)) { + std::fprintf(stderr, + "gen-texture-bark: '%s' is not a valid hex color\n", + crackHex.c_str()); + return 1; + } + uint32_t state = seed ? seed : 1u; + auto next01 = [&state]() -> float { + state = state * 1664525u + 1013904223u; + return (state >> 8) * (1.0f / 16777216.0f); + }; + // Pick crack columns up front (sparse). + int crackCount = static_cast(W * density); + std::vector crackCols; + crackCols.reserve(crackCount); + for (int k = 0; k < crackCount; ++k) { + crackCols.push_back(static_cast(next01() * W)); + } + std::vector pixels(static_cast(W) * H * 3, 0); + float seedF = static_cast(seed); + // Per-column shade variation (each vertical streak has its own + // brightness derived from a column hash). Pre-compute so each + // pixel just reads the column. + std::vector colShade(W); + for (int x = 0; x < W; ++x) { + // Stable column hash → 0.85..1.10 shade + uint32_t h = static_cast(x) * 2654435761u + seed; + h = (h ^ (h >> 13)) * 1274126177u; + h = h ^ (h >> 16); + float n = (h >> 8) * (1.0f / 16777216.0f); + colShade[x] = 0.85f + 0.25f * n; + } + for (int y = 0; y < H; ++y) { + // Slow horizontal sway so vertical streaks waver per row. + float sway = std::sin(y * 0.04f + seedF * 0.3f) * 1.5f; + for (int x = 0; x < W; ++x) { + int sx = x + static_cast(sway); + if (sx < 0) sx = 0; + if (sx >= W) sx = W - 1; + float shade = colShade[sx]; + uint8_t r = static_cast(std::clamp(br * shade, 0.0f, 255.0f)); + uint8_t g = static_cast(std::clamp(bg * shade, 0.0f, 255.0f)); + uint8_t b = static_cast(std::clamp(bb_ * shade, 0.0f, 255.0f)); + // Crack overlay: any pixel within 1 px of a crack column + // (with sway applied) becomes the crack color. + for (int cc : crackCols) { + if (std::abs(sx - cc) <= 1) { + r = cr; g = cg; b = cb; + break; + } + } + size_t i2 = (static_cast(y) * W + x) * 3; + pixels[i2 + 0] = r; + pixels[i2 + 1] = g; + pixels[i2 + 2] = b; + } + } + if (!stbi_write_png(outPath.c_str(), W, H, 3, + pixels.data(), W * 3)) { + std::fprintf(stderr, + "gen-texture-bark: stbi_write_png failed for %s\n", + outPath.c_str()); + return 1; + } + std::printf("Wrote %s\n", outPath.c_str()); + std::printf(" size : %dx%d\n", W, H); + std::printf(" base/crack : %s / %s\n", + baseHex.c_str(), crackHex.c_str()); + std::printf(" density : %.4f (%d cracks)\n", + density, crackCount); + std::printf(" seed : %u\n", seed); + return 0; +} + } // namespace bool handleGenTexture(int& i, int argc, char** argv, int& outRc) { @@ -1947,6 +2057,9 @@ bool handleGenTexture(int& i, int argc, char** argv, int& outRc) { if (std::strcmp(argv[i], "--gen-texture-tile") == 0 && i + 3 < argc) { outRc = handleTile(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--gen-texture-bark") == 0 && i + 3 < argc) { + outRc = handleBark(i, argc, argv); return true; + } return false; } diff --git a/tools/editor/cli_help.cpp b/tools/editor/cli_help.cpp index 51cf9254..62f8d7bb 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -75,6 +75,8 @@ void printUsage(const char* argv0) { std::printf(" Lava: dark cooled crust with bright glowing cracks via Worley cell boundaries\n"); std::printf(" --gen-texture-tile [tilePx] [groutPx] [W H]\n"); std::printf(" Square stone tiles with grout grid (default 32px tiles, 2px grout)\n"); + std::printf(" --gen-texture-bark [seed] [crackDensity] [W H]\n"); + std::printf(" Tree bark: vertical wavy streaks + dark vertical cracks (default density=0.04)\n"); std::printf(" --add-texture-to-zone [renameTo]\n"); std::printf(" Copy an existing PNG into (optionally renaming it on the way in)\n"); std::printf(" --gen-mesh [size]\n"); diff --git a/tools/editor/main.cpp b/tools/editor/main.cpp index 058f7c76..8629c17f 100644 --- a/tools/editor/main.cpp +++ b/tools/editor/main.cpp @@ -571,7 +571,7 @@ int main(int argc, char* argv[]) { "--gen-texture-wood", "--gen-texture-grass", "--gen-texture-fabric", "--gen-texture-cobble", "--gen-texture-marble", "--gen-texture-metal", "--gen-texture-leather", "--gen-texture-sand", "--gen-texture-snow", - "--gen-texture-lava", "--gen-texture-tile", + "--gen-texture-lava", "--gen-texture-tile", "--gen-texture-bark", "--validate-glb", "--info-glb", "--info-glb-tree", "--info-glb-bytes", "--validate-jsondbc", "--check-glb-bounds", "--validate-stl", "--validate-png", "--validate-blp",