forked from phenom/healer-man
updated the dungeon loading so it doesnt fail first try
This commit is contained in:
1 parent
e39aebf3a1
commit
7e60864e70
11 files changed
+383
-69
No files matched your search
+45
-38
@@ -36,6 +36,7 @@ import {
|
||||
wailingStaticSpawnsForPhase,
|
||||
} from "../game/wailingCavernsEncounter";
|
||||
import { WailingNaralexEvent } from "./WailingNaralexEvent";
|
||||
import { GameGltfLoaderLifecycle } from "./useGameGLTF";
|
||||
|
||||
export function GameScene() {
|
||||
const activeDungeonId = useGameStore((state) => state.activeDungeonId);
|
||||
@@ -198,45 +199,51 @@ export function GameScene() {
|
||||
powerPreference: safeGraphics ? "default" : "high-performance",
|
||||
}}
|
||||
>
|
||||
<color attach="background" args={[dungeon.presentation.background]} />
|
||||
<fog attach="fog" args={[dungeon.presentation.fog.color, dungeon.presentation.fog.near, dungeon.presentation.fog.far]} />
|
||||
<ambientLight intensity={dungeon.presentation.ambientLight.intensity} color={dungeon.presentation.ambientLight.color} />
|
||||
<hemisphereLight args={[
|
||||
dungeon.presentation.hemisphereLight.skyColor,
|
||||
dungeon.presentation.hemisphereLight.groundColor,
|
||||
dungeon.presentation.hemisphereLight.intensity,
|
||||
]} />
|
||||
<directionalLight
|
||||
castShadow
|
||||
color={dungeon.presentation.directionalLight.color}
|
||||
intensity={dungeon.presentation.directionalLight.intensity}
|
||||
position={[
|
||||
dungeon.entrance.footPosition[0] + dungeon.presentation.directionalLight.offset[0],
|
||||
dungeon.entrance.footPosition[1] + dungeon.presentation.directionalLight.offset[1],
|
||||
dungeon.entrance.footPosition[2] + dungeon.presentation.directionalLight.offset[2],
|
||||
]}
|
||||
shadow-mapSize={[1024, 1024]}
|
||||
shadow-bias={-0.00025}
|
||||
/>
|
||||
<Physics
|
||||
key={encounterWorldKey(activeDungeonId, sessionRevision)}
|
||||
gravity={[0, -18, 0]}
|
||||
paused={simulationBlocked}
|
||||
timeStep={1 / 60}
|
||||
>
|
||||
<DungeonEnvironment onReady={markWorldReady} />
|
||||
<PlayerRig />
|
||||
<PartyPopulation active={!simulationBlocked} />
|
||||
<MobPopulation
|
||||
active={!simulationBlocked}
|
||||
entities={stagePopulation.entities}
|
||||
roamingPacks={roamingPacks}
|
||||
staticSpawns={staticSpawns}
|
||||
allowedRuntimeIds={allowedRuntimeIds}
|
||||
<GameGltfLoaderLifecycle>
|
||||
<color attach="background" args={[dungeon.presentation.background]} />
|
||||
<fog attach="fog" args={[dungeon.presentation.fog.color, dungeon.presentation.fog.near, dungeon.presentation.fog.far]} />
|
||||
<ambientLight intensity={dungeon.presentation.ambientLight.intensity} color={dungeon.presentation.ambientLight.color} />
|
||||
<hemisphereLight args={[
|
||||
dungeon.presentation.hemisphereLight.skyColor,
|
||||
dungeon.presentation.hemisphereLight.groundColor,
|
||||
dungeon.presentation.hemisphereLight.intensity,
|
||||
]} />
|
||||
<directionalLight
|
||||
castShadow
|
||||
color={dungeon.presentation.directionalLight.color}
|
||||
intensity={dungeon.presentation.directionalLight.intensity}
|
||||
position={[
|
||||
dungeon.entrance.footPosition[0] + dungeon.presentation.directionalLight.offset[0],
|
||||
dungeon.entrance.footPosition[1] + dungeon.presentation.directionalLight.offset[1],
|
||||
dungeon.entrance.footPosition[2] + dungeon.presentation.directionalLight.offset[2],
|
||||
]}
|
||||
shadow-mapSize={[1024, 1024]}
|
||||
shadow-bias={-0.00025}
|
||||
/>
|
||||
<WailingNaralexEvent />
|
||||
<ManastormPortal />
|
||||
</Physics>
|
||||
<Physics
|
||||
key={encounterWorldKey(activeDungeonId, sessionRevision)}
|
||||
gravity={[0, -18, 0]}
|
||||
paused={simulationBlocked}
|
||||
timeStep={1 / 60}
|
||||
>
|
||||
<DungeonEnvironment onReady={markWorldReady} />
|
||||
{worldReady && (
|
||||
<>
|
||||
<PlayerRig />
|
||||
<PartyPopulation active={!simulationBlocked} />
|
||||
<MobPopulation
|
||||
active={!simulationBlocked}
|
||||
entities={stagePopulation.entities}
|
||||
roamingPacks={roamingPacks}
|
||||
staticSpawns={staticSpawns}
|
||||
allowedRuntimeIds={allowedRuntimeIds}
|
||||
/>
|
||||
<WailingNaralexEvent />
|
||||
<ManastormPortal />
|
||||
</>
|
||||
)}
|
||||
</Physics>
|
||||
</GameGltfLoaderLifecycle>
|
||||
</Canvas>
|
||||
{graphicsRecovery && (
|
||||
<div
|
||||
|
||||
+47
-14
@@ -1,7 +1,7 @@
|
||||
import { Html } from "@react-three/drei";
|
||||
import { useFrame } from "@react-three/fiber";
|
||||
import { Suspense, useEffect, useMemo, useRef, useState } from "react";
|
||||
import type { CSSProperties, ReactNode } from "react";
|
||||
import { Component, Suspense, useEffect, useMemo, useRef, useState } from "react";
|
||||
import type { CSSProperties, ErrorInfo, ReactNode } from "react";
|
||||
import {
|
||||
AdditiveBlending,
|
||||
AnimationMixer,
|
||||
@@ -138,6 +138,37 @@ interface ProxyMobProps {
|
||||
readonly useOriginalModels: boolean;
|
||||
}
|
||||
|
||||
interface CreatureModelErrorBoundaryProps {
|
||||
readonly children: ReactNode;
|
||||
readonly fallback: ReactNode;
|
||||
readonly resetKey: string;
|
||||
}
|
||||
|
||||
class CreatureModelErrorBoundary extends Component<
|
||||
CreatureModelErrorBoundaryProps,
|
||||
{ readonly failed: boolean }
|
||||
> {
|
||||
state = { failed: false };
|
||||
|
||||
static getDerivedStateFromError() {
|
||||
return { failed: true };
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, info: ErrorInfo): void {
|
||||
console.warn("Creature model fell back to procedural rendering", error, info.componentStack);
|
||||
}
|
||||
|
||||
componentDidUpdate(previous: Readonly<CreatureModelErrorBoundaryProps>): void {
|
||||
if (previous.resetKey !== this.props.resetKey && this.state.failed) {
|
||||
this.setState({ failed: false });
|
||||
}
|
||||
}
|
||||
|
||||
render(): ReactNode {
|
||||
return this.state.failed ? this.props.fallback : this.props.children;
|
||||
}
|
||||
}
|
||||
|
||||
function prepareCreatureModel(root: Object3D): Object3D {
|
||||
const cachedTemplate = preparedCreatureTemplates.get(root);
|
||||
if (cachedTemplate) return cachedTemplate;
|
||||
@@ -902,18 +933,20 @@ function ProxyMob({
|
||||
forceFullFidelity={forceFullFidelity}
|
||||
>
|
||||
{model ? (
|
||||
<Suspense fallback={proxyBody}>
|
||||
<OriginalCreatureModel
|
||||
model={model}
|
||||
active={active}
|
||||
proceduralAnimation={proceduralModelAnimation}
|
||||
animationState={animationState}
|
||||
attackRevision={attackRevision}
|
||||
attackAnimation={attackAnimation}
|
||||
woundRevision={woundRevision}
|
||||
forceFullFidelity={forceFullFidelity}
|
||||
/>
|
||||
</Suspense>
|
||||
<CreatureModelErrorBoundary fallback={proxyBody} resetKey={model.url}>
|
||||
<Suspense fallback={proxyBody}>
|
||||
<OriginalCreatureModel
|
||||
model={model}
|
||||
active={active}
|
||||
proceduralAnimation={proceduralModelAnimation}
|
||||
animationState={animationState}
|
||||
attackRevision={attackRevision}
|
||||
attackAnimation={attackAnimation}
|
||||
woundRevision={woundRevision}
|
||||
forceFullFidelity={forceFullFidelity}
|
||||
/>
|
||||
</Suspense>
|
||||
</CreatureModelErrorBoundary>
|
||||
) : (
|
||||
proxyBody
|
||||
)}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useEffect, useMemo, useRef } from "react";
|
||||
import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { RigidBody, TrimeshCollider } from "@react-three/rapier";
|
||||
import {
|
||||
AdditiveBlending,
|
||||
@@ -15,6 +15,11 @@ import { useGameStore } from "../game/store";
|
||||
import { collisionTrimeshes, prepareCollision } from "./productionDungeonCollision";
|
||||
import { disposeObject3DResources } from "./threeResourceDisposal";
|
||||
import { useGameGLTF } from "./useGameGLTF";
|
||||
import {
|
||||
DUNGEON_COLLISION_LOAD_BATCH_SIZE,
|
||||
DUNGEON_VISUAL_LOAD_BATCH_SIZE,
|
||||
advanceDungeonAssetCount,
|
||||
} from "./dungeonLoadSequencing";
|
||||
|
||||
function matchesRole(name: string, patterns: readonly string[], names: readonly string[]): boolean {
|
||||
const lower = name.toLowerCase();
|
||||
@@ -90,6 +95,40 @@ function VisualChunk({ url, definition }: { readonly url: string; readonly defin
|
||||
return <primitive object={visual} position={position} rotation={rotation} scale={scale} />;
|
||||
}
|
||||
|
||||
function AssetProbe({ url }: { readonly url: string }) {
|
||||
useGameGLTF(url);
|
||||
return null;
|
||||
}
|
||||
|
||||
function CommitLoadedBatch({ onReady }: { readonly onReady: () => void }) {
|
||||
useEffect(() => {
|
||||
const frame = window.requestAnimationFrame(onReady);
|
||||
return () => window.cancelAnimationFrame(frame);
|
||||
}, [onReady]);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Suspends only the small batch currently being fetched. Previously prepared
|
||||
* visuals and colliders stay mounted while the next batch enters the GLTF
|
||||
* cache, avoiding one cold-start burst across the entire dungeon package.
|
||||
*/
|
||||
function AssetBatchLoader({
|
||||
urls,
|
||||
onReady,
|
||||
}: {
|
||||
readonly urls: readonly string[];
|
||||
readonly onReady: () => void;
|
||||
}) {
|
||||
if (!urls.length) return null;
|
||||
return (
|
||||
<Suspense fallback={null}>
|
||||
{urls.map((url) => <AssetProbe key={url} url={url} />)}
|
||||
<CommitLoadedBatch onReady={onReady} />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
function CollisionChunk({
|
||||
url,
|
||||
definition,
|
||||
@@ -146,8 +185,53 @@ export function ProductionDungeon({
|
||||
if (!assets.visual.length || !assets.collision.length) {
|
||||
throw new Error(`${definition.title} mounted without its required visual and collision GLBs.`);
|
||||
}
|
||||
const [loadedVisualCount, setLoadedVisualCount] = useState(0);
|
||||
const [loadedCollisionCount, setLoadedCollisionCount] = useState(0);
|
||||
const preparedCollisions = useRef(new Set<string>());
|
||||
const signalledFirstFrame = useRef(false);
|
||||
const pendingVisuals = assets.visual.slice(
|
||||
loadedVisualCount,
|
||||
advanceDungeonAssetCount(
|
||||
loadedVisualCount,
|
||||
assets.visual.length,
|
||||
DUNGEON_VISUAL_LOAD_BATCH_SIZE,
|
||||
),
|
||||
);
|
||||
const visualsLoaded = loadedVisualCount >= assets.visual.length;
|
||||
const pendingCollisions = visualsLoaded
|
||||
? assets.collision.slice(
|
||||
loadedCollisionCount,
|
||||
advanceDungeonAssetCount(
|
||||
loadedCollisionCount,
|
||||
assets.collision.length,
|
||||
DUNGEON_COLLISION_LOAD_BATCH_SIZE,
|
||||
),
|
||||
)
|
||||
: [];
|
||||
const pendingUrls = pendingVisuals.length ? pendingVisuals : pendingCollisions;
|
||||
const commitLoadedBatch = useCallback(() => {
|
||||
if (!visualsLoaded) {
|
||||
setLoadedVisualCount((current) => advanceDungeonAssetCount(
|
||||
current,
|
||||
assets.visual.length,
|
||||
DUNGEON_VISUAL_LOAD_BATCH_SIZE,
|
||||
));
|
||||
return;
|
||||
}
|
||||
setLoadedCollisionCount((current) => advanceDungeonAssetCount(
|
||||
current,
|
||||
assets.collision.length,
|
||||
DUNGEON_COLLISION_LOAD_BATCH_SIZE,
|
||||
));
|
||||
}, [assets.collision.length, assets.visual.length, visualsLoaded]);
|
||||
|
||||
useEffect(() => {
|
||||
useGameStore.getState().setAssetStatus(
|
||||
"loading",
|
||||
`Preparing ${loadedVisualCount}/${assets.visual.length} visual and ${loadedCollisionCount}/${assets.collision.length} collision chunks.`,
|
||||
);
|
||||
}, [assets.collision.length, assets.visual.length, loadedCollisionCount, loadedVisualCount]);
|
||||
|
||||
const registerPrepared = useCallback((url: string, ready: boolean) => {
|
||||
if (!ready) {
|
||||
preparedCollisions.current.delete(url);
|
||||
@@ -163,8 +247,10 @@ export function ProductionDungeon({
|
||||
|
||||
return (
|
||||
<group name={`${definition.id}-production`}>
|
||||
{assets.visual.map((url) => <VisualChunk key={url} url={url} definition={definition} />)}
|
||||
{assets.collision.map((url) => (
|
||||
{assets.visual.slice(0, loadedVisualCount).map((url) => (
|
||||
<VisualChunk key={url} url={url} definition={definition} />
|
||||
))}
|
||||
{assets.collision.slice(0, loadedCollisionCount).map((url) => (
|
||||
<CollisionChunk
|
||||
key={url}
|
||||
url={url}
|
||||
@@ -172,6 +258,11 @@ export function ProductionDungeon({
|
||||
registerPrepared={registerPrepared}
|
||||
/>
|
||||
))}
|
||||
<AssetBatchLoader
|
||||
key={`${loadedVisualCount}:${loadedCollisionCount}:${pendingUrls.join("|")}`}
|
||||
urls={pendingUrls}
|
||||
onReady={commitLoadedBatch}
|
||||
/>
|
||||
</group>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
DUNGEON_COLLISION_LOAD_BATCH_SIZE,
|
||||
DUNGEON_VISUAL_LOAD_BATCH_SIZE,
|
||||
advanceDungeonAssetCount,
|
||||
} from "./dungeonLoadSequencing";
|
||||
|
||||
describe("dungeon asset load sequencing", () => {
|
||||
it("loads visuals individually and collision meshes in bounded batches", () => {
|
||||
expect(DUNGEON_VISUAL_LOAD_BATCH_SIZE).toBe(1);
|
||||
expect(DUNGEON_COLLISION_LOAD_BATCH_SIZE).toBe(4);
|
||||
expect(advanceDungeonAssetCount(0, 48, DUNGEON_COLLISION_LOAD_BATCH_SIZE)).toBe(4);
|
||||
expect(advanceDungeonAssetCount(44, 48, DUNGEON_COLLISION_LOAD_BATCH_SIZE)).toBe(48);
|
||||
});
|
||||
|
||||
it("clamps malformed counters instead of creating an unbounded window", () => {
|
||||
expect(advanceDungeonAssetCount(-3, 2, 0)).toBe(1);
|
||||
expect(advanceDungeonAssetCount(8, 2, 4)).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
export const DUNGEON_VISUAL_LOAD_BATCH_SIZE = 1;
|
||||
export const DUNGEON_COLLISION_LOAD_BATCH_SIZE = 4;
|
||||
|
||||
/** Advances a staged asset window without overshooting the available files. */
|
||||
export function advanceDungeonAssetCount(
|
||||
loaded: number,
|
||||
total: number,
|
||||
batchSize: number,
|
||||
): number {
|
||||
const safeLoaded = Math.max(0, Math.trunc(loaded));
|
||||
const safeTotal = Math.max(0, Math.trunc(total));
|
||||
const safeBatchSize = Math.max(1, Math.trunc(batchSize));
|
||||
return Math.min(safeTotal, safeLoaded + safeBatchSize);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useGLTF } from "@react-three/drei";
|
||||
import { useThree } from "@react-three/fiber";
|
||||
import { useCallback } from "react";
|
||||
import { useCallback, useEffect, type ReactNode } from "react";
|
||||
import type { WebGLRenderer } from "three";
|
||||
import { KTX2Loader, type GLTFLoader } from "three-stdlib";
|
||||
import { resolveContentUrl } from "../content/contentManager";
|
||||
@@ -20,6 +20,18 @@ function ktx2LoaderFor(renderer: WebGLRenderer): KTX2Loader {
|
||||
return loader;
|
||||
}
|
||||
|
||||
/** Owns the decoder worker pool for one Canvas and releases it on renderer teardown. */
|
||||
export function GameGltfLoaderLifecycle({ children }: { readonly children: ReactNode }) {
|
||||
const renderer = useThree((state) => state.gl);
|
||||
useEffect(() => () => {
|
||||
const loader = ktx2Loaders.get(renderer);
|
||||
if (!loader) return;
|
||||
loader.dispose();
|
||||
ktx2Loaders.delete(renderer);
|
||||
}, [renderer]);
|
||||
return children;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads shipped world and creature GLBs with Meshopt and KTX2 support.
|
||||
* Keeping one KTX2Loader per renderer also keeps Three from creating a worker
|
||||
|
||||
Reference in new issue
Block a user