Nodes/BrainDead Nodes/BD Mask Flatten
ComfyUI Node

BD Mask Flatten

Flatten cutouts onto a background — with the alpha-bleed trick that kills UV-edge halos

By BizaNator·Created 8 months ago·Updated 3 days ago· 15
BD Mask Flatten
  • image
  • mask
  • background_image
  • image
  • alpha_used
background_colorwhite
background_color_hex#808080
flatten_modealpha_composite
mask_strength1.00
invert_maskfalse
mask_threshold0.00
edge_pad_pixels0

BD Mask Flatten takes an image with transparency and lays it flat onto a background, so downstream nodes get a plain RGB image and never have to think about alpha. It sounds trivial - every image editor has "flatten" - but it's one of the workhorse nodes of this pack's game-asset prep, because it handles the three things that always complicate a real flatten: the background, the composite math, and the dreaded edge bleed.

How it works

The composite math is standard alpha blending: image × alpha + background × (1 − alpha). Semi-transparent pixels become background-blended rather than cut, which is exactly what you want when the result might later be chroma-keyed - percentage-based recovery only works if the semi-transparency was preserved, not snapped to 0 or 1. The output is always RGB (no alpha channel), which is the point: downstream nodes like BD Pack Channels or save nodes won't trip over unexpected transparency.

The mask input is optional but overrides everything: if you pass one, it becomes the alpha. If you don't, the node uses the image's own alpha channel (or treats RGB input as fully opaque). That's a flexible design - you can flatten a mask you got from SAM3 onto any background even if the source image had no alpha at all.

The inputs that matter

  • background_color - white/black by default, but the interesting ones are the chroma keys (red/green/blue/magenta) and checker for debug, plus custom with a hex color. Game pipelines flatten onto a key color when they want to re-extract later.
  • flatten_mode - the power move. Beyond alpha_composite there's grayscale (luminance, then composite) and image_to_red / image_to_green / image_to_blue, which pack the image's grayscale into a single RGB channel with the background filling the others. That's how you build engine-packed maps (e.g. AO in red, roughness in green, metallic in blue) without leaving ComfyUI.
  • edge_pad_pixels - the game-dev trick worth the most. It runs a Voronoi edge pad: foreground RGB extends outward into transparent areas by N pixels using the nearest foreground color. This is the standard "alpha bleed" that prevents the UV-edge halos you get when a game engine samples a texture at a silhouette boundary and hits transparent black. 16–32 is typical for game textures; the pad happens before flattening, so the background only shows beyond the padded zone.
  • mask_threshold - >0 binarizes the alpha (hard edges) instead of the default soft continuous alpha. Use it when you want crisp edges, not partial transparency.

You also get mask_strength (multiply alpha before compositing) and invert_mask (flatten the outside instead of the inside). Outputs are the flattened image and alpha_used - the actual alpha that went in, for debugging or reuse.

Where it fits

The README's Mask Tools template chains it with BD Luminance Mask, crop-to-mask, and fill-holes; the game-packing template uses it before channel packing. It's the "make this presentable" node: whatever weird masked thing you're holding, this turns it into a clean flattened texture that exports without surprises.

Installing

Same as every node here - ComfyUI Manager, search "BrainDead", install; or clone into custom_nodes and pip install -r requirements.txt. Restart, and it's under 🧠BrainDead/Segmentation. Keep ComfyUI current (V3 API pack).

The habit worth forming: if your flattened textures show dark fringes in-engine, it's almost certainly missing edge_pad_pixels, not a wrong background. Set it before you export, not after you see the halo.

Category🧠BrainDead/Segmentation

Inputs (10)

NameTypeDefaultDescription
imageIMAGE
maskoptMASKOptional alpha mask. OVERRIDES image's alpha channel if both present. If not provided AND image is RGBA, uses image's alpha. If not provided AND image is RGB, treats as fully opaque (alpha=1).
background_imageoptIMAGEOptional image to composite ONTO instead of a solid color. When provided, background_color and background_color_hex are ignored. Resized to match the input image's dimensions.
background_coloroptCOMBOwhiteSolid color background. red/green/blue/magenta = chroma keys. checker = gray checkerboard for debug visualization. custom = use background_color_hex.
background_color_hexoptSTRING#808080Hex color when background_color='custom'. Format: '#RRGGBB' or '#RGB'. Falls back to gray if invalid.
flatten_modeoptCOMBOalpha_compositeHow to flatten: alpha_composite — standard alpha blend image RGB onto background. grayscale — convert image to luminance, alpha-composite onto background. image_to_red — put image's grayscale into ONLY the red channel (green/blue stay as bg). Useful for packing multiple grayscale maps into one RGB image for game engines. image_to_green / image_to_blue — same but for those channels.
mask_strengthoptFLOAT1.000–2Multiply alpha values before composite. >1 makes alpha stronger (less bg shows through), <1 weaker (more bg shows through). Clamped to [0, 1] after.
invert_maskoptBOOLEANfalseInvert the alpha before applying — flattens the OUTSIDE of the mask onto background instead of the inside.
mask_thresholdoptFLOAT0.000–1If > 0, binarize the alpha at this threshold before composite. 0 = soft (continuous alpha), >0 = hard binary cutoff. Useful when you want sharp edges instead of partial transparency on the flatten.
edge_pad_pixelsoptINT00–512Voronoi edge padding — extend foreground RGB outward into transparent areas by N pixels using nearest-foreground color. Standard game-engine 'alpha bleed' to prevent UV-edge halos when textures are sampled at silhouette boundaries. 0 = off (no padding). 16-32 = typical for game textures. Applied BEFORE flattening, so the flatten background only shows beyond the padded zone.

Outputs (2)

NameTypeDescription
imageIMAGE
alpha_usedMASK