CarbonO2 commited on
Commit
0ffc500
·
verified ·
1 Parent(s): d9194f8

Upload components/StarField.jsx with huggingface_hub

Browse files
Files changed (1) hide show
  1. components/StarField.jsx +51 -0
components/StarField.jsx ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import * as THREE from 'three';
3
+
4
+ const StarField = ({ count = 1000 }) => {
5
+ const starRef = React.useRef();
6
+
7
+ React.useEffect(() => {
8
+ const geometry = new THREE.BufferGeometry();
9
+ const positions = [];
10
+ const colors = [];
11
+
12
+ const color = new THREE.Color();
13
+
14
+ for (let i = 0; i < count; i++) {
15
+ // Random position in a large cube
16
+ const x = (Math.random() - 0.5) * 2000;
17
+ const y = (Math.random() - 0.5) * 2000;
18
+ const z = (Math.random() - 0.5) * 2000;
19
+
20
+ positions.push(x, y, z);
21
+
22
+ // Random colors for stars
23
+ color.setHSL(Math.random(), 0.8, Math.random() * 0.5 + 0.5);
24
+ colors.push(color.r, color.g, color.b);
25
+ }
26
+
27
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
28
+ geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
29
+
30
+ const material = new THREE.PointsMaterial({
31
+ size: 2,
32
+ vertexColors: true,
33
+ transparent: true,
34
+ opacity: 0.8,
35
+ });
36
+
37
+ const stars = new THREE.Points(geometry, material);
38
+ starRef.current = stars;
39
+
40
+ return () => {
41
+ if (starRef.current) {
42
+ starRef.current.geometry.dispose();
43
+ starRef.current.material.dispose();
44
+ }
45
+ };
46
+ }, [count]);
47
+
48
+ return <primitive ref={starRef} />;
49
+ };
50
+
51
+ export default StarField;