Hypertrace
Theory & implementation

Tracing light through curved space.

Hypertrace keeps the local rules of reflection and scattering, while changing the geometry that carries light between surfaces.

This guide develops the ideas in Alexey Gerasev’s original 2020 article and connects them to the current WGPU implementation. Watch the original walkthrough for a moving view of the project.

1. The space and its boundary

The hyperbolic scene lives in three-dimensional space of constant sectional curvature −1. The renderer uses the upper half-space model: a point has coordinates p = (x, y, z) with z > 0. These coordinates describe positions; the metric determines physical distances:

ds2 = (dx2 + dy2 + dz2) / z2

A small coordinate displacement has hyperbolic length approximately its Euclidean length divided by z. Equal coordinate steps therefore cover more physical distance close to z = 0. The reference point is o = (0, 0, 1).

The plane z = 0, together with the point at infinity, is the ideal boundary, also called the absolute. It is not a wall that a ray can hit after a finite travel distance. Along a vertical line, the distance is |log(z₂ / z₁)|, which diverges as either endpoint approaches the boundary while the other stays fixed.

d(p, q) = 2 asinh( ‖p − q‖ / (2 √(pz qz)) )

This distance formula is equivalent to the usual inverse-hyperbolic-cosine expression. It is better suited to nearby points because it avoids subtracting nearly equal quantities around cosh(d) = 1.

2. What a straight ray means

Free light follows geodesics, the locally straight paths of the metric. In half-space coordinates they appear as vertical lines or circular arcs perpendicular to the boundary plane. Their coordinate curvature is not caused by a material bending the ray.

Geodesics in a two-dimensional slice of hyperbolic half-spaceA semicircular arc and a vertical line both meet the ideal boundary at right angles. Both are intrinsically straight geodesics.Geodesic arcVerticalgeodesicIdeal boundary · z = 0z > 0
A two-dimensional slice through H³. The interactive viewer renders what an observer inside the space sees; it is not an external drawing of this coordinate model.

For example, two unit-speed paths starting at the reference point are:

Vertical:    γ(s) = (0, 0, eˢ)
Horizontal:  γ(s) = (tanh s, 0, 1 / cosh s)

The second begins in a horizontal direction but immediately curves in the coordinate picture. The renderer finds intersections with these geodesics and compares hits using hyperbolic distance. After a hit, the next ray uses the geodesic’s tangent at the hit point.

3. Complex matrices, quaternion action

Orientation-preserving isometries of H³ form the group PSL(2, ℂ). Represent one by a complex 2 × 2 matrix with determinant one; M and −M describe the same isometry. This representation has six real degrees of freedom: three for position and three for orientation.

    ⎡ a  b ⎤
M = ⎣ c  d ⎦,    a, b, c, d ∈ ℂ,    ad − bc = 1

For an interior point, introduce the quaternion q = x + iy + jz. The fourth component is zero. The matrix acts by:

fM(q) = (aq + b)(cq + d)−1

The stored matrix remains complex. Quaternions are temporary values used to evaluate its action inside three-dimensional space. This expression is a fractional transformation, not ordinary multiplication of a matrix by a position vector. Quaternion multiplication is noncommutative, so the inverse stays on the right.

Writing u = x + iy gives an equivalent complex-and-real formula that makes preservation of z > 0 explicit:

D = |cu + d|² + |c|²z²
u′ = ((au + b) conjugate(cu + d) + a conjugate(c) z²) / D
z′ = z / D

Composition becomes ordinary complex matrix multiplication: fMN = fM ∘ fN. The determinant-one inverse is [[d, −b], [−c, a]]. These operations let camera and object transforms share the same representation.

Useful motions

Sz(L) = diag(eL/2, e−L/2)
Rz(φ) = diag(eiφ/2, e−iφ/2)

        ⎡ cosh(L/2)  sinh(L/2) ⎤
Sx(L) = ⎣ sinh(L/2)  cosh(L/2) ⎦

Sz scales all three coordinates by eᴸ; it translates by hyperbolic distance L along the vertical geodesic through the reference point. Sx moves that point along the horizontal-starting geodesic above. In contrast, [[1, w], [0, 1]] shifts u by w at unchanged height. It is also an isometry, but its continuous orbit is a horocycle rather than a geodesic. Coordinate displacement and geodesic translation are different notions here.

4. Transporting directions

A direction belongs to the tangent space at a point. Transforming only the position leaves the ray inconsistent. Directions transform by the real differential of the same map.

Set P = aq + b and Q = cq + d. For a tangent quaternion v, use the product rule and D(Q−1)[v] = −Q−1(cv)Q−1:

Dfq[v] = avQ⁻¹ − PQ⁻¹cvQ⁻¹
         = (a − f(q)c)vQ⁻¹

This form gives the same directional derivative as the expanded conjugate-and-dot-product expression in the original article and the current shader. The order of the factors is essential; no quaternionic analogue of a scalar quotient rule is being assumed.

The half-space metric is conformal: it multiplies all directions at a point by the same scale. An isometry’s differential rotates directions and rescales coordinate lengths by z′ / z. Hypertrace normalizes the transported Euclidean direction because rays store orientation; it computes travel distance separately with the hyperbolic metric.

5. Planes, horospheres, and tilings

The hyperbolic example contains two kinds of surfaces. Their spherical appearance in coordinates does not make them ordinary Euclidean spheres.

SurfaceHalf-space descriptionIntrinsic geometry
Geodesic planeA hemisphere centered on the boundary, or a vertical planeHyperbolic plane, curvature −1
HorosphereA sphere tangent to the boundary, or a horizontal planeEuclidean plane, curvature 0

The canonical primitives are the upper hemisphere x² + y² + z² = 1 and the horosphere z = 1. Object isometries place copies throughout the scene. To intersect a mapped object, transform the ray into its local coordinates, solve the canonical intersection, then transport the hit and tangent back.

Square and hexagonal patterns therefore fit naturally on the horospheres. The geodesic planes carry pentagonal and star patterns. The current pentagonal reduction uses a finite iteration depth, so the implementation does not promise an arbitrarily deep tessellation near the ideal boundary.

6. Materials and path tracing

Curvature changes ray propagation between interactions. At a surface, reflection and scattering are evaluated in its local tangent space. Conformality lets the shader use Euclidean dot products between normalized local directions for angles.

Each sample jitters a primary camera ray, finds the nearest hit, collects emission, chooses a material interaction, and continues with the new direction. A ray that misses samples the environment. Repeated samples estimate the outgoing radiance; the GPU keeps a running sum and sample count.

Lo(p, ωo) = Le(p, ωo) + ∫hemisphere fr(p, ωi, ωo) Li(p, ωi) |n · ωi| dωi

This is the local reflection equation; transmissive materials also admit incoming directions on the other side of the surface. Hypertrace implements mixtures of diffuse, specular, transparent, and refractive interactions. The hyperbolic example uses straight-through transparency; the Euclidean sphere uses refraction.

The example scenes terminate paths after a fixed number of interactions: three in the hyperbolic scene and four in the Euclidean scene. More samples reduce Monte Carlo noise, roughly as 1 / √N, but do not remove the bias from that finite bounce limit. The gallery uses 4096 samples per pixel at the same camera and bounce settings as the interactive examples.

Samples are averaged in linear RGB. The preview then applies the renderer’s display transform, a gamma of 1/2.2, and clamps to the display range. Display conversion belongs after accumulation, not inside it.

7. Other coordinate models

The Poincaré disk describes H². For this three-dimensional renderer, the corresponding bounded model is the Poincaré ball. It describes the same space, with different coordinates and the unit sphere as its ideal boundary.

One half-space-to-ball map taking (0, 0, 1) to the origin is:

A = x² + y² + (z + 1)²
C(x, y, z) = (2x, 2y, x² + y² + z² − 1) / A

ds² = 4 ‖db‖² / (1 − ‖b‖²)²

If C changes coordinates, an isometry in the ball is C ∘ fM ∘ C−1. The abstract isometry is still represented by the same complex matrix in PSL(2, ℂ). A quaternionic formula for the coordinate change does not require storing general quaternion-valued matrix coefficients.

The hyperboloid model is another option: points satisfy −X₀² + X₁² + X₂² + X₃² = −1, X₀ > 0, and Lorentz matrices act linearly. It offers useful algebra, but faraway coordinates grow exponentially. The Klein ball makes geodesics straight chords while losing conformality, so angle and material calculations need its metric.

8. Precision and current limits

No coordinate model gives uniform floating-point precision at the absolute. The boundary is infinitely far away: half-space heights approach zero, ball radii approach one, and hyperboloid coordinates grow without bound. Bounded coordinates alone do not solve cancellation or loss of relative precision.

The current implementation:

A useful next step is camera-relative coordinates: re-express nearby objects and rays in a frame centered near the observer, keeping numerical values moderate. Periodic recentering, carefully maintained isometries, and higher-precision calculations where they matter can improve robustness together. Those are future improvements, not guarantees of the current renderer.

Read the implementation