feat(ui): implement home page and ai chat input with voice orb
Implement the core user interface components including the home page, a sophisticated AI chat input component, and a 3D voice visualization service using Three.js. - Add `HomePage` component with responsive layout and theme integration. - Add `AiChatInputComponent` supporting text, file attachments, and voice mode transitions. - Implement `VoiceOrbService` using Three.js to provide a reactive 3D animated orb for voice interaction. - Update routing to include the new home page. - Refactor `SettingsPage` to clean up unused RTL logic and improve UI styling. - Adjust Angular build budgets in `angular.json` to accommodate increased bundle size from Three.js.
This commit is contained in:
@@ -0,0 +1,295 @@
|
||||
import { Injectable, NgZone, inject } from '@angular/core';
|
||||
import * as THREE from 'three';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class VoiceOrbService {
|
||||
private scene: THREE.Scene | null = null;
|
||||
private camera: THREE.PerspectiveCamera | null = null;
|
||||
private renderer: THREE.WebGLRenderer | null = null;
|
||||
private orb: THREE.Mesh | null = null;
|
||||
private orbGeometry: THREE.IcosahedronGeometry | null = null;
|
||||
private glowShell: THREE.Mesh | null = null;
|
||||
private originalPositions: Float32Array | null = null;
|
||||
private animationId: number | null = null;
|
||||
private audioContext: AudioContext | null = null;
|
||||
private analyser: AnalyserNode | null = null;
|
||||
private mediaStream: MediaStream | null = null;
|
||||
private audioDataArray: Uint8Array | null = null;
|
||||
private isListening = false;
|
||||
private readonly reducedMotion: boolean;
|
||||
|
||||
constructor(private readonly ngZone: NgZone) {
|
||||
this.reducedMotion = typeof window !== 'undefined'
|
||||
&& window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||||
}
|
||||
|
||||
init(container: HTMLElement): void {
|
||||
if (this.scene) {
|
||||
if (this.renderer && this.renderer.domElement.parentNode !== container) {
|
||||
container.appendChild(this.renderer.domElement);
|
||||
}
|
||||
this.resize(container);
|
||||
return;
|
||||
}
|
||||
|
||||
const width = container.clientWidth || 280;
|
||||
const height = container.clientHeight || 280;
|
||||
|
||||
this.scene = new THREE.Scene();
|
||||
|
||||
this.camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100);
|
||||
this.camera.position.z = 4;
|
||||
|
||||
this.renderer = new THREE.WebGLRenderer({
|
||||
antialias: true,
|
||||
alpha: true,
|
||||
powerPreference: 'high-performance',
|
||||
});
|
||||
this.renderer.setSize(width, height);
|
||||
this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
|
||||
this.renderer.setClearColor(0x000000, 0);
|
||||
container.appendChild(this.renderer.domElement);
|
||||
|
||||
const ambientLight = new THREE.AmbientLight(0x404060, 2);
|
||||
this.scene.add(ambientLight);
|
||||
|
||||
const pointLight1 = new THREE.PointLight(0x3b82f6, 12, 10);
|
||||
pointLight1.position.set(0, 0, 3);
|
||||
this.scene.add(pointLight1);
|
||||
|
||||
const pointLight2 = new THREE.PointLight(0x8b5cf6, 6, 10);
|
||||
pointLight2.position.set(-2, 1, 2);
|
||||
this.scene.add(pointLight2);
|
||||
|
||||
const pointLight3 = new THREE.PointLight(0x06b6d4, 4, 10);
|
||||
pointLight3.position.set(2, -1, 2);
|
||||
this.scene.add(pointLight3);
|
||||
|
||||
this.orbGeometry = new THREE.IcosahedronGeometry(1, 48);
|
||||
const orbMaterial = new THREE.MeshPhysicalMaterial({
|
||||
color: 0x60a5fa,
|
||||
emissive: 0x1e3a5f,
|
||||
emissiveIntensity: 0.35,
|
||||
roughness: 0.12,
|
||||
metalness: 0.05,
|
||||
clearcoat: 1,
|
||||
clearcoatRoughness: 0.08,
|
||||
});
|
||||
this.orb = new THREE.Mesh(this.orbGeometry, orbMaterial);
|
||||
this.scene.add(this.orb);
|
||||
|
||||
this.originalPositions = new Float32Array(this.orbGeometry.attributes['position'].array);
|
||||
|
||||
const glowGeometry = new THREE.IcosahedronGeometry(1.18, 32);
|
||||
const glowMaterial = new THREE.MeshBasicMaterial({
|
||||
color: 0x3b82f6,
|
||||
transparent: true,
|
||||
opacity: 0.07,
|
||||
blending: THREE.AdditiveBlending,
|
||||
side: THREE.BackSide,
|
||||
depthWrite: false,
|
||||
});
|
||||
this.glowShell = new THREE.Mesh(glowGeometry, glowMaterial);
|
||||
this.scene.add(this.glowShell);
|
||||
|
||||
this.setupResizeObserver(container);
|
||||
}
|
||||
|
||||
private setupResizeObserver(container: HTMLElement): void {
|
||||
if (typeof ResizeObserver === 'undefined') return;
|
||||
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
for (const entry of entries) {
|
||||
const { width, height } = entry.contentRect;
|
||||
if (width > 0 && height > 0 && this.renderer && this.camera) {
|
||||
this.renderer.setSize(width, height);
|
||||
this.camera.aspect = width / height;
|
||||
this.camera.updateProjectionMatrix();
|
||||
}
|
||||
}
|
||||
});
|
||||
observer.observe(container);
|
||||
}
|
||||
|
||||
startAnimation(): void {
|
||||
if (this.animationId !== null) return;
|
||||
|
||||
this.ngZone.runOutsideAngular(() => {
|
||||
const animate = (): void => {
|
||||
this.animationId = requestAnimationFrame(animate);
|
||||
this.renderFrame();
|
||||
};
|
||||
animate();
|
||||
});
|
||||
}
|
||||
|
||||
stopAnimation(): void {
|
||||
if (this.animationId !== null) {
|
||||
cancelAnimationFrame(this.animationId);
|
||||
this.animationId = null;
|
||||
}
|
||||
}
|
||||
|
||||
private renderFrame(): void {
|
||||
if (!this.scene || !this.camera || !this.renderer || !this.orb || !this.orbGeometry) {
|
||||
return;
|
||||
}
|
||||
|
||||
const time = performance.now() * 0.001;
|
||||
const audioLevel = this.getAudioLevel();
|
||||
const speed = this.reducedMotion ? 0.35 : 1;
|
||||
const t = time * speed;
|
||||
|
||||
this.updateOrbGeometry(t, audioLevel);
|
||||
|
||||
if (this.glowShell) {
|
||||
const glowOpacity = 0.05 + audioLevel * 0.18;
|
||||
const glowScale = 1 + Math.sin(t * 1.5) * 0.025 + audioLevel * 0.06;
|
||||
this.glowShell.scale.setScalar(glowScale);
|
||||
if (this.glowShell.material instanceof THREE.MeshBasicMaterial) {
|
||||
this.glowShell.material.opacity = glowOpacity;
|
||||
}
|
||||
}
|
||||
|
||||
const pulse = 1 + Math.sin(t * 2) * 0.018 + audioLevel * 0.07;
|
||||
this.orb.scale.setScalar(pulse);
|
||||
|
||||
this.orb.rotation.y = t * 0.08;
|
||||
this.orb.rotation.x = Math.sin(t * 0.15) * 0.1;
|
||||
|
||||
this.renderer.render(this.scene, this.camera);
|
||||
}
|
||||
|
||||
private updateOrbGeometry(time: number, audioLevel: number): void {
|
||||
if (!this.orbGeometry || !this.originalPositions) return;
|
||||
|
||||
const positions = this.orbGeometry.attributes['position'];
|
||||
const original = this.originalPositions;
|
||||
|
||||
for (let i = 0; i < positions.count; i++) {
|
||||
const ox = original[i * 3];
|
||||
const oy = original[i * 3 + 1];
|
||||
const oz = original[i * 3 + 2];
|
||||
|
||||
const n1 =
|
||||
Math.sin(ox * 3 + time * 1.2) *
|
||||
Math.cos(oy * 3 + time * 0.8) *
|
||||
Math.sin(oz * 3 + time * 1.5) *
|
||||
0.06;
|
||||
|
||||
const n2 =
|
||||
Math.sin(ox * 6 + time * 2.3) *
|
||||
Math.cos(oy * 6 + time * 1.7) *
|
||||
Math.sin(oz * 6 + time * 2.1) *
|
||||
0.02;
|
||||
|
||||
const n3 =
|
||||
Math.cos(ox * 2 + time * 0.6) *
|
||||
Math.sin(oy * 2 + time * 1.1) *
|
||||
0.015;
|
||||
|
||||
const displacement = 1 + n1 + n2 + n3 + audioLevel * 0.12;
|
||||
|
||||
positions.setXYZ(i, ox * displacement, oy * displacement, oz * displacement);
|
||||
}
|
||||
|
||||
positions.needsUpdate = true;
|
||||
this.orbGeometry.computeVertexNormals();
|
||||
}
|
||||
|
||||
resetOrbGeometry(): void {
|
||||
if (!this.orbGeometry || !this.originalPositions) return;
|
||||
const positions = this.orbGeometry.attributes['position'];
|
||||
const original = this.originalPositions;
|
||||
for (let i = 0; i < positions.count; i++) {
|
||||
positions.setXYZ(i, original[i * 3], original[i * 3 + 1], original[i * 3 + 2]);
|
||||
}
|
||||
positions.needsUpdate = true;
|
||||
this.orbGeometry.computeVertexNormals();
|
||||
}
|
||||
|
||||
resize(container: HTMLElement): void {
|
||||
if (!this.renderer || !this.camera) return;
|
||||
const width = container.clientWidth || 280;
|
||||
const height = container.clientHeight || 280;
|
||||
this.renderer.setSize(width, height);
|
||||
this.camera.aspect = width / height;
|
||||
this.camera.updateProjectionMatrix();
|
||||
}
|
||||
|
||||
async startListening(): Promise<void> {
|
||||
if (this.isListening) return;
|
||||
|
||||
try {
|
||||
this.mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
this.audioContext = new AudioContext();
|
||||
const source = this.audioContext.createMediaStreamSource(this.mediaStream);
|
||||
this.analyser = this.audioContext.createAnalyser();
|
||||
this.analyser.fftSize = 256;
|
||||
this.analyser.smoothingTimeConstant = 0.75;
|
||||
source.connect(this.analyser);
|
||||
this.audioDataArray = new Uint8Array(this.analyser.frequencyBinCount);
|
||||
this.isListening = true;
|
||||
} catch (err) {
|
||||
console.warn('VoiceOrbService: unable to access microphone.', err);
|
||||
this.isListening = false;
|
||||
}
|
||||
}
|
||||
|
||||
stopListening(): void {
|
||||
if (this.mediaStream) {
|
||||
this.mediaStream.getTracks().forEach((track) => track.stop());
|
||||
this.mediaStream = null;
|
||||
}
|
||||
if (this.audioContext && this.audioContext.state !== 'closed') {
|
||||
this.audioContext.close().catch(() => {});
|
||||
}
|
||||
this.audioContext = null;
|
||||
this.analyser = null;
|
||||
this.audioDataArray = null;
|
||||
this.isListening = false;
|
||||
}
|
||||
|
||||
private getAudioLevel(): number {
|
||||
if (!this.isListening || !this.analyser || !this.audioDataArray) return 0;
|
||||
this.analyser.getByteFrequencyData(this.audioDataArray as Uint8Array<ArrayBuffer>);
|
||||
let sum = 0;
|
||||
for (let i = 0; i < this.audioDataArray.length; i++) {
|
||||
sum += this.audioDataArray[i];
|
||||
}
|
||||
return Math.min(1, sum / this.audioDataArray.length / 200);
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.stopAnimation();
|
||||
this.stopListening();
|
||||
|
||||
if (this.orbGeometry) this.orbGeometry.dispose();
|
||||
if (this.orb && this.orb.material instanceof THREE.Material) {
|
||||
this.orb.material.dispose();
|
||||
}
|
||||
if (this.glowShell) {
|
||||
if (this.glowShell.material instanceof THREE.Material) {
|
||||
this.glowShell.material.dispose();
|
||||
}
|
||||
if (this.glowShell.geometry) {
|
||||
this.glowShell.geometry.dispose();
|
||||
}
|
||||
}
|
||||
if (this.renderer) {
|
||||
this.renderer.dispose();
|
||||
const canvas = this.renderer.domElement;
|
||||
if (canvas.parentNode) {
|
||||
canvas.parentNode.removeChild(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
this.scene = null;
|
||||
this.camera = null;
|
||||
this.renderer = null;
|
||||
this.orb = null;
|
||||
this.orbGeometry = null;
|
||||
this.glowShell = null;
|
||||
this.originalPositions = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user