diff --git a/tools/editor/cli_gen_texture.cpp b/tools/editor/cli_gen_texture.cpp index 5d11e4f2..4faef6a3 100644 --- a/tools/editor/cli_gen_texture.cpp +++ b/tools/editor/cli_gen_texture.cpp @@ -1992,6 +1992,107 @@ int handleBark(int& i, int argc, char** argv) { return 0; } +int handleClouds(int& i, int argc, char** argv) { + // Sky with puffy clouds. Multi-octave smooth noise (4 + // octaves of cosine-product noise at doubling frequencies) + // gives soft cloud blobs; the result is thresholded by + // `coverage` so values above the threshold blend toward + // cloud color, and values below fade smoothly to sky. + std::string outPath = argv[++i]; + std::string skyHex = argv[++i]; + std::string cloudHex = argv[++i]; + uint32_t seed = 1; + float coverage = 0.5f; // 0=clear sky, 1=overcast + 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 { coverage = 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 || + coverage < 0.0f || coverage > 1.0f) { + std::fprintf(stderr, + "gen-texture-clouds: invalid dims (W/H 1..8192, coverage 0..1)\n"); + return 1; + } + uint8_t sr, sg, sb, cr, cg, cb; + if (!parseHex(skyHex, sr, sg, sb)) { + std::fprintf(stderr, + "gen-texture-clouds: '%s' is not a valid hex color\n", + skyHex.c_str()); + return 1; + } + if (!parseHex(cloudHex, cr, cg, cb)) { + std::fprintf(stderr, + "gen-texture-clouds: '%s' is not a valid hex color\n", + cloudHex.c_str()); + return 1; + } + float seedF = static_cast(seed); + auto cloudNoise = [&](float x, float y) -> float { + // 4 octaves of sin/cos noise at doubling frequency, + // halving amplitude. Output in 0..1 after normalize. + float n = 0.0f; + float total = 0.0f; + float freq = 0.015f; + float amp = 1.0f; + for (int o = 0; o < 4; ++o) { + n += amp * (0.5f + 0.5f * + std::sin(x * freq + seedF * (1.0f + o * 0.7f)) * + std::cos(y * freq + seedF * (0.5f + o * 0.4f))); + total += amp; + freq *= 2.0f; + amp *= 0.5f; + } + return n / total; + }; + std::vector pixels(static_cast(W) * H * 3, 0); + // Coverage maps to a noise threshold: low coverage = high + // threshold (only the brightest noise becomes clouds); + // high coverage = low threshold (more area is cloudy). + float thresh = 1.0f - coverage; + int cloudPixels = 0; + for (int y = 0; y < H; ++y) { + for (int x = 0; x < W; ++x) { + float n = cloudNoise(static_cast(x), + static_cast(y)); + // Smooth blend across a 0.15-wide band around the + // threshold so cloud edges feather rather than step. + float t = std::clamp((n - thresh) / 0.15f, 0.0f, 1.0f); + if (t > 0.5f) ++cloudPixels; + uint8_t r = static_cast(sr * (1 - t) + cr * t); + uint8_t g = static_cast(sg * (1 - t) + cg * t); + uint8_t b = static_cast(sb * (1 - t) + cb * t); + 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-clouds: 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(" sky/cloud : %s / %s\n", + skyHex.c_str(), cloudHex.c_str()); + std::printf(" coverage : %.2f (%d cloud pixels)\n", + coverage, cloudPixels); + std::printf(" seed : %u\n", seed); + return 0; +} + } // namespace bool handleGenTexture(int& i, int argc, char** argv, int& outRc) { @@ -2060,6 +2161,9 @@ bool handleGenTexture(int& i, int argc, char** argv, int& outRc) { if (std::strcmp(argv[i], "--gen-texture-bark") == 0 && i + 3 < argc) { outRc = handleBark(i, argc, argv); return true; } + if (std::strcmp(argv[i], "--gen-texture-clouds") == 0 && i + 3 < argc) { + outRc = handleClouds(i, argc, argv); return true; + } return false; } diff --git a/tools/editor/cli_help.cpp b/tools/editor/cli_help.cpp index 4da3045c..9c1a1ece 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -77,6 +77,8 @@ void printUsage(const char* argv0) { 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(" --gen-texture-clouds [seed] [coverage] [W H]\n"); + std::printf(" Sky with puffy clouds: multi-octave noise thresholded by coverage (default 0.5)\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 8322ee13..fd441c39 100644 --- a/tools/editor/main.cpp +++ b/tools/editor/main.cpp @@ -573,6 +573,7 @@ int main(int argc, char* argv[]) { "--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-bark", + "--gen-texture-clouds", "--validate-glb", "--info-glb", "--info-glb-tree", "--info-glb-bytes", "--validate-jsondbc", "--check-glb-bounds", "--validate-stl", "--validate-png", "--validate-blp",