t2-mapper/app/page.tsx

129 lines
4.3 KiB
TypeScript
Raw Normal View History

2025-09-11 16:48:23 -07:00
"use client";
2025-12-02 22:16:40 -08:00
import { useState, useEffect, useCallback, Suspense } from "react";
import { useSearchParams, useRouter } from "next/navigation";
2025-11-13 22:55:58 -08:00
import { Canvas } from "@react-three/fiber";
import { NoToneMapping, SRGBColorSpace } from "three";
import { Mission } from "@/src/components/Mission";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ObserverControls } from "@/src/components/ObserverControls";
import { InspectorControls } from "@/src/components/InspectorControls";
import { SettingsProvider } from "@/src/components/SettingsProvider";
import { ObserverCamera } from "@/src/components/ObserverCamera";
2025-11-15 16:33:18 -08:00
import { AudioProvider } from "@/src/components/AudioContext";
import { DebugElements } from "@/src/components/DebugElements";
2025-11-26 14:37:49 -08:00
import { CamerasProvider } from "@/src/components/CamerasProvider";
import { getMissionList, getMissionInfo } from "@/src/manifest";
2025-11-13 22:55:58 -08:00
// three.js has its own loaders for textures and models, but we need to load other
// stuff too, e.g. missions, terrains, and more. This client is used for those.
const queryClient = new QueryClient();
2025-09-11 16:48:23 -07:00
// Renderer settings to match Tribes 2's simple rendering pipeline.
// Tribes 2 (Torque engine, 2001) worked entirely in gamma/sRGB space with no HDR
// or tone mapping. We disable tone mapping and ensure proper sRGB output.
const glSettings = {
toneMapping: NoToneMapping,
outputColorSpace: SRGBColorSpace,
};
2025-11-15 12:55:08 -08:00
function MapInspector() {
2025-11-14 03:30:33 -08:00
const searchParams = useSearchParams();
const router = useRouter();
// Initialize state from query params
const [missionName, setMissionName] = useState(
2025-11-29 09:08:20 -08:00
searchParams.get("mission") || "TWL2_WoodyMyrk",
2025-11-14 03:30:33 -08:00
);
const [loadingProgress, setLoadingProgress] = useState(0);
const [showLoadingIndicator, setShowLoadingIndicator] = useState(true);
const isLoading = loadingProgress < 1;
// Keep the loading indicator visible briefly after reaching 100%
useEffect(() => {
if (isLoading) {
setShowLoadingIndicator(true);
} else {
const timer = setTimeout(() => setShowLoadingIndicator(false), 500);
return () => clearTimeout(timer);
}
}, [isLoading]);
2025-11-14 03:30:33 -08:00
useEffect(() => {
// For automation, like the t2-maps app!
window.setMissionName = setMissionName;
window.getMissionList = getMissionList;
window.getMissionInfo = getMissionInfo;
return () => {
delete window.setMissionName;
delete window.getMissionList;
delete window.getMissionInfo;
};
}, []);
2025-11-14 03:30:33 -08:00
// Update query params when state changes
useEffect(() => {
const params = new URLSearchParams();
params.set("mission", missionName);
router.replace(`?${params.toString()}`, { scroll: false });
}, [missionName, router]);
2025-09-12 10:30:40 -07:00
const handleLoadingChange = useCallback(
(_loading: boolean, progress: number = 0) => {
setLoadingProgress(progress);
},
[],
);
2025-12-02 22:16:40 -08:00
2025-09-11 16:48:23 -07:00
return (
<QueryClientProvider client={queryClient}>
<main>
<SettingsProvider>
2025-12-02 16:58:35 -08:00
<div id="canvasContainer">
{showLoadingIndicator && (
<div id="loadingIndicator" data-complete={!isLoading}>
<div className="LoadingSpinner" />
<div className="LoadingProgress">
<div
className="LoadingProgress-bar"
style={{ width: `${loadingProgress * 100}%` }}
/>
</div>
<div className="LoadingProgress-text">
{Math.round(loadingProgress * 100)}%
</div>
</div>
)}
<Canvas shadows frameloop="always" gl={glSettings}>
2025-12-02 16:58:35 -08:00
<CamerasProvider>
<AudioProvider>
2025-12-02 22:16:40 -08:00
<Mission
key={missionName}
name={missionName}
onLoadingChange={handleLoadingChange}
/>
2025-12-02 16:58:35 -08:00
<ObserverCamera />
<DebugElements />
<ObserverControls />
</AudioProvider>
</CamerasProvider>
</Canvas>
</div>
2025-11-13 22:55:58 -08:00
<InspectorControls
missionName={missionName}
onChangeMission={setMissionName}
2025-09-12 10:30:40 -07:00
/>
</SettingsProvider>
</main>
</QueryClientProvider>
2025-09-11 16:48:23 -07:00
);
}
2025-11-15 12:55:08 -08:00
export default function HomePage() {
return (
<Suspense>
<MapInspector />
</Suspense>
);
}