8.5 KiB
Selection outline — High Level Design
Purpose and scope
When the Outline view option is on, the selected volumes of the 3D canvas are outlined: a ring of constant screen width around their silhouette, and thinner lines along their inner edges, the creases between faces, the contours where one part of the selection hides another, and the seams between neighboring parts. It is a view setting. It changes nothing in the model or the project, and it reaches neither the picking pass nor thumbnails.
Preferences > Graphics > Outline provides a Persistent option, off by default, which outlines all visible, active volumes except the wipe tower even without selection. The Outline view option in the 3D canvas menu controls visibility in both modes: when it is off, no outlines are drawn. When it is on, Persistent chooses between all eligible volumes and selected volumes only. Selected volumes use the Selection color and persistent outlines of unselected volumes use the Persistent color, both configurable and white by default. The Persistent color button is disabled while Persistent is off, as it has no effect then. These application preferences are saved immediately and redraw the current canvas when Preferences closes; they do not require a restart.
SelectionOutline draws it after the object passes of GLCanvas3D::_render_scene(). Its cost
depends on their screen area, not on their triangle count, apart from one extra draw of the outlined
volumes.
Without Persistent, all selected volumes share one outline, drawn once per frame. With Persistent, each object instance gets an outline of its own, drawn from back to front by the view depth of its bounding box center, so an object in front of another keeps its full ring over it instead of the thin line that marks two parts meeting. The passes below then run once per instance, each within the screen box of that instance, so their cost grows with the number of outlined objects and the screen area of their boxes. A third composite draw puts the ring of each instance over the outlined volumes around it as well, where the depth test lets a nearer volume hide it.
Passes
GLVolumeCollection::render() collects the selected volumes it draws (all eligible volumes in persistent
mode), when the canvas passes it a list,
and sets bit SelectionOutline::StencilBit of the canvas stencil wherever one of them passes the depth
test. The stencil is cleared with the depth buffer at the start of the frame. SelectionOutline::render()
then runs three passes, restricted by the scissor to the screen box of the selection, padded by how far
each pass reads the one before it.
- Mask (
outline_mask). The selected volumes are drawn again, with the clipping of the object passes, into a 16 bit RGBA target with a depth texture of its own. Each texel packs, in two bytes per channel, the octahedral encoded view normal turned towards the camera, the part index of its volume, and the distances from the texel center to the edge of its triangle along +x, -x, +y and -y. Selected volumes take part indices above 32768 and unselected ones those below, so the composite tells them apart from the mask alone. GLModel gives every triangle three vertices of its own, sogl_VertexID % 3yields the barycentrics, and their screen derivatives the distances. - Edges (
outline_edges). For every texel, and each pair it forms with its right and upper neighbor, an inner edge is classified and located, and packed as a 4 bit strength and a 4 bit crossing position between the two texel centers. Along its row, the pass also finds the nearest covered texel and sums the coverage under a gaussian. - Composite (
outline). A full screen quad, drawn twice into the canvas framebuffer. The ring pass draws where the stencil bit is clear, so its inner edge is the multisampled silhouette of the selection itself. The lines pass draws where it is set, so inner lines stop exactly at the silhouette.
The targets, 16 bytes a pixel at canvas size, are allocated when something is outlined and kept from frame to frame, and they are freed in the first frame with nothing to outline.
Inner edges
A pair of covered texels makes an edge when its normals differ by more than 25 degrees, fully at 40, when one surface hides the other, or when the texels belong to different parts. One surface hides the other when the view space step between the texels lies more than a few pixels off their planes, and each texel lies more than a pixel off the surface of the other side carried on over the pair. A curved surface seen edge on carries on smoothly, so it does not break into lines, nor does a fillet whose facets turn a little at every texel on both sides. A texel on a triangle smaller than a pixel both ways takes the normal of a neighbor beside it: the normals of such slivers can be far off their surface and would otherwise show as dots.
The crossing is placed between the texel centers, in this order of preference:
- where the triangles under both texels end, when they agree: the edge they share;
- where the depth planes of the two sides meet, extrapolated from two texels on each side, when each side is flat over them and the planes part clearly along the axis;
- for a hidden surface, where the triangle of the nearer one ends, when the texel behind it or the two beside it end on the same straight line, as a curved contour made of strips narrower than a pixel does not;
- otherwise half way.
The lines pass takes the nearest crossing along the row of the pixel and the nearest along its column. For a straight line at distance d, they lie at d / |cos| and d / |sin| of its normal, so 1 / d² = 1 / dx² + 1 / dy², which gives the distance at any angle without knowing it. The line is drawn with a smooth edge around its half width.
Ring
The binary mask places the silhouette only within half a texel, which would show as a ripple along the outer edge of the ring. The coverage of the mask under a gaussian, summed over the rows by the edges pass and over the columns by the ring pass, is the normal tail beyond d / sigma for a straight silhouette at distance d, which the ring pass inverts for a sub-texel distance. Corners and features thinner than the gaussian lose coverage faster, so the distance is kept within half a texel of the one to the nearest covered texel, which the same two passes find exactly.
Occlusion
The composite is depth tested against the canvas, so what stands in front of the selection hides its outline. It writes the depth of the selection at each pixel, from the mask, moved forward by the spread of that depth over the pixel, so that the selection passes the test on every sample. The ring takes the depth of the nearest covered texel, brought forward with its distance from the silhouette, so that it also shows over the plate the object stands on, which lies in front of the silhouette right below it.
The outline sets SelectionOutline::DrawnBit of the stencil wherever it draws, and the screen space
ambient occlusion pass of the realistic view, which runs after it, leaves those samples as they are. The
outline lies on top of the surfaces, so their occlusion would only darken it, unevenly, in the creases it
marks.
Colors and sizes
The outline uses the solid RGB colors selected in Preferences, without theme remapping: the Selection color for selected volumes and the Persistent color for the other persistent outlines. Each pixel takes the color of the volume its ring or line belongs to: the covered texel under it, or for the ring outside the silhouette, the nearest covered texel. Square color swatch buttons display both colors and open the color picker; a disabled one shows its color grayed. The lines have a width on the model, 0.35 mm for the ring and 0.18 mm for the inner lines, so they grow as the camera closes in, kept between 2.5 and 5 pixels for the ring and 1.2 and 2.4 for the inner lines, scaled by the canvas scale. An inner line that would be thinner than 1.2 pixels fades instead, down to 40%, so that a far selection does not turn into lines. Inner lines are drawn at 75% opacity. Both have a smooth edge 1.2 pixels wide.
Requirements
The outline needs ARB framebuffer objects, a 16 bit RGBA color renderable texture and the outline
shaders, and it is not drawn without them. Under GLSL 1.10 there is no gl_VertexID: crossings fall
back on the depth planes and the half way point, and slivers are not detected. Without 8 stencil bits,
which the two bits used need, the ring stays off the pixels the mask covers and loses the exact inner
edge, and the occlusion pass shades the outline too.