{
 "id": "recursive-oblique-portal-rendering",
 "title": "Recursive Non-Euclidean Portal Rendering with Oblique Near-Plane Clipping",
 "aliases": [
  "recursive portal rendering",
  "non-Euclidean portals",
  "oblique-clipped portals",
  "stencil portal rendering"
 ],
 "domain": "visibility",
 "subdomain": "portals & visibility",
 "stage": "visibility",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "unity",
  "unreal",
  "godot"
 ],
 "year_introduced": 2005,
 "inputs": [
  "mesh",
  "depth",
  "camera-poses"
 ],
 "outputs": [
  "visibility-mask",
  "radiance",
  "texture"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "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.",
 "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.\n\nA 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.\n\nTo 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_use": "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.\n\nPrefer [planar-reflections-oblique-clipping](planar-reflections-oblique-clipping) when reflecting a single flat mirror or water surface rather than teleporting the camera to a distinct exit frame. Prefer [portal-pvs-culling](portal-pvs-culling) when portals only act as Euclidean room-to-room visibility boundaries for frustum culling rather than spatial warps.",
 "cost": {
  "class": "heavy",
  "notes": "Each visible portal and recursion level adds a full scene pass; screen-space bounding-rect scissor tests, frustum narrowing, and recursion depth caps (typically 2–6) keep frame times bounded."
 },
 "failure_modes": [
  "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:http://www.terathon.com/lengyel/Lengyel-Oblique.pdf",
   "type": "web",
   "title": "Oblique View Frustum Depth Projection and Clipping",
   "authors": [
    "Eric Lengyel"
   ],
   "year": 2005,
   "venue": "Journal of Game Development, 2005",
   "url": "http://www.terathon.com/lengyel/Lengyel-Oblique.pdf",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261030074350/https://terathon.com/lengyel/Lengyel-Oblique.pdf",
     "timestamp": "20261030074350"
    },
    "page_title": null,
    "host": "terathon.com",
    "title_confirmed_by_page": false,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET http://www.terathon.com/lengyel/Lengyel-Oblique.pdf → 200",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": null,
   "__bib": "lengyel2005oblique"
  },
  {
   "role": "implementation",
   "key": "gh:acher249/codevember-2026",
   "type": "code",
   "title": "acher249/codevember-2026",
   "description": "#codevember ",
   "authors": [
    "acher249"
   ],
   "year": 2026,
   "venue": "GitHub",
   "url": "https://github.com/acher249/codevember-2026",
   "ids": {
    "repo": "acher249/codevember-2026"
   },
   "license": "MIT",
   "extra": {
    "stars": 133,
    "stars_checked": "2026-11-30",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-29T23:19:53Z",
    "language": "TypeScript",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/acher249/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/acher249/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/acher249/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/acher249/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/acher249/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/acher249/codevember-2026",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": null,
   "__bib": "acher2026acher249"
  }
 ],
 "presets": [
  {
   "id": "recursive-oblique-portal-rendering-live",
   "title": "Recursive Oblique Portal Rendering — unclipped near-plane leak (left) vs oblique-clipped recursive non-Euclidean portal tunnel (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_RECURSION": 5,
    "P_TWIST": 0.35,
    "P_ORBIT": 0.45,
    "P_SPLIT": 0.46
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_RECURSION = 5.00; // P Portal Bounces [1.0, 7.0, 1.0]\nconst float P_TWIST = 0.35; // P Exit Frame Twist [0.0, 0.9, 0.05]\nconst float P_ORBIT = 0.45; // P Camera Motion [0.0, 1.5, 0.05]\nconst float P_SPLIT = 0.46; // P No Oblique / Clipped [0.0, 1.0, 0.02]\n\nvec2 rot2(vec2 p, float a) {\n  float c = cos(a);\n  float s = sin(a);\n  return vec2(c * p.x - s * p.y, s * p.x + c * p.y);\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  bool useOblique = screenX >= split;\n  int maxBounces = int(clamp(floor(P_RECURSION + 0.5), 1.0, 7.0));\n  float twist = clamp(P_TWIST, 0.0, 1.0);\n  float t = iTime * max(P_ORBIT, 0.0);\n\n  // Camera inside sunlit non-Euclidean gallery looking directly into Portal A (z = -0.85)\n  vec3 ro = vec3(0.28 * sin(t * 0.65), 0.06 + 0.08 * cos(t * 0.8), 1.35);\n  vec3 rd = normalize(vec3(uv.x - 0.14 * sin(t * 0.65), uv.y - 0.02, -1.35));\n\n  vec3 col = vec3(0.0);\n  float atten = 1.0;\n\n  // Trace ray with up to maxBounces non-Euclidean portal teleports!\n  for (int bounce = 0; bounce < 8; bounce++) {\n    if (bounce > maxBounces) break;\n\n    float tBest = 1e5;\n    int hitKind = 0; // 1=room, 2=portal aperture, 3=portal rim, 4=occluding pillar behind exit portal!\n    vec3 hitP = vec3(0.0);\n    vec3 hitN = vec3(0.0, 0.0, 1.0);\n\n    // Gallery chamber planes: x in [-1.2, 1.2], y in [-0.75, 0.75], z in [-0.85, 1.65]\n    vec3 bMin = vec3(-1.20, -0.75, -0.85);\n    vec3 bMax = vec3(1.20, 0.75, 1.65);\n    vec3 invR = 1.0 / (rd + vec3(1e-6));\n    vec3 tA = (bMin - ro) * invR;\n    vec3 tB = (bMax - ro) * invR;\n    vec3 tFarV = max(tA, tB);\n    float tRoom = min(min(tFarV.x, tFarV.y), tFarV.z);\n    if (tRoom > 0.005) {\n      tBest = tRoom;\n      hitP = ro + rd * tRoom;\n      hitKind = 1;\n      if (abs(tRoom - tFarV.x) < 1e-4) hitN = vec3(-sign(rd.x), 0.0, 0.0);\n      else if (abs(tRoom - tFarV.y) < 1e-4) hitN = vec3(0.0, -sign(rd.y), 0.0);\n      else hitN = vec3(0.0, 0.0, -sign(rd.z));\n\n      // Check if hit on back wall (z = -0.85) falls inside Portal A frame\n      if (abs(hitP.z + 0.85) < 0.01) {\n        vec2 q = abs(hitP.xy) - vec2(0.46, 0.44);\n        float dFrame = max(q.x, q.y);\n        if (dFrame < 0.0) {\n          hitKind = 2; // Inside Portal A aperture!\n        } else if (dFrame < 0.055) {\n          hitKind = 3; // Glowing portal frame rim\n        }\n      }\n    }\n\n    // Hero floating brass sphere inside gallery\n    vec3 sc = vec3(0.62, -0.22, 0.10);\n    vec3 oc = ro - sc;\n    float bs = dot(oc, rd), ds = bs * bs - (dot(oc, oc) - 0.22 * 0.22);\n    if (ds > 0.0) {\n      float ts = -bs - sqrt(ds);\n      if (ts > 0.01 && ts < tBest) {\n        tBest = ts; hitKind = 5; hitP = ro + rd * ts; hitN = normalize(hitP - sc);\n      }\n    }\n\n    // On LEFT side (bounce >= 1), simulate missing oblique near-plane clipping:\n    // An intrusive red blocker pillar sitting BEHIND the exit portal leaks into the virtual camera!\n    if (!useOblique && bounce >= 1) {\n      vec3 pc = vec3(-0.12, 0.0, 1.05);\n      vec3 pr = vec3(0.16, 0.72, 0.14);\n      vec3 p0 = (pc - pr - ro) * invR;\n      vec3 p1 = (pc + pr - ro) * invR;\n      vec3 pn = min(p0, p1), pf = max(p0, p1);\n      float tn = max(max(pn.x, pn.y), pn.z);\n      float tf = min(min(pf.x, pf.y), pf.z);\n      if (tf > max(tn, 0.01) && tn < tBest) {\n        tBest = tn; hitKind = 4; hitP = ro + rd * tn;\n        hitN = vec3(0.0, 0.0, 1.0);\n      }\n    }\n\n    if (hitKind == 2 && bounce < maxBounces) {\n      // Teleport ray from Portal A (z = -0.85) to Portal B (z = 1.48) with slight twist & scale!\n      // With oblique near-plane clipping enabled, ray starts strictly AT the exit plane (z = 1.46).\n      // Without oblique clipping (left), virtual camera starts behind exit plane (z = 1.62) and clips wrong geometry!\n      vec2 exitXY = rot2(hitP.xy, twist * 0.38) * 0.96;\n      ro = vec3(exitXY.x, exitXY.y, useOblique ? 1.44 : 1.62);\n      rd.xy = rot2(rd.xy, twist * 0.38);\n      atten *= 0.88;\n      continue;\n    }\n\n    // Shade terminal hit\n    vec3 lDir = normalize(vec3(0.35, 0.82, 0.45));\n    float ndl = max(dot(hitN, lDir), 0.0);\n    vec3 hitCol = vec3(0.05);\n\n    if (hitKind == 1) {\n      vec2 tileUv = (abs(hitN.y) > 0.5) ? hitP.xz : ((abs(hitN.x) > 0.5) ? hitP.zy : hitP.xy);\n      float chk = mod(floor(tileUv.x * 3.0) + floor(tileUv.y * 3.0), 2.0);\n      vec3 wallBase = (hitN.x > 0.5) ? vec3(0.84, 0.38, 0.26)\n                    : ((hitN.x < -0.5) ? vec3(0.22, 0.62, 0.78)\n                    : mix(vec3(0.54, 0.56, 0.60), vec3(0.80, 0.80, 0.78), chk));\n      float depthHue = float(bounce) * 0.14;\n      wallBase = mix(wallBase, vec3(0.24, 0.78, 0.95), depthHue * 0.25);\n      hitCol = wallBase * (0.28 + 0.85 * ndl);\n    } else if (hitKind == 3) {\n      // Alternating cyan/amber energy rim per recursion depth\n      hitCol = (mod(float(bounce), 2.0) < 0.5) ? vec3(0.22, 1.45, 1.75) : vec3(1.75, 0.78, 0.20);\n    } else if (hitKind == 4) {\n      // Leaked unclipped near-plane occluder behind exit portal (crimson warning hazard stripes!)\n      float stripe = step(0.5, fract((hitP.x + hitP.y) * 9.0));\n      hitCol = mix(vec3(0.95, 0.18, 0.24), vec3(0.24, 0.05, 0.08), stripe);\n    } else if (hitKind == 5) {\n      float spec = pow(max(dot(reflect(-lDir, hitN), -rd), 0.0), 32.0);\n      hitCol = vec3(0.92, 0.74, 0.36) * (0.25 + 0.90 * ndl) + vec3(1.1) * spec;\n    } else {\n      hitCol = vec3(0.14, 0.26, 0.42);\n    }\n\n    col += hitCol * atten;\n    break;\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.98, 1.0), sep);\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "recursive-oblique-portal-rendering",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T21:07:23.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.7287,
      "std": 0.1239,
      "min": 0.3053,
      "max": 0.9456
     },
     "image": "/specimens/recursive-oblique-portal-rendering-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "planar-reflections-oblique-clipping",
  "portal-pvs-culling",
  "reverse-z-depth-buffer"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "reverse-z-depth-buffer",
   "why": "Preserves distant depth precision when oblique near-plane matrices tilt the frustum's far plane."
  },
  {
   "id": "portal-pvs-culling",
   "why": "Narrows each recursive portal camera's frustum to the screen-space bounds of the aperture to skip unseen geometry."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-30T17:59:51.239Z",
  "draft": "data/drafts/visibility/recursive-oblique-portal-rendering.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Lengyel, 2005",
 "url": "https://texelatlas.vercel.app/t/recursive-oblique-portal-rendering/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 3.6,
  "tier": "light",
  "p50_ms": 0.1186,
  "p95_ms": 0.1191,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "recursive-oblique-portal-rendering-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 3.08,
   "tier": "light",
   "p50_ms": 0.0489,
   "p95_ms": 0.0492,
   "speedup_x": 2.43,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 2.35,
   "tier": "light",
   "p50_ms": 0.0676,
   "p95_ms": 0.0679,
   "speedup_x": 1.75,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}