Here’s a single‐file HTML black-hole simulator inspired by Gargantua. It uses Three.js, a custom lens-lensing shader, a glowing accretion disk, real “asteroid” particles orbiting under gravity, and a dynamic lens flare. Camera controls let you fly around the scene.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gargantua Black Hole Simulator</title>
<style>
body { margin:0; overflow:hidden; background:#000; }
#info {
position:absolute; top:10px; left:10px;
color:#fff; font-family:Arial,sans-serif;
background:rgba(0,0,0,0.6); padding:12px;
border-radius:6px; line-height:1.4em;
z-index:10;
}
a { color:#6cf; }
</style>
</head>
<body>
<div id="info">
<strong>Gargantua Black Hole Simulator</strong><br>
Controls:<br>
• Mouse drag: rotate view<br>
• Mouse wheel: zoom<br>
• W/A/S/D or arrow keys: move camera<br>
• Shift: speed boost
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/objects/Lensflare.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/objects/LensflareElement.js"></script>
<script>
//— SCENE / RENDERER / CAMERA —//
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({ antialias:true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
60,
window.innerWidth/window.innerHeight,
0.1, 5000
);
camera.position.set(0, 30, 80);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.1;
//— STARFIELD BACKGROUND WITH LENSING SHADER —//
// 1) make a canvas texture of random stars
const starCanvas = document.createElement('canvas');
starCanvas.width = 1024; starCanvas.height = 512;
const ctx = starCanvas.getContext('2d');
ctx.fillStyle = 'black';
ctx.fillRect(0,0,1024,512);
for(let i=0;i<3000;i++){
const x = Math.random()*1024, y=Math.random()*512;
const r = Math.random()*1.2;
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2);
ctx.fill();
}
const starTex = new THREE.CanvasTexture(starCanvas);
starTex.wrapS = starTex.wrapT = THREE.RepeatWrapping;
// 2) sphere geometry “inside out”
const bgGeo = new THREE.SphereBufferGeometry(1000,64,32);
bgGeo.scale(-1,1,1);
// 3) custom shader material to distort (gravitational lens)
const bgMat = new THREE.ShaderMaterial({
uniforms: {
map: { value: starTex },
strength:{ value: 0.5 } // tweak lens strength
},
vertexShader: `
varying vec2 vUv;
void main(){
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
`,
fragmentShader: `
uniform sampler2D map;
uniform float strength;
varying vec2 vUv;
void main(){
vec2 c = vec2(0.5);
vec2 d = vUv - c;
float r = length(d);
// gravitational lensing warp formula
float f = 1.0 + strength/(r*r + 0.02);
vec2 uv = c + d*f;
gl_FragColor = texture2D(map, uv);
}
`
});
scene.add(new THREE.Mesh(bgGeo, bgMat));
//— GARGANTUA EVENT HORIZON & ACCRETION DISK —//
// event horizon
const bhRadius = 5;
const bhGeo = new THREE.SphereBufferGeometry(bhRadius, 64, 32);
const bhMat = new THREE.MeshBasicMaterial({ color:0x000000 });
const blackHole = new THREE.Mesh(bhGeo, bhMat);
blackHole.position.set(0,0,0);
scene.add(blackHole);
// glowing accretion disk (two tori with emissive gradient)
function makeDisk(innerR, outerR, colorA, colorB){
const tor = new THREE.TorusBufferGeometry((innerR+outerR)/2, (outerR-innerR)/2, 16, 256);
const mat = new THREE.MeshBasicMaterial({
side: THREE.DoubleSide,
transparent: true,
onBeforeCompile: shader => {
shader.uniforms.time = { value:0 };
shader.fragmentShader = `
uniform float time;
varying vec2 vUv;
${shader.fragmentShader.replace(
`#include <clipping_planes_fragment>`,
`
float r = length(vUv - 0.5);
// color ramp
vec3 c = mix(vec3(${colorA}), vec3(${colorB}), smoothstep(0.3,0.8,r));
// swirl by time
float a = atan(vUv.y-0.5, vUv.x-0.5) + time*2.0;
float swirl = 0.5 + 0.5*sin(a*10.0 + r*20.0);
gl_FragColor = vec4(c * swirl, swirl);
#include <clipping_planes_fragment>
`
);
};
mat.userData.shader = shader;
}
});
const mesh = new THREE.Mesh(tor, mat);
mesh.rotation.x = Math.PI/2;
return mesh;
}
const disk1 = makeDisk(6,10,'1.0,0.5,0.0','1.0,0.1,0.0');
const disk2 = makeDisk(11,13,'0.8,0.8,0.1','0.4,0.1,0.0');
scene.add(disk1, disk2);
//— LENS FLARE —//
function makeFlare(){
// small radial gradient
const c = document.createElement('canvas');
c.width = c.height = 256;
const g = c.getContext('2d');
const grd = g.createRadialGradient(128,128,0,128,128,128);
grd.addColorStop(0,'rgba(255,240,200,0.8)');
grd.addColorStop(0.4,'rgba(255,180,100,0.4)');
grd.addColorStop(1,'rgba(0,0,0,0)');
g.fillStyle = grd; g.fillRect(0,0,256,256);
return new THREE.CanvasTexture(c);
}
const lensflare = new THREE.Lensflare();
const flareTex = makeFlare();
lensflare.addElement(new THREE.LensflareElement(flareTex, 700, 0));
blackHole.add(lensflare);
//— ASTEROID PARTICLES WITH GRAVITY —//
const Gm = 2000; // gravitational constant × mass
const astCount = 200;
const astGeo = new THREE.SphereBufferGeometry(0.3, 6, 6);
const astMat = new THREE.MeshStandardMaterial({color:0x888888});
const asteroids = [];
for(let i=0;i<astCount;i++){
const m = astGeo.clone();
const mesh = new THREE.Mesh(astGeo,astMat);
// random orbit radius
const r = THREE.MathUtils.randFloat(15,80);
const ang = Math.random()*Math.PI*2;
mesh.position.set(Math.cos(ang)*r, Math.sin(ang)*r, (Math.random()-0.5)*4);
mesh.userData.vel = new THREE.Vector3(-Math.sin(ang)*Math.sqrt(Gm/r),
Math.cos(ang)*Math.sqrt(Gm/r),
0);
scene.add(mesh);
asteroids.push(mesh);
}
//— LIGHTS —//
scene.add(new THREE.AmbientLight(0x202020));
const pl = new THREE.PointLight(0xffeeaa, 1.5, 500);
pl.position.set(0,0,0); // glow at the BH center
scene.add(pl);
//— HANDLE RESIZE —//
window.addEventListener('resize',() => {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
//— KEYBOARD MOVEMENT —//
const key = {};
window.addEventListener('keydown', e=>key[e.code]=true);
window.addEventListener('keyup', e=>key[e.code]=false);
//— ANIMATION LOOP —//
let last = performance.now();
function animate(t){
requestAnimationFrame(animate);
const dt = (t - last)/1000; last = t;
// rotate disks & update their shader time
disk1.rotation.z += dt*0.1;
disk2.rotation.z -= dt*0.07;
[disk1,disk2].forEach(d=>{
const sh = d.material.userData.shader;
if(sh) sh.uniforms.time.value += dt;
});
// move asteroids under gravity
asteroids.forEach(a=>{
const pos = a.position;
const r2 = pos.lengthSq();
const acc = pos.clone().multiplyScalar(-Gm/(r2 * Math.sqrt(r2)));
a.userData.vel.add(acc.multiplyScalar(dt));
pos.add(a.userData.vel.clone().multiplyScalar(dt));
});
// camera keyboard
const speed = dt * (key['ShiftLeft']||key['ShiftRight'] ? 100 : 30);
if(key['KeyW']||key['ArrowUp']) camera.translateZ(-speed);
if(key['KeyS']||key['ArrowDown']) camera.translateZ( speed);
if(key['KeyA']||key['ArrowLeft']) camera.translateX(-speed);
if(key['KeyD']||key['ArrowRight']) camera.translateX( speed);
controls.update();
renderer.render(scene,camera);
}
animate();
</script>
</body>
</html>
How it works
-
Design
- A gigantic inverted sphere with a procedural starfield textured through a custom shader that warps (“lenses”) it around the hole.
- A pure black sphere as the event horizon.
- Two semi-transparent, swirling tori with a time-driven gradient shader for the hot accretion disk.
- A point light & dynamic lens flare at the center.
-
Functionality
- Gravitational lensing: background distortion via fragment shader.
- Accretion disk: animated shader gives fiery swirl.
- Orbiting asteroids: a cloud of chunks in Keplerian orbits under (a=-GM/r^2).
- Dynamic lens flare: rendered with Three.js’s Lensflare helpers.
-
Controls
- Mouse drag: rotate the view around the center.
- Mouse wheel: zoom camera in/out.
- W/A/S/D or arrows: fly the camera through the grav-field.
- Shift: holds to boost speed.
Just save this as an .html file, open in a modern browser, and you’re off on a relativistic joy-ride around Gargantua! Feel free to tweak strength, disk colors, asteroid count, etc., to dial up realism or performance. Enjoy! 🚀🕳️