r/GraphicsProgramming • • Nov 02 '26

Video I replaced the in-scattering in a cloud raymarch with a tiny neural network, then checked it against a path tracer

Light inside a cloud is expensive to get right, so games use a few simple formulas tuned until it looks good. I wanted to see how far off that really is, and whether a small network could do better.

I wrote a path tracer as the reference, trained a network on it in Python, and ran it in plain HLSL in Unity 6. It replaces one thing, the light scattered toward the camera at each step. Everything else in the raymarch stays the same.

Then I moved the sun from behind the camera to behind the cloud and scored both against the path tracer, where 1.00 is correct. The classic lighting goes from 0.21 to 2.17. The network stays between 0.90 and 1.01. It costs about 20% more GPU time.

I had a great time building this, and I think we've barely started finding out what tiny networks can do inside a shader. If you try one yourself, I'd love to see what you make!

https://charlesgrassi.dev/blog/neural-volumetric-clouds/

827 Upvotes

58 comments sorted by

16

u/Worth-Competition134 Nov 02 '26

Advice: you're just lacking multiscattering and a few hacks for the analytic model, which artificially boosts your arbitrary target to be "better", you'll notice no game has actually shipped with clouds that look nearly as bad as your ray marched example. Your timings also reveal how terrible the the raymarcher is, raymarched clouds shipped with around a millisecond of cost for the entire sky in some instances back on Xbox One, let alone whatever you're running this on.

Instead of reinventing the wheel, which so much of this "neural" stuff is doing because it's "new" (it's not) and "exciting" my advice is to learn about the original wheel before deciding the new thing must be better.

2

u/CharlesGrassi Nov 03 '26

Yeah, that's a really bad advice. But thank you for your comment.

1

u/CharlesGrassi Nov 03 '26

The goal here was to explore how neural shader could be use more creatively than it seems to have be done before, I am fully aware that I could have coded a better cloud with more work on the shader

5

u/CodyDuncan1260 Nov 03 '26

Reinventing the wheel is standard in research. We find new wheels with new tradeoffs. E.G. Voxel Cone tracing never really took off, but we found faster voxelization methods from the exercise.

My advice would be to reinvent the wheel on the new methods, and use that as motivation to study how wheels were made before, find parallels.

11

u/slapcover Nov 02 '26

Does the network generalize to other cloud shapes ?

3

u/Antagonin Nov 02 '26

Seems like it's hardcoded.

1

u/CharlesGrassi Nov 02 '26

Yep if you train it on enough shape

1

u/scrufflor_d Nov 02 '26

how long does training it in another shape take?

13

u/-2qt Nov 02 '26

Cool idea, wanted to learn more, but the LLM-ness of the text in the blog post makes it so irritating to read :/

1

u/CharlesGrassi Nov 02 '26

I'm sorry for that, I'm definitely not a good writer, it was very practical for me to explain everything in such detail but that's totally understandable

6

u/-2qt Nov 02 '26

To me personally, the writing is too detailed, to the point it makes it quite hard to focus on the essentials. It also feels very marketing-like, like you're trying extremely hard to sell me on training tiny neural nets that render clouds lol

Cool project still, I'd be interested in exploring the topic myself.

1

u/CharlesGrassi Nov 02 '26

Noted I'll try to think about that when I'll write my next article thanks

24

u/EarlMarshal Nov 02 '26

If the analytics models derivates from true brightness so much it can't be a good analytical model though?

5

u/claypunk Nov 02 '26

the time constraint is likely the key here

13

u/ybungalobill Nov 02 '26

I don't think so. It seems to capture the details just right, but the brightness deviation correlates with the camera-light direction. Therefore it is straight-forward to add an analytical correction term for brightness and contrast. Ergo there's something wrong with the model used in this video.

2

u/CharlesGrassi Nov 02 '26

It's Henyey-Greenstein plus Wrenninge's multiple scattering octaves, three of them, with the same raymarch and transmittance as the network. The sweep numbers use the conventional constants

5

u/James20k Nov 02 '26

It really looks like the implementation is just borked, cloud models aren't usually that bad. I'd guess they mixed up srgb and linear colour, or something like that, because it isn't even close to the ground truth

Analytic cloud models are way better than this

2

u/blackrack Nov 04 '26 edited Nov 04 '26

It's likely just slapped together by Claude and not necessarily made to make traditional methods look good (which they can look good but noticeably different from the accurate results)

3

u/claypunk Nov 02 '26

fair, I stand corrected

151

u/wektor420 Nov 02 '26

Now convert the NN model into a formula and use genetic algorithm to search for simplifications - you might find a better model this way

33

u/BladesongDev Nov 02 '26

Indeed, let the NN ‘learn’ the approach, then try to collapse and simplify it to something tangible a human can understand, optimize and maybe further improve upon. This should be the way.

8

u/wektor420 Nov 02 '26

This comment reminded me that KANs were designed for such uses - tho I have not tracked progress with them for a long time

17

u/PoopyDootyBooty Nov 02 '26

This is not necessarily true. The ground truth is incredibly computationally expensive to calculate, it’s not obvious an efficient formula exists.

However perceptually, there are many things that make a cloud look correct. A neural network is actually the correct tool to approximate these features.

9

u/wektor420 Nov 02 '26

You never know until you try

3

u/JohnHuffYT Nov 02 '26

The thing is that a neural network is already "a formula". It's just one with many terms. Your idea of using a genetic algorithm to search for a smaller formula/neural network model is actually valid. This is a form of hyperparameter search/optimization, and is something I explored myself many years ago with decent results. Unfortunately I never wrote up a paper on it but I'm sure there's many papers published on the topic now.

Fair warning though, performing this kind of search is very computationally expensive. Calculating the fitness of each individual in the population requires training its neural network. When I was looking into this I was using ~30 GPUs (gtx 970) in my universities computer lab. The good thing is that in this case, part of the fitness here is how small the network is.

11

u/Hoshiqua Nov 02 '26

Isn't the NN model a formula already ? I'm not sure what you mean and why it would be required in order to apply a genetic algorithm ?

Maybe I'm being a tool, sorry

27

u/ybungalobill Nov 02 '26

NN is a formula of a very specific form; one can use a genetic search over a broader space of formulas using the NN formula as an initial guess.

6

u/Hoshiqua Nov 02 '26

Ah, yes I guess I didn't think of it that way. So this means the genetic algorithm is able to add operations that wouldn't be expressable in NN form ? Like.. adding the sum of two initially unrelated weights / biases somewhere ?

6

u/wektor420 Nov 02 '26

Yeah, but my focus would be on dropping useless weights first to reduce formula size

4

u/zshift Nov 02 '26

I would love to learn more about this approach. What resources would you recommend for learning this?

4

u/ybungalobill Nov 02 '26

Go to wikipedia: Evolutionary Algorithms, Genetic Algorithms, and Genetic Programming articles. Check the references at the end of each.

4

u/-2qt Nov 02 '26

It's distillation all the way down

4

u/wektor420 Nov 02 '26

Why not btw there is a recent numerical improvement in how cross entropy is calculated during distillation

https://arxiv.org/html/2610.31642

I ran a simple expirements on it and there is actual improvement

1

u/PyroRampage Nov 02 '26

Ablation is more logical to begin with. Distillation is basically just compression of weights. Ablation is modifying the approach to find compromises and deficits.

2

u/AnArmoredPony Nov 02 '26

do you mean something like symbolic regression?or are you just saying things that sound smart?

im so lost rn

3

u/wektor420 Nov 02 '26

Yes, symbolic regression fits my mental image NNs tend to have a lot not usefull parameters you can drop

If the network is small it may be viable

1

u/brownianhacker Nov 02 '26

Would be more efficient to just try different loss functions, like adding L0 or L1 norms on the weights to promote sparsity, and then being able to reduce the layer sizes. Genetic algorithms are much slower than anything based on backprop

26

u/1c3Type Nov 02 '26

So what happens when you're not trying to render this specific cloud shape in this specific lighting condition with this specific resolution?

18

u/venturepulse Nov 02 '26

You probably would need to include bigger dataset and avoid overfitting. It’s a classic machine learning problem, usually can be avoided without an issue

Generalization is like the whole purpose of neural networks

8

u/PyroRampage Nov 02 '26 edited Nov 02 '26

No it’s not. Neural graphics like NERFs for example or Gaussian Splats (not technically a neural network but optimised like one) are literally overfit per scene.

Generalisation is not the ‘whole’ purpose of NN’s, universal function approximation is.

1

u/1c3Type Nov 02 '26

Surely this will not ramp up performance cost, memory use, and risks of artifacting

5

u/venturepulse Nov 02 '26

Bigger dataset? It won’t increase any cost of inference, only during training maybe. Artifacts may not mean much if they allow better clouds for lower cost and happen only 1 in 10000 cases for example

4

u/PyroRampage Nov 02 '26

It would if the model is bigger due to needing to generalise. I don’t think you know what you’re talking about given your responses on this thread.

0

u/1c3Type Nov 02 '26

This obviously doesn’t work? Obviously keeping model size the same while drastically increasing the size and complexity of the workload is going to dumpster results. That would look terrible, it is going to output utter nonsense in almost every scene.

-1

u/venturepulse Nov 02 '26

I dont know where youre drawing all these conclusions from

1

u/1c3Type Nov 02 '26

Do you know how ml tech works, like, at all?

6

u/PyroRampage Nov 02 '26

The answer is no they clearly don’t lol. But they got upvoted which makes me concerned for this sub.

11

u/vetta-vetka Nov 02 '26

Really cool!

2

u/deftware Nov 02 '26

Seems like the analytical cloud rendering just needs more retro-reflectivity to be closer to ground truth. It still wouldn't capture the deep dark crannies and self-shading as well as the neural model. At the end of the day nothing ever needs to be "accurate", it only ever needed to be good enough to fool someone's eye.

24FPS was fast enough to make people feel like they were seeing motion instead of a bunch of individual images, even though we can tell the difference between two animations whose framerates different only by a few milliseconds per frame at 100+ FPS.

1

u/heliosythic Nov 02 '26

Nice, I was just thinking about doing this yesterday while try to implement Nubis system from Horizon Zero Dawn (and getting no where near the quality from the papers). Taking a break from the game and training a NN sounds like a fun detour. But the performance hit is unfortunate need to find a way around that

1

u/Affectionate_Bed2925 Nov 03 '26

Besides the brightness it looks way better and it's faster

1

u/DapperNurd Nov 03 '26

Very interesting. I would love to see more on this

1

u/S48GS Nov 05 '26

probably - "baking lightmap to shader with some NN compression"

it works because "lightmap" is gradients - so "NN" can build very little "parameters"(for superformula) - so it work fast in shaders

you can do it with Siren or "2d gausian splats"

look this Jure Triglav blog

there even webgl demo

and obviously - you can do same as above - with interpolation of cubemaps as lightmap... no NN

1

u/East_Ad_5801 29d ago

Disney has a paper on exactly this

-2

u/GunpowderGuy Nov 02 '26

wow, less than a milisecond and it looks good

0

u/[deleted] Nov 03 '26

[removed] — view removed comment

1

u/Next-Blackberry-991 Nov 03 '26

Wouldn't that cause a lot of overdraw?