Part 9 of 13 in Ghostveil
Casting Fantasma's shadow
Fantasma's shadow in Ghostveil is now cast by the room's light from her own outline in every frame, round and centred under her, and every shadow in a room is half as long as it was.
Ghostveil is a puzzle game about Fantasma, alone in a machine buried under a castle, where every room is a lock you open by walking: stones slide, turnstiles swing, plates sink. Since 9th October the rooms have had one light. Each frame, everything standing in the room stamps how tall it is into a height map, one texel per art pixel, and a fragment shader works out the shadows of a sun high in the north from that map, so every shadow falls south.
Fantasma was the odd one out. She still drew the art sheet's own soft oval under her feet, and the room's light cast a second shadow from her as well. That one came from her outline: each of her columns stood on the two rows at her feet, as tall as she is in that column, so what reached the floor was her silhouette laid down flat to the south of her. Front on she is 14 pixels wide and 16 tall, and with a wall's 16 pixels throwing six rows, she threw a block about six rows deep across eight of her columns. She had two shadows, and the one the light gave her was a slab.
This is the light's shadow on its own, now, as she walks a little loop on open floor:
An oval under her feet
Her columns now stand on an oval instead of in a line. Every column still casts, but its height is taken from her tallest column and lowered towards the ends:
Here is a column of her 32-pixel frame, numbered from 0 at the left, so that 16 is the line through the middle of her feet; is her tallest column in the frame she is showing; is half her width; is how far column sits from that middle line, as a share of ; and is the height column stamps into the map. Half her width is measured as the average of how far she reaches either side:
where and are the leftmost and rightmost columns her pixels reach; the code calls rx. Side on, facing right, she reaches 7 pixels behind her feet (her hood) and 6 in front, so and the oval is centred on her feet, the same both ways, although her outline is not. Front on, .
final rx = ((16 - l) + (r + 1 - 16)) / 2;
final tallest = columns.reduce(max);
// Each column she stands in, on the underside of every part of her
// there (her hood hanging over her legs side on, her feet), so the
// shadow starts right where she ends and the floor seen under any part
// of her is in it; one past the oval (her cape behind her) casts a
// little, never none
for (var x = 0; x < 32; x++) {
if (undersides[x].isEmpty) continue;
final u = (x + 0.5 - 16) / rx;
final e = max(0.35, sqrt(max(0.0, 1 - u * u)));
final p = paint(tallest * e * _shadowScale);
for (final under in undersides[x]) {
canvas.drawRect(Rect.fromLTWH(x0 + x, y + 2 - 24 + under, 1, 1), p);
}
}
The floor of 0.35 is for the outermost column. Side on, her back edge is exactly from the middle, , and the square root alone would give it nothing; with the floor it stamps 0.35 of her height, 5.6 pixels when she stands 16 tall. _shadowScale is 1 while she stands and runs down to nothing as she falls.
The other change in that loop is where each column stamps. It used to be the two rows at her feet. Now it is every row in the column where a run of her pixels ends with clear floor below it: the bottom of her feet and, side on, the lower edge of the hood where it hangs out past her legs. In the hood's columns the old stamp sat down at her feet row, so the floor under the hood stayed lit; now the shadow begins at the hood's edge.
Half as long, and rounded
The shader decides whether a floor pixel is in the sun by firing fifteen rays at it, five spread along the horizon and three at different heights, each stepping north a row at a time:
float sunlight(vec2 p) {
float hit = 0.0;
for (int a = 0; a < 5; a++) {
float drift = (float(a) - 2.0) * 0.5 * uSpread;
for (int e = 0; e < 3; e++) {
float slope = uSlope * (0.75 + 0.25 * float(e));
for (int i = 1; i <= 16; i++) {
float t = float(i);
vec2 q = p - vec2(drift * t, t);
if (heightAt(q) * slope >= t && rounded(q) * slope >= t &&
heightAt(vec2(q.x, p.y)) <= 0.0) {
hit += 1.0;
break;
}
}
}
}
return 1.0 - hit / 15.0;
}
A ray is blocked at the first point rows north of the pixel where
where is the point rows north (shifted sideways a little by the ray's drift), is the height stamped there, is the sun's slope in rows of shadow per pixel of height, and picks which of the three ray heights this is. So on the middle ray a thing pixels tall throws rows. Leaving out the shade at the foot of things, a floor pixel takes the dark tone when 11 of its 15 rays are blocked and the lighter one when 4 are.
The slope had been , six rows for a 16-pixel wall. Six rows read as too thick, so I halved it:
static const double _slope = 3 / 16, _spread = 0.12, _dark = 0.36;
That halves every shadow in the room at once, hers included. Facing down, her middle columns now throw rows on the middle ray and her outermost ones , where before the middle eight all threw 6 and the ends 4.5.
The far end of a shadow was still square on anything with straight sides, so rounded(q) reports a thing as 0.6 of its height in its outermost column on either side and 0.85 in the next one in. Those columns' rays stop earlier, which takes the corners off. The shade at the foot of everything, which stands for the dim light from overhead, is weakened the same way at a thing's two side columns.
A crate had the same double shadow Fantasma had: its sprite comes with its own drawn shadow round its foot, and the light cast another. Where the light can run, the crate is now drawn clipped to the box alone, columns 10 to 21 and down to row 20 of its 32, and it stamps its lower half, rows 16 to 20 of those columns, at height 7, so its shadow starts at its foot.
The sheet's oval has not gone. If the light's shader cannot run on a device, or a room's shadows are the pack's own painted ones, she draws it again, since in those rooms nothing else gives her a shadow. Lying down after a fall she still stamps a plain block, 16 pixels by 4 at height 5, so that shadow keeps its square corners.
Fantasma and the dungeon art are from SnowHex's Dungeon Gathering and its expansions; the room light, the shadow shapes and the rules are Ghostveil's.