r/webdev • • 18d ago

Showoff Saturday Real-time gravitational lensing in the browser: a black hole shader as my portfolio home page (Three.js + GLSL)

I rebuilt my personal portfolio around a fragment shader that does real-time gravitational lensing.

Each pixel fires a light ray, and the ray is bent step by step with the Schwarzschild light-bending acceleration (a = -1.5 · h² · r̂ / r⁴, with the Schwarzschild radius as the unit). If the ray crosses the accretion disk plane it picks up the disk color, and if it falls inside r = 1 the pixel is black. The bright arch over the top of the black hole is not a texture: it is light from the back of the disk, bent over the hole.

The disk rotates with speed proportional to r^(-3/2), so it spins faster near the center. The noise is sampled with the cos/sin of the angle to avoid a seam, and a simple Doppler factor makes the approaching side brighter (an approximation, not full GR).

Around it, my three main projects orbit on Kepler ellipses, using a Newton solver for Kepler's equation with T² ∝ a³. The About section uses a camera fall into the hole.

Stack: Next.js, React Three Fiber and custom GLSL. On small screens the shader uses fewer integration steps. Performance on different GPUs is what I'm least sure about, so I'd like to hear how it runs for you.

Live: https://tobiasteles.dev

I'll put the main ray loop in a comment.

0 Upvotes

9 comments sorted by

2

u/chrisso123 12d ago edited 12d ago

It's not bad. But there were easier ways to accomplish this. Plus I dabbled with the same idea of using this as a potential portfolio background. I mean it is decent but screams amateur work.

2

u/United_Baseball_3040 12d ago

Thanks for the honest take. You're right that there are easier ways, like a pre-rendered video loop or a ready-made shader. I went the hard way on purpose because I wanted to learn the physics behind it (the light bending is computed per pixel, so it reacts when you rotate and zoom).

Which easier approach did you have in mind? I'd like to compare, especially for performance on phones. And if you have your version somewhere, I'd like to see it.

1

u/chrisso123 12d ago edited 12d ago

You're welcome. I used shaders and then customized it further. Also I was attempting to use a circular 4k star map to have a proper background to simulate what it would be like if earth was a blackhole. this was a year ago so I don't remember much about it anymore.

blackhole_sim - Pastebin.com

here is the link. copy code in notepad and save as html extension (any file type) to run. Let me know if it doesn't work.

here is the link to view the website live if you want to view it live:
https://rose-agnella-72.tiiny.site

3

u/Wiltix 18d ago

What does 90% of react mean?

2

u/No_Thought_3099 18d ago

It’s a common but somewhat misguided way to add visualization of technology competency. I personally don’t care for it. How does one quantify their skill to an accurate percentage? I would rather see objective results that testify. Perhaps even links to projects that leverage those skills. Because anyone, discounting the difficulty in quantifying a percentage-based skills assessment, can write any arbitrary number in that list.

That’s just me though.

3

u/Wiltix 18d ago

I see it all the time and it’s one of the most meaningless ways to tell me about your technology competency .

Would rather see years of experience with a technology than a random percentage.

If you can’t give me yoe with something it probably doesn’t belong on your portfolio / CV.

2

u/United_Baseball_3040 18d ago

Fair point, thanks both. The percentages were just my own rough self-assessment, and you're right that they don't say much. I'm going to replace them with the projects where I actually used each technology, so people can check it themselves. Appreciate the honest feedback.

0

u/United_Baseball_3040 18d ago

vec3 vel = dir;

float h2 = dot(cross(pos, dir), cross(pos, dir));

for (int i = 0; i < 140; i++) {

float r2 = dot(pos, pos);

if (r2 < 1.0) break; // fell into the horizon

float dt = max(0.05, 0.1 * (sqrt(r2) - 0.9));

vel += (-1.5 * h2 * pos / (r2 * r2 * sqrt(r2))) * dt;

vec3 np = pos + vel * dt;

if (pos.y * np.y < 0.0) {

// ray crossed the disk plane: sample the disk color here

}

pos = np;

}