raytracing
stableA software raytracer that builds scenes with spheres, planes, boxes, and triangles, configures cameras and point lights, and renders to an RGBA byte buffer with Blinn-Phong shading and reflections.
use plugin raytracing::{RayScene, add_sphere, add_plane, …} Functions (18)
- RayScene Creates a new scene with default settings: camera at (0, 1, -5) looking at origin, 60° FOV, Earth-ish light at (-2, 5, -3), blue sky…
- add_sphere Add a sphere to the scene
- add_plane Add an infinite plane to the scene
- add_box Add an axis-aligned box to the scene
- add_triangle Add a triangle to the scene
- set_camera Set camera position and look-at target
- set_fov Set camera field of view in degrees
- set_light Set the legacy directional light position
- add_point_light Add a point light source
- set_ambient Set ambient light color
- set_background Set background gradient colors
- set_material Set PBR-like material for an object
- set_max_bounces Set maximum reflection bounce count
- render Render scene to RGBA bytes
- render_pixel Render a single pixel
- cast_ray Test a ray against the scene
- object_count Number of objects in the scene
- clear Remove all objects and lights
Creates a new scene with default settings: camera at (0, 1, -5) looking at origin, 60° FOV, Earth-ish light at (-2, 5, -3), blue sky…
Creates a new scene with default settings: camera at (0, 1, -5) looking at origin, 60° FOV, Earth-ish light at (-2, 5, -3), blue sky gradient background, and 3 reflection bounces.
use plugin raytracing::{RayScene}
let scene = RayScene()
Add a sphere to the scene
Adds a sphere at center (cx, cy, cz) with radius r and color (cr, cg, cb) as 0–1 floats. Returns the object index used by set_material.
let idx = scene.add_sphere(0.0, 0.5, 0.0, 0.5, 1.0, 0.2, 0.2)
scene.set_material(idx, 0.9, 0.1) // metallic, low roughness
Add an infinite plane to the scene
Adds an infinite plane defined by normal (nx, ny, nz) and distance d from origin. Returns the object index.
let floor = scene.add_plane(0.0, 1.0, 0.0, 0.0, 0.8, 0.8, 0.8)
Add an axis-aligned box to the scene
Adds an axis-aligned bounding box. Returns the object index.
let box_idx = scene.add_box(-0.5, 0.0, -0.5, 0.5, 1.0, 0.5, 0.2, 0.6, 1.0)
Add a triangle to the scene
Adds a triangle specified by three vertices. Uses Moller-Trumbore intersection. Returns the object index.
let tri = scene.add_triangle(
-1.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 2.0, 0.0,
0.8, 0.5, 0.1
)
Set camera position and look-at target
Positions the camera at (px, py, pz) pointing toward look-at target (lx, ly, lz).
scene.set_camera(0.0, 3.0, -8.0, 0.0, 0.0, 0.0)
Set camera field of view in degrees
Sets the camera's vertical field of view in degrees (must be between 0 and 180).
scene.set_fov(90.0)
Set the legacy directional light position
Sets the position of the legacy single directional light. Ignored when point lights are defined.
scene.set_light(-3.0, 8.0, -5.0)
Add a point light source
Adds a point light at (px, py, pz) with a color (r, g, b as 0–1) and intensity. Multiple point lights accumulate. When any point lights exist, the legacy light is not used.
scene.add_point_light(2.0, 5.0, -3.0, 1.0, 1.0, 1.0, 1.5)
scene.add_point_light(-2.0, 3.0, 2.0, 0.4, 0.6, 1.0, 0.8)
Set ambient light color
Sets the ambient light color applied to all surfaces. Values are 0–1 floats.
scene.set_ambient(0.05, 0.05, 0.1)
Set background gradient colors
Sets the sky gradient: rays that miss all geometry interpolate between bottom and top colors based on vertical direction.
scene.set_background(0.3, 0.5, 0.9, 1.0, 1.0, 1.0)
Set PBR-like material for an object
Sets PBR-like material properties for a specific object. metallic (0–1) controls reflectivity; roughness (0–1) affects specular highlights.
let sphere = scene.add_sphere(0.0, 1.0, 0.0, 1.0, 0.9, 0.9, 0.9)
scene.set_material(sphere, 1.0, 0.05) // mirror sphere
Set maximum reflection bounce count
Sets the maximum number of reflection ray bounces. Higher values produce more accurate reflections but are slower to render.
scene.set_max_bounces(5)
Render scene to RGBA bytes
Renders the scene to a width×height RGBA byte buffer (4 bytes per pixel, top-to-bottom, left-to-right row order).
use plugin raytracing::{RayScene}
let scene = RayScene()
scene.add_sphere(0.0, 0.5, 0.0, 0.5, 1.0, 0.3, 0.3)
scene.add_plane(0.0, 1.0, 0.0, 0.0, 0.7, 0.7, 0.7)
let pixels = scene.render(320, 240)
print("rendered {pixels.len()} bytes")
Render a single pixel
Traces a single ray for pixel (x, y) in a width×height virtual frame. Returns #{r, g, b, a} as 0–255 integers. Useful for progressive rendering or debugging.
let px = scene.render_pixel(160, 120, 320, 240)
print("center pixel: r={px["r"]} g={px["g"]} b={px["b"]}")
Test a ray against the scene
Tests a ray from origin (ox, oy, oz) in direction (dx, dy, dz) against the scene. Returns #{t, px, py, pz, nx, ny, nz, object_idx} on hit, or nil if no intersection.
let hit = scene.cast_ray(0.0, 5.0, 0.0, 0.0, -1.0, 0.0)
if hit != nil {
print("hit object {hit["object_idx"]} at distance {hit["t"]}")
}
Number of objects in the scene
Returns the number of objects currently in the scene.
print("{scene.object_count()} objects in scene")
Remove all objects and lights
Removes all objects, materials, and point lights from the scene. Camera, background, and ambient settings are preserved.
scene.clear()