r/GraphicsProgramming • • 17d ago

Question I built a raytracer in Scratch. Looking for feedback on optimizing it

Post image

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.

131 Upvotes

21 comments sorted by

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?!

20

u/Icy-Call-4860 17d ago

scratch is turing complete, therefore, given infinite ram and computation power, anything can be made.

if you want a practical answer:

raytracing is simply a bunch of maths formula's stringed together in an attempt to approximate the rendering equation, raytracing does this by calculating each pixel by shooting a ray into a scene from a given point and using that intersection point to calculate weather it is a shadow or light, after this, using a shading formula, like BDRF or phong shading, it calculates the lighting given, after this, extra stuff like reflective surfaces etc can be done with more fancy ray calculation.

you can read the raytraicng in one weekend to get more info

10

u/kinokomushroom 17d ago

I mean I understand that it's theoretically possible to do this in just about any programming language, but I can't imagine what a nightmare it must've been to do this in Scratch.

7

u/SwimSquirrel 15d ago

Check out the raytracer some mad person made in CMake 💀

5

u/PantuflasDev 15d ago

Hows that even possible?! Im searching that rn 🫪

1

u/bonzajplc 16d ago

it smells like a model work. UI for this is not responsive, so it had to be done outside of the editor. And looking at that code it looks like agentic work

2

u/IDatedSuccubi 16d ago

It's all simple shapes, you can make this in a single GLSL shader

2

u/Big_Hovercraft8317 16d ago

If you think this is crazy in scratch you should look at my project that’s a fully working engine in scratch with raytraced shadows at a stable 20-30 fps in turbowarp at a res of 2. You can expect around 10-15 at a res of 1.

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.

3

u/jgage 17d ago

Also, if you have four adjacent pixels that are very similar in color in your low-res render then you can just interpolate between those pixels when you go to the next higher resolution and that should cut down on the number of rays you need to cast on later passes.

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 .

5

u/Antagonin 17d ago

For 6 spheres and 1 quad, BVH will only slow things down.

2

u/jgage 17d ago

This. There’s a threshold of objects you need before you start using acceleration structures.

0

u/Feisty_Bike_9614 16d ago

Absolutely but the fun begins when you can render more complex objects. ( notice I mentioned meshes if you read the comment)

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