the Twarchive

This is a record of a twitter thread, originally posted in 2019

Thew
@AmazingThew

Ultrasound sim project at @LevelExGameDev is starting to produce vaguely plausible results

here's a gas bubble reverberating; sound gets trapped inside and bounces multiple times before escaping, creating a characteristic trail of echoes

Thew
@AmazingThew

The real-world artifact doesn't precisely look like this, but I'm pretty proud of the fact that the sim just *does this* when presented with a pocket of low-impedance, non-scattering material, without any special-case code

Thew
@AmazingThew

Also check out my kickass mad-scientist debug visualizations

Thew
@AmazingThew

Mouse selects blue scanline on the left, which is displayed as an amplitude signal ("A-mode" in ultrasound terminology) on the right

Black graph is raw signal, white graph is after time-gain compensation and tonemapping

Amplitude at the mouse (red circle) is printed at bottom

Thew
@AmazingThew

really dig how ominous these can end up looking lol

Thew
@AmazingThew

somebody make a game with this aesthetic

I mean, in addition to the one we're already making here I guess

Thew
@AmazingThew

weird strata

Thew
@AmazingThew

this is a bug but it's also extremely good

Thew
@AmazingThew

So earlier images simulating 1D wave propagation on a texel grid. Now trying an alternate approach using huge numbers of particles, representing stochastic effects like diffuse reflections by just doing dice rolls for every particle

Makes a very cool visualization at least

Thew
@AmazingThew

did you know that compute shaders are Really Fast

Thew
@AmazingThew

update: did you know that compute shaders are Not Fast Enough

Thew
@AmazingThew

So here's an (idealized) single pulse of energy along a single direction, analogous to an A-mode line in actual ultrasound

(the grey image isn't coming from the sim; it's just displaying the underlying anatomy that the waves interact with)

Thew
@AmazingThew

The handful of stationary particles are those that followed paths back to the transducer to register an echo signal

What it SHOULD produce is a nice yellow line of varying brightness, but instead it runs out of particles almost entirely after like two centimeters

Thew
@AmazingThew

I have thus learned the problem with rendering participating media via huge numbers of particles: Everything falls off exponentially, so it turns out the sheer quantity of particles required is completely untenable

That dot had *ten million particles* in it

Thew
@AmazingThew

When a wave strikes an interface, some portion of its energy is reflected and the rest transmitted

I'm simulating this by having particles check a random roll to see if they should bounce or continue on

A wave of 10,000,000 particles is down to single digits by the third bounce

Thew
@AmazingThew

...and that's just reflection/refraction at interfaces, I haven't even added scattering yet, which is an even faster exponential energy loss

I can do 10M particles in 16ms via compute shaders, but to actually fully resolve an image you'd need... trillions? quadrillions?

Thew
@AmazingThew

Anyway this is why nobody writes renderers that just naively track discrete photons, obviously. I was hoping that since ultrasound operates in 2D (sorta) I could at least produce a recognizable image with a reasonably tractable particle count, but, nope lol

Thew
@AmazingThew

Obviously the real solution here is Just Write A Path Tracer Dude but that's boring and hard and also probably too slow for phones (probably)

The grid-based propagation sim from earlier in the thread is still super promising in any case

Thew
@AmazingThew

Added kinda hacky scattering math, updated it so that particles that leave the sim space get re-emitted from the transducer, and ran it for 17.5 hours

Result is... not *correct*, but not wildly implausible either

Thew
@AmazingThew

for an A/B comparison, here's 30 minutes vs 17.5 hours, which is something like 450 million particles vs 15.7 billion

notice how the shadows are almost completely absent in the first image; they're a result of the way scattering works, which requires a TON of samples to capture

Thew
@AmazingThew

For further comparison, here's the current state of the grid-based sim, which runs at 60FPS

Note you can actually make out separate organs. The huge dark shapes are bones, which SHOULD be black bc ultrasound can't penetrate them. Not sure why particle sim lights them up

Thew
@AmazingThew

I've had dithering in the tonemapping for a while, but: I just weighted each RGB channel differently according to its perceived luminance, thus simulating higher bit depth by spreading the gradient across three channels

Green drops off before red and blue bc it's brighter:

Thew
@AmazingThew

Absolutely zero people will ever notice this but I feel extremely smart

Thew
@AmazingThew

One day I'll be as powerful as those dudes who publish siggraph papers about like An Improved BDRF Thing with a picture of spheres and then a picture of IMPROVED spheres and then they subtract the two in Photoshop and have to put the diff on an inverse log scale for you to see it

Thew
@AmazingThew

procedurally generating thyroids wrong