%PDF- %PDF-
| Direktori : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/auth/ |
| Current File : /home2/rezakur1/ai.rezakurnia.com/zh-router-frontend/src/components/auth/Auth3DScene.tsx |
import { Suspense, useMemo, useRef } from "react";
import { Canvas, useFrame, useThree } from "@react-three/fiber";
import { Float } from "@react-three/drei";
import { EffectComposer, Bloom } from "@react-three/postprocessing";
import * as THREE from "three";
/** Posisi partikel dalam bola (halo). */
function makeSpherePositions(count: number, rMin = 3.2, rMax = 7): Float32Array {
const arr = new Float32Array(count * 3);
for (let i = 0; i < count; i++) {
const r = rMin + Math.random() * (rMax - rMin);
const theta = Math.random() * Math.PI * 2;
const phi = Math.acos(2 * Math.random() - 1);
arr[i * 3] = r * Math.sin(phi) * Math.cos(theta);
arr[i * 3 + 1] = r * Math.sin(phi) * Math.sin(theta);
arr[i * 3 + 2] = r * Math.cos(phi);
}
return arr;
}
/** Kamera mengikuti kursor dengan halus (parallax). */
function CameraRig() {
const { camera, pointer } = useThree();
useFrame((_, delta) => {
const k = Math.min(1, delta * 2.2);
camera.position.x = THREE.MathUtils.lerp(camera.position.x, pointer.x * 0.9, k);
camera.position.y = THREE.MathUtils.lerp(camera.position.y, pointer.y * 0.6, k);
camera.lookAt(0, 0, 0);
});
return null;
}
/** Partikel melayang — rotasi sangat lambat. */
function Particles({ positions }: { positions: Float32Array }) {
const ref = useRef<THREE.Points>(null);
useFrame((_, delta) => {
if (!ref.current) return;
ref.current.rotation.y += delta * 0.028;
ref.current.rotation.x += delta * 0.008;
});
return (
<points ref={ref} frustumCulled={false}>
<bufferGeometry>
<bufferAttribute attach="attributes-position" args={[positions, 3]} />
</bufferGeometry>
<pointsMaterial
size={0.05}
color="#60a5fa"
transparent
opacity={0.5}
sizeAttenuation
depthWrite={false}
/>
</points>
);
}
/** Jaringan garis tipis antar partikel terdekat (kesan "neural network"). */
function Constellation({ positions }: { positions: Float32Array }) {
const pairs = useMemo(() => {
// Batasi jumlah partikel agar pasangan garis wajar (O(n²) sekali di mount).
const n = Math.min(200, positions.length / 3);
const threshold = 1.05;
const out: number[] = [];
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const dx = positions[i * 3] - positions[j * 3];
const dy = positions[i * 3 + 1] - positions[j * 3 + 1];
const dz = positions[i * 3 + 2] - positions[j * 3 + 2];
if (dx * dx + dy * dy + dz * dz < threshold * threshold) {
out.push(
positions[i * 3], positions[i * 3 + 1], positions[i * 3 + 2],
positions[j * 3], positions[j * 3 + 1], positions[j * 3 + 2],
);
}
}
}
return new Float32Array(out);
}, [positions]);
if (pairs.length === 0) return null;
return (
<lineSegments frustumCulled={false}>
<bufferGeometry>
<bufferAttribute attach="attributes-position" args={[pairs, 3]} />
</bufferGeometry>
<lineBasicMaterial color="#3b82f6" transparent opacity={0.12} depthWrite={false} />
</lineSegments>
);
}
/** Inti: torus knot wireframe — melayang, berputar pelan, sedikit bernapas. */
function CoreOrb() {
const ref = useRef<THREE.Mesh>(null);
useFrame(({ clock }, delta) => {
if (!ref.current) return;
ref.current.rotation.x += delta * 0.08;
ref.current.rotation.y += delta * 0.12;
const pulse = 1 + Math.sin(clock.elapsedTime * 0.9) * 0.035;
ref.current.scale.setScalar(pulse);
});
return (
<Float speed={1.2} rotationIntensity={0.5} floatIntensity={0.9}>
<mesh ref={ref}>
<torusKnotGeometry args={[1.2, 0.32, 180, 28]} />
<meshBasicMaterial color="#3b82f6" wireframe transparent opacity={0.38} />
</mesh>
</Float>
);
}
/** Bola menyala di pusat (berdenyut). */
function PulsingCore() {
const mat = useRef<THREE.MeshBasicMaterial>(null);
useFrame(({ clock }) => {
if (!mat.current) return;
mat.current.opacity = 0.5 + Math.sin(clock.elapsedTime * 1.1) * 0.15;
});
return (
<mesh>
<sphereGeometry args={[0.45, 32, 32]} />
<meshBasicMaterial ref={mat} color="#60a5fa" transparent opacity={0.5} />
</mesh>
);
}
/** Cincin orbit transparan. */
function OrbitRing({ radius, tilt, color }: { radius: number; tilt: number; color: string }) {
const line = useMemo(() => {
const seg = 160;
const pts: THREE.Vector3[] = [];
for (let i = 0; i <= seg; i++) {
const a = (i / seg) * Math.PI * 2;
pts.push(new THREE.Vector3(Math.cos(a) * radius, 0, Math.sin(a) * radius));
}
const geo = new THREE.BufferGeometry().setFromPoints(pts);
const mat = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.16,
depthWrite: false,
});
return new THREE.Line(geo, mat);
}, [radius, color]);
return (
<group rotation={[tilt, 0, 0]}>
<primitive object={line} />
</group>
);
}
/** Node kecil yang bergerak di atas cincin orbit. */
function OrbitingNodes({ count = 6 }: { count?: number }) {
const group = useRef<THREE.Group>(null);
useFrame((_, delta) => {
if (!group.current) return;
group.current.rotation.y += delta * 0.3;
});
const nodes = useMemo(
() =>
Array.from({ length: count }, (_, i) => {
const angle = (i / count) * Math.PI * 2;
return {
x: Math.cos(angle) * 2.85,
y: (i % 2 === 0 ? 0.28 : -0.28),
z: Math.sin(angle) * 2.85,
};
}),
[count],
);
return (
<group rotation={[0.4, 0, 0]} ref={group}>
{nodes.map((n, i) => (
<mesh key={i} position={[n.x, n.y, n.z]}>
<sphereGeometry args={[0.07, 16, 16]} />
<meshBasicMaterial color={i % 2 === 0 ? "#60a5fa" : "#93c5fd"} />
</mesh>
))}
</group>
);
}
/** Scene 3D latar halaman auth (kiri). `pointer-events-none` agar tidak menghalangi UI. */
export default function Auth3DScene() {
const particlePos = useMemo(() => makeSpherePositions(900), []);
return (
<div className="pointer-events-none absolute inset-0">
<Canvas
dpr={[1, 1.5]}
camera={{ position: [0, 0, 7], fov: 45 }}
gl={{ antialias: true, alpha: true }}
style={{ background: "transparent" }}
>
<ambientLight intensity={0.55} />
<pointLight position={[5, 5, 5]} intensity={1.3} color="#3b82f6" />
<pointLight position={[-5, -3, 3]} intensity={0.6} color="#60a5fa" />
<CameraRig />
<Suspense fallback={null}>
<EffectComposer>
<Bloom intensity={0.75} luminanceThreshold={0.25} luminanceSmoothing={0.9} mipmapBlur />
</EffectComposer>
</Suspense>
<Particles positions={particlePos} />
<Constellation positions={particlePos} />
{/* Inti visual digeser ke kiri; partikel tetap memenuhi seluruh layar */}
<group position={[-1.6, 0, 0]}>
<OrbitRing radius={2.85} tilt={0.4} color="#3b82f6" />
<OrbitRing radius={3.75} tilt={-0.5} color="#60a5fa" />
<CoreOrb />
<PulsingCore />
<OrbitingNodes />
</group>
</Canvas>
</div>
);
}