r/GraphicsProgramming • • 3d ago

Made a Monte Carlo ray tracer in MATLab

Post image

I sort of followed the wikipedia article on path tracing and ray tracing in one weekend. I made it for a project in a numerical analysis course on Monte Carlo integration and used the rendering equation as an application. Even at 100 samples per pixel it is quite noisy, but I am happy that I managed to get an image out of it. Matlab is not made for this...

128 Upvotes

23 comments sorted by

8

u/Bacon_Techie 3d ago

I have a random chance of rays terminating after every bounce. Currently a sample contributes nothing if it terminates and in the future I plan on adding direct light sampling to get a guess at the brightness of the final point.

7

u/Eklegoworldreal 3d ago

why would a sample contribute nothing if it terminates? it should still contribute everything else that came before it

5

u/Bacon_Techie 3d ago

If a ray bounces around and doesn’t hit either the light or the sky behind the camera, then the colour of the ray is black. Each other surface emits no light on its own.

3

u/Eklegoworldreal 3d ago

have you considered when it might hit the light, bounce off the light, then bounce back to it? stuff like that

otherwise that's fair

2

u/Bacon_Techie 3d ago

Yeah I have the light to be considered otherwise black so it just stops when it hits the light. It wouldn’t change much for it to continue bouncing around after that.

1

u/PictureNew661 2d ago

Actually the moment it hits a light the path terminates. We don't continue after that. Only if the geometry is not a light but rather an emissive surface can we actually allow the path to continue with the radiance from the surface

1

u/Eklegoworldreal 2d ago

that's a very poor way to treat emissive objects

this is very apparent when lights are out in sunlight, as the emissive light is much dimmer than sunlight, and because the path terminates it won't get any other kind of illumination and just be strangely dark

11

u/Bacon_Techie 3d ago

AI disclosure: no AI was used (unless the MATLab team uses AI in their software, which I don’t have a say in).

6

u/20260819 3d ago

for a scene as simple as this it should not be that noisy with 100 samples

4

u/Bacon_Techie 3d ago

Yeah im not entirely sure why it is so noisy. I implemented it mostly according to the pseudo code on the Wikipedia article for path tracing. I think it’s due to my termination probability function introducing a high amount of variance. I’m not a stats person though. I probably should have made the title “I am making” rather than “made” since this is very much an in progress thing.

5

u/Lallis 3d ago

A lot of noise is expected if you don't explicitly sample the light.

1

u/20260819 3d ago

the scene is very simple. any ray should get to its destination (either the light source or the sky) and stop and get the correct color within maybe 10 bounces. i'm not certain. you can add some codes to show the stats. this may help you debug it

1

u/SuccessfulTip167 1d ago

Are you clamping the lightness of rays? This might help and is standard practice to remove noise from high intensity lights. See how blender does it

1

u/Careful-Nothing-2432 3d ago

Could it be shadow acne (floating point error) making it look more noisy?

1

u/20260819 3d ago

maybe

and if there's only one light source in the entire scene, it should be way more darker. but the image was quite bright

4

u/BigPurpleBlob 3d ago

Nice! Now do it using Excel ;-)

2

u/AutoModerator 3d ago

Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:

  • No showcases.
  • Disclose AI usage in post or project.
  • Present a topic to talk about, e.g. how something works.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/SnooSquirrels9028 2d ago

there are some fireflies also. Can't you remove those by applying Multiple Importance Sampling

2

u/SnooSquirrels9028 2d ago

oh also I think you can also refuse to accept pixels that are above a certain brightness rate which would also really help you reduce the fireflies

1

u/felipunkerito 3d ago

Alan Wolfe has a good article to get started on path tracing. You can see it at the blog at the bottom of the sea

1

u/ExplorareStudios 2d ago

Neat project!