r/GraphicsProgramming • u/Objective-Silver-770 • 17d ago
Question I built a raytracer in Scratch. Looking for feedback on optimizing it
I made a raytracer using Scratch's block language, and I'm trying to improve its rendering speed. The source is public: https://scratch.mit.edu/projects/1437715094/ (click See inside to inspect the blocks).
It has reflective chrome and gold, refracting glass, soft shadows, fog, and a movable camera. Rendering starts at a coarse resolution and progressively sharpens after camera movement. The output is Scratch's 480 x 360 stage; the anti-aliasing mode casts four rays per pixel. The attached screenshot is from that mode.
The main Raytracer sprite has custom procedures for tracing, scene intersection, shading, lighting, glass, and anti-aliasing.
Given the constraints of Scratch, what would you profile first, and which changes would you expect to give the biggest speedup without losing the glass and reflections? Feedback on the implementation is welcome.
43
u/SyntheticDuckFlavour 17d ago
Drawing shiny balls is a rite of passage for every graphics programmer.
Next task, implement this in a native language and use your hand rolled shaders to parallelise this.
18
u/kinokomushroom 17d ago
How tf did you do in this Scratch?!
9
u/hanotak 17d ago
Rewrite it in Rust
/s
5
u/jgage 17d ago
Wow! Good job!
This is something that I have some experience in because I wrote a ray-tracer in Scratch nearly 20 years ago.
Your ray-tracer is already really fast. It's not real time, but it's almost instant if you're running it with turbowarp. If you're concerned about it not running fast enough in stock Scratch I wouldn't worry about it too much because you're already doing a lot of techniques to make it fast.
If you're smart with your code you could utilize your screen cache. When you render a low-res pixel you only need to render three more pixels at a higher resolution and reuse the already rendered pixel. This will give less than a 25% speed boost though.
2
u/Relevant_Computer982 17d ago
look inwards, youre a level of smart and dedicated far beyond my comprehension
2
u/pants75 17d ago
Everyone asking about different languages and improvements is missing the point. Good job man. Doing this in scratch is crazy.
2
u/Feisty_Bike_9614 17d ago
The largest improvement in mine was a BVH if you don’t already have one, followed by an embedded language to construct scenes . Triangles and meshes before that .
1
u/Big_Hovercraft8317 16d ago
I built a fully working engine with raytraced shadows that’s runs at a stable fps when at a res of two though the version I have posted on scratch is not the completed version as I just finished the engine and I am going to post it on gamer jolt soon. My user is @that-three-d_guy if you want to check it out.
1
u/Antagonin 17d ago
Optimize it? Maybe don't write it in scratch. xD