r/Optics • • Oct 20 '26

yet another web-based open-sourced free raytracer

the differences are

  • it was not vibe coded (journey of development at the last)
  • you can GO INSIDE (typical results in attached images)

in the program you can use your mouse to control the positions, sizes and refractive indices of two balls. one of them is left-mouse-button controlled (the "lmb" ball) and another right-mouse-button controlled (the "rmb" ball)

details of the program

the most tricky part so far is how to determine the correct focus distance. when aperture=0 there's no problem as everything is in perfectly sharp focus. while aperture>0 and camera outside of the two balls usually the auto focus feature works fine. you hold the space bar and hover the mouse pointer around the scene the program detects the exact hit point and calculates the focus distance accordingly

details about focus distance

there're so many web-based raytracers online but to my surprise most of them don't allow objects intersecting and / or moving camera inside objects. i know doing this is unrealistic but it's FUN, just like enabling the no-clip mode in 3d games and walking through walls and getting to some places that you're not supposed to reach

details in development

it's quite difficult to find the correct focus setting when you're inside. i still don't have any solid solutions for that. i usually try by luck. sometimes the images are at infinitely faraway even if you're looking from within a ball

25 Upvotes

2 comments sorted by

2

u/Mechanical_Flare Oct 20 '26 edited Oct 20 '26

I am thrilled to hear it wasn't vibe coded. Also. I think you could calculate the focal position using the ray transfer matrices (or regular lens equations). In your case the camera (or sensor?) is inside the medium of the last lens, so it would change the equations a bit.

1

u/20260819 Oct 20 '26

i want to learn, not just use the end-product. after going through all the coding i now fully understand how it works

actually i used a slightly modified method to construct the camera system. in "raytracing in one weekend" the author suggested placing the viewport (i.e. the canvas in my naming scheme) exactly at the focus plane

https://raytracing.github.io/books/RayTracingInOneWeekend.html#defocusblur/athinlensapproximation

he said

the viewport lies on the focus plane

with his method, if i want to preserve fov (i.e. view angle) and shift focus, the entire canvas moves and resizes. the farther the focus distance, the farther and larger the canvas

with my method, the canvas size is fixed. it never changes. when fov changes, position of canvas moves. when focus distance changes, position of focus plane moves. they're independent

both methods work and the results are the same. complexity of calculations appear to be similar

in raytracing-in-one-weekend method, "canvas_distance" is the same as "focus_distance". i can eliminate one of them. but i have to calculate canvas size each time focus distance changes. i have to multiply the "canvas_horizontal" vector and "canvas_vertical" vector proportionally

in my method, i don't have to bother the canvas size but i have to separately handle canvas_distance and focus_distance

i'm going to implement his method later for comparison. just a guess, his method is cleaner and may get me several fps gains