contactShadows.package
- ContactShadows.component
- README.md8.8 KB
- contactShadows.module
- contactShadows.renderFeature
- contact_shadows.computeShader
- contact_shadows_blur.computeShader
- contact_shadows_sun.computeShader
- contact_sun_blur.computeShader
- package.yaml21 B
This package holds more than this page lists. See all of its files in its world.
README.md
contactShadows system
Short screen-space shadow traces that supply the small-scale contact a shadow map cannot resolve.
A shadow map has a texel size. Contact finer than that texel — a chair leg on a floor, a prop on a table, fingers on a desk — falls between texels and is simply not there, which is why small objects read as hovering slightly above the surface they are resting on. No amount of shadow resolution fixes it, because the missing detail is below the sampling scale.
This traces a short ray through the depth buffer from each shaded pixel toward the light and darkens the pixel when the ray passes behind visible geometry. It adds the contact term; the shadow map keeps doing everything else.
On by default
Every world draws contact shadows without anything added, on every platform
and at every quality level: the world loader runs them at the settings a
ContactShadows component holds when it is added with none of its own
(contactShadows.DEFAULTS), while the world's .world_settings states
[render]
contact_shadows = true # on when the file names nothing
The setting is read every few frames, so an edit reaches the scene within a
quarter of a second. Anything that states contact shadows takes the default's
place: a ContactShadows component in the scene (one at strength = 0 keeps
them off for as long as it stands), a placed member of the feature in a scene
setup, a feature a script creates, or a script's own contactShadows.set /
clear. A component or a member that leaves hands the scene back to the
default. Each of them runs the one feature, so the default never adds a second
pass. contactShadows.byWorldDefault() says whether the default is what draws,
and contactShadows.useWorldDefault() brings it back in place of a script's
own set or clear.
Using it
local id = entity.spawn("contact_shadows").id
entity(id).component.add("ContactShadows", {
strength = 0.9, -- how dark a fully occluded contact goes, 0..1
length = 0.35, -- trace length in WORLD units
steps = 16, -- samples along the trace
})
Every field is live. Setting strength to 0 turns it off without losing the
rest. For runtime control (a quality slider, a cutscene) drive the module:
local contactShadows = require("@builtin::systems.contactShadows.contactShadows")
contactShadows.set({ strength = 0.9, length = 0.35 })
contactShadows.get()
contactShadows.active()
contactShadows.byWorldDefault() -- whether the world's default is what draws
contactShadows.lights() -- the lights this frame's traces aim at
contactShadows.drawn() -- the newest frame the passes drew, and the lights they traced
contactShadows.clear()
What the knobs do
| field | effect |
|---|---|
strength | How dark a fully occluded contact goes, in [0, 1]. 0 turns it off and releases the pass. |
length | Trace length in world units. This is the scale of contact supplied, so it scales with the scene rather than the screen. |
steps | Samples along the trace. More resolves finer occluders, at linear cost. |
bias | Normal offset before the march starts, so a surface does not read as its own occluder. |
thickness | World-space thickness assumed for an occluder. Past it the ray has emerged out the far side and stops counting. |
localLights | Whether the scene's point, spot and distant lights supply contact of their own alongside the sun. |
maxTracedLights | How many of them a single pixel may trace toward — the heaviest contributors at that pixel. Each one costs that pixel another march, so this is the per-pixel ceiling. |
lightDirection tracks the scene's sun on its own, re-read on each frame the scene's lights move. Pin it
through contactShadows.set when a scene wants the sun's trace aimed
elsewhere; a later set that names no direction goes back to tracking the sun.
Lights
A lamp on a desk grounds the props under it the same way the sun does, and the
same shadow-map texel that loses the sun's contact loses its. So with
localLights the point, spot and distant lights supply contact too, each
traced toward and each weighted by the share of the pixel's light it put there
— its intensity and colour luminance against the same distance attenuation and
cone the renderer shades it with. A pixel darkens by the share of its light the
traces found blocked, which is what keeps a lamp's contact out of the regions
that lamp does not reach.
A local light's ray also stops at the light itself rather than running the full
length: geometry past a lamp stands between nothing.
The pool holds point, spot and distant lights. An area light shades from a surface rather than a point, and the trace takes a single direction per light, so contact under one comes from the sun and whatever punctual lights share the scene with it.
The module selects the pool on each frame the scene's lights or the camera move — every distant light, then the local
lights whose sphere of influence touches the view, ranked by what each is worth
at the camera — and that rank is what a scene holding more lights than the pool
has rows spends them on. contactShadows.lights() reads back exactly what it
selected. The view it selects against is the main camera's, so an offscreen
camera looking somewhere else traces toward the lights the main view holds.
At each pixel the pass then weighs the whole pool — intensity and colour
luminance against that pixel's own N·L, distance attenuation and cone — and
marches toward the maxTracedLights heaviest. So a lamp across the room that
ranked into the pool does not spend the budget of a pixel the lamp beside it is
lighting.
How it renders
contactShadows.renderFeature runs the sun's trace at the depth seam, before
anything shades, and the local lights' traces after lighting.
The sun's march (contact_shadows_sun) starts at each surface, its normal read
off this frame's depth, and steps toward the sun. Each sample along the ray is
projected back to the screen and compared, in world space, against what the
depth buffer holds there: the ray is blocked while it is behind visible
geometry and still within thickness of it. The march is jittered per pixel
so the step pattern lands as noise rather than visible stair-stepping, and a
surface facing away from the sun takes nothing: it is already unlit by it. The
pass writes strength times how blocked the ray is, contact_sun_blur smooths
it along the depth, and a fragment pass composes it into the scene's sun
occlusion channel (@scene.sunOcclusion) with the union blend
(src + dst - src * dst). The sun's term is the only one that reads that
channel, so the sun's contact darkens the sun's light and nothing else: the
sky, the probes, an emissive surface and the other lights keep theirs, and a
participating medium's shadow written into the same channel composes with it.
With localLights, the local march (contact_shadows) reads the deferred
G-buffer's normal, so it needs the deferred path (the default). Its blur
composites the result over the shaded colour scaled by how lit the pixel
already is (@scene.material against the shaded colour), so a region the
shadow map has already darkened takes little further darkening and the two
shadow sources do not stack.
The forward view-projection needed to project a sample back to the screen is the one the frame provides, rather than one passed in from script, so an offscreen capture or an RTT camera projects through its own matrix.
Files
contactShadows.package/
ContactShadows.component/ -- the scene's contact shadows, authored and persisted
contactShadows.module/ -- .set / .get / .clear / .active / .lights / .drawn / .refresh
contactShadows.renderFeature/ -- the passes, and the settings they read
contact_shadows_sun.computeShader/ -- the sun's trace, at the depth seam
contact_sun_blur.computeShader/ -- the sun's share, smoothed
contact_shadows.computeShader/ -- the local lights' trace
contact_shadows_blur.computeShader/ -- the local lights' composite
At lower quality levels
The settings that carry its cost scale with the device's quality level (Field.scaled): the values a setup authors are the high look, and renderer.feature.resolved(handle) states what each level draws. The graphics-quality guide lists the rules.
The local lights' traces are most of what the effect costs a frame, so the high level alone traces them: the medium and low levels trace the sun alone, with localLights reading false there whatever a setup authors, and an authored false reads false at every level. A handheld's high tier draws at the medium level, so a phone or a tablet grounds what stands under its lamps through their shadow maps and the sun's contact.