Fireflies.jsx
This file
Path
examples/demo/src/components/Fireflies.jsx
67 lines · 1.7 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than f85898f point at it.
chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f
lines 1–67why, one step back1import { Vector3, CatmullRomCurve3 } from 'three'2import { useRef, useMemo } from 'react'3import { extend, useFrame } from '@react-three/fiber'4import * as meshline from 'meshline'56extend(meshline)78const r = () => Math.max(0.2, Math.random())910function Fatline({ curve, color }) {11 const material = useRef()12 useFrame(13 (state, delta) =>14 (material.current.uniforms.dashOffset.value -= delta / 100),15 )16 return (17 <mesh>18 <meshLineGeometry points={curve} />19 <meshLineMaterial20 ref={material}21 transparent22 lineWidth={0.01}23 color={color}24 dashArray={0.1}25 dashRatio={0.99}26 />27 </mesh>28 )29}3031export default function Fireflies({ count, colors, radius = 10 }) {32 const lines = useMemo(33 () =>34 new Array(count).fill().map(() => {35 const pos = new Vector3(36 Math.sin(0) * radius * r(),37 Math.cos(0) * radius * r(),38 0,39 )40 const points = new Array(30).fill().map((_, index) => {41 const angle = (index / 20) * Math.PI * 242 return pos43 .add(44 new Vector3(45 Math.sin(angle) * radius * r(),46 Math.cos(angle) * radius * r(),47 0,48 ),49 )50 .clone()51 })52 const curve = new CatmullRomCurve3(points).getPoints(100)53 return {54 color: colors[parseInt(colors.length * Math.random())],55 curve,56 }57 }),58 [count, radius, colors],59 )60 return (61 <group position={[-radius * 2, -radius, 0]}>62 {lines.map((props, index) => (63 <Fatline key={index} {...props} />64 ))}65 </group>66 )67}