Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions src/components/GalaxyScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { type ComponentRef, type RefObject, useEffect, useMemo, useRef } from 'r
import * as THREE from 'three'
import type { ActivityBucket, ActivityKind } from '../activity-data'

type ActivityPoint = {
key: string
kind: ActivityKind
color: string
position: [number, number, number]
type ActivityPointPosition = [number, number, number]

type ActivityOrbit = {
bucket: ActivityBucket
pointPositions: ActivityPointPosition[]
}

type GalaxyProps = {
Expand All @@ -34,38 +34,40 @@ const INITIAL_CAMERA = {
const CANVAS_PIXEL_RATIO: [number, number] = [1, 1.75]
const CANVAS_BACKGROUND: [string] = ['#f8fbff']

const createActivityPoint = (bucket: ActivityBucket, bucketIndex: number, index: number): ActivityPoint => {
const createActivityPointPosition = (
bucket: ActivityBucket,
bucketIndex: number,
index: number,
): ActivityPointPosition => {
const angle = (index / bucket.count) * Math.PI * 2 + bucketIndex * 0.7
const spiral = bucket.radius + index * 0.006

return {
key: `${bucket.kind}-${index}`,
kind: bucket.kind,
color: bucket.color,
position: [
Math.cos(angle) * spiral,
Math.sin(index * 1.7) * 0.22 + bucketIndex * 0.08,
Math.sin(angle) * spiral,
],
}
return [
Math.cos(angle) * spiral,
Math.sin(index * 1.7) * 0.22 + bucketIndex * 0.08,
Math.sin(angle) * spiral,
]
}

const createActivityPoints = (buckets: readonly ActivityBucket[]) =>
buckets.flatMap((bucket, bucketIndex) =>
Array.from({ length: bucket.count }, (_, index) => createActivityPoint(bucket, bucketIndex, index)),
)
const createActivityOrbits = (buckets: readonly ActivityBucket[]): ActivityOrbit[] =>
buckets.map((bucket, bucketIndex) => ({
bucket,
pointPositions: Array.from({ length: bucket.count }, (_, index) =>
createActivityPointPosition(bucket, bucketIndex, index),
),
}))

function Galaxy({ buckets, selectedKind, isRotating }: GalaxyProps) {
const groupRef = useRef<THREE.Group>(null)
const activityPoints = useMemo(() => createActivityPoints(buckets), [buckets])
const activityOrbits = useMemo(() => createActivityOrbits(buckets), [buckets])

useFrame((_, delta) => {
if (groupRef.current && isRotating) groupRef.current.rotation.y += Math.min(delta, 0.1) * 0.08
})

return (
<group ref={groupRef}>
{buckets.map((bucket) => {
{activityOrbits.map(({ bucket }) => {
const isSelected = bucket.kind === selectedKind

return (
Expand All @@ -75,23 +77,23 @@ function Galaxy({ buckets, selectedKind, isRotating }: GalaxyProps) {
</mesh>
)
})}
{activityPoints.map((point) => {
const isSelected = point.kind === selectedKind
{activityOrbits.flatMap(({ bucket, pointPositions }) => {
const isSelected = bucket.kind === selectedKind

return (
<mesh key={point.key} position={point.position} scale={isSelected ? 1.38 : 0.82}>
return pointPositions.map((position, index) => (
<mesh key={`${bucket.kind}-${index}`} position={position} scale={isSelected ? 1.38 : 0.82}>
<sphereGeometry args={[0.042, 12, 12]} />
<meshStandardMaterial
color={point.color}
emissive={point.color}
color={bucket.color}
emissive={bucket.color}
emissiveIntensity={isSelected ? 0.78 : 0.32}
transparent
opacity={isSelected ? 1 : 0.56}
/>
</mesh>
)
))
})}
{buckets.map((bucket, index) => (
{activityOrbits.map(({ bucket }, index) => (
<Html key={bucket.kind} position={[bucket.radius + 0.2, 0.48 + index * 0.18, 0]}>
<span
className={`orbit-label${bucket.kind === selectedKind ? ' selected' : ''}`}
Expand Down
Loading