fix: load all models/

This commit is contained in:
math-pixel
2026-04-27 16:07:57 +02:00
parent 2001955625
commit 868f7a1cfd
56 changed files with 318 additions and 86 deletions
+15 -40
View File
@@ -269,52 +269,27 @@ export function EditorPage(): React.JSX.Element {
return;
}
const mapNodes = await response.json();
const mapNodes: MapNode[] = await response.json();
const models = new Map<string, string>();
try {
const traverseModels = async (path: string): Promise<void> => {
try {
const response = await fetch(path);
if (!response.ok) return;
const text = await response.text();
const uniqueModelNames = [
...new Set(mapNodes.map((n: MapNode) => n.name)),
];
console.log("Unique model names in map:", uniqueModelNames);
if (text.includes("index")) {
const modelUrl = path.replace(/\/$/, "") + "/model.glb";
const modelResponse = await fetch(modelUrl);
if (modelResponse.ok) {
const blob = await modelResponse.blob();
const blobUrl = URL.createObjectURL(blob);
const pathParts = path.split("/").filter(Boolean);
const modelName = pathParts[pathParts.length - 1] || "";
models.set(modelName, blobUrl);
}
}
} catch {
/* empty */
}
};
const baseResponse = await fetch("/models/");
if (baseResponse.ok) {
const text = await baseResponse.text();
const lines = text.split("\n");
for (const line of lines) {
if (line.includes("href") && line.includes("/")) {
const match = line.match(/href="([^"]+)"/);
if (match && match[1]) {
const href = match[1];
if (href.endsWith("/")) {
await traverseModels(href);
}
}
}
for (const modelName of uniqueModelNames) {
try {
const modelUrl = `/models/${modelName}/model.gltf`;
const modelResponse = await fetch(modelUrl);
if (modelResponse.ok) {
models.set(modelName, modelUrl);
}
} catch {
/* empty */
}
} catch (error) {
console.warn("Could not scan models directory:", error);
}
console.log("Loaded models:", Array.from(models.keys()));
setSceneData({ mapNodes, models });
setHasMapJson(true);
@@ -356,7 +331,7 @@ export function EditorPage(): React.JSX.Element {
const models = new Map<string, string>();
for (const [path, file] of fileMap.entries()) {
const modelMatch = path && path.match(/^\/models\/(.+)\/model\.glb$/);
const modelMatch = path && path.match(/^\/models\/(.+)\/model\.gltf$/);
if (modelMatch && modelMatch[1]) {
const modelName = modelMatch[1];
const blobUrl = URL.createObjectURL(file);
+112
View File
@@ -0,0 +1,112 @@
import { useEffect, useState, useMemo, useRef } from "react";
import { useGLTF } from "@react-three/drei";
import * as THREE from "three";
import { useOctreeGraphNode } from "@/hooks/useOctreeGraphNode";
import type { OctreeReadyHandler } from "@/types/3d";
interface MapNode {
name: string;
type: string;
position: [number, number, number];
rotation: [number, number, number];
scale: [number, number, number];
}
const MAP_JSON_PATH = "/map.json";
const clonedScenesCache = new Map<string, THREE.Group>();
interface GameMapProps {
onOctreeReady: OctreeReadyHandler;
}
export function GameMap({ onOctreeReady }: GameMapProps): React.JSX.Element {
const [mapNodes, setMapNodes] = useState<MapNode[]>([]);
const [availableModels, setAvailableModels] = useState<Set<string>>(
new Set(),
);
const [isLoading, setIsLoading] = useState(true);
const groupRef = useRef<THREE.Group>(null);
useOctreeGraphNode(groupRef, onOctreeReady);
useEffect(() => {
const loadMap = async () => {
try {
const nodesResponse = await fetch(MAP_JSON_PATH);
if (!nodesResponse.ok) {
console.warn("map.json not found");
setIsLoading(false);
return;
}
const nodes: MapNode[] = await nodesResponse.json();
setMapNodes(nodes);
const uniqueModelNames = [...new Set(nodes.map((n) => n.name))];
const available = new Set<string>();
for (const modelName of uniqueModelNames) {
try {
const modelUrl = `/models/${modelName}/model.gltf`;
const modelResponse = await fetch(modelUrl);
if (modelResponse.ok) {
available.add(modelName);
}
} catch {
/* empty */
}
}
setAvailableModels(available);
} catch (error) {
console.error("Error loading map:", error);
} finally {
setIsLoading(false);
}
};
loadMap();
}, []);
const nodesToRender = useMemo(() => {
return mapNodes.filter((node) => availableModels.has(node.name));
}, [mapNodes, availableModels]);
if (isLoading) {
return <></>;
}
return (
<group ref={groupRef}>
{nodesToRender.map((node, index) => (
<ModelInstance key={index} node={node} />
))}
</group>
);
}
function ModelInstance({ node }: { node: MapNode }): React.JSX.Element {
const modelPath = `/models/${node.name}/model.gltf`;
const { scene } = useGLTF(modelPath);
const groupRef = useRef<THREE.Group>(null);
const clonedScene = useMemo(() => {
if (!clonedScenesCache.has(modelPath)) {
const clone = scene.clone(true);
clonedScenesCache.set(modelPath, clone);
return clone;
}
return clonedScenesCache.get(modelPath)!;
}, [modelPath, scene]);
const instance = useMemo(() => {
const inst = clonedScene.clone(true);
inst.position.set(...node.position);
inst.rotation.set(...node.rotation);
inst.scale.set(...node.scale);
return inst;
}, [clonedScene, node.position, node.rotation, node.scale]);
return <primitive ref={groupRef} object={instance} />;
}
+2 -2
View File
@@ -10,7 +10,7 @@ import { DebugCameraControls } from "@/utils/debug/scene/DebugCameraControls";
import { DebugHelpers } from "@/utils/debug/scene/DebugHelpers";
import { Environment } from "@/world/Environment";
import { Lighting } from "@/world/Lighting";
import { Map } from "@/world/Map";
import { GameMap } from "@/components/game/GameMap";
import { PlayerComponent } from "@/world/player/PlayerComponent";
import { TestScene } from "@/world/debug/TestScene";
@@ -31,7 +31,7 @@ export function World(): React.JSX.Element {
{cameraMode === "debug" ? <DebugCameraControls /> : null}
{sceneMode === "game" ? (
<Map onOctreeReady={setOctree} />
<GameMap onOctreeReady={setOctree} />
) : (
<TestScene onOctreeReady={setOctree} />
)}