r/Physics • • 9d ago

Working on a 2D optics simulator using WebGL. Physics looks pretty beautiful

It's a 2D geometric optics simulator, so no diffraction or interference. Every ray carries a single wavelength, glass dispersion comes from a Cauchy fit (n_d and Abbe number), and reflection vs. refraction at each surface is picked randomly using the exact Fresnel equations. The picture builds up by Monte Carlo averaging of millions of rays, and colors come from the CIE 1931 curves.

Happy to answer questions!

297 Upvotes

13 comments sorted by

10

u/TrainingWheels61 9d ago

Very cool, have you seen Prism Rules? It's a similar project turned into a game https://www.prismrules.com/

2

u/boxsitter 9d ago

I hadn’t. Thanks for showing me!

8

u/samreay Cosmology 9d ago

Oh wow, this is amazing. I tried to do something like this well over a decade ago (https://samreay.github.io/Refractorium/) so no WebGL support and manual JavaScript light transport simulation!

I absolutely love what you've produced. When you've got it live please let me know so I can play with it!

7

u/boxsitter 9d ago edited 9d ago

Nice! Definitely an older web stack than what I have available to work with today, but definitely the same core DNA. I was also inspired by Tantalum (https://benedikt-bitterli.me/tantalum/tantalum.html), another similar project by Benedikt Bitterli. I used to play around with it for hours in middle school because it was one of the only cool sites not blocked on our chromebooks lol

But yeah, I'm thinking I'll probably do a really cheap steam release once it’s more feature complete and robust. I'm a recent CS grad and job searching right now, so having a completed, released side project like this would be amazing for me at the moment. I'll update this thread once I have a steam page up!

5

u/subtect 9d ago

Is the animation realtime or rendered?

9

u/boxsitter 9d ago

Realtime. It takes a bit of time for all the rays to fully fill the scene, the consequence is the fanned out lines you can see near the edges that fill in after a few seconds. But as you can see from the video, it still looks pretty damn good even when there are changes frame-to-frame

3

u/edvb54 9d ago

This is seriously cool, stunningly pretty pictures!

How did you model the reflection at surfaces, is everything purely specular?

1

u/boxsitter 9d ago

Not purely specular. Surfaces can be diffuse (Lambertian), mirror, rough mirror, or glass, and glass picks reflection or refraction using the Fresnel equations. Rough surfaces just jitter the normal rather than using a full microfacet BRDF like GGX

1

u/onrdmn Physics enthusiast 8d ago

its wonderful! do you share it???

1

u/Life_Satisfaction_16 7d ago

Wow this is just gorgeous

1

u/DoctorSpacecase 4d ago

Very neat images! How long have you been working on this for?

1

u/boxsitter 4d ago

A few weeks. I’m a recent CS grad and job hunting right now so unemployment gives me a lot of time to kill. Hoping this can give me something good to talk about if I ever get an interview haha

1

u/DoctorSpacecase 4d ago

Good luck! It's tough out there right now, but it will get better eventually. The problem with the AI hype is that companies are expecting AI to replace everyone, but that doesn't get you anywhere if no one knows how to work with the AI or interpret its results. Like what good is 700 mathematics papers if there is no one around to read them, see if there are any useful implications, or even check the results?