feat(editor): add --gen-texture-metal brushed-metal pattern

Per-pixel white noise heavily blurred along one axis (long
brush strokes) and lightly along the other (thin variation
across strokes), then contrast-stretched and applied as a
multiplicative tint on the base color. Result: long thin
streaks of varying brightness — the visual signature of
brushed steel/aluminum/iron.

Defaults: horizontal orientation, seed=1. Useful for armor,
weapons, machinery, sci-fi panels. Brings the procedural
texture pattern set to 18.
This commit is contained in:
Kelsi 2026-05-08 12:31:54 -07:00
parent d9f16547ec
commit 4acf4612af

View file

@ -556,6 +556,8 @@ static void printUsage(const char* argv0) {
std::printf(" Cobblestone street pattern: irregular packed stones (default stone=24px, seed 1)\n");
std::printf(" --gen-texture-marble <out.png> <baseHex> <veinHex> [seed] [veinSharpness] [W H]\n");
std::printf(" Marble pattern with sinusoidal veining (default seed 1, sharpness 8)\n");
std::printf(" --gen-texture-metal <out.png> <baseHex> [seed] [orientation] [W H]\n");
std::printf(" Brushed metal: directional anisotropic noise (orientation: horizontal|vertical)\n");
std::printf(" --add-texture-to-zone <zoneDir> <png-path> [renameTo]\n");
std::printf(" Copy an existing PNG into <zoneDir> (optionally renaming it on the way in)\n");
std::printf(" --gen-mesh <wom-base> <cube|plane|sphere|cylinder|torus|cone|ramp> [size]\n");
@ -1135,7 +1137,7 @@ int main(int argc, char* argv[]) {
"--gen-texture-radial", "--gen-texture-stripes", "--gen-texture-dots",
"--gen-texture-rings", "--gen-texture-checker", "--gen-texture-brick",
"--gen-texture-wood", "--gen-texture-grass", "--gen-texture-fabric",
"--gen-texture-cobble", "--gen-texture-marble",
"--gen-texture-cobble", "--gen-texture-marble", "--gen-texture-metal",
"--validate-glb", "--info-glb", "--info-glb-tree", "--info-glb-bytes",
"--validate-jsondbc", "--check-glb-bounds", "--validate-stl",
"--validate-png", "--validate-blp",
@ -20895,6 +20897,147 @@ int main(int argc, char* argv[]) {
std::printf(" sharpness : %.1f\n", sharpness);
std::printf(" seed : %u\n", seed);
return 0;
} else if (std::strcmp(argv[i], "--gen-texture-metal") == 0 && i + 2 < argc) {
// Brushed-metal pattern. We generate per-pixel white
// noise then box-blur it heavily along one axis (the
// brush direction) and lightly along the other. Result:
// long thin streaks of varying brightness, the visual
// signature of brushed steel/aluminum/iron. Apply that
// streaky shade as a multiplicative tint on the base
// metal color.
std::string outPath = argv[++i];
std::string baseHex = argv[++i];
uint32_t seed = 1;
std::string orientation = "horizontal";
int W = 256, H = 256;
if (i + 1 < argc && argv[i + 1][0] != '-') {
try { seed = static_cast<uint32_t>(std::stoul(argv[++i])); } catch (...) {}
}
if (i + 1 < argc && argv[i + 1][0] != '-') {
orientation = argv[++i];
}
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) {
std::fprintf(stderr,
"gen-texture-metal: invalid dims (W/H 1..8192)\n");
return 1;
}
if (orientation != "horizontal" && orientation != "vertical") {
std::fprintf(stderr,
"gen-texture-metal: orientation must be horizontal|vertical\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<uint8_t>((v[0] << 4) | v[1]);
g = static_cast<uint8_t>((v[2] << 4) | v[3]);
b = static_cast<uint8_t>((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<uint8_t>((v[0] << 4) | v[0]);
g = static_cast<uint8_t>((v[1] << 4) | v[1]);
b = static_cast<uint8_t>((v[2] << 4) | v[2]);
return true;
}
return false;
};
uint8_t mr, mg, mb;
if (!parseHex(baseHex, mr, mg, mb)) {
std::fprintf(stderr,
"gen-texture-metal: '%s' is not a valid hex color\n",
baseHex.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);
};
// Step 1: per-pixel white noise.
std::vector<float> noise(static_cast<size_t>(W) * H);
for (auto& v : noise) v = next01();
// Step 2: directional blur. For horizontal orientation,
// blur strongly in X (long brush strokes) and lightly
// in Y (thin variation across strokes). Vertical
// orientation flips X and Y.
std::vector<float> blurred(noise.size(), 0.0f);
int rxLong = (orientation == "horizontal") ? 24 : 2;
int ryLong = (orientation == "horizontal") ? 2 : 24;
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
float sum = 0.0f;
int n = 0;
for (int dy = -ryLong; dy <= ryLong; ++dy) {
int py = y + dy;
if (py < 0 || py >= H) continue;
for (int dx = -rxLong; dx <= rxLong; ++dx) {
int px = x + dx;
if (px < 0 || px >= W) continue;
sum += noise[static_cast<size_t>(py) * W + px];
n++;
}
}
blurred[static_cast<size_t>(y) * W + x] = sum / n;
}
}
// Step 3: stretch contrast back out so the streaks
// are visible (blurring narrows the range).
float minV = 1.0f, maxV = 0.0f;
for (float v : blurred) { minV = std::min(minV, v); maxV = std::max(maxV, v); }
float range = std::max(maxV - minV, 1e-6f);
std::vector<uint8_t> pixels(static_cast<size_t>(W) * H * 3, 0);
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
float t = (blurred[static_cast<size_t>(y) * W + x] - minV) / range;
// Map noise to a multiplicative shade in [0.7, 1.1]
// so the metal looks polished but not flat.
float shade = 0.7f + t * 0.4f;
size_t i2 = (static_cast<size_t>(y) * W + x) * 3;
pixels[i2 + 0] = static_cast<uint8_t>(
std::clamp(mr * shade, 0.0f, 255.0f));
pixels[i2 + 1] = static_cast<uint8_t>(
std::clamp(mg * shade, 0.0f, 255.0f));
pixels[i2 + 2] = static_cast<uint8_t>(
std::clamp(mb * shade, 0.0f, 255.0f));
}
}
if (!stbi_write_png(outPath.c_str(), W, H, 3,
pixels.data(), W * 3)) {
std::fprintf(stderr,
"gen-texture-metal: 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 color : %s\n", baseHex.c_str());
std::printf(" orientation : %s\n", orientation.c_str());
std::printf(" seed : %u\n", seed);
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