Ray Tracing Simulator

Simulation #78 NEW Computer Graphics A-Level Physics / CS Progressive Path Tracer
Scene Preset
Samples per Pixel (spp)
Controls
Keyboard Shortcuts
P Pause / Resume   R Reset   S Save PNG
What you're seeing:
A Monte Carlo path tracer runs entirely in your browser. Each frame fires random rays through the scene and averages the results — quality improves as spp grows. Noise decreases as approximately 1/vspp.
Materials:
Diffuse — Lambertian cosine scatter
Mirror — perfect specular reflection
Glass — Fresnel + Snell's Law refraction
Emissive — area light source (soft shadows)

What is Ray Tracing?

Ray tracing is a rendering algorithm that simulates the physical behaviour of light by following rays from the camera through each pixel and computing how they interact with scene geometry. Unlike rasterisation — which projects triangles onto a screen — ray tracing produces global illumination effects such as reflections, refractions, shadows and colour bleeding naturally from the underlying physics.

A path tracer extends this further: instead of evaluating all possible light paths, it importance-samples random paths through the scene using Monte Carlo integration. The rendering equation

Lo(x,?o) = Le(x,?o) + ?O fr(x,?i,?o) Li(x,?i) (n^·?i) d?i

is the Kajiya rendering equation (1986). The left side is outgoing radiance; the integral sums all incoming light weighted by the BRDF fr and cosine foreshortening. Path tracing estimates this integral stochastically — each ray bounce samples one random incoming direction.

Ray–Sphere Intersection

A ray is defined as P(t) = O + tD. Substituting into the sphere equation |P - C|² = r² gives a quadratic in t:

|O + tD - C|² = r²
t² (D·D) + 2t (D·(O-C)) + (O-C)·(O-C) - r² = 0
discriminant ? = b² - c  ?  t = -b ± v?

If ? < 0 the ray misses the sphere. The smallest positive root gives the closest hit.

Reflection

R = D - 2(D·N^)N^

The reflected direction is simply the incident direction with the normal component negated. Mirror surfaces bounce the ray without any colour or angle randomness.

Refraction (Snell's Law)

n1 sin ?1 = n2 sin ?2   ?   T = (n1/n2) I + ((n1/n2) cos ?1 - cos ?2) N^

Glass objects use Snell's law for refraction and the Schlick approximation for Fresnel blending between reflection and transmission:

F(?) ˜ r0 + (1 - r0)(1 - cos ?)5    where r0 = ((n1-n2)/(n1+n2))²

At grazing incidence (? ? 90°) almost all light reflects; at normal incidence (? = 0°) most transmits.

Monte Carlo Path Tracing

The rendering equation cannot be solved analytically for complex scenes. Monte Carlo integration approximates it by drawing N random samples from the hemisphere above each hit point:

L ˜ (1/N) Si=1..N f(?i) L(?i) (n^·?i) / p(?i)

where p(?i) is the sampling PDF. Using cosine-weighted hemisphere sampling (p = n^·? / p) cancels the cosine factor, leaving:

L ˜ p/N S f(?i) L(?i)

Error decreases as 1/vN — doubling quality requires four times the samples. Progressive refinement accumulates samples over multiple frames, so the image automatically improves while you watch.

Russian Roulette

Recursion is terminated stochastically: at each bounce a ray is killed with probability (1 - albedo) and surviving rays are scaled up by 1/albedo to keep the estimator unbiased. This prevents infinite loops while correctly handling diffuse global illumination.

Tone Mapping

Real-world luminance ranges far exceed a monitor's [0, 1] range. Reinhard tone mapping compresses HDR radiance values:

Ldisplay = L / (1 + L)    then gamma ˜ L1/2.2

Gamma correction converts linear light values to the perceptual sRGB colour space of your display.

Scene Presets — Phenomena Demonstrated

Preset Key Phenomena Materials Educational Highlight
Cornell Box Colour bleeding, area shadows, global illumination Diffuse, mirror, glass Classic GI benchmark scene (Cornell 1984)
Mirrors Infinite reflections, light tunnelling Mirror walls, diffuse spheres Eigenvalue recursion depth limit
Glass Spheres Fresnel reflection/refraction, caustics Glass (IOR 1.33–1.6), diffuse floor Index of refraction table: water/glass/diamond
Soft Shadows Penumbra, umbra, two-light interference Area lights, diffuse/mirror spheres Shadow hardness vs light source size
Night City Neon colour bleeding, glossy reflections Emissive neon, mirror, glass, diffuse Multiple light sources, additive colour
Colourful Interreflections between coloured spheres Diffuse, mirror, glass mix Colour bleeding — light carries surface colour

Material Models

Material BRDF Type Parameters Real-World Examples
Diffuse (Lambertian) fr = ?/p (constant) Albedo colour Chalk, plaster, matte paint
Mirror (Specular) Delta BRDF (Dirac peak) Reflectance tint Polished silver, perfect metal
Glass (Dielectric) Fresnel blend — reflect + transmit IOR n (1.33–1.9), tint Water (1.33), glass (1.5), diamond (2.42)
Emissive (Area Light) Le = constant emission Emission colour + intensity Light panels, neon tubes, LEDs

Curriculum Connections

TopicQualificationConcepts Covered
Optics — reflection & refraction GCSE / A-Level Physics Snell's law, total internal reflection, critical angle
Wave & ray optics A-Level Physics / IB Huygen's principle, Fermat's principle of least time
Computer graphics pipeline A-Level / BTEC CS Rasterisation vs ray tracing, z-buffer, shading models
Numerical methods A-Level Maths / Further Maths Monte Carlo integration, random sampling, convergence 1/vN
Linear algebra Further Maths / University Vector dot/cross products, ONB construction, matrix transforms
Probability & statistics A-Level Maths Variance, standard error, importance sampling, PDF/CDF

Related Simulations