Path Tracing / Ray Tracing Engine I've Been Working On

Hi Roblox Developers,

I’m pretty new to the Roblox community, especially when it comes to writing code and such, but I wanted to post something today to kind of show off what I’ve recently been working on!

The stuff you probably don't care about, but I include anyways:

(btw, if you just want to cut to the chase, skip this part).

About around November of last year I started working on my first video game (which is still no where near being finished, but that’s aside from the point) and I’ve been really enjoying writing my own custom code for a lot of things in Roblox. Usually I never do these types of things, and I will admit that I still think that the stuff I am working on is merely just a small hobby compared to all of the other work I do, but I still find quite a charm in working in the Roblox community. I feel as though Roblox, even with all of the new features they have added over the years, is still a growing and developing community- and while I could have easily decided to make my first video game on higher level software than what Roblox Studio provides, I’m glad that I didn’t because it means that I get to share my work with others to help them learn as well! With that being said, let me show off what I’ve been working on, I think if you’ve read this far you’ll probably enjoy it.

The stuff you actually want to read:

Alright so I’m going to try to explain this as fast as possible.

Basically, while I was working on developing my first video game I ended up writing some code for an NPC- very basic code that most of it I won’t use in the future BUT it serves as a baseline for more complex code. Now, I wanted to make pretty realistic NPCs- ones that could easily be mistaken for actual players, so I didn’t want their movements to be weird or robotic.

I could just make static animations for these NPCs… but that’s boring.
And I could just use Roblox’s path finding service… but I’m stubborn and I don’t like it.

So I started working on my own path finding service, and accidentally ended up making a raytracing algorithm. Don’t ask me how I accidentally did that, I’d waste as much time explaining it to you as I did when I wasted my time writing the first version of the code.

Once I realized that the code I had written was 1) stupidly complicated and 2) not even useful for what I initially was trying to create- I was pretty conflicted as I didn’t want to get rid of the code I had worked so hard on but I also didn’t have a use for it either.

Yeah so I decided to do it anyways because why not. Here’s what I ended up with:

Pretty nice am I right? If you’re mad or confused, give me a second to explain.

So this is, technically speaking, a path-tracing/ray-tracing engine – and if you’re really not familiar with the code behind these things or if you have been deceived by the RTX brand (if you don’t understand that joke, don’t worry about it), this is probably going to look pretty boring to you. I don’t blame you for thinking it’s boring. I’d assume that most of you out there would think that this post would be showing off some beautiful imagery where the sun shines brightly and there are reflections everywhere… and while this isn’t showing that type of imagery, what it does show is how the code for that imagery is made.

The image you see is literally a representation (roughly) of what happens in order to make those reflections and sun light (etc.) look so great, and what this means is that as long as I build more onto this those reflections and such will be 100% possible.

So I mean, idk… I think that’s kinda exciting.

Anyways, let me know what you think, and thanks for reading my very mal-formed post. I unfortunately went to school for mathematics and science, not language, so I apologize.

Thanks!

11 Likes

Ok, so I’m pretty confused (I’m very new to scripting myself). My guess is that your program tracks reflections off of surfaces and stuff? (correct me if I’m wrong).

1 Like

Yes, that’s kind of the general idea behind it.
Basically what happens is that there is a line (or ray) that is calculated as if it were like a ray of light. Like if you threw a tennis ball but instead of it falling back to the ground it just keeps going further and further until it hits something. Once the ray (or tennis ball in the given example) hits something, I have written code to determine how the ray should bounce off of the thing it has hit. All of the rays will keep on bouncing until it has nothing to bounce into, like if it bounces off of a wall and then goes straight up into the sky.

The code can do simple things like drawing lines and such, but we can also use this code for simulating actual light rays, which is why it’s very powerful.

1 Like

Oh, I get it now, thank you. Your right, that is pretty powerful, especially if you want to make a custom physics engine.

1 Like

I realize this thread is old, but, WOW. I’ve been doing research on Global Illumination and RTX recently and that, although does not have the absurd amount of Rays of Pure Ray Tracing, it, at least, works :slight_smile:

Keep up the good work, if you’re still on it, of course

1 Like

Also, I’d like to know if you will, eventually, make the source code Open Source.

Also, I’ve been searching, and, apparently, there is a Patent(I kid you not) from NVidia. A patent for their Ray Tracing technology. So, maybe you could look into it, so, if you end up using it on a game for your personal gain you won’t have legal issues. But, I think you’re fine since it states hardware, A LOT and you your system does not require an RTX Card, etc. So, Idk

1 Like

Hey, thanks for taking interest in my work! I appreciate it! I unfortunately won’t be making the code open source any time soon, as it’s super fundamental and all it really is right now is just the mathematics behind ray tracing in general. There’s not a super big use case for it right now, and I’d have to make a more API-like system for it to actually be of any use— otherwise the code right here is essentially just a tech demo. I haven’t worked on this code in quite a while, as I’ve been focused on making custom character replication within roblox, but when I do go back to working on it you will most likely find it in this game: Starving Sorority 3.53* - Roblox . If I do end up making some open source resources, I will definitely make sure to let you know and it will probably be within another roblox forum post! Cheers!

1 Like

Oh and also, the ray count and bounces can go on for any number specified, even infinite! I’ve limited the system to sample counts like most other general purpose rendering engines, just for my computer’s sake.

2 Likes

Thanks for the reply. Even if it isn’t a finished product, I still think is amazing. When you stated you could set whatever you want the rays to be, you said they could be a big number, but your PC couldn’t handle. That got me thinking. How much time does it take for it to trace all of that? A few seconds? Minutes? Milliseconds?

1 Like

Sorry for the late response. Realistically its not that the computer couldn’t handle it, its just that creating a bunch of instances on a roblox game is really performance hitting. The actual calculations could probably be made within a couple of milliseconds honestly, I wouldn’t put it past you to assume that it would take less than a second per couple rays shot. However, this system doesn’t have any significant data attached to it, only the path a ray takes when bouncing off surfaces. If you were to take it from a rendering perspective, it’s like tracing lines without a BSDF, or instruction set for when you reflect. There is no diffusion or reflection, just path tracing. Extra work would have to be done to make that possible, and that would probably make it longer than a couple milliseconds at that point. Right now the base data attached within a ray instance is structured like this:

table.insert(samplesstatic[key]["subsamples"], {["referenceposition"] = raytracecache.Position, ["referencevector"] = finalraycache, ["referencenormal"] = raytracecache.Normal})

I wrote this a while back, and back then I was super new to programming, so my naming conventions kinda suck. But essentially there’s a packet of information for each ray shot, and each sub ray shot after that. All we need for these calculations is the initial shot ray’s vector, the impact/intersection position, and the vector formed by the angle of incidence (the vector that the next ray should be shot in as a reflection).

TLDR: only information that this system has is ray hit position and the ray reflection vector so that you can continue shooting rays.

Let me know if you have any other questions, it’s kinda hard for me to answer most as this code is really old and cough kinda bad, but I’ll try my best lol. Thanks again for your interest in my work!

2 Likes

I know this is an old post but how exactly does this work. From my understanding path tracing is sending as many rays from the camera as there are pixels. not just from a single point right?