Sub topic 10
Cage, Extrusion, and Max Ray Distance
This chapter assumes you can set up a Selected to Active bake. You should know the high-poly and low-poly objects need applied transforms and that the target image is already assigned. Here we look at the three settings that decide where each bake ray starts and how far it travels: cage, extrusion, and max ray distance.
How a bake ray finds the high-poly surface
For each pixel of the target image, Cycles finds the matching point on the low-poly surface through the UV map. It then fires a ray to look for the high-poly surface. By default the ray starts at the low-poly surface and travels along the low-poly normal. Anything the ray hits is recorded in the bake.
This is where problems start. If the high-poly surface sits outside the low-poly surface, the ray starting there points away from it and misses. If two surfaces of the high-poly mesh are close together, a ray can hit the wrong one. The settings in this chapter move the ray's start point and limit its length.
Extrusion
With no cage object, Extrusion (cage_extrusion in Python) pushes the ray's start point outward along the low-poly normals by a fixed distance. The ray then travels back inward. The distance is in scene units, so it depends on your object's scale.
- Too small: parts of the high-poly mesh that stick out past the extruded surface are not reached. They show up as missing detail or flat areas.
- Too large: the start point can move past nearby geometry. Rays then hit the wrong surface, such as the neighboring finger of a hand or the opposite wall of a groove.
A practical starting value is a little more than the largest gap between the two meshes. For an asset a couple of meters wide, 0.02 is a common first try. Increase it only until the missing areas fill in.
Max ray distance
Max Ray Distance (max_ray_distance) limits how far a ray may travel from its start point. A value of 0.0 means unlimited. Any positive value means rays that do not hit within that distance count as misses.
Setting a limit protects you from long stray rays. For example, a ray that passes through a thin gap can otherwise hit a surface on the far side of the model. A small limit rejects that hit.
The limit has to be at least as large as the extrusion, because the ray first has to travel back across the extruded gap. A frequent mistake is to raise extrusion to fix misses but leave a small max ray distance, which makes the misses continue.
| Setting | What it controls | Too low | Too high |
|---|---|---|---|
| Extrusion | Offset of the ray start along low-poly normals | Misses on protruding high-poly detail | Rays start inside or beyond neighboring geometry |
| Max Ray Distance | Longest allowed ray length | Valid hits rejected, leaving flat patches | Stray hits from distant surfaces (0 means no limit) |
Using a cage object
Extrusion pushes every vertex along its own normal. On a hard-edged model this can pull corners apart, because the normals at a corner point in different directions. Rays near the edge may then cross over and hit the wrong face. A cage is a separate mesh that gives you direct control over the ray start positions.
To make one:
- Duplicate the low-poly object and name it, for example,
Asset_cage. - In Edit Mode, scale the duplicate outward with Alt+S (Shrink/Fatten), which moves vertices along their normals. You can also add a Displace modifier.
- Check that the cage fully encloses the high-poly mesh. Move individual vertices where detail still pokes out.
- Keep the vertex count, order, and UVs identical to the low-poly mesh. Do not add or delete geometry. Changing the topology breaks the match between cage and low poly, and the bake will fail or give wrong results.
- In the Bake panel, under Selected to Active, enable Cage and pick the cage object in the Cage Object field.
Select the high-poly object, then the low-poly object last, so it is the active one. The cage does not need to be selected.
With a cage assigned, the extrusion value is not used. The cage defines the start points. Max Ray Distance still applies and is measured from the cage.
Setting the values in Python
The script below sets extrusion and a ray limit. It also shows how to switch to a cage object. It assumes the objects from the earlier scripts exist.
import bpy
scene = bpy.context.scene
bake = scene.render.bake
bake.use_selected_to_active = True
use_cage_object = False
if use_cage_object:
cage = bpy.data.objects["Asset_cage"]
bake.use_cage = True
bake.cage_object = cage
else:
bake.use_cage = False
bake.cage_extrusion = 0.02
bake.max_ray_distance = 0.05Here max_ray_distance is larger than cage_extrusion, so valid hits are not cut off. When the cage is used, set max_ray_distance relative to the cage gap, not the original low-poly surface.
Tuning workflow
Change one value at a time and run a quick, low-sample bake. A 512 px image at 16 samples is enough to see ray problems.
- Start with extrusion at
0.01to0.02and max ray distance at0.0. - Bake and look at the normal map. Large flat-colored patches (the default normal blue, or black in an AO map) mean misses.
- Raise extrusion in small steps until the patches close.
- If you now see details copied from the wrong place, such as ghost shapes from a neighboring part, set a max ray distance slightly above the extrusion.
- If hard edges still break, switch to a cage and move the problem vertices by hand.
- Do the final high-resolution bake only when the quick bakes look right.
Edge cases
- Scale: Both values are in scene units. A model scaled to 100 times its size needs values scaled the same way, and unapplied object scale gives confusing results. Apply scale first.
- Overlapping parts: Pieces of the high-poly mesh that sit inside each other can be hit by a ray before the intended surface. A small max ray distance or a more careful cage helps. Moving the parts apart for the bake is also an option.
- Concave areas: Inside deep grooves, an extruded start point can pass through the opposite wall. Use a cage and pull those vertices in by hand.
- Ambient occlusion bakes: AO uses the same ray start rules to find the surface, then casts its own sampling rays. A bad cage or extrusion causes the same misses and wrong hits in an AO map as in a normal map.
Exercise
Your normal bake shows flat default-colored patches on a bolt head that sticks out of the high-poly mesh. You raise Cage Extrusion from 0.02 to 0.06, but the patches stay. Max Ray Distance is set to 0.03. What is the likely cause, and what do you change?
Show answer
The max ray distance is smaller than the extrusion. Rays start 0.06 away from the low-poly surface but may travel only 0.03, so they stop before reaching the bolt. Raise Max Ray Distance above the extrusion (for example 0.1), or set it back to 0 for no limit, and bake again.
Exercise
Why can a cage object give better results than extrusion on a model with hard edges, and what must you not change on the cage?
Show answer
Extrusion moves each vertex along its own normal, which pulls corners apart at hard edges and sends rays across the wrong faces. A cage lets you place the start points by hand so the shell encloses the high-poly mesh evenly. You must not change the cage's topology (vertex count, order, or UVs), because it has to match the low-poly mesh exactly.