File size: 1,375 Bytes
0ffc500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React from 'react';
import * as THREE from 'three';

const StarField = ({ count = 1000 }) => {
  const starRef = React.useRef();
  
  React.useEffect(() => {
    const geometry = new THREE.BufferGeometry();
    const positions = [];
    const colors = [];
    
    const color = new THREE.Color();
    
    for (let i = 0; i < count; i++) {
      // Random position in a large cube
      const x = (Math.random() - 0.5) * 2000;
      const y = (Math.random() - 0.5) * 2000;
      const z = (Math.random() - 0.5) * 2000;
      
      positions.push(x, y, z);
      
      // Random colors for stars
      color.setHSL(Math.random(), 0.8, Math.random() * 0.5 + 0.5);
      colors.push(color.r, color.g, color.b);
    }
    
    geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
    geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
    
    const material = new THREE.PointsMaterial({
      size: 2,
      vertexColors: true,
      transparent: true,
      opacity: 0.8,
    });
    
    const stars = new THREE.Points(geometry, material);
    starRef.current = stars;
    
    return () => {
      if (starRef.current) {
        starRef.current.geometry.dispose();
        starRef.current.material.dispose();
      }
    };
  }, [count]);
  
  return <primitive ref={starRef} />;
};

export default StarField;