import { blueDark, orangeDark } from "@radix-ui/colors"; import { ContactShadows } from "@react-three/drei"; import { invalidate, useFrame } from "@react-three/fiber"; import { clamp, times } from "lodash-es"; import { Quicklime } from "quicklime"; import { useCallback, useEffect, useRef, useState, type RefObject, } from "react"; import { HemisphereLight, SpotLight, type Group } from "three"; import { degToRad, lerp } from "three/src/math/MathUtils.js"; import { Tankopedia } from "../stores/tankopedia"; import { TankopediaPersistent } from "../stores/tankopediaPersistent"; import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants"; const ANGLE = degToRad(15); const REVEAL_ANIMATION_TIME = 3; export const TRANSITION_ANIMATION_TIME = 0.5; const LIGHTS_COUNT = 3; const THETA_OFFSET = degToRad(180 - 45); const LIGHT_DISTANCE = 20; const LIGHT_HEIGHT_0 = 7; const LIGHT_HEIGHT_1 = 10; const INTENSITY_0 = 2 ** 6; const INTENSITY_1 = 2 ** 3; const HEMISPHERE_INTENSITY = 2 ** 0.7; const NON_PBR_FACTOR = 2; const SHADOW_RESOLUTION = 2 ** 9; const SHADOW_FOCUS = 2 ** 0; export const transitionEvent = new Quicklime(0); interface Props { hasPbr: boolean; // keeps the lights off; toggling fades them out/in hidden?: boolean; } export function Lighting({ hasPbr, hidden = false }: Props) { const wrapper = useRef(null); const highGraphics = TankopediaPersistent.use((state) => state.highGraphics); const requestedDisplay = Tankopedia.use( (state) => state.loaders.display.requested, ); const skinPending = Tankopedia.use( (state) => state.loaders.skin.requested !== state.loaders.skin.loaded || state.loaders.attachment.requested !== state.loaders.attachment.loaded, ); const animationTime = useRef(REVEAL_ANIMATION_TIME); const t0 = useRef(performance.now() / 1e3 - animationTime.current); // starts held at the fully-faded point when hidden so there's no fade-in const holding = useRef(hidden); const isRevealing = useRef(true); const isFirstReveal = useRef(true); const [animate, setAnimate] = useState(true); useEffect(() => { Tankopedia.mutate((draft) => { draft.showControls = true; draft.revealed = true; draft.loaders.model.loaded = true; }); }, []); useEffect(() => { if (isRevealing.current) { isRevealing.current = false; } else { animationTime.current = TRANSITION_ANIMATION_TIME; t0.current = performance.now() / 1e3; } setAnimate(true); }, [requestedDisplay]); useEffect(() => { if (isFirstReveal.current) { isFirstReveal.current = false; return; } if (hidden) { animationTime.current = TRANSITION_ANIMATION_TIME; t0.current = performance.now() / 1e3; holding.current = true; } else if (holding.current) { animationTime.current = REVEAL_ANIMATION_TIME; t0.current = performance.now() / 1e3 - animationTime.current; holding.current = false; } else { return; } setAnimate(true); invalidate(); }, [hidden]); useEffect(() => { if (!skinPending) return; animationTime.current = TRANSITION_ANIMATION_TIME; t0.current = performance.now() / 1e3; setAnimate(true); }, [skinPending]); return ( <> {animate && ( { setAnimate(false); }} /> )} {times(LIGHTS_COUNT, (index) => { const x = index / (LIGHTS_COUNT - 1); const theta = 2 * Math.PI * (index / LIGHTS_COUNT) + THETA_OFFSET; const position = [ -LIGHT_DISTANCE * Math.sin(theta), lerp(LIGHT_HEIGHT_0, LIGHT_HEIGHT_1, x), LIGHT_DISTANCE * Math.cos(theta), ] as const; const intensity = lerp(INTENSITY_0, INTENSITY_1, x); const light = useRef(null!); // useHelper(light, SpotLightHelper); return ( ); })} ); } interface AnimatorProps { stop: () => void; t0: RefObject; animationTime: RefObject; holding: RefObject; wrapper: RefObject; hasPbr: boolean; } function Animator({ stop, t0, animationTime, holding, wrapper, hasPbr, }: AnimatorProps) { const requestedDisplay = Tankopedia.use( (state) => state.loaders.display.requested, ); const display = Tankopedia.use((state) => state.loaders.display.loaded); const factor = !hasPbr && display !== TankopediaDisplay.StaticArmor ? NON_PBR_FACTOR : 1; useEffect(() => { invalidate(); }, [requestedDisplay === display]); const apply = useCallback((t: number) => { if (!wrapper.current) return; for (const child of wrapper.current.children) { if (child instanceof SpotLight) { child.angle = ANGLE * t * factor; child.intensity = child.userData.intensity; if (child.angle === 0) { child.intensity = 0; } } else if (child instanceof HemisphereLight) { child.intensity = HEMISPHERE_INTENSITY * t * factor; } } transitionEvent.dispatch(t); }, []); useFrame(({ invalidate }) => { const dt = performance.now() / 1e3 - t0.current; const x = clamp(dt / animationTime.current, 0, holding.current ? 1 : 2); if (x === 2) { apply(1); stop(); return; } // https://www.desmos.com/calculator/awsbahxjku const t = (0.5 * Math.sin(Math.PI * (x + 0.5)) + 0.5) ** 2; apply(t); if (holding.current && x === 1) return; invalidate(); if ( x >= 1 && Tankopedia.state.loaders.display.loaded !== requestedDisplay ) { Tankopedia.mutate((draft) => { draft.loaders.display.loaded = requestedDisplay; }); } }); return null; }