From 25ada9bbfbc5c58ab93331961ab9542e37b0a336 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sat, 9 May 2026 14:20:02 -0700 Subject: [PATCH] feat(editor): add --gen-texture-bayer ordered-dither pattern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 81st procedural texture: classic 4x4 Bayer ordered-dither matrix tiled across the image. Each pixel's color comes from interpolating bg → fg by the matrix value at (x mod 4, y mod 4) normalized to [0, 1]: 0 8 2 10 12 4 14 6 3 11 1 9 15 7 13 5 cellSize parameter scales the matrix block (default 4 px, giving a 16-px seamless tile; cellSize=8 gives a 32-px chunky retro look). Useful for 8-bit / monochrome-CRT-style backdrops, ordered- shadow approximations on low-bit palettes, retro arcade splash overlays, paper-spritesheet rendering, and as a deterministic alternative to --gen-texture-noise for binary or near-binary palettes. --- tools/editor/cli_arg_required.cpp | 1 + tools/editor/cli_gen_texture.cpp | 55 +++++++++++++++++++++++++++++++ tools/editor/cli_help.cpp | 2 ++ 3 files changed, 58 insertions(+) diff --git a/tools/editor/cli_arg_required.cpp b/tools/editor/cli_arg_required.cpp index 4ec75420..bf97c783 100644 --- a/tools/editor/cli_arg_required.cpp +++ b/tools/editor/cli_arg_required.cpp @@ -123,6 +123,7 @@ const char* const kArgRequired[] = { "--gen-texture-dewdrops", "--gen-texture-pinwheel", "--gen-texture-scratched-metal", "--gen-texture-crackle", "--gen-texture-star", "--gen-texture-halftone", + "--gen-texture-bayer", "--validate-glb", "--info-glb", "--info-glb-tree", "--info-glb-bytes", "--validate-jsondbc", "--check-glb-bounds", "--validate-stl", "--validate-png", "--validate-blp", diff --git a/tools/editor/cli_gen_texture.cpp b/tools/editor/cli_gen_texture.cpp index 252c96fb..7e33b9c4 100644 --- a/tools/editor/cli_gen_texture.cpp +++ b/tools/editor/cli_gen_texture.cpp @@ -3500,6 +3500,60 @@ int handleKnit(int& i, int argc, char** argv) { return 0; } +int handleBayer(int& i, int argc, char** argv) { + // Classic 4x4 ordered-dither Bayer matrix tiled across the + // image. Each pixel's color comes from interpolating bg → fg + // by the matrix value at (x mod 4, y mod 4) normalized to + // [0, 1]. Distinctive retro / dithered look — useful for + // 8-bit-style backdrops, monochrome-CRT effects, ordered- + // shadow approximations on low-bit palettes. + std::string outPath = argv[++i]; + std::string aHex = argv[++i]; // dark color (matrix value 0) + std::string bHex = argv[++i]; // light color (matrix value 15) + int cellSize = 4; // pixels per matrix cell + int W = 256, H = 256; + parseOptInt(i, argc, argv, cellSize); + parseOptInt(i, argc, argv, W); + parseOptInt(i, argc, argv, H); + if (W < 1 || H < 1 || W > 8192 || H > 8192 || + cellSize < 1 || cellSize > 256) { + std::fprintf(stderr, + "gen-texture-bayer: invalid dims (W/H 1..8192, " + "cellSize 1..256)\n"); + return 1; + } + uint8_t ar, ag, ab, br_, bg_, bb_; + if (!parseHexOrError(aHex, ar, ag, ab, "gen-texture-bayer")) return 1; + if (!parseHexOrError(bHex, br_, bg_, bb_, + "gen-texture-bayer")) return 1; + // 4x4 Bayer matrix values 0..15 (ordered-dither standard). + static const int kBayer4[4][4] = { + { 0, 8, 2, 10}, + {12, 4, 14, 6}, + { 3, 11, 1, 9}, + {15, 7, 13, 5}, + }; + std::vector pixels(static_cast(W) * H * 3, 0); + for (int y = 0; y < H; ++y) { + int my = (y / cellSize) & 3; + for (int x = 0; x < W; ++x) { + int mx = (x / cellSize) & 3; + // Normalize 0..15 to 0..1 and interpolate. + float t = kBayer4[my][mx] / 15.0f; + uint8_t r = static_cast(ar + t * (br_ - ar)); + uint8_t g = static_cast(ag + t * (bg_ - ag)); + uint8_t b = static_cast(ab + t * (bb_ - ab)); + setPixelRGB(pixels, W, x, y, r, g, b); + } + } + if (!savePngOrError(outPath, W, H, pixels, "gen-texture-bayer")) return 1; + printPngWrote(outPath, W, H); + std::printf(" a / b : %s / %s\n", aHex.c_str(), bHex.c_str()); + std::printf(" cell : %d px (4x4 matrix → %d-px tile)\n", + cellSize, cellSize * 4); + return 0; +} + int handleHalftone(int& i, int argc, char** argv) { // Halftone: regular grid of dots whose radii grow with a // configurable gradient direction (vertical, horizontal, or @@ -5916,6 +5970,7 @@ constexpr TextureEntry kTextureTable[] = { {"--gen-texture-crackle", 3, handleCrackle}, {"--gen-texture-star", 3, handleStar}, {"--gen-texture-halftone", 3, handleHalftone}, + {"--gen-texture-bayer", 3, handleBayer}, }; } // namespace diff --git a/tools/editor/cli_help.cpp b/tools/editor/cli_help.cpp index 0dc4d173..9e6b88da 100644 --- a/tools/editor/cli_help.cpp +++ b/tools/editor/cli_help.cpp @@ -195,6 +195,8 @@ void printUsage(const char* argv0) { std::printf(" Star: solid N-pointed star polygon centered (medallions / shields / religious symbols)\n"); std::printf(" --gen-texture-halftone [stride] [maxR] [v|h|r] [W H]\n"); std::printf(" Halftone: grid of dots whose radii grow with a v/h/r gradient (comic / newspaper print look)\n"); + std::printf(" --gen-texture-bayer [cellSize] [W H]\n"); + std::printf(" Bayer: 4x4 ordered-dither matrix tiled (retro / 8-bit / monochrome-CRT effects)\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");