{
  "format": "putcode/playground@1",
  "id": "763bfd06-1724-4510-b1eb-e5fb289ba2c6",
  "url": "https://putcode.dev/u/mighil/s/odxsmyvuh7",
  "title": "Three.js Singularity",
  "description": "A cinematic pitch-black universe featuring a gravitational singularity, luminous accretion disk, orbital energy trails, distant stars and interactive camera controls. Drag to orbit, scroll to zoom, click to trigger a gravitational pulse. Built with Three.js.",
  "category": "experimental",
  "tags": [
    "three-js",
    "3d",
    "webgl",
    "interactive",
    "particles"
  ],
  "visibility": "public",
  "status": "published",
  "revision": 3,
  "author": "mighil",
  "forked_from": null,
  "created_at": "2026-12-02T18:58:06.742Z",
  "updated_at": "2026-12-02T19:42:18.447Z",
  "published_at": "2026-12-02T19:14:21.268Z",
  "source": {
    "html": "<div class=\"scene\" aria-label=\"Interactive 3D black hole singularity\"></div>",
    "css": "*{box-sizing:border-box}html,body,#app{width:100%;height:100%;margin:0;overflow:hidden;background:#000}\n.scene{position:relative;width:100%;height:100%;min-height:320px;overflow:hidden;background:radial-gradient(ellipse at 58% 52%,#09090d 0%,#020204 42%,#000 78%)}\ncanvas{position:absolute;inset:0;width:100%;height:100%;display:block}",
    "js": "import * as THREE from \"https://esm.sh/three@0.160.0\";\n\nconst host = document.querySelector(\".scene\");\nconst scene = new THREE.Scene();\nconst camera = new THREE.PerspectiveCamera(48, host.clientWidth / host.clientHeight, 0.1, 180);\ncamera.position.set(0, 2.2, 13);\n\nconst renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });\nrenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));\nrenderer.setSize(host.clientWidth, host.clientHeight);\nrenderer.outputColorSpace = THREE.SRGBColorSpace;\nrenderer.toneMapping = THREE.ACESFilmicToneMapping;\nrenderer.toneMappingExposure = 1.45;\nhost.appendChild(renderer.domElement);\n\nconst blackHole = new THREE.Group();\nscene.add(blackHole);\n\nconst core = new THREE.Mesh(\n  new THREE.SphereGeometry(1.12, 96, 96),\n  new THREE.MeshBasicMaterial({ color: 0x000000 })\n);\nblackHole.add(core);\n\nconst horizon = new THREE.Mesh(\n  new THREE.TorusGeometry(1.13, 0.025, 16, 180),\n  new THREE.MeshBasicMaterial({ color: 0xf4d6b2, transparent: true, opacity: 0.72 })\n);\nhorizon.rotation.x = Math.PI / 2;\nblackHole.add(horizon);\n\n// Accretion disk: 19,000 glowing particles.\nconst diskCount = 19000;\nconst diskPositions = new Float32Array(diskCount * 3);\nconst diskColors = new Float32Array(diskCount * 3);\nconst colorInner = new THREE.Color(0xffe6c6);\nconst colorMid = new THREE.Color(0xe47b39);\nconst colorOuter = new THREE.Color(0x6e2a22);\n\nfor (let i = 0; i < diskCount; i++) {\n  const radius = 1.42 + Math.pow(Math.random(), 1.7) * 3.15;\n  const angle = Math.random() * Math.PI * 2;\n  const spread = (Math.random() - 0.5) * (0.035 + radius * 0.025);\n  diskPositions[i * 3] = Math.cos(angle) * radius;\n  diskPositions[i * 3 + 1] = spread;\n  diskPositions[i * 3 + 2] = Math.sin(angle) * radius;\n\n  const t = (radius - 1.42) / 3.15;\n  const c = t < 0.48\n    ? colorInner.clone().lerp(colorMid, t / 0.48)\n    : colorMid.clone().lerp(colorOuter, (t - 0.48) / 0.52);\n  const brightness = 0.55 + Math.random() * 0.8;\n  diskColors[i * 3] = c.r * brightness;\n  diskColors[i * 3 + 1] = c.g * brightness;\n  diskColors[i * 3 + 2] = c.b * brightness;\n}\n\nconst diskGeometry = new THREE.BufferGeometry();\ndiskGeometry.setAttribute(\"position\", new THREE.BufferAttribute(diskPositions, 3));\ndiskGeometry.setAttribute(\"color\", new THREE.BufferAttribute(diskColors, 3));\nconst disk = new THREE.Points(\n  diskGeometry,\n  new THREE.PointsMaterial({\n    size: 0.045,\n    vertexColors: true,\n    transparent: true,\n    opacity: 0.94,\n    blending: THREE.AdditiveBlending,\n    depthWrite: false,\n    sizeAttenuation: true\n  })\n);\ndisk.rotation.x = 0.16;\nblackHole.add(disk);\n\n// Fine luminous rings around the event horizon.\n[1.48, 1.62, 2.0, 2.55, 3.15, 3.8].forEach((r, i) => {\n  const ring = new THREE.Mesh(\n    new THREE.TorusGeometry(r, i < 2 ? 0.012 : 0.006, 8, 220),\n    new THREE.MeshBasicMaterial({\n      color: i < 2 ? 0xffd6a2 : 0xb65a36,\n      transparent: true,\n      opacity: i < 2 ? 0.52 : 0.16,\n      blending: THREE.AdditiveBlending\n    })\n  );\n  ring.rotation.x = Math.PI / 2 + 0.16;\n  blackHole.add(ring);\n});\n\nconst halo = new THREE.Mesh(\n  new THREE.SphereGeometry(1.42, 48, 48),\n  new THREE.MeshBasicMaterial({\n    color: 0xe58c51,\n    transparent: true,\n    opacity: 0.055,\n    side: THREE.BackSide,\n    blending: THREE.AdditiveBlending\n  })\n);\nblackHole.add(halo);\n\n// Faint polar jets.\nconst jetMaterial = new THREE.MeshBasicMaterial({\n  color: 0x9d8bba,\n  transparent: true,\n  opacity: 0.12,\n  blending: THREE.AdditiveBlending\n});\n[-1, 1].forEach(sign => {\n  const jet = new THREE.Mesh(\n    new THREE.ConeGeometry(0.22, 3.8, 32, 1, true),\n    jetMaterial\n  );\n  jet.position.y = sign * 2.9;\n  jet.rotation.x = sign === 1 ? 0 : Math.PI;\n  blackHole.add(jet);\n});\n\n// Distant star field.\nconst starCount = 4200;\nconst starPositions = new Float32Array(starCount * 3);\nconst starColors = new Float32Array(starCount * 3);\nconst starPalette = [\n  new THREE.Color(0xffffff),\n  new THREE.Color(0x9faecb),\n  new THREE.Color(0xffcba0)\n];\n\nfor (let i = 0; i < starCount; i++) {\n  const radius = 12 + Math.random() * 65;\n  const theta = Math.random() * Math.PI * 2;\n  starPositions[i * 3] = Math.cos(theta) * radius;\n  starPositions[i * 3 + 1] = Math.sin(theta) * radius;\n  starPositions[i * 3 + 2] = (Math.random() - 0.5) * 90;\n\n  const c = starPalette[Math.floor(Math.random() * starPalette.length)];\n  const k = 0.35 + Math.random() * 0.65;\n  starColors[i * 3] = c.r * k;\n  starColors[i * 3 + 1] = c.g * k;\n  starColors[i * 3 + 2] = c.b * k;\n}\n\nconst starsGeo = new THREE.BufferGeometry();\nstarsGeo.setAttribute(\"position\", new THREE.BufferAttribute(starPositions, 3));\nstarsGeo.setAttribute(\"color\", new THREE.BufferAttribute(starColors, 3));\nconst stars = new THREE.Points(\n  starsGeo,\n  new THREE.PointsMaterial({\n    size: 0.055,\n    vertexColors: true,\n    transparent: true,\n    opacity: 0.82,\n    sizeAttenuation: true\n  })\n);\nscene.add(stars);\n\n// Sparse foreground dust.\nconst dustCount = 700;\nconst dustPositions = new Float32Array(dustCount * 3);\nfor (let i = 0; i < dustCount; i++) {\n  dustPositions[i * 3] = (Math.random() - 0.5) * 34;\n  dustPositions[i * 3 + 1] = (Math.random() - 0.5) * 22;\n  dustPositions[i * 3 + 2] = (Math.random() - 0.5) * 28;\n}\nconst dustGeo = new THREE.BufferGeometry();\ndustGeo.setAttribute(\"position\", new THREE.BufferAttribute(dustPositions, 3));\nconst dust = new THREE.Points(\n  dustGeo,\n  new THREE.PointsMaterial({\n    color: 0xb8a9a0,\n    size: 0.018,\n    transparent: true,\n    opacity: 0.36\n  })\n);\nscene.add(dust);\n\n// Subtle warm and cool edge lighting.\nscene.add(new THREE.AmbientLight(0x29212a, 0.6));\nconst warmLight = new THREE.PointLight(0xffa15d, 35, 18);\nwarmLight.position.set(4, 3, 4);\nscene.add(warmLight);\nconst coolLight = new THREE.PointLight(0x625c92, 18, 16);\ncoolLight.position.set(-4, -3, -4);\nscene.add(coolLight);\n\n// Pointer interaction: drag to orbit, wheel to zoom, click to pulse.\nlet dragging = false;\nlet lastX = 0;\nlet lastY = 0;\nlet rotationY = 0;\nlet rotationX = 0;\nlet targetX = 0;\nlet targetY = 0;\nlet pulse = 0;\nlet zoom = 13;\n\nrenderer.domElement.addEventListener(\"pointerdown\", e => {\n  dragging = true;\n  lastX = e.clientX;\n  lastY = e.clientY;\n  renderer.domElement.setPointerCapture(e.pointerId);\n});\n\nrenderer.domElement.addEventListener(\"pointermove\", e => {\n  if (!dragging) return;\n  rotationY += (e.clientX - lastX) * 0.006;\n  rotationX += (e.clientY - lastY) * 0.004;\n  lastX = e.clientX;\n  lastY = e.clientY;\n});\n\nrenderer.domElement.addEventListener(\"pointerup\", () => dragging = false);\nrenderer.domElement.addEventListener(\"pointercancel\", () => dragging = false);\nrenderer.domElement.addEventListener(\"click\", () => pulse = 1);\nrenderer.domElement.addEventListener(\"wheel\", e => {\n  zoom = THREE.MathUtils.clamp(zoom + e.deltaY * 0.009, 7.5, 25);\n}, { passive: true });\n\nconst clock = new THREE.Clock();\n\nfunction animate() {\n  requestAnimationFrame(animate);\n  const t = clock.getElapsedTime();\n\n  disk.rotation.y = t * 0.075;\n  disk.rotation.z = Math.sin(t * 0.16) * 0.018;\n\n  blackHole.rotation.y = rotationY + Math.sin(t * 0.08) * 0.035;\n  blackHole.rotation.x += (rotationX + targetX - blackHole.rotation.x) * 0.025;\n\n  stars.rotation.y = t * 0.002;\n  dust.rotation.y = -t * 0.004;\n\n  const energy = 1 + pulse * 0.12;\n  blackHole.scale.setScalar(energy);\n  halo.material.opacity = 0.045 + Math.sin(t * 1.3) * 0.012 + pulse * 0.09;\n  horizon.material.opacity = 0.58 + Math.sin(t * 2.1) * 0.12 + pulse * 0.25;\n  pulse *= 0.94;\n\n  camera.position.z += (zoom - camera.position.z) * 0.035;\n  camera.position.x += (targetY * 1.6 - camera.position.x) * 0.018;\n  camera.lookAt(0, 0, 0);\n  renderer.render(scene, camera);\n}\n\nanimate();\n\nconst resize = new ResizeObserver(() => {\n  const w = host.clientWidth;\n  const h = host.clientHeight;\n  camera.aspect = w / h;\n  camera.updateProjectionMatrix();\n  renderer.setSize(w, h);\n});\nresize.observe(host);"
  }
}