|
| 1 | +import React, { useMemo, useRef, useState, useEffect } from "react" |
| 2 | +import { useThree, useFrame } from "@react-three/fiber" |
| 3 | +import * as THREE from "three" |
| 4 | +import { BigCoords, downscaleCoords } from "../libraries/Constructs.js" |
| 5 | +import { UNIVERSE_SIZE_HALF, UNIVERSE_DOWNSCALE, UNIVERSE_SIZE } from "../libraries/Cyberspace.js" |
| 6 | +import { invert } from "three/examples/jsm/nodes/Nodes.js" |
| 7 | + |
| 8 | +const INTERACTION_RESET_DELAY = 5_000 |
| 9 | + |
| 10 | +// const LOGO_TEAL = 0x06a4a4 |
| 11 | +const LOGO_PURPLE = 0x78004e |
| 12 | +const LOGO_BLUE = 0x0062cd |
| 13 | + |
| 14 | +// const TealLineMaterial = new THREE.LineBasicMaterial({ |
| 15 | +// color: LOGO_TEAL, |
| 16 | +// }) |
| 17 | +const PurpleLineMaterial = new THREE.LineBasicMaterial({ |
| 18 | + color: LOGO_PURPLE, |
| 19 | +}) |
| 20 | + |
| 21 | +const BlueLineMaterial = new THREE.LineBasicMaterial({ |
| 22 | + color: LOGO_BLUE, |
| 23 | +}) |
| 24 | + |
| 25 | +const SunMaterial = new THREE.MeshBasicMaterial({ |
| 26 | + color: 0x2b0c40, |
| 27 | + side: THREE.DoubleSide, |
| 28 | +}) |
| 29 | + |
| 30 | +interface CyberspaceProps { |
| 31 | + targetSize: number, // size of construct to determine camera orbit radius |
| 32 | + targetCoord: BigCoords, // for camera orbit |
| 33 | + children: React.ReactNode, |
| 34 | +} |
| 35 | + |
| 36 | +const centerVec = new THREE.Vector3(UNIVERSE_SIZE_HALF, UNIVERSE_SIZE_HALF, UNIVERSE_SIZE_HALF) // The center of cyberspace |
| 37 | + |
| 38 | +export const Cyberspace: React.FC<CyberspaceProps> = ({ targetSize, targetCoord, children }) => { |
| 39 | + const groupRef = useRef<THREE.Group>(null) |
| 40 | + const [interactionActive, setInteractionActive] = useState(false) |
| 41 | + const [defaultView, setDefaultView] = useState(true) |
| 42 | + const defaultViewTimeoutRef = useRef<NodeJS.Timeout|undefined>(undefined) |
| 43 | + const [elapsedTime, setElapsedTime] = useState(0) |
| 44 | + const [lerpAlpha, setLerpAlpha] = useState(1) |
| 45 | + const { camera } = useThree() |
| 46 | + |
| 47 | + const targetPosition = centerVec |
| 48 | + const downscaledTargetCoord = downscaleCoords(targetCoord, UNIVERSE_DOWNSCALE) |
| 49 | + // const radius = targetSize * 10 // The radius of the circular path the camera will follow |
| 50 | + |
| 51 | +const minSize = 1 |
| 52 | +const maxSize = 2**50/256/256 |
| 53 | + |
| 54 | +// const ratio = ( targetSize + (maxSize * 0.1)) / maxSize |
| 55 | +const ratio = targetSize / maxSize |
| 56 | + |
| 57 | +// const radius = 100 * Math.pow(targetSize, ratio) + targetSize |
| 58 | +// const radius = 100 + targetSize * (1 + ratio) |
| 59 | + const invertRatioLimit = Math.max(1-ratio, minSize/maxSize) |
| 60 | + |
| 61 | + const radius = Math.max(100, targetSize + targetSize * invertRatioLimit) |
| 62 | + |
| 63 | + // Attach pointerdown and pointerup event listeners |
| 64 | + useEffect(() => { |
| 65 | + const handleInteractionStart = () => { |
| 66 | + setLerpAlpha(0.05) // permanent |
| 67 | + clearTimeout(defaultViewTimeoutRef.current) |
| 68 | + setInteractionActive(true) |
| 69 | + setDefaultView(false) |
| 70 | + } |
| 71 | + const handleInteractionEnd = () => { |
| 72 | + setInteractionActive(false) |
| 73 | + defaultViewTimeoutRef.current = setTimeout(() => { |
| 74 | + // LERP camera back to default orbit |
| 75 | + setDefaultView(true) |
| 76 | + }, INTERACTION_RESET_DELAY) |
| 77 | + } |
| 78 | + |
| 79 | + window.addEventListener('pointerdown', handleInteractionStart) |
| 80 | + window.addEventListener('pointerup', handleInteractionEnd) |
| 81 | + window.addEventListener('wheel', handleInteractionStart) |
| 82 | + |
| 83 | + // Cleanup event listeners on unmount |
| 84 | + return () => { |
| 85 | + window.removeEventListener('pointerdown', handleInteractionStart) |
| 86 | + window.removeEventListener('pointerup', handleInteractionEnd) |
| 87 | + window.addEventListener('wheel', handleInteractionStart) |
| 88 | + } |
| 89 | + }, []) |
| 90 | + |
| 91 | + // Compute the lines and grids only once |
| 92 | + const { grids, blacksun } = useMemo(() => { |
| 93 | + const grids = [ |
| 94 | + <gridHelper |
| 95 | + key="y+" |
| 96 | + args={[UNIVERSE_SIZE, 32]} |
| 97 | + position={[UNIVERSE_SIZE_HALF, UNIVERSE_SIZE, UNIVERSE_SIZE_HALF]} |
| 98 | + material={BlueLineMaterial} |
| 99 | + renderOrder={1} |
| 100 | + />, |
| 101 | + <gridHelper |
| 102 | + key="y-" |
| 103 | + args={[UNIVERSE_SIZE, 32]} |
| 104 | + position={[UNIVERSE_SIZE_HALF, 0, UNIVERSE_SIZE_HALF]} |
| 105 | + material={PurpleLineMaterial} |
| 106 | + renderOrder={1} |
| 107 | + />, |
| 108 | + ] |
| 109 | + |
| 110 | + const blacksun = ( |
| 111 | + <mesh geometry={new THREE.CircleGeometry(UNIVERSE_SIZE_HALF/2, 64)} material={SunMaterial} position={[UNIVERSE_SIZE_HALF,UNIVERSE_SIZE_HALF,-UNIVERSE_SIZE_HALF]} renderOrder={-1}/> |
| 112 | + ) |
| 113 | + |
| 114 | + return { grids, blacksun } |
| 115 | + }, []) |
| 116 | + |
| 117 | + useFrame(({ clock }) => { |
| 118 | + if (defaultView) { |
| 119 | + if (!clock.running) clock.start() |
| 120 | + const angle = (clock.elapsedTime + elapsedTime) * 0.2 // Controls the speed of rotation |
| 121 | + targetPosition.set( |
| 122 | + downscaledTargetCoord.x + radius * Math.sin(angle), |
| 123 | + downscaledTargetCoord.y + radius/5, |
| 124 | + downscaledTargetCoord.z + radius * Math.cos(angle) |
| 125 | + ) |
| 126 | + camera.position.lerp(targetPosition, lerpAlpha) |
| 127 | + } else { |
| 128 | + if (clock.running) { |
| 129 | + setElapsedTime(clock.elapsedTime + elapsedTime) |
| 130 | + clock.stop() |
| 131 | + } |
| 132 | + } |
| 133 | + }) |
| 134 | + |
| 135 | + // if (groupRef.current) { |
| 136 | + // groupRef.current.scale.set(scale, scale, scale) |
| 137 | + // } |
| 138 | + |
| 139 | + return ( |
| 140 | + <group ref={groupRef}> |
| 141 | + {blacksun} |
| 142 | + {grids} |
| 143 | + {children} |
| 144 | + </group> |
| 145 | + ) |
| 146 | +} |
0 commit comments