283 lines
9.2 KiB
TypeScript
283 lines
9.2 KiB
TypeScript
'use client';
|
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
import * as THREE from 'three';
|
|
|
|
interface Hero3DCanvasProps {
|
|
className?: string;
|
|
}
|
|
|
|
export function Hero3DCanvas({ className = '' }: Hero3DCanvasProps) {
|
|
const [mounted, setMounted] = useState<boolean>(false);
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
setMounted(true);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!mounted) return;
|
|
const container = containerRef.current;
|
|
if (!container) return;
|
|
|
|
// Check prefers-reduced-motion safely
|
|
let prefersReducedMotion = false;
|
|
try {
|
|
prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
} catch {
|
|
prefersReducedMotion = false;
|
|
}
|
|
|
|
// Dimensions
|
|
let width = container.clientWidth || 340;
|
|
let height = container.clientHeight || 340;
|
|
|
|
let renderer: THREE.WebGLRenderer | null = null;
|
|
let animFrameId: number = 0;
|
|
let observer: IntersectionObserver | null = null;
|
|
|
|
// Geometries & Materials for cleanup
|
|
let crystalGeo: THREE.OctahedronGeometry | null = null;
|
|
let crystalMat: THREE.MeshPhysicalMaterial | null = null;
|
|
let edgesGeo: THREE.EdgesGeometry | null = null;
|
|
let edgesMat: THREE.LineBasicMaterial | null = null;
|
|
let innerGeo: THREE.OctahedronGeometry | null = null;
|
|
let innerMat: THREE.MeshPhysicalMaterial | null = null;
|
|
let dotGeo: THREE.BufferGeometry | null = null;
|
|
let dotMat: THREE.PointsMaterial | null = null;
|
|
let particlesGeo: THREE.BufferGeometry | null = null;
|
|
let particlesMat: THREE.PointsMaterial | null = null;
|
|
|
|
try {
|
|
// Scene, Camera, WebGLRenderer with Safe Error Handling
|
|
const scene = new THREE.Scene();
|
|
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100);
|
|
camera.position.z = 6.2;
|
|
|
|
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 Translucent Diamond Crystal Prism (Octahedron)
|
|
crystalGeo = new THREE.OctahedronGeometry(1.8, 0);
|
|
crystalMat = new THREE.MeshPhysicalMaterial({
|
|
color: 0x2563eb, // Studio Sapphire
|
|
roughness: 0.08,
|
|
metalness: 0.15,
|
|
transmission: 0.85,
|
|
opacity: 0.7,
|
|
transparent: true,
|
|
emissive: 0x1d4ed8,
|
|
emissiveIntensity: 0.25,
|
|
});
|
|
const crystalMesh = new THREE.Mesh(crystalGeo, crystalMat);
|
|
rootGroup.add(crystalMesh);
|
|
|
|
// Subtle Champagne Glowing Edge Highlights on Crystal
|
|
edgesGeo = new THREE.EdgesGeometry(crystalGeo);
|
|
edgesMat = new THREE.LineBasicMaterial({
|
|
color: 0xc5a880, // Champagne Gold
|
|
transparent: true,
|
|
opacity: 0.85,
|
|
});
|
|
const edgesLines = new THREE.LineSegments(edgesGeo, edgesMat);
|
|
rootGroup.add(edgesLines);
|
|
|
|
// 2. Inner Nested Core Prism (Counter-rotating Gold Core)
|
|
innerGeo = new THREE.OctahedronGeometry(1.0, 0);
|
|
innerMat = new THREE.MeshPhysicalMaterial({
|
|
color: 0xd4af37, // Metallic Gold
|
|
roughness: 0.2,
|
|
metalness: 0.85,
|
|
emissive: 0xb45309,
|
|
emissiveIntensity: 0.45,
|
|
transparent: true,
|
|
opacity: 0.85,
|
|
});
|
|
const innerMesh = new THREE.Mesh(innerGeo, innerMat);
|
|
rootGroup.add(innerMesh);
|
|
|
|
// Vertex Spark Points
|
|
const vertexPositions = crystalGeo.attributes.position;
|
|
dotGeo = new THREE.BufferGeometry();
|
|
dotGeo.setAttribute('position', vertexPositions);
|
|
dotMat = new THREE.PointsMaterial({
|
|
color: 0xfef3c7,
|
|
size: 0.14,
|
|
transparent: true,
|
|
opacity: 0.95,
|
|
blending: THREE.AdditiveBlending,
|
|
});
|
|
const vertexPoints = new THREE.Points(dotGeo, dotMat);
|
|
rootGroup.add(vertexPoints);
|
|
|
|
// 3. Delicate Floating Neural Particles (35 Points)
|
|
const particleCount = prefersReducedMotion ? 12 : 36;
|
|
const particlePositions = new Float32Array(particleCount * 3);
|
|
|
|
for (let i = 0; i < particleCount; i++) {
|
|
const radius = 2.2 + Math.random() * 1.4;
|
|
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);
|
|
}
|
|
|
|
particlesGeo = new THREE.BufferGeometry();
|
|
particlesGeo.setAttribute('position', new THREE.BufferAttribute(particlePositions, 3));
|
|
particlesMat = new THREE.PointsMaterial({
|
|
color: 0x93c5fd,
|
|
size: 0.08,
|
|
transparent: true,
|
|
opacity: 0.75,
|
|
blending: THREE.AdditiveBlending,
|
|
});
|
|
const particlesMesh = new THREE.Points(particlesGeo, particlesMat);
|
|
rootGroup.add(particlesMesh);
|
|
|
|
// Lights: Ambient + Sapphire Key + Champagne Fill
|
|
const ambientLight = new THREE.AmbientLight(0xffffff, 1.1);
|
|
scene.add(ambientLight);
|
|
|
|
const keyLight = new THREE.PointLight(0x38bdf8, 3.2, 10);
|
|
keyLight.position.set(3, 3, 3);
|
|
scene.add(keyLight);
|
|
|
|
const fillLight = new THREE.PointLight(0xc5a880, 2.8, 10);
|
|
fillLight.position.set(-3, -2, 2);
|
|
scene.add(fillLight);
|
|
|
|
// Mouse Tracking with Damped Inertia
|
|
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;
|
|
|
|
mouseX = (e.clientX - centerX) / (window.innerWidth * 0.5);
|
|
mouseY = (e.clientY - centerY) / (window.innerHeight * 0.5);
|
|
|
|
targetRotY = mouseX * 0.75;
|
|
targetRotX = mouseY * 0.55;
|
|
};
|
|
|
|
window.addEventListener('mousemove', handleMouseMove, { passive: true });
|
|
|
|
// Handle Resize
|
|
const handleResize = () => {
|
|
if (!container || !renderer) return;
|
|
width = container.clientWidth || 340;
|
|
height = container.clientHeight || 340;
|
|
camera.aspect = width / height;
|
|
camera.updateProjectionMatrix();
|
|
renderer.setSize(width, height);
|
|
};
|
|
|
|
window.addEventListener('resize', handleResize);
|
|
|
|
// IntersectionObserver
|
|
let isVisible = true;
|
|
|
|
observer = new IntersectionObserver(
|
|
(entries) => {
|
|
const [entry] = entries;
|
|
isVisible = entry.isIntersecting;
|
|
if (isVisible && !animFrameId) {
|
|
animate();
|
|
}
|
|
},
|
|
{ threshold: 0.05 }
|
|
);
|
|
observer.observe(container);
|
|
|
|
const clock = new THREE.Clock();
|
|
|
|
const animate = () => {
|
|
if (!isVisible || !renderer) {
|
|
animFrameId = 0;
|
|
return;
|
|
}
|
|
|
|
animFrameId = requestAnimationFrame(animate);
|
|
|
|
const elapsedTime = clock.getElapsedTime();
|
|
|
|
rootGroup.rotation.y += (targetRotY - rootGroup.rotation.y) * 0.045;
|
|
rootGroup.rotation.x += (targetRotX - rootGroup.rotation.x) * 0.045;
|
|
|
|
if (!prefersReducedMotion) {
|
|
crystalMesh.rotation.y += 0.004;
|
|
crystalMesh.rotation.x += 0.002;
|
|
edgesLines.rotation.y += 0.004;
|
|
edgesLines.rotation.x += 0.002;
|
|
vertexPoints.rotation.y += 0.004;
|
|
vertexPoints.rotation.z += 0.002;
|
|
|
|
innerMesh.rotation.y -= 0.006;
|
|
innerMesh.rotation.z += 0.003;
|
|
const breath = 1.0 + Math.sin(elapsedTime * 2.2) * 0.06;
|
|
innerMesh.scale.set(breath, breath, breath);
|
|
|
|
particlesMesh.rotation.y -= 0.0015;
|
|
particlesMesh.rotation.x += 0.001;
|
|
}
|
|
|
|
renderer.render(scene, camera);
|
|
};
|
|
|
|
animate();
|
|
|
|
return () => {
|
|
if (animFrameId) cancelAnimationFrame(animFrameId);
|
|
window.removeEventListener('mousemove', handleMouseMove);
|
|
window.removeEventListener('resize', handleResize);
|
|
if (observer) observer.disconnect();
|
|
|
|
if (crystalGeo) crystalGeo.dispose();
|
|
if (crystalMat) crystalMat.dispose();
|
|
if (edgesGeo) edgesGeo.dispose();
|
|
if (edgesMat) edgesMat.dispose();
|
|
if (innerGeo) innerGeo.dispose();
|
|
if (innerMat) innerMat.dispose();
|
|
if (dotGeo) dotGeo.dispose();
|
|
if (dotMat) dotMat.dispose();
|
|
if (particlesGeo) particlesGeo.dispose();
|
|
if (particlesMat) particlesMat.dispose();
|
|
if (renderer) {
|
|
renderer.dispose();
|
|
if (renderer.domElement && renderer.domElement.parentNode) {
|
|
renderer.domElement.parentNode.removeChild(renderer.domElement);
|
|
}
|
|
}
|
|
};
|
|
} catch (err) {
|
|
console.warn('Three.js / WebGL not supported or failed to initialize, continuing safely:', err);
|
|
}
|
|
}, [mounted]);
|
|
|
|
return (
|
|
<div
|
|
ref={containerRef}
|
|
className={`relative w-full h-full pointer-events-none select-none ${className}`}
|
|
aria-hidden="true"
|
|
role="presentation"
|
|
/>
|
|
);
|
|
}
|