Sub topic 11
Margin and padding to prevent edge bleeding
This chapter assumes you can already run a Selected to Active bake and have a low-poly mesh with UV islands and a Non-Color target image. It covers what the Margin setting does, how to size it, and how it interacts with your UV layout.
What the margin is
A bake only writes pixels that fall inside a UV island. Every pixel outside the islands stays at the image's initial color, which is black for a new image. That empty space is a problem for two reasons.
- Filtering at island borders. When the renderer samples a texture near an island edge, bilinear filtering blends in pixels from just outside the island. Those pixels are empty, so a dark halo or a thin line appears along the edge.
- Mipmaps. A game engine shrinks the texture into smaller versions called mip levels. At each level the islands get smaller and the empty space bleeds further in. A map that looks clean up close can show seams at a distance.
The margin fixes this. After the bake, Blender extends the island's border pixels outward by a set number of pixels. This padding area is also called the "bleed" or "dilation" in other tools.
The two margin settings
In the Cycles Bake panel, open the Output section. The margin has a type and a size.
| Setting | Python property | What it does |
|---|---|---|
| Type: Adjacent Faces | `margin_type = 'ADJACENT_FACES'` | Fills the margin with pixels taken from the surface of faces next to the island, across a UV seam. |
| Type: Extend | `margin_type = 'EXTEND'` | Smears the island's own border pixels outward. |
| Size | `margin` | Padding width in pixels. |
Extend is the safer default for data maps. It never invents new surface data, and it behaves the same on every mesh. Adjacent Faces can look more accurate across a seam, but it samples real geometry. With a normal bake that means the padding may hold normals that don't fit the neighboring island's orientation.
The margin is measured in pixels, not in UV units. A 16 px margin is a large band on a 512 px image and a thin one on a 4096 px image.
Choosing a margin size
The margin has to survive mip reduction. Each mip level halves the image, so a band of padding loses half its width at every step. A 16 px margin shrinks to 8, 4, 2, then 1 pixel over four mip levels. After that, the neighbor's color starts to mix in.
A common rule of thumb:
| Texture resolution | Margin (px) | Mip levels the padding survives (approx.) |
|---|---|---|
| 512 | 4 to 8 | 2 to 3 |
| 1024 | 8 | 3 |
| 2048 | 16 | 4 |
| 4096 | 16 to 32 | 4 to 5 |
Bigger is not always better. A large margin needs large gaps between islands, which wastes texture space. Pick the smallest value that gives clean results at the mip level where your asset is viewed.
Island spacing must match the margin
The margin only works if the islands have room. Two neighboring islands each grow a band of padding, so the gap between them should be at least twice the margin.
Convert pixels to UV space by dividing by the resolution. For a 2048 px image with a 16 px margin, the gap is 2 × 16 / 2048 ≈ 0.0156 in UV units. This is the value you give to Pack Islands as its margin. The script below sets both values together, so the layout and the bake agree.
import bpy
RES = 2048
MARGIN_PX = 16
low = bpy.data.objects["Asset_low"]
scene = bpy.context.scene
# Bake padding
bake = scene.render.bake
bake.margin_type = 'EXTEND'
bake.margin = MARGIN_PX
# Pack UV islands with a gap of two margins, expressed in UV units
pack_gap = 2 * MARGIN_PX / RES
bpy.ops.object.select_all(action='DESELECT')
low.select_set(True)
bpy.context.view_layer.objects.active = low
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.select_all(action='SELECT')
bpy.ops.uv.pack_islands(margin=pack_gap)
bpy.ops.object.mode_set(mode='OBJECT')
print(f"Margin {MARGIN_PX}px, pack gap {pack_gap:.4f} UV units")Run this before the bake. If you repack after baking, the image no longer matches the layout and you must bake again.
Margin and the empty background
The margin only extends pixels that were baked. Anything outside the margin keeps its initial color. This matters for each map type.
- Normal maps. A new image starts black, but a flat tangent-space normal is a light purple-blue (RGB 0.5, 0.5, 1.0). Black background means "no normal at all," and filtering toward it darkens the edges. The margin covers this near the islands. For safety, set the new image's color to that flat purple-blue when you create it.
- AO maps. An empty background is black, which reads as "fully occluded." If padding is too small, the filtered border darkens. Creating the image with a white background is a common fix, because white means no occlusion.
You can set the starting color when creating the image:
import bpy
img = bpy.data.images.new(
"Asset_normal", 2048, 2048,
float_buffer=False,
)
img.generated_color = (0.5, 0.5, 1.0, 1.0)
img.colorspace_settings.name = 'Non-Color'Also keep Clear Image enabled in the Bake panel so each bake starts from that background rather than stacking on a previous result.
Common margin problems
- Dark lines along island edges in the viewport. The margin is too small, or the Margin type is Adjacent Faces with bad neighbor data. Raise the size, or switch to Extend.
- Details from one island showing inside another. Islands overlap or are closer than twice the margin. Repack with a larger gap.
- Seams that appear only at a distance. Mip levels are eating the padding. Increase the margin to cover more mip levels.
- A bright or dark outline after exporting. The target engine may apply its own dilation or compress the texture. Compression such as block-compressed formats works in 4 × 4 pixel blocks, so a border pixel can pick up color from the background inside the same block. A margin of at least 4 px helps here.
- The margin has no visible effect. Check that you baked after changing it. The setting does nothing to an image that is already baked.
Checking your result
Open the baked image in the Image Editor and zoom in on an island border. You should see a smooth band of color continuing past the edge, not a hard cut to black. Then apply the map in a material and orbit the model at several distances. Seams that vanish up close but return far away point to an undersized margin.
Exercise
You bake a 1024 px AO map with a 4 px margin and islands packed with a gap of 0.004 UV units. The map shows thin dark lines along island edges when the model is viewed from far away. Name two changes that would fix this, and give the pack gap you would use for an 8 px margin.
Show answer
Raise the margin (for example to 8 px) so the padding survives more mip levels, and repack the islands with a larger gap so neighboring padding bands do not collide. You could also create the image with a white background so filtered borders do not darken. For an 8 px margin at 1024 px, the gap is 2 × 8 / 1024 ≈ 0.0156 UV units.
Exercise
Why is the Extend margin type usually the safer choice for a tangent-space normal map than Adjacent Faces?
Show answer
Extend only repeats the island's own border pixels, so it adds no new data. Adjacent Faces copies surface data from neighboring faces, and those normals may not match the orientation of the island they pad, which can produce wrong shading at edges.