diff options
| author | yum <yum.food.vr@gmail.com> | 2024-12-09 01:16:52 -0800 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2024-12-09 01:16:52 -0800 |
| commit | 81537629668cdda03f83b0b63870d53963ba5229 (patch) | |
| tree | 6a648845e75d4278dfb7de7491f1becf6f883815 /Third_Party | |
| parent | 2356cdf85d2c52f70052828bb5a18419a30d4de9 (diff) | |
Add SDF mode to decals
Also:
* add gen_sdf script, which converts solid color (ideally b&w) decals
into sdf textures
* add 6 more decal slots
* add color, sdf options, alpha multiplier, tiling mode, and mask to
decals
* add token pasting macros (MERGE) to minimize code size of new decal
slots
Diffstat (limited to 'Third_Party')
| -rw-r--r-- | Third_Party/gen_sdf | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Third_Party/gen_sdf b/Third_Party/gen_sdf new file mode 100644 index 0000000..84677c2 --- /dev/null +++ b/Third_Party/gen_sdf @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import numpy as np +import cv2 +import argparse +import os + +def compute_sdf(img, scale_factor): + # Convert to binary image if not already + _, binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) + + # Compute distance transform for both foreground and background + dist_transform_fg = cv2.distanceTransform(binary, cv2.DIST_L2, 5) + dist_transform_bg = cv2.distanceTransform(255 - binary, cv2.DIST_L2, 5) + + # Combine the distance fields and scale by factor + sdf = (dist_transform_fg - dist_transform_bg) / scale_factor + + # Clamp values to [0, 255] range + sdf = np.clip(sdf + 128, 0, 255) + + return sdf.astype(np.uint8) + +def main(): + parser = argparse.ArgumentParser(description='Generate SDF from black and white image') + parser.add_argument('input_image', help='Path to input image') + parser.add_argument('--scale', type=float, default=1.0, + help='Scale factor for distance (in texels)') + args = parser.parse_args() + + # Get input and output paths + input_path = args.input_image + filename, ext = os.path.splitext(input_path) + output_path = f"{filename}-sdf{ext}" + + # Read input image + img = cv2.imread(input_path, cv2.IMREAD_GRAYSCALE) + if img is None: + print(f"Error: Could not read image {input_path}") + return + + # Compute SDF with scale factor + sdf = compute_sdf(img, args.scale) + + # Save result + cv2.imwrite(output_path, sdf) + print(f"SDF generated and saved to {output_path}") + +if __name__ == "__main__": + main() |
