import * as THREE from 'three'; import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js'; import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js'; import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass.js'; import { OutputPass } from 'three/examples/jsm/postprocessing/OutputPass.js'; const BACKGROUND = 0x0f0b12; const TOP_COLOR = new THREE.Color('#d9c2ff'); const MIDDLE_COLOR = new THREE.Color('#8a3cff'); const BOTTOM_COLOR = new THREE.Color('#ff52d9'); function octagonPath(path, radius) { // Vertex at the top, matching the app icon. for (let index = 0; index <= 8; index += 1) { const angle = Math.PI / 2 + (index * Math.PI) / 4; const x = Math.cos(angle) * radius; const y = Math.sin(angle) * radius; if (index === 0) path.moveTo(x, y); else path.lineTo(x, y); } return path; } function createRingGeometry() { const shape = octagonPath(new THREE.Shape(), 1); shape.holes.push(octagonPath(new THREE.Path(), 0.76)); const geometry = new THREE.ExtrudeGeometry(shape, { depth: 0.12, bevelEnabled: true, bevelThickness: 0.07, bevelSize: 0.06, bevelSegments: 1, }); geometry.center(); // Faceted shading needs unshared vertices, then a vertical lilac-to-pink gradient. const faceted = geometry.toNonIndexed(); faceted.computeVertexNormals(); const positions = faceted.attributes.position; const colors = new Float32Array(positions.count * 3); const color = new THREE.Color(); for (let index = 0; index < positions.count; index += 1) { const t = (positions.getY(index) + 1.1) / 2.2; if (t > 0.5) color.copy(MIDDLE_COLOR).lerp(TOP_COLOR, (t - 0.5) * 2); else color.copy(BOTTOM_COLOR).lerp(MIDDLE_COLOR, t * 2); color.toArray(colors, index * 3); } faceted.setAttribute('color', new THREE.BufferAttribute(colors, 3)); return faceted; } function createMaterial(emissiveIntensity) { return new THREE.MeshStandardMaterial({ vertexColors: true, flatShading: true, metalness: 0.25, roughness: 0.32, emissive: new THREE.Color('#6d28d9'), emissiveIntensity, transparent: true, }); } export function mountOctagons(container, { animate }) { const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' }); renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); renderer.setClearColor(BACKGROUND, 1); container.append(renderer.domElement); const scene = new THREE.Scene(); scene.background = new THREE.Color(BACKGROUND); scene.fog = new THREE.Fog(BACKGROUND, 7, 26); const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 60); camera.position.set(0, 0, 8); scene.add(new THREE.AmbientLight(0xc5b3df, 0.25)); const keyLight = new THREE.PointLight(0xf2b394, 22, 30); keyLight.position.set(4, 4, 6); const fillLight = new THREE.PointLight(0xff52d9, 18, 30); fillLight.position.set(-5, -3, 4); scene.add(keyLight, fillLight); const geometry = createRingGeometry(); const heroRing = new THREE.Mesh(geometry, createMaterial(0.35)); heroRing.scale.setScalar(3.3); heroRing.position.z = -0.6; heroRing.material.transparent = false; scene.add(heroRing); const composer = new EffectComposer(renderer); composer.addPass(new RenderPass(scene, camera)); const bloom = new UnrealBloomPass(new THREE.Vector2(1, 1), 0.4, 0.25, 0.45); composer.addPass(bloom); composer.addPass(new OutputPass()); const pointer = { x: 0, y: 0, targetX: 0, targetY: 0 }; function onPointerMove(event) { pointer.targetX = (event.clientX / window.innerWidth) * 2 - 1; pointer.targetY = (event.clientY / window.innerHeight) * 2 - 1; } window.addEventListener('pointermove', onPointerMove, { passive: true }); function resize() { const width = container.clientWidth; const height = container.clientHeight; if (!width || !height) return; camera.aspect = width / height; // Keep the front ring framing the headline on narrow, tall screens. camera.position.z = camera.aspect < 1 ? 8 / Math.max(camera.aspect, 0.5) : 8; camera.updateProjectionMatrix(); renderer.setSize(width, height, false); composer.setSize(width, height); } const resizeObserver = new ResizeObserver(resize); resizeObserver.observe(container); resize(); const clock = new THREE.Clock(); let running = false; let frame = 0; let elapsed = 4; function render(elapsed) { pointer.x += (pointer.targetX - pointer.x) * 0.04; pointer.y += (pointer.targetY - pointer.y) * 0.04; heroRing.rotation.x = Math.sin(elapsed * 0.35) * 0.22 + pointer.y * 0.12; heroRing.rotation.y = Math.cos(elapsed * 0.28) * 0.3 + pointer.x * 0.18; heroRing.rotation.z = elapsed * 0.06; composer.render(); } function loop() { elapsed += Math.min(clock.getDelta(), 0.05); render(elapsed); frame = requestAnimationFrame(loop); } render(4); return { setRunning(next) { const shouldRun = next && animate(); if (shouldRun === running) return; running = shouldRun; if (running) { clock.getDelta(); loop(); } else { cancelAnimationFrame(frame); } }, renderStill() { render(elapsed); }, }; } export function supportsWebGL() { try { const canvas = document.createElement('canvas'); return Boolean(canvas.getContext('webgl2') || canvas.getContext('webgl')); } catch { return false; } }