This project has been deleted.

Godot Ray Tracer

Godot Ray Tracer

15 devlogs
33h 33m
•  Ship certified
Created by Doodl0

A basic ray/path-tracing implementation in Godot. Created as a way for me to learn compute shaders and learn more about graphics programming

Timeline

Ship 2

1 payout of shell 84.0 shells

Doodl0

about 1 year ago

Doodl0 • Covers 2 devlogs and 3h 28m
Earned sticker

Added new main menu, which exposes multiple ray tracer config variables to allow you to change the resolution scale, random seed, maximum ray bounces, rays per pixel, and the ability to toggle backface rendering, the ground plane and the FPS counter

Update attachment
Earned sticker

Added functionality to render multiple rays per pixel, allowed variable max ray bounces, refactored material system to be a single material struct and allowed Vector3s as specular, which was an oversight before. You can also toggle backface rendering for meshes.

The render attached was rendered with 2 rays per pixel, at 8 max bounces and with backface culling enabled.

Update attachment
Doodl0 Doodl0 about 1 year ago

Correction: backface rendering was enabled, not backface culling

Ship 1

1 payout of shell 780.0 shells

Doodl0

about 1 year ago

Doodl0 • Covers 13 devlogs and 30h 4m

I accidentally deleted a full commit, so here's a hastily recreated version of what I did:
Created a new demo scene, fixed some rendering bugs and added render image, updated README

Update attachment
Earned sticker

Added AABB intersections before intersecting the triangles of a mesh. This should in theory improve performance, but the ray tracer may also use more VRAM now, because I had to add some padding to the mesh objects.

Also the image attached is of a bug I encountered, but it helps to demonstrate the bounding box, as normally you wouldn't see it

Update attachment
Earned sticker

Added mesh rendering that is editable in the scene. It uses the Godot getfaces() function, meaning it isn't the most memory efficient as some vertices are duplicated, but it was the easiest way for me to do it. I also had to face some issues to make the mesh render properly, such as converting the getfaces() output from a Vector3 array to a Vector4 array to prevent memory alignment issues, and making sure the triangles rendered in the right order, as Godot has a clockwise winding order.

I plan to soon check the AABB bounding box of a mesh before the triangles, which should significantly improve performance.

Update attachment
Earned sticker

Added triangles, meaning I am not that far from full mesh rendering. However, I am currently having issues getting the positions of the vertices to work, and it sometimes seems that the triangle is infinitely long / tall.

Also fixed the black pixel artifacting (may still be shown in screenshot) after spending a long time troubleshooting, as I knew it was the frame sampling and potentially an NaN error, but I didn't understand how it could cause a NaN to occur. I narrowed it down to texels being NaN when loaded in the shader, which I stopped (?) by clamping the pixel values before storing them, and also included an early return if any of the texel values are NaN.

Update attachment

Added Phong BRDF specular reflections, and emission and smoothness material properties. Also used importance sampling for both Phong and Lambert sampling

Update attachment
Earned sticker

Added proper Lambertian diffuse lighting into the ray tracer, meaning there is now soft shadows, global illumination and ambient occulsion built in.
I also have been having issues with random black pixels appearing on and staying on the screen. I have asked friends to test this and tested this on multiple devices, and it still occurs. I believe this is due to how I blend frames to get a full render, but I cannot figure out why this happens specifically.

Update attachment

Ray traced spheres are now editable in the Godot editor rather than requiring to be changed in the inspector. To create a sphere, you simply create a Node3D and attach the sphere script, then add it to the list of sphere in the ray tracer node. You can change the sphere's position, radius and material properties in the inspector.

I also fixed a bug with the sky texture, causing it to be darker than it should. This was due to converting the texture to an RGBA8 texture, whereas now it is converted to RGBAF

Update attachment

Added simple diffuse lighting from a directional light specified in the scene.
Also attempted to optimise writing the render to the screen, as I found there was a lot of overhead creating the image to set the TextureRect to. This overhead seems to be lessened with a release build, but I am unsure if I can optimise it further.
Finally, I changed the update_compute() shader to update the shader uniforms instead of reconstructing them entirely. I'm not sure if this affects performance, but it cleans the code a bit

Update attachment

Added reflection, for now both the ground and sphere are equally reflective
Added a skybox image that is passed to the shader to render spherically around the camera
Optimised the antialiasing - previously, it got the viewport texture every frame and copied it to a TextureRect. This is slow due to passing data from the GPU to CPU and require seperate shaders. Now, the AA is part of the main compute shader, and I realised I can just resuse the image2D with the last frame's output, instead of getting the frame from the GPU to the CPU and back

Update attachment

Fixed a bug in the antialiasing causing it to not overlay the current render over the last frame properly.
Despite my multiple hours contemplating how to implement the progressive AA in my last devlog, I found it hard to find a way to make the effect multipass in Godot. I previously tried to set the texture of a second screen texture to the last render, but what I should have done is set it to the last overall frame, so that the effect actually overlays properly. I didn't notice this and simply thought my implementation was subpar, but you can tell the difference looking at the edge of the sphere in the screenshots.

Update attachment

Attempted to implement a progressive antialiasing method on the raytraced output, but I'm not sure that it was worth the time or effort. The end result does have less jagged edges, but the image is somewhat jittery. It works by shifting the render slightly every frame and then overlaying it over previous frames. I tried to put it in a seperate shader, but found that the way I wanted it to work, it would only work in the main compute shader

I may later implement SSAA or a custom MSAA, but that would require some work and I have alreayd wasted a lot of time on this

Update attachment

Implemented basic ray tracing with a global ground plane and a sphere. I took a long time to debug this partly due to a stupid mistake on my part, where I forgot to move the Camera3D above the ground plane, making me think that my code wasn't functional, so the time spend on this is somewhat (unintentionally) inflated due to my idiocy and me not thinking to check the sceneview for debugging and only the shader

Update attachment

After spending much time learning and failing to use compute shaders, I've setup a basic compute shader pipeline and created a basic shader to show the UV of the screen coords. Hopefully I can fully focus on writing GLSL now and less of learning to set up rendering devices and compute pipelines

Update attachment