From fb4421e5dfee71ce7850ff56f704e0174105d807 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Thu, 7 May 2026 20:08:34 -0700 Subject: [PATCH] feat(editor): add --gen-texture-checker with custom colors The existing --gen-texture's "checker" pattern is fixed black/white at 32px. This is the configurable variant: pass two colors and an optional cell size to generate boards in any palette. Useful for chess/checker game boards, kitchen-floor textures, hazard markers in colors other than monochrome, retro UI backgrounds. Args: [cellPx] [W H] Defaults: cellPx 32, 256x256. 10th procedural texture pattern (joining solid, the BW checker, grid, vertical/horizontal/radial gradients, noise, stripes, dots, rings). --- tools/editor/main.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/tools/editor/main.cpp b/tools/editor/main.cpp index b0fb7318..00359fea 100644 --- a/tools/editor/main.cpp +++ b/tools/editor/main.cpp @@ -539,6 +539,8 @@ static void printUsage(const char* argv0) { std::printf(" Synthesize a polka-dot pattern (default radius 8, spacing 32, 256x256)\n"); std::printf(" --gen-texture-rings [ringPx] [W H]\n"); std::printf(" Synthesize concentric ring pattern (target/seal style; default 16px rings, 256x256)\n"); + std::printf(" --gen-texture-checker [cellPx] [W H]\n"); + std::printf(" Synthesize checkerboard with custom colors (gen-texture's checker is BW only)\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"); @@ -1050,7 +1052,7 @@ int main(int argc, char* argv[]) { "--smooth-mesh-normals", "--merge-meshes", "--gen-texture-radial", "--gen-texture-stripes", "--gen-texture-dots", - "--gen-texture-rings", + "--gen-texture-rings", "--gen-texture-checker", "--validate-glb", "--info-glb", "--info-glb-tree", "--info-glb-bytes", "--validate-jsondbc", "--check-glb-bounds", "--validate-stl", "--validate-png", "--validate-blp", @@ -17796,6 +17798,100 @@ int main(int argc, char* argv[]) { std::printf(" ring px : %d\n", ringPx); std::printf(" colors : %s + %s\n", aHex.c_str(), bHex.c_str()); return 0; + } else if (std::strcmp(argv[i], "--gen-texture-checker") == 0 && i + 3 < argc) { + // Two-color checkerboard with custom colors. The + // existing --gen-texture's "checker" pattern is fixed + // black/white at 32px; this is the configurable variant + // for game boards, kitchen floors, hazard markers in + // colors other than monochrome. + std::string outPath = argv[++i]; + std::string aHex = argv[++i]; + std::string bHex = argv[++i]; + int cellPx = 32; + int W = 256, H = 256; + if (i + 1 < argc && argv[i + 1][0] != '-') { + try { cellPx = std::stoi(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 || + cellPx < 1 || cellPx > 4096) { + std::fprintf(stderr, + "gen-texture-checker: invalid dims (W/H 1..8192, cellPx 1..4096)\n"); + return 1; + } + auto parseHex = [](std::string hex, + uint8_t& r, uint8_t& g, uint8_t& b) -> bool { + std::transform(hex.begin(), hex.end(), hex.begin(), + [](unsigned char c) { return std::tolower(c); }); + if (!hex.empty() && hex[0] == '#') hex.erase(0, 1); + auto fromHexC = [](char c) -> int { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return 10 + c - 'a'; + return -1; + }; + int v[6]; + if (hex.size() == 6) { + for (int k = 0; k < 6; ++k) { + v[k] = fromHexC(hex[k]); + if (v[k] < 0) return false; + } + r = static_cast((v[0] << 4) | v[1]); + g = static_cast((v[2] << 4) | v[3]); + b = static_cast((v[4] << 4) | v[5]); + return true; + } + if (hex.size() == 3) { + for (int k = 0; k < 3; ++k) { + v[k] = fromHexC(hex[k]); + if (v[k] < 0) return false; + } + r = static_cast((v[0] << 4) | v[0]); + g = static_cast((v[1] << 4) | v[1]); + b = static_cast((v[2] << 4) | v[2]); + return true; + } + return false; + }; + uint8_t ra, ga, ba, rb, gb, bb; + if (!parseHex(aHex, ra, ga, ba)) { + std::fprintf(stderr, + "gen-texture-checker: '%s' is not a valid hex color\n", + aHex.c_str()); + return 1; + } + if (!parseHex(bHex, rb, gb, bb)) { + std::fprintf(stderr, + "gen-texture-checker: '%s' is not a valid hex color\n", + bHex.c_str()); + return 1; + } + std::vector pixels(static_cast(W) * H * 3, 0); + for (int y = 0; y < H; ++y) { + for (int x = 0; x < W; ++x) { + bool isA = ((x / cellPx) + (y / cellPx)) % 2 == 0; + size_t i2 = (static_cast(y) * W + x) * 3; + pixels[i2 + 0] = isA ? ra : rb; + pixels[i2 + 1] = isA ? ga : gb; + pixels[i2 + 2] = isA ? ba : bb; + } + } + if (!stbi_write_png(outPath.c_str(), W, H, 3, + pixels.data(), W * 3)) { + std::fprintf(stderr, + "gen-texture-checker: 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(" cell px : %d\n", cellPx); + std::printf(" colors : %s + %s\n", aHex.c_str(), bHex.c_str()); + return 0; } else if (std::strcmp(argv[i], "--gen-mesh") == 0 && i + 2 < argc) { // Synthesize a procedural primitive WOM. Generates proper // per-face normals, planar UVs, a bounding box, and a