Files
webb/src/components/3d/Hero3DCanvas.tsx
T

245 lines
7.5 KiB
TypeScript
Raw Normal View History

'use client';
import React, { useEffect, useRef } from 'react';
import * as THREE from 'three';
interface Hero3DCanvasProps {
className?: string;
}
export function Hero3DCanvas({ className = '' }: Hero3DCanvasProps) {
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const container = containerRef.current;
if (!container) return;
// Check prefers-reduced-motion
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Dimensions
let width = container.clientWidth || 400;
let height = container.clientHeight || 400;
// Scene, Camera, Renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100);
camera.position.z = 7;
const renderer = new THREE.WebGLRenderer({
alpha: true,
antialias: true,
powerPreference: 'high-performance',
});
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
container.appendChild(renderer.domElement);
// Root Group for Mouse Parallax
const rootGroup = new THREE.Group();
scene.add(rootGroup);
// 1. Outer Geodesic Wireframe Prism (Icosahedron)
const icoGeo = new THREE.IcosahedronGeometry(2.1, 1);
const wireframeGeo = new THREE.WireframeGeometry(icoGeo);
const wireframeMat = new THREE.LineBasicMaterial({
color: 0x3b82f6, // Electric Blue
transparent: true,
opacity: 0.5,
blending: THREE.AdditiveBlending,
});
const wireframeMesh = new THREE.LineSegments(wireframeGeo, wireframeMat);
rootGroup.add(wireframeMesh);
// Outer Node Dots on Vertices
const vertexPositions = icoGeo.attributes.position;
const vertexCount = vertexPositions.count;
const dotGeo = new THREE.BufferGeometry();
dotGeo.setAttribute('position', vertexPositions);
const dotMat = new THREE.PointsMaterial({
color: 0x60a5fa,
size: 0.12,
transparent: true,
opacity: 0.85,
blending: THREE.AdditiveBlending,
});
const vertexPoints = new THREE.Points(dotGeo, dotMat);
rootGroup.add(vertexPoints);
// 2. Inner Glowing Core Sphere
const innerGeo = new THREE.SphereGeometry(1.0, 32, 32);
const innerMat = new THREE.MeshStandardMaterial({
color: 0x4f46e5, // Indigo
roughness: 0.15,
metalness: 0.85,
emissive: 0x1e40af, // Deep Blue glow
emissiveIntensity: 0.6,
transparent: true,
opacity: 0.9,
});
const innerMesh = new THREE.Mesh(innerGeo, innerMat);
rootGroup.add(innerMesh);
// 3. Orbiting Neural Tensor Particles (60 Particles)
const particleCount = prefersReducedMotion ? 20 : 60;
const particlePositions = new Float32Array(particleCount * 3);
const particleScales = new Float32Array(particleCount);
for (let i = 0; i < particleCount; i++) {
const radius = 2.6 + Math.random() * 1.6;
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(2 * Math.random() - 1);
particlePositions[i * 3] = radius * Math.sin(phi) * Math.cos(theta);
particlePositions[i * 3 + 1] = radius * Math.sin(phi) * Math.sin(theta);
particlePositions[i * 3 + 2] = radius * Math.cos(phi);
particleScales[i] = Math.random() * 0.08 + 0.04;
}
const particlesGeo = new THREE.BufferGeometry();
particlesGeo.setAttribute('position', new THREE.BufferAttribute(particlePositions, 3));
const particlesMat = new THREE.PointsMaterial({
color: 0x818cf8, // Lavender Indigo
size: 0.1,
transparent: true,
opacity: 0.75,
blending: THREE.AdditiveBlending,
});
const particlesMesh = new THREE.Points(particlesGeo, particlesMat);
rootGroup.add(particlesMesh);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);
const blueLight = new THREE.PointLight(0x3b82f6, 3, 12);
blueLight.position.set(4, 3, 4);
scene.add(blueLight);
const purpleLight = new THREE.PointLight(0xa855f7, 2.5, 12);
purpleLight.position.set(-4, -3, 3);
scene.add(purpleLight);
// Mouse Tracking with Smooth Damping
let mouseX = 0;
let mouseY = 0;
let targetRotX = 0;
let targetRotY = 0;
const handleMouseMove = (e: MouseEvent) => {
const rect = container.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
// Distance from center of container normalized
mouseX = (e.clientX - centerX) / (window.innerWidth * 0.5);
mouseY = (e.clientY - centerY) / (window.innerHeight * 0.5);
targetRotY = mouseX * 0.65;
targetRotX = mouseY * 0.45;
};
window.addEventListener('mousemove', handleMouseMove, { passive: true });
// Handle Resize
const handleResize = () => {
if (!container) return;
width = container.clientWidth || 400;
height = container.clientHeight || 400;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
};
window.addEventListener('resize', handleResize);
// IntersectionObserver to halt loop when not visible (Zero waste when scrolled away)
let isVisible = true;
let animFrameId: number;
const observer = new IntersectionObserver(
(entries) => {
const [entry] = entries;
isVisible = entry.isIntersecting;
if (isVisible && !animFrameId) {
animate();
}
},
{ threshold: 0.05 }
);
observer.observe(container);
// Animation Loop
let clock = new THREE.Clock();
const animate = () => {
if (!isVisible) {
animFrameId = 0;
return;
}
animFrameId = requestAnimationFrame(animate);
const elapsedTime = clock.getElapsedTime();
// Smooth Inertial Lerp to Target Rotation
rootGroup.rotation.y += (targetRotY - rootGroup.rotation.y) * 0.04;
rootGroup.rotation.x += (targetRotX - rootGroup.rotation.x) * 0.04;
if (!prefersReducedMotion) {
// Continuous Gentle Oscillation & Spin
wireframeMesh.rotation.y += 0.0035;
wireframeMesh.rotation.z += 0.0015;
vertexPoints.rotation.y += 0.0035;
vertexPoints.rotation.z += 0.0015;
// Inner core breathing scale
const breath = 1.0 + Math.sin(elapsedTime * 1.8) * 0.05;
innerMesh.scale.set(breath, breath, breath);
// Counter-orbit particles
particlesMesh.rotation.y -= 0.002;
particlesMesh.rotation.x += 0.001;
}
renderer.render(scene, camera);
};
animate();
// Clean up
return () => {
if (animFrameId) cancelAnimationFrame(animFrameId);
window.removeEventListener('mousemove', handleMouseMove);
window.removeEventListener('resize', handleResize);
observer.disconnect();
// Dispose Geometries and Materials
icoGeo.dispose();
wireframeGeo.dispose();
wireframeMat.dispose();
dotGeo.dispose();
dotMat.dispose();
innerGeo.dispose();
innerMat.dispose();
particlesGeo.dispose();
particlesMat.dispose();
renderer.dispose();
if (renderer.domElement && renderer.domElement.parentNode) {
renderer.domElement.parentNode.removeChild(renderer.domElement);
}
};
}, []);
return (
<div
ref={containerRef}
className={`relative w-full h-full pointer-events-none select-none ${className}`}
aria-hidden="true"
role="presentation"
/>
);
}