specular_highlight
Add a small specular glint to a shaded pixel art region so it reads as polished or gem-like. Run after shading, matching light angle and ramp, to place the shine inside the lit side.
Instructions
Place a small specular highlight where the light actually reflects at the viewer.
`shade_region_by_light` spreads the ramp's top step over the whole lit side, which is
what a matte surface does and why its output reads as plastic. A specular is the other
thing at the top of the ramp: a two or three pixel glint on the one part of the form
whose normal sends the light straight back at you. It is the difference between a
stone and a gem, and it is small by definition.
Run this **after** `shade_region_by_light`, on the same region and the same light: it
reuses that tool's region mask, distance field and surface normals, so the glint lands
inside the highlight rather than beside it.
**Reserve the top step for it.** Shade the form with the ramp *minus its last entry*
and then call this with the whole ramp:
shade_region_by_light(f, ramp[:-1], light_angle=135)
specular_highlight(f, ramp, light_angle=135, size=2)
Shading with the full ramp spreads its top step over the entire lit side, which leaves
nothing above it for a glint to be: the specular then paints pixels the colour they
already are, and that call is refused rather than reported as a success that changed
nothing. The refusal says this and names the way out, so it is a reminder rather than
a puzzle.
Where it goes: on the normal closest to the half-vector between the light and the
viewer, not on the normal closest to the light. That offset toward the viewer is what
makes a specular sit inside the lit side instead of out on its shoulder, and it is the
whole reason this is a separate tool rather than a brighter `bias`.
What it will not do: touch an edge pixel. A specular on the silhouette's border reads
as a hole punched in the form rather than as a shine, so only pixels with all eight
neighbours inside the region are candidates. On a region with no such pixel there is
nothing to put a glint on, and the call is refused with the same message
`shade_region_by_light` gives for a region too thin to shade.
Args:
ramp: Colours darkest first, the same ramp the form was shaded with.
light_angle: Degrees, and it must match the shading pass or the glint contradicts
the form. 0 is from the right, 90 from above, 135 from the upper left.
light_z: How much the light comes from the viewer, 0 to 1. Match the shading pass.
size: How many pixels the glint covers. A count, not a radius: a specular is two
or three pixels on most sprites, and the point of this tool is that it stays
that small. The pixels are grown outward from the brightest one and stay
touching, so the result is one glint rather than scattered dots.
tightness: How narrowly the surface has to face the reflection to count, 0 to 1,
as a threshold on the normal against the half-vector. High is a tiny hard
glint on a polished surface; low lets a broader shoulder qualify, from which
`size` still takes only the best pixels. If nothing clears it the call is
refused and the message names the best alignment the form actually offers, so
the number to lower it to is in the error rather than a guess.
bulge: How rounded the form reads. Match the shading pass, or the normals this
works from are not the normals the shading used.
highlight_color: The glint's colour, defaulting to the ramp's top step so
`palette_conformance` stays at 1.0. This is the flag for metal, which is the
one material whose specular is genuinely brighter than its own ramp: name a
near-white there. A colour that is not on `ramp` will drop conformance against
that ramp, which is the honest trade rather than a bug, and the result says
whether it happened.
base_color: Only consider pixels near this colour, as for `shade_region_by_light`.
Scope it the same way you scoped the shading.
tolerance: How close a pixel must be to `base_color` to count as part of the
region. Ignored when `base_color` is omitted.
layer: Target layer (default: top layer).
frame: Target frame, 1-based.
Returns `specular_pixels` (how many were placed, which is below `size` when the
eligible area is smaller than the budget), `pixels` (where they went, so a later
`remove_stray_pixels` can be told to protect them), `pixels_changed` (how many were
not already that colour) and `peak_alignment`, the best normal against the
half-vector found anywhere in the region.
Also `region_components` and `region_bounds`, which say whether the region is where it
was meant to be. Worth reading on this tool in particular: the normals come from the
*whole* region, so scoping a glint to a compound object rather than to one of its parts
puts it wherever that object's overall mass bulges. A sword scoped by its whole cell
got its only glint on the leather grip, with none on the blade, and
`specular_pixels: 2` said nothing about that.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ramp | Yes | ||
| size | No | Default: 2. | |
| bulge | No | Default: 1.0. | |
| frame | No | Default: 1. | |
| layer | No | ||
| light_z | No | Default: 0.45. | |
| filename | Yes | ||
| tightness | No | Default: 0.7. | |
| tolerance | No | Default: 24.0. | |
| base_color | No | ||
| light_angle | No | Default: 135.0. | |
| highlight_color | No |