2026-08-11 14:07:57 -05:00
|
|
|
import { blueDark, orangeDark } from "@radix-ui/colors";
|
2026-08-10 16:42:35 -05:00
|
|
|
import { ContactShadows } from "@react-three/drei";
|
2026-08-11 19:45:09 -05:00
|
|
|
import { invalidate, useFrame } from "@react-three/fiber";
|
2026-06-26 13:22:24 -05:00
|
|
|
import { clamp, times } from "lodash-es";
|
2025-11-27 13:42:37 -06:00
|
|
|
import { Quicklime } from "quicklime";
|
2026-07-11 17:29:15 -05:00
|
|
|
import {
|
|
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
|
|
|
|
useRef,
|
|
|
|
|
useState,
|
|
|
|
|
type RefObject,
|
|
|
|
|
} from "react";
|
2025-11-27 14:10:57 -06:00
|
|
|
import { HemisphereLight, SpotLight, type Group } from "three";
|
2026-06-26 13:22:24 -05:00
|
|
|
import { degToRad, lerp } from "three/src/math/MathUtils.js";
|
2026-09-23 17:25:51 -05:00
|
|
|
import { Tankopedia } from "../stores/tankopedia";
|
|
|
|
|
import { TankopediaPersistent } from "../stores/tankopediaPersistent";
|
|
|
|
|
import { TankopediaDisplay } from "../stores/tankopediaPersistent/constants";
|
2025-09-02 22:44:46 -05:00
|
|
|
|
2026-08-10 17:50:48 -05:00
|
|
|
const ANGLE = degToRad(15);
|
2025-11-27 13:05:08 -06:00
|
|
|
const REVEAL_ANIMATION_TIME = 3;
|
2026-10-22 16:12:03 +13:00
|
|
|
export const TRANSITION_ANIMATION_TIME = 0.5;
|
2025-11-26 21:10:03 -06:00
|
|
|
|
2026-08-10 16:42:35 -05:00
|
|
|
const LIGHTS_COUNT = 3;
|
2026-06-26 13:58:30 -05:00
|
|
|
const THETA_OFFSET = degToRad(180 - 45);
|
2026-08-11 10:52:33 -05:00
|
|
|
|
2026-06-26 13:58:30 -05:00
|
|
|
const LIGHT_DISTANCE = 20;
|
2026-08-10 17:50:48 -05:00
|
|
|
const LIGHT_HEIGHT_0 = 7;
|
|
|
|
|
const LIGHT_HEIGHT_1 = 10;
|
2026-08-11 10:52:33 -05:00
|
|
|
|
2026-08-10 17:50:48 -05:00
|
|
|
const INTENSITY_0 = 2 ** 6;
|
|
|
|
|
const INTENSITY_1 = 2 ** 3;
|
|
|
|
|
const HEMISPHERE_INTENSITY = 2 ** 0.7;
|
2026-08-11 10:01:34 -05:00
|
|
|
const NON_PBR_FACTOR = 2;
|
2025-11-27 13:05:08 -06:00
|
|
|
|
2026-08-11 10:52:33 -05:00
|
|
|
const SHADOW_RESOLUTION = 2 ** 9;
|
|
|
|
|
const SHADOW_FOCUS = 2 ** 0;
|
|
|
|
|
|
2025-11-27 13:42:37 -06:00
|
|
|
export const transitionEvent = new Quicklime<number>(0);
|
|
|
|
|
|
2026-08-12 13:41:59 -05:00
|
|
|
interface Props {
|
|
|
|
|
hasPbr: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function Lighting({ hasPbr }: Props) {
|
2026-08-10 16:42:35 -05:00
|
|
|
const wrapper = useRef<Group>(null);
|
2026-06-26 13:22:24 -05:00
|
|
|
|
2025-11-27 13:05:08 -06:00
|
|
|
const highGraphics = TankopediaPersistent.use((state) => state.highGraphics);
|
2026-10-31 13:57:56 -05:00
|
|
|
const requestedDisplay = Tankopedia.use(
|
|
|
|
|
(state) => state.loaders.display.requested,
|
|
|
|
|
);
|
2026-10-22 16:12:03 +13:00
|
|
|
const revealed = Tankopedia.use((state) => state.revealed);
|
|
|
|
|
const skinPending = Tankopedia.use(
|
2026-10-28 20:49:14 +13:00
|
|
|
(state) =>
|
2026-10-30 07:11:31 +13:00
|
|
|
state.loaders.skin.requested !== state.loaders.skin.loaded ||
|
|
|
|
|
state.loaders.attachment.requested !== state.loaders.attachment.loaded,
|
2026-10-22 16:12:03 +13:00
|
|
|
);
|
2026-06-26 13:22:24 -05:00
|
|
|
|
|
|
|
|
const animationTime = useRef(REVEAL_ANIMATION_TIME);
|
|
|
|
|
const t0 = useRef(performance.now() / 1e3 - animationTime.current);
|
2026-10-22 16:12:03 +13:00
|
|
|
const holding = useRef(false);
|
2026-06-26 13:22:24 -05:00
|
|
|
|
|
|
|
|
const isRevealing = useRef(true);
|
2026-10-22 16:12:03 +13:00
|
|
|
const isFirstReveal = useRef(true);
|
2026-06-26 13:22:24 -05:00
|
|
|
|
|
|
|
|
const [animate, setAnimate] = useState(true);
|
|
|
|
|
|
2025-11-26 21:10:03 -06:00
|
|
|
useEffect(() => {
|
|
|
|
|
Tankopedia.mutate((draft) => {
|
2026-10-31 13:57:56 -05:00
|
|
|
draft.showControls = true;
|
2025-11-26 21:10:03 -06:00
|
|
|
draft.revealed = true;
|
2026-10-30 07:11:31 +13:00
|
|
|
draft.loaders.model.loaded = true;
|
2025-11-26 21:10:03 -06:00
|
|
|
});
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-06-26 13:22:24 -05:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (isRevealing.current) {
|
|
|
|
|
isRevealing.current = false;
|
|
|
|
|
} else {
|
|
|
|
|
animationTime.current = TRANSITION_ANIMATION_TIME;
|
|
|
|
|
t0.current = performance.now() / 1e3;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setAnimate(true);
|
|
|
|
|
}, [requestedDisplay]);
|
|
|
|
|
|
2026-10-22 16:12:03 +13:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (isFirstReveal.current) {
|
|
|
|
|
isFirstReveal.current = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!revealed) {
|
|
|
|
|
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();
|
|
|
|
|
}, [revealed]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!skinPending) return;
|
|
|
|
|
|
|
|
|
|
animationTime.current = TRANSITION_ANIMATION_TIME;
|
|
|
|
|
t0.current = performance.now() / 1e3;
|
|
|
|
|
|
|
|
|
|
setAnimate(true);
|
|
|
|
|
}, [skinPending]);
|
|
|
|
|
|
2026-06-26 13:22:24 -05:00
|
|
|
return (
|
|
|
|
|
<>
|
2026-08-11 19:37:57 -05:00
|
|
|
<ContactShadows
|
|
|
|
|
width={1.5}
|
|
|
|
|
height={1.5}
|
2026-08-19 13:57:36 -05:00
|
|
|
near={0}
|
|
|
|
|
far={1}
|
|
|
|
|
opacity={2 ** 1}
|
|
|
|
|
blur={2 ** 1}
|
|
|
|
|
resolution={2 ** 9}
|
2026-08-11 19:45:09 -05:00
|
|
|
depthWrite={false}
|
2026-10-05 17:28:01 +13:00
|
|
|
renderOrder={1}
|
2026-08-11 19:37:57 -05:00
|
|
|
/>
|
2026-08-10 16:42:35 -05:00
|
|
|
|
2026-06-26 13:22:24 -05:00
|
|
|
{animate && (
|
|
|
|
|
<Animator
|
2026-08-12 13:41:59 -05:00
|
|
|
hasPbr={hasPbr}
|
2026-06-26 13:22:24 -05:00
|
|
|
t0={t0}
|
|
|
|
|
animationTime={animationTime}
|
2026-10-22 16:12:03 +13:00
|
|
|
holding={holding}
|
2026-06-26 13:22:24 -05:00
|
|
|
wrapper={wrapper}
|
|
|
|
|
stop={() => {
|
|
|
|
|
setAnimate(false);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<group ref={wrapper}>
|
2026-08-11 11:30:36 -05:00
|
|
|
<hemisphereLight
|
|
|
|
|
intensity={0}
|
|
|
|
|
color={blueDark.blue12}
|
2026-08-11 14:07:57 -05:00
|
|
|
groundColor={orangeDark.orange8}
|
2026-08-11 11:30:36 -05:00
|
|
|
/>
|
2026-06-26 13:22:24 -05:00
|
|
|
|
|
|
|
|
{times(LIGHTS_COUNT, (index) => {
|
|
|
|
|
const x = index / (LIGHTS_COUNT - 1);
|
|
|
|
|
const theta = 2 * Math.PI * (index / LIGHTS_COUNT) + THETA_OFFSET;
|
|
|
|
|
const position = [
|
2026-08-10 16:58:40 -05:00
|
|
|
-LIGHT_DISTANCE * Math.sin(theta),
|
2026-06-26 13:22:24 -05:00
|
|
|
lerp(LIGHT_HEIGHT_0, LIGHT_HEIGHT_1, x),
|
|
|
|
|
LIGHT_DISTANCE * Math.cos(theta),
|
|
|
|
|
] as const;
|
|
|
|
|
const intensity = lerp(INTENSITY_0, INTENSITY_1, x);
|
|
|
|
|
|
2026-08-11 09:56:58 -05:00
|
|
|
const light = useRef<SpotLight>(null!);
|
|
|
|
|
|
|
|
|
|
// useHelper(light, SpotLightHelper);
|
|
|
|
|
|
2026-06-26 13:22:24 -05:00
|
|
|
return (
|
2026-08-10 16:42:35 -05:00
|
|
|
<spotLight
|
2026-08-11 09:56:58 -05:00
|
|
|
ref={light}
|
2026-06-26 13:22:24 -05:00
|
|
|
key={index}
|
|
|
|
|
position={position}
|
|
|
|
|
intensity={intensity}
|
2026-10-30 16:49:43 -05:00
|
|
|
userData={{ intensity }}
|
2026-06-26 13:22:24 -05:00
|
|
|
penumbra={1}
|
|
|
|
|
castShadow={highGraphics}
|
2026-07-11 17:38:11 -05:00
|
|
|
decay={1}
|
2026-06-26 13:22:24 -05:00
|
|
|
color="#ffffff"
|
|
|
|
|
angle={0}
|
2026-08-11 09:56:58 -05:00
|
|
|
target-position={[0, 1.5, 0]}
|
2026-08-11 10:52:33 -05:00
|
|
|
shadow-mapSize={[SHADOW_RESOLUTION, SHADOW_RESOLUTION]}
|
|
|
|
|
shadow-focus={SHADOW_FOCUS}
|
2026-06-26 13:22:24 -05:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</group>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface AnimatorProps {
|
|
|
|
|
stop: () => void;
|
|
|
|
|
|
|
|
|
|
t0: RefObject<number>;
|
|
|
|
|
animationTime: RefObject<number>;
|
2026-10-22 16:12:03 +13:00
|
|
|
holding: RefObject<boolean>;
|
2026-08-10 16:42:35 -05:00
|
|
|
wrapper: RefObject<Group | null>;
|
2026-08-12 13:41:59 -05:00
|
|
|
hasPbr: boolean;
|
2026-06-26 13:22:24 -05:00
|
|
|
}
|
|
|
|
|
|
2026-10-22 16:12:03 +13:00
|
|
|
function Animator({
|
|
|
|
|
stop,
|
|
|
|
|
t0,
|
|
|
|
|
animationTime,
|
|
|
|
|
holding,
|
|
|
|
|
wrapper,
|
|
|
|
|
hasPbr,
|
|
|
|
|
}: AnimatorProps) {
|
2026-10-31 13:57:56 -05:00
|
|
|
const requestedDisplay = Tankopedia.use(
|
|
|
|
|
(state) => state.loaders.display.requested,
|
|
|
|
|
);
|
|
|
|
|
const display = Tankopedia.use((state) => state.loaders.display.loaded);
|
2026-08-11 10:01:34 -05:00
|
|
|
|
|
|
|
|
const factor =
|
|
|
|
|
!hasPbr && display !== TankopediaDisplay.StaticArmor ? NON_PBR_FACTOR : 1;
|
|
|
|
|
|
2026-08-11 19:45:09 -05:00
|
|
|
useEffect(() => {
|
|
|
|
|
invalidate();
|
|
|
|
|
}, [requestedDisplay === display]);
|
|
|
|
|
|
2026-07-11 17:29:15 -05:00
|
|
|
const apply = useCallback((t: number) => {
|
2026-08-11 19:45:09 -05:00
|
|
|
if (!wrapper.current) return;
|
2026-08-10 16:42:35 -05:00
|
|
|
|
2025-11-26 21:10:03 -06:00
|
|
|
for (const child of wrapper.current.children) {
|
2025-11-27 13:05:08 -06:00
|
|
|
if (child instanceof SpotLight) {
|
2026-08-11 10:01:34 -05:00
|
|
|
child.angle = ANGLE * t * factor;
|
2026-10-30 16:49:43 -05:00
|
|
|
child.intensity = child.userData.intensity;
|
|
|
|
|
|
|
|
|
|
if (child.angle === 0) {
|
|
|
|
|
child.intensity = 0;
|
|
|
|
|
}
|
2025-11-27 14:10:57 -06:00
|
|
|
} else if (child instanceof HemisphereLight) {
|
2026-08-11 10:01:34 -05:00
|
|
|
child.intensity = HEMISPHERE_INTENSITY * t * factor;
|
2025-11-26 21:10:03 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-26 13:22:24 -05:00
|
|
|
transitionEvent.dispatch(t);
|
2026-07-11 17:29:15 -05:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
useFrame(({ invalidate }) => {
|
|
|
|
|
const dt = performance.now() / 1e3 - t0.current;
|
2026-10-22 16:12:03 +13:00
|
|
|
const x = clamp(dt / animationTime.current, 0, holding.current ? 1 : 2);
|
2026-07-11 17:29:15 -05:00
|
|
|
|
|
|
|
|
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);
|
2026-10-22 16:12:03 +13:00
|
|
|
|
|
|
|
|
if (holding.current && x === 1) return;
|
|
|
|
|
|
2026-06-26 13:22:24 -05:00
|
|
|
invalidate();
|
2025-11-26 21:10:03 -06:00
|
|
|
|
2026-10-31 13:57:56 -05:00
|
|
|
if (
|
|
|
|
|
x >= 1 &&
|
|
|
|
|
Tankopedia.state.loaders.display.loaded !== requestedDisplay
|
|
|
|
|
) {
|
2025-11-26 21:10:03 -06:00
|
|
|
Tankopedia.mutate((draft) => {
|
2026-10-31 13:57:56 -05:00
|
|
|
draft.loaders.display.loaded = requestedDisplay;
|
2025-11-26 21:10:03 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-26 13:22:24 -05:00
|
|
|
return null;
|
2025-09-02 22:44:46 -05:00
|
|
|
}
|