A professional, optimized ray tracing engine written in Python. This project demonstrates advanced vectorized computation, geometric acceleration, and high configurability:
- Optimized code using numpy for maximum speed and parallelism
- Supports interactive editing of 3D scenes described in plain text
- Efficiently renders scenes with 100+ objects in seconds
- Robust recursive tracing (up to 10 layers deep for reflection and transparency)
![]() City at Sunset |
![]() Colorful Pool Balls |
- Geometry: Spheres, cubes, infinite planes, and more
- Materials: Diffuse & specular colors, reflectivity, transparency, Phong highlights
- Lighting: Multiple colored lights, soft shadows, sun/sky simulation
- Reflections & Transparency: Recursive ray-tracing with realistic mirrors and glass
- Numpy Vectorization: Blazing-fast computation using fully parallelized math
- Scene Files: Human-readable text scenes for easy editing (see
scenes/) - BSP Acceleration: Smart space partitioning for ultra-fast intersection tests
- Camera shoots rays through every pixel into the scene
- For each ray:
- Find what surface it hits first (spheres 🟡, cubes 🟪, planes 🟫)
- Calculate intersection point & normal
- Evaluate material: base color, shininess, reflectivity, transparency
- Compute lighting:
- Diffuse (matte color)
- Specular (shiny highlight)
- Shadows and soft shadow blending
- Reflections (recursively bounce rays!)
- Transparency (trace light through surface)
- Numpy powers all math: intersections, color blending, recursion — almost every function runs on whole image arrays at once 🚀
- BSP (Binary Space Partitioning): Accelerates collision by ignoring objects not in the ray's path.
- 🟡 Spheres: Defined by center & radius
- 🟪 Cubes: Axis-aligned, center & size (no rotation)
- 🟫 Infinite Planes: By normal and offset
- 🌌 Background: Customizable scene fill color
- 🎨 Diffuse Color: Surface base color
- ✨ Specular Color: Shininess (mirror highlight)
- 🎯 Phong Power: Control highlight sharpness
- 🪞 Reflection: Mirror-like reflection color
- 🪟 Transparency: 0=opaque, 1=fully transparent
ray_tracer.py: Entry point, high-level pipelineray_functions.py: Ray generation, reflections, intersections; all numpy-acceleratedBSPNode.py: BSP tree for geometric accelerationMaterial.py,Light.py: Material & lighting modelsCamera.py,SceneSettings.py: Scene configurationsurfaces/: Sphere, Cube, Plane, and abstract base classesParser.py: Loads text scene files and objectsutil.py: Math helpers, color/image utilitiesscenes/: Example customizable scene filesoutput/: Rendered PNG results
python ray_tracer.py scenes/original.txt output/original.png --width 512 --height 512- Tweak any
.txtfile inscenes/and rerun for new images!
- Compose your 3D world with plain text: add spheres, cubes, lights and set all colors/materials!
- Example (
scenes/original.txt) — add new lines to customize:sph x y z radius material(add spheres)box x y z size material(cubes/boxes)pln nx ny nz offset material(planes)lgt x y z r g b spec shadow width(lights)mtl r g b ...(define new materials)
- All core computations are vectorized:
- Rays are processed in giant numpy arrays (no loops)
- Intersection, color blending, and shadowing all run in parallel for max speed
- BSP trees: Scene subdivision skips irrelevant geometry for each ray
- Recursion: Reflection/transparency depth is configurable for performance

