Recursive Non-Euclidean Portal Rendering with Oblique Near-Plane Clipping
Renders seamless non-Euclidean portals by transforming a virtual camera into the linked twin portal's frame, rewriting the projection matrix with an oblique near plane aligned to the exit face, and recursing via render targets or stencil masking.
Also known as recursive portal rendering, non-Euclidean portals, oblique-clipped portals, stencil portal rendering
In the family Visibility, shadows & ambient occlusion, portals & visibility
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | |
| Unity | None found | |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
A non-Euclidean portal connects two arbitrarily placed and oriented apertures A and B in space so looking into A reveals the scene as seen from B. To compute the virtual camera pose, the viewer's world transform is mapped into portal A's local frame, rotated 180 degrees around the portal's up axis to face outward, and placed into portal B's world frame.
A naive virtual camera renders geometry sitting behind portal B (between the virtual camera and the exit plane), which wrongly occludes the view through the opening. Using Lengyel's oblique near-plane clipping (2005), the exit portal plane is transformed into the virtual camera's view space and used to replace the third row of the perspective projection matrix, scaling it so the far plane still encloses the view frustum. Hardware clip-space culling then discards every fragment behind the exit portal with zero extra fragment shader cost.
To render portals seen through portals, the renderer recurses up to a bounded depth—either nesting stencil-buffer masks and clearing depth inside each portal silhouette, or rendering nested off-screen portal textures bottom-up from deepest bounce to shallowest—while culling portals that are back-facing or outside the parent portal's screen-space bounds.
When to choose it
Reach for it when
- render seamless non-euclidean doorways and infinite hall mirrors
- geometry behind a portal plane pokes through the virtual camera
- need nested portal views without stencil buffer depth glitches
- clip near plane cleanly to an arbitrary angled portal quad
- mirrors or portals show objects situated between eye and frame
Choose it for non-Euclidean spatial puzzles, impossible architecture, seamless teleporters, and linked wormhole apertures where players can inspect and walk directly through arbitrary wall-to-wall connections.
Prefer Planar reflections with oblique near-plane clipping when reflecting a single flat mirror or water surface rather than teleporting the camera to a distinct exit frame. Prefer Portal and PVS visibility culling when portals only act as Euclidean room-to-room visibility boundaries for frustum culling rather than spatial warps.
Field marks
- Severe oblique angles tilt the far clipping plane toward the viewer and compress depth buffer precision, causing distant z-fighting unless paired with reverse-Z depth buffers
- Crossing a portal plane causes near-plane clipping gaps if the portal surface is paper-thin instead of extruded into a shallow box or switched smoothly on camera traversal
- Mutually facing portals trigger exponential pass counts without strict recursion depth limits and screen-space occlusion culling
- Screen-space post-processing effects (SSAO, SSR, bloom) fail to cross portal boundaries seamlessly when portals are composited from separate render targets
Presets
Recursive Oblique Portal Rendering — unclipped near-plane leak (left) vs oblique-clipped recursive non-Euclidean portal tunnel (right)
{
"P_RECURSION": 5,
"P_TWIST": 0.35,
"P_ORBIT": 0.45,
"P_SPLIT": 0.46
}Show GLSL (143 lines)
const float P_RECURSION = 5.00; // P Portal Bounces [1.0, 7.0, 1.0]
const float P_TWIST = 0.35; // P Exit Frame Twist [0.0, 0.9, 0.05]
const float P_ORBIT = 0.45; // P Camera Motion [0.0, 1.5, 0.05]
const float P_SPLIT = 0.46; // P No Oblique / Clipped [0.0, 1.0, 0.02]
vec2 rot2(vec2 p, float a) {
float c = cos(a);
float s = sin(a);
return vec2(c * p.x - s * p.y, s * p.x + c * p.y);
}
vec3 aces(vec3 x) {
x = max(x, 0.0);
return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
float screenX = fragCoord.x / max(iResolution.x, 1.0);
float split = clamp(P_SPLIT, 0.0, 1.0);
bool useOblique = screenX >= split;
int maxBounces = int(clamp(floor(P_RECURSION + 0.5), 1.0, 7.0));
float twist = clamp(P_TWIST, 0.0, 1.0);
float t = iTime * max(P_ORBIT, 0.0);
// Camera inside sunlit non-Euclidean gallery looking directly into Portal A (z = -0.85)
vec3 ro = vec3(0.28 * sin(t * 0.65), 0.06 + 0.08 * cos(t * 0.8), 1.35);
vec3 rd = normalize(vec3(uv.x - 0.14 * sin(t * 0.65), uv.y - 0.02, -1.35));
vec3 col = vec3(0.0);
float atten = 1.0;
// Trace ray with up to maxBounces non-Euclidean portal teleports!
for (int bounce = 0; bounce < 8; bounce++) {
if (bounce > maxBounces) break;
float tBest = 1e5;
int hitKind = 0; // 1=room, 2=portal aperture, 3=portal rim, 4=occluding pillar behind exit portal!
vec3 hitP = vec3(0.0);
vec3 hitN = vec3(0.0, 0.0, 1.0);
// Gallery chamber planes: x in [-1.2, 1.2], y in [-0.75, 0.75], z in [-0.85, 1.65]
vec3 bMin = vec3(-1.20, -0.75, -0.85);
vec3 bMax = vec3(1.20, 0.75, 1.65);
vec3 invR = 1.0 / (rd + vec3(1e-6));
vec3 tA = (bMin - ro) * invR;
vec3 tB = (bMax - ro) * invR;
vec3 tFarV = max(tA, tB);
float tRoom = min(min(tFarV.x, tFarV.y), tFarV.z);
if (tRoom > 0.005) {
tBest = tRoom;
hitP = ro + rd * tRoom;
hitKind = 1;
if (abs(tRoom - tFarV.x) < 1e-4) hitN = vec3(-sign(rd.x), 0.0, 0.0);
else if (abs(tRoom - tFarV.y) < 1e-4) hitN = vec3(0.0, -sign(rd.y), 0.0);
else hitN = vec3(0.0, 0.0, -sign(rd.z));
// Check if hit on back wall (z = -0.85) falls inside Portal A frame
if (abs(hitP.z + 0.85) < 0.01) {
vec2 q = abs(hitP.xy) - vec2(0.46, 0.44);
float dFrame = max(q.x, q.y);
if (dFrame < 0.0) {
hitKind = 2; // Inside Portal A aperture!
} else if (dFrame < 0.055) {
hitKind = 3; // Glowing portal frame rim
}
}
}
// Hero floating brass sphere inside gallery
vec3 sc = vec3(0.62, -0.22, 0.10);
vec3 oc = ro - sc;
float bs = dot(oc, rd), ds = bs * bs - (dot(oc, oc) - 0.22 * 0.22);
if (ds > 0.0) {
float ts = -bs - sqrt(ds);
if (ts > 0.01 && ts < tBest) {
tBest = ts; hitKind = 5; hitP = ro + rd * ts; hitN = normalize(hitP - sc);
}
}
// On LEFT side (bounce >= 1), simulate missing oblique near-plane clipping:
// An intrusive red blocker pillar sitting BEHIND the exit portal leaks into the virtual camera!
if (!useOblique && bounce >= 1) {
vec3 pc = vec3(-0.12, 0.0, 1.05);
vec3 pr = vec3(0.16, 0.72, 0.14);
vec3 p0 = (pc - pr - ro) * invR;
vec3 p1 = (pc + pr - ro) * invR;
vec3 pn = min(p0, p1), pf = max(p0, p1);
float tn = max(max(pn.x, pn.y), pn.z);
float tf = min(min(pf.x, pf.y), pf.z);
if (tf > max(tn, 0.01) && tn < tBest) {
tBest = tn; hitKind = 4; hitP = ro + rd * tn;
hitN = vec3(0.0, 0.0, 1.0);
}
}
if (hitKind == 2 && bounce < maxBounces) {
// Teleport ray from Portal A (z = -0.85) to Portal B (z = 1.48) with slight twist & scale!
// With oblique near-plane clipping enabled, ray starts strictly AT the exit plane (z = 1.46).
// Without oblique clipping (left), virtual camera starts behind exit plane (z = 1.62) and clips wrong geometry!
vec2 exitXY = rot2(hitP.xy, twist * 0.38) * 0.96;
ro = vec3(exitXY.x, exitXY.y, useOblique ? 1.44 : 1.62);
rd.xy = rot2(rd.xy, twist * 0.38);
atten *= 0.88;
continue;
}
// Shade terminal hit
vec3 lDir = normalize(vec3(0.35, 0.82, 0.45));
float ndl = max(dot(hitN, lDir), 0.0);
vec3 hitCol = vec3(0.05);
if (hitKind == 1) {
vec2 tileUv = (abs(hitN.y) > 0.5) ? hitP.xz : ((abs(hitN.x) > 0.5) ? hitP.zy : hitP.xy);
float chk = mod(floor(tileUv.x * 3.0) + floor(tileUv.y * 3.0), 2.0);
vec3 wallBase = (hitN.x > 0.5) ? vec3(0.84, 0.38, 0.26)
: ((hitN.x < -0.5) ? vec3(0.22, 0.62, 0.78)
: mix(vec3(0.54, 0.56, 0.60), vec3(0.80, 0.80, 0.78), chk));
float depthHue = float(bounce) * 0.14;
wallBase = mix(wallBase, vec3(0.24, 0.78, 0.95), depthHue * 0.25);
hitCol = wallBase * (0.28 + 0.85 * ndl);
} else if (hitKind == 3) {
// Alternating cyan/amber energy rim per recursion depth
hitCol = (mod(float(bounce), 2.0) < 0.5) ? vec3(0.22, 1.45, 1.75) : vec3(1.75, 0.78, 0.20);
} else if (hitKind == 4) {
// Leaked unclipped near-plane occluder behind exit portal (crimson warning hazard stripes!)
float stripe = step(0.5, fract((hitP.x + hitP.y) * 9.0));
hitCol = mix(vec3(0.95, 0.18, 0.24), vec3(0.24, 0.05, 0.08), stripe);
} else if (hitKind == 5) {
float spec = pow(max(dot(reflect(-lDir, hitN), -rd), 0.0), 32.0);
hitCol = vec3(0.92, 0.74, 0.36) * (0.25 + 0.90 * ndl) + vec3(1.1) * spec;
} else {
hitCol = vec3(0.14, 0.26, 0.42);
}
col += hitCol * atten;
break;
}
float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
col = mix(col, vec3(0.95, 0.98, 1.0), sep);
fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}Attribution: MIT — written for this index.
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| recursive-oblique-portal-rendering-live | Tesla T4 1920x1080 | 3.60× baseline (light) · 0.119 ms / 0.119 ms | opengl-timer-query · 32 frames · GPU Weight Atlas · Colab T4 sweep |
| recursive-oblique-portal-rendering-live | NVIDIA L4 1920x1080 | 3.08× baseline (light) · 0.049 ms / 0.049 ms · 2.4× faster on L4 | opengl-timer-query · Cross-architecture validation |
| recursive-oblique-portal-rendering-live | NVIDIA A100-SXM4-40GB 1920x1080 | 2.35× baseline (light) · 0.068 ms / 0.068 ms · 1.8× faster on A100 | opengl-timer-query · Cross-architecture validation |
Crosses
- Reverse-Z and floating-point depth buffers: Preserves distant depth precision when oblique near-plane matrices tilt the frustum's far plane.
- Portal and PVS visibility culling: Narrows each recursive portal camera's frustum to the screen-space bounds of the aperture to skip unseen geometry.
Sources
- primaryEric Lengyel (2005). Oblique View Frustum Depth Projection and Clipping. Journal of Game Development, 2005.Verified via live page · 2026-11-30
- implementationacher249 (2026). acher249/codevember-2026. #codevemberGitHub API · license MIT · 2026-11-30