Part 12 of 13 in Ghostveil
Looking through a portal
Ghostveil's portals project pixel artwork without scaling it. The same geometry lets the renderer skip drawing surfaces outside the view.
A portal in Ghostveil has to show enough of the other room to make its connection readable. Ghostveil is a puzzle game set inside an ancient machine, with stones to push, turnstiles to swing and circuits to complete. Its portals sit in wall arches and movable stone pillars. Rotate a pillar and the opening changes direction with it, so the view has to follow the same turn while preserving the original pixel artwork.

That last constraint took a few attempts to get right. Shrinking a room into a tiny camera view made Fantasma and the objects around her unreadable. Resampling damaged their pixel clusters. Keeping their original dimensions gave the view a firmer rule: a small opening reveals a small part of the world, and anything larger than that opening gets clipped by its edges.
The resulting renderer uses an affine frame with separate ground and upright surfaces. Depth helps decide what is in front and, for a downward floor view, where a ground point appears vertically. Every upright sprite keeps its native dimensions.
A coordinate frame at the other opening
All distances below are in art pixels. The world uses screen coordinates, with positive horizontal coordinates to the right and positive vertical coordinates downwards. Each floor cell is 16 pixels square.
The destination portal supplies an origin at its physical face and an outward unit vector. From that vector I get a perpendicular vector for the horizontal direction of the view:
Here points out of the destination portal, points right across its view, is a world ground anchor, is the portal origin, is their displacement, is the anchor's horizontal position in portal space and is its depth beyond the opening.
This handles all four directions. A north-facing destination has an outward vector of and a right vector of . A south-facing destination has and . The same world displacement therefore changes sign across the view when the destination turns halfway round.
The code is small:
Offset get right => Offset(-_normal.dy, _normal.dx);
double depth(Offset world) {
final p = world - origin, n = _normal;
return n.dx * p.dx + n.dy * p.dy;
}
The portal frame changes the character's viewing direction too. The portal renderer picks an existing directional frame for Fantasma based on the destination's orientation. Her direction in the game and her animation timing stay unchanged. A view from another side needs the corresponding artwork.

Height without shrinking
Vertical placement has two cases. In the ordinary upright view, a sprite's ground anchor sits on a common baseline. In the approved close-wall floor view, depth also moves that anchor upwards through the image:
Here is a projected point, is the target centre of the native scene crop, and are the horizontal position and depth calculated above, is the portal's height registration, is the point's height above its ground anchor, and is zero for the upright view or one for the downward floor view.
The implementation follows that expression directly:
Offset project(Offset base, double aboveFloor) {
final p = base - origin;
return target +
Offset(
right.dx * p.dx + right.dy * p.dy,
height - aboveFloor - (lookDown ? depth(base) : 0),
);
}
For an upright sprite, I project its ground anchor once, round the displacement onto the native pixel grid, then translate the original artwork so its source base meets that projected anchor:
void upright(Canvas canvas, Offset base, {Offset? sourceBase}) {
final at = pixelGround(base);
final source = sourceBase ?? base;
// Translation only. Sprite artwork has no portal scale transform.
canvas.translate(at.dx - source.dx, at.dy - source.dy);
}
There are two anchors because the world ground point and the bitmap's existing drawing anchor can differ. The same subtraction handles those differences for every upright body. Walking and sliding positions are projected before being rounded onto the portal's art-pixel grid.
Two pixels separated vertically by 24 rows stay 24 rows apart at every depth, so an opening that reveals eight of those rows clips the rest of the artwork without changing the size of either pixel.
Putting the floor on its own plane
Flat scenery needs a ground transform. With the ordinary upright frame, ground height is zero and depth contributes no vertical displacement, so the floor projects to a line. That is an explicit limit of this presentation. Painting a compressed strip of distant wall or floor across the opening would introduce an artificial horizon.
The close-wall view uses the other frame: when a pillar has one clear floor cell before a wall or doorway frame, it looks down at that cell. With two clear cells it keeps the upright view. A character or solid object occupying the mouth retains the upright presentation. This rule chooses which part of the destination artwork reaches the opening while the puzzle continues to use the same traversal, collision and fire-conduction rules, so looking down at a floor tile cannot change where a traveller lands or how a circuit connects.
The stair test gives a concrete example. Its south-facing destination has origin , crop centre and height registration . The adjacent stair cell has centre . Its depth is , its horizontal displacement is zero, and its centre projects to . The full 16-pixel floor tile retains its extent through the cardinal ground transform, with only the portion inside the opening visible.

The stairs had been missing because their component inherited the fallback for an upright sprite. Giving them a ground surface corrected their position through the shared renderer. Their artwork stayed unchanged. The portal traverses the live scene graph. Each component supplies its own geometry, including ground or upright surfaces, physical volumes and source anchors, so the renderer can use the existing lamp flame frames and furniture artwork without maintaining a separate collection of objects that portals are allowed to show. Components without extra geometry metadata still render conservatively through the fallback.
Clipping and occlusion
The destination is rasterised into a 20 by 20 pixel crop. One source art pixel maps to one buffer pixel before the final display zoom. The opening samples whole-pixel strips from that crop, with interpolation and antialiasing disabled for the copy; its mask determines which samples reach the pillar face, and the final room zoom enlarges those pixels together with the rest of the game.
Ground polygons are clipped at the physical portal plane. Upright surfaces use their visible near faces, discard back-facing surfaces where a normal is supplied, and paint from far to near, with a stable order at equal depth. Actual source alpha supplies the holes between opaque pixels. Lighting is replayed in source space and masked back to the source picture's alpha.
Reveal detection is separate from drawing. Scenery can activate the live view within two floor-cell lengths of the destination mouth; visible moving subjects keep their existing visibility rule. The displayed scene has no matching two-tile cutoff. This distinction lets nearby scenery wake the view without removing visible destination content behind it.
A portal looking at another pillar also exposed an orientation bug. The pillar body already composed its turn with the receiving camera's angle, but the pink opening used the main camera's angle, leaving the body and its pane in different coordinate frames whenever another portal looked at the pillar from the side. Applying the receiving angle to both body and opening removed that pane.

Other portal surfaces retain their veil and rim inside this view. There is no recursive portal rendering.
Rejecting paint outside the opening
In the workshop, the shared scene pass was drawing and shading room tiles that fell outside the portal's 20 by 20 pixel crop. Clipping the final canvas kept those pixels invisible while still paying for their paint work.
I added paint bounds to surfaces that can report them. For a rectangular ground patch, an affine transform gives its projected bounding box directly:
Here and are the projected bounding box's width and height, is the portal's right vector, and are the source paint rectangle's width and height, and is the negative outward vector in the downward view or the zero vector in the upright view.
Its centre is the transformed source centre. If that rectangle misses the scene crop, the renderer skips the surface before painting or shading it. Upright paint bounds use their actual ground-to-source translation for the same overlap test.
Bounds include painted overhangs.
A component with unknown bounds stays in the render pass, so adding an unfamiliar object cannot silently make it disappear. The renderer still visits the room's components, including those with unknown paint bounds, and uses the projected rectangles only to reject drawing and shading that cannot contribute pixels to the crop, so this change reduces paint work without introducing a distance limit.
The aperture geometry cache also now keeps separate slots for the main view and each cardinal destination view. Nested and main views had been evicting each other's single cached result. The new cache has five slots: one for the main view and one for each cardinal destination view, with each changing turn angle replacing the entry in its existing slot so a continuously rotating pillar cannot grow the cache indefinitely.

The same scripted workshop sequence, including pillar turns and pushes, produced 343 frames in 20.002 seconds before the changes and 1,202 in 20.008 seconds afterwards: 17.1 fps and 60.1 fps. The earlier run had already fallen back to simpler room lighting. The corrected run retained the full lighting shader.
Those are single Mac Profile runs, with a ten-second warm-up and no recording during measurement. The 95th-percentile total frame span fell from 123.06 ms to 17.994 ms; individual frames can still exceed a 60 Hz budget. Because the two builds also differ in the geometry corrections and aperture cache, the before-and-after result measures the combined change; isolating the contribution of each optimisation would require further runs with those changes enabled individually, under the same workload.
For the culling regression, eight cardinal ground and upright cases produce byte-identical native images with culling enabled and disabled. In the controlled scene, 41 paint callbacks fall to at most three, including a case that retains visible upright scenery 160 pixels away. Separate tests cover the carried opening's angle and stair artwork in all four directions.
The current Mac build has those corrections. Phone performance and longer-session behaviour still need measurement.
Update, 20th October 2026: every pillar view now looks down at the floor; Looking down through every portal follows on from this post.
The sprites come from SnowHex's Dungeon Gathering: Under The Castle; Ghostveil supplies the room construction, lighting, portal rendering and puzzle rules.