raytrace calculator

Interactive Raytrace Calculator (Ray → Sphere)

Use this tool to compute ray-sphere intersection, hit position, surface normal, reflected ray, and refracted ray (Snell's law). Great for computer graphics, optics basics, and debugging shader math.

Enter values and click Calculate Raytrace to see the intersection and ray outputs.

What this raytrace calculator does

This calculator simulates a single ray interacting with a sphere, which is one of the most fundamental operations in ray tracing. Even advanced path tracers rely on this exact family of computations millions of times per frame.

  • Finds whether a ray intersects a sphere.
  • Returns the nearest positive intersection distance (t).
  • Computes the hit point and unit surface normal.
  • Computes the reflected direction vector.
  • Computes refracted direction (if possible) based on refractive indices.
  • Estimates Fresnel reflectance using Schlick’s approximation.

Core equations used

1) Ray equation

A ray is represented as P(t) = O + tD, where O is origin, D is direction, and t ≥ 0 is distance along the ray. This calculator normalizes D internally for stable, interpretable output.

2) Sphere intersection

For sphere center C and radius r, solve:

|O + tD - C|² = r²

This forms a quadratic equation at² + bt + c = 0. The discriminant Δ = b² - 4ac determines hit status:

  • Δ < 0: no intersection
  • Δ = 0: tangent hit
  • Δ > 0: two intersections; nearest positive root is used

3) Reflection and refraction

At hit point, normal N is calculated and used for ray response:

  • Reflection: R = D - 2(D·N)N
  • Refraction: Snell’s law using n₁ and n₂

If the incidence angle is too steep while moving from denser to thinner medium, refraction may fail and total internal reflection occurs.

How to use the calculator effectively

Practical input workflow

  • Set ray origin to camera position (or arbitrary test point).
  • Set direction toward your object.
  • Enter sphere center and radius.
  • Use n₁ = 1.0 for air and n₂ ≈ 1.5 for glass as a starting point.

Interpreting outputs

The hit distance tells you how far along the normalized ray the collision occurs. The hit point and normal are typically the next inputs to shading equations. Reflection and refraction directions can be fed into secondary rays for recursive or iterative tracing.

Example scenario

Default values cast a ray from (0, 0, -5) toward positive Z onto a unit sphere at the origin. You should see a hit near (0, 0, -1), a normal near (0, 0, -1), and a reflected direction reversing on Z.

Limitations and next steps

  • This page traces one ray against one sphere only.
  • It does not include shadows, multiple bounces, or acceleration structures (BVH/KD-tree).
  • It is ideal as a learning and validation tool for shader math and geometry debugging.

To extend this into a complete renderer, add scene traversal, material BRDFs, light sampling, and Monte Carlo integration for global illumination.

🔗 Related Calculators