Awakened_RiShre / src /components /ThreeAwakenedMatrix.tsx
usrrootkali's picture
Upload 34 files
ffe328b verified
Raw History Blame Contribute Delete
6.29 kB
import React, { useEffect, useRef } from 'react';
import * as THREE from 'three';
interface ThreeAwakenedMatrixProps {
statusText?: string;
tokensCount?: number;
className?: string;
}
export const ThreeAwakenedMatrix: React.FC<ThreeAwakenedMatrixProps> = ({
statusText = 'RiShre Awakened is synthesizing...',
tokensCount = 0,
className = '',
}) => {
const mountRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const container = mountRef.current;
if (!container) return;
const isSoftwareGl = (() => {
try {
const gl = document.createElement('canvas').getContext('webgl2');
if (!gl) return true;
const ext = gl.getExtension('WEBGL_debug_renderer_info');
const param = ext ? ext.UNMASKED_RENDERER_WEBGL : gl.RENDERER;
const gpu = String(gl.getParameter(param));
gl.getExtension('WEBGL_lose_context')?.loseContext();
return /SwiftShader|llvmpipe|softpipe|Software|Basic Render/i.test(gpu);
} catch (e) {
return true;
}
})();
const renderer = new THREE.WebGLRenderer({
antialias: !isSoftwareGl,
powerPreference: 'high-performance',
alpha: true,
});
renderer.setPixelRatio(isSoftwareGl ? 1 : Math.min(window.devicePixelRatio, 1.5));
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(50, container.clientWidth / container.clientHeight, 0.1, 50);
camera.position.set(0, 0, 4.2);
const group = new THREE.Group();
scene.add(group);
// Wireframe Box (Amethyst Violet)
const boxGeo = new THREE.BoxGeometry(1.4, 1.4, 1.4);
const boxMat = new THREE.MeshBasicMaterial({
color: 0xa855f7,
wireframe: true,
transparent: true,
opacity: 0.85,
});
const boxMesh = new THREE.Mesh(boxGeo, boxMat);
group.add(boxMesh);
// Inner Octahedron (Fuchsia Neon)
const octaGeo = new THREE.OctahedronGeometry(0.8, 0);
const octaMat = new THREE.MeshBasicMaterial({
color: 0xf472b6,
wireframe: true,
transparent: true,
opacity: 0.95,
});
const octaMesh = new THREE.Mesh(octaGeo, octaMat);
group.add(octaMesh);
// Core Sphere (White Starlight)
const sphereGeo = new THREE.SphereGeometry(0.3, 12, 12);
const sphereMat = new THREE.MeshBasicMaterial({
color: 0xffffff,
wireframe: false,
});
const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
group.add(sphereMesh);
// Floating data particles
const particleCount = isSoftwareGl ? 35 : 70;
const pGeo = new THREE.BufferGeometry();
const positions = new Float32Array(particleCount * 3);
for (let i = 0; i < particleCount * 3; i += 3) {
positions[i] = (Math.random() - 0.5) * 3;
positions[i + 1] = (Math.random() - 0.5) * 3;
positions[i + 2] = (Math.random() - 0.5) * 3;
}
pGeo.setAttribute('position', new THREE.BufferAttribute(positions, 3));
const pMat = new THREE.PointsMaterial({
color: 0xe879f9,
size: 0.055,
transparent: true,
opacity: 0.85,
});
const particles = new THREE.Points(pGeo, pMat);
group.add(particles);
let frameId = 0;
const clock = new THREE.Clock();
function afterDraw(cb: () => void) {
try {
const gl = renderer.getContext() as WebGL2RenderingContext;
const sync = gl.fenceSync(gl.SYNC_GPU_COMMANDS_COMPLETE, 0);
gl.flush();
const poll = () => {
const state = sync && gl.getSyncParameter(sync, gl.SYNC_STATUS);
if (state === gl.UNSIGNALED) {
frameId = window.setTimeout(poll, 5);
} else {
if (sync) gl.deleteSync(sync);
frameId = window.setTimeout(cb, 60);
}
};
frameId = window.setTimeout(poll, 5);
} catch (e) {
frameId = window.setTimeout(cb, 60);
}
}
const tick = () => {
const delta = clock.getDelta();
const time = clock.getElapsedTime();
boxMesh.rotation.x += 1.0 * delta;
boxMesh.rotation.y += 1.2 * delta;
octaMesh.rotation.y -= 1.4 * delta;
octaMesh.rotation.z += 1.0 * delta;
const scale = 1 + Math.sin(time * 6) * 0.08;
sphereMesh.scale.set(scale, scale, scale);
group.rotation.z = Math.sin(time) * 0.2;
renderer.render(scene, camera);
if (isSoftwareGl) {
afterDraw(tick);
} else {
frameId = requestAnimationFrame(tick);
}
};
tick();
const onResize = () => {
if (!container) return;
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
};
window.addEventListener('resize', onResize);
return () => {
cancelAnimationFrame(frameId);
clearTimeout(frameId);
window.removeEventListener('resize', onResize);
boxGeo.dispose();
boxMat.dispose();
octaGeo.dispose();
octaMat.dispose();
sphereGeo.dispose();
sphereMat.dispose();
pGeo.dispose();
pMat.dispose();
renderer.dispose();
if (renderer.domElement && renderer.domElement.parentNode) {
renderer.domElement.parentNode.removeChild(renderer.domElement);
}
};
}, []);
return (
<div className={`relative flex flex-col items-center justify-center p-4 bg-[#110724]/90 border border-purple-500/40 rounded-3xl backdrop-blur-2xl shadow-[0_0_40px_rgba(168,85,247,0.3)] ${className}`}>
<div ref={mountRef} className="w-44 h-44 my-1" />
<div className="flex flex-col items-center text-center mt-2">
<div className="flex items-center gap-2">
<span className="w-2.5 h-2.5 rounded-full bg-purple-400 shadow-[0_0_10px_#c084fc]"></span>
<span className="text-xs font-mono font-bold tracking-wider text-purple-200 uppercase">
{statusText}
</span>
</div>
{tokensCount > 0 && (
<span className="text-[11px] font-mono text-purple-300/80 mt-1">
Synthesized {tokensCount} tokens
</span>
)}
</div>
</div>
);
};