import React, { useEffect, useRef } from 'react'; import * as THREE from 'three'; interface ThreeAwakenedMatrixProps { statusText?: string; tokensCount?: number; className?: string; } export const ThreeAwakenedMatrix: React.FC = ({ statusText = 'RiShre Awakened is synthesizing...', tokensCount = 0, className = '', }) => { const mountRef = useRef(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 (
{statusText}
{tokensCount > 0 && ( Synthesized {tokensCount} tokens )}
); };