feat: support glb model assets

This commit is contained in:
Tom Boullay
2026-04-29 16:18:24 +02:00
parent 471424f83d
commit 3a0639bdaa
9 changed files with 59 additions and 36 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ This document describes the code that exists today in the repository.
## Map Data ## Map Data
- `public/map.json` is expected to be a `MapNode[]`. - `public/map.json` is expected to be a `MapNode[]`.
- Each map node `name` maps to `public/models/{name}/model.gltf`. - Each map node `name` maps to `public/models/{name}/model.glb` when available, with `public/models/{name}/model.gltf` kept as fallback.
- The editor renders a fallback cube for missing models. - The editor renders a fallback cube for missing models.
- The game scene filters out nodes whose model cannot be resolved. - The game scene filters out nodes whose model cannot be resolved.
+3 -3
View File
@@ -57,7 +57,7 @@ src/
`src/controls/editor/FlyController.tsx` provides editor movement controls for player-style navigation. `src/controls/editor/FlyController.tsx` provides editor movement controls for player-style navigation.
`src/utils/loadMapSceneData.ts` is shared by the game map and editor. It loads `/map.json` and resolves available `public/models/{name}/model.gltf` files. `src/utils/loadMapSceneData.ts` is shared by the game map and editor. It loads `/map.json` and resolves available `public/models/{name}/model.glb` files first, then falls back to `public/models/{name}/model.gltf`.
`src/utils/editor/loadEditorScene.ts` contains editor-only upload handling for user-selected folders. `src/utils/editor/loadEditorScene.ts` contains editor-only upload handling for user-selected folders.
@@ -96,10 +96,10 @@ public/
├── map.json ├── map.json
└── models/ └── models/
└── pylone/ └── pylone/
└── model.gltf └── model.glb
``` ```
If a model is missing, the editor renders a fallback cube so the node can still be selected and transformed. If `model.glb` and `model.gltf` are both missing, the editor renders a fallback cube so the node can still be selected and transformed.
## Editor Flow ## Editor Flow
+1 -1
View File
@@ -9,7 +9,7 @@ Use the editor when you need to move, rotate, or scale existing map objects with
The editor reads the same map data as the runtime scene: The editor reads the same map data as the runtime scene:
- `public/map.json` contains the object list. - `public/map.json` contains the object list.
- `public/models/{name}/model.gltf` contains the matching 3D model for each object name. - `public/models/{name}/model.glb` contains the matching 3D model for each object name. `model.gltf` is still supported as a fallback during migration.
- Missing models are displayed as gray fallback cubes, so incomplete maps remain editable. - Missing models are displayed as gray fallback cubes, so incomplete maps remain editable.
## Map Node Format ## Map Node Format
+2 -2
View File
@@ -5,7 +5,7 @@ This document lists features that are implemented in the current codebase.
## Scene ## Scene
- Fullscreen React Three Fiber scene - Fullscreen React Three Fiber scene
- Main map scene loaded from `public/map.json` and matching `public/models/{name}/model.gltf` assets - Main map scene loaded from `public/map.json` and matching `public/models/{name}/model.glb` or `model.gltf` assets
- Debug physics test scene selectable from the debug panel - Debug physics test scene selectable from the debug panel
- Ambient and directional lighting - Ambient and directional lighting
- Environment background setup - Environment background setup
@@ -43,7 +43,7 @@ This document lists features that are implemented in the current codebase.
- `/editor` route for inspecting and editing `public/map.json` - `/editor` route for inspecting and editing `public/map.json`
- Automatic loading of `public/map.json` when available - Automatic loading of `public/map.json` when available
- Folder upload fallback when `map.json` is missing - Folder upload fallback when `map.json` is missing
- Rendering of available `public/models/{name}/model.gltf` assets - Rendering of available `public/models/{name}/model.glb` or `model.gltf` assets
- Fallback cubes for nodes whose model is missing - Fallback cubes for nodes whose model is missing
- Object selection by click - Object selection by click
- Transform modes for translate, rotate, and scale - Transform modes for translate, rotate, and scale
+2 -2
View File
@@ -214,7 +214,7 @@ Ce document liste les fonctionnalités présentes dans le code actuel.
## Scène ## Scène
- Scène React Three Fiber plein écran - Scène React Three Fiber plein écran
- Carte principale chargée depuis \`public/models/map/model.gltf\` - Carte principale chargée depuis \`public/models/{name}/model.glb\`, avec fallback vers \`model.gltf\`
- Scène de test physique debug sélectionnable depuis le panneau debug - Scène de test physique debug sélectionnable depuis le panneau debug
- Éclairage ambiant et directionnel - Éclairage ambiant et directionnel
- Configuration de l'environnement de fond - Configuration de l'environnement de fond
@@ -268,7 +268,7 @@ L'éditeur travaille sur la liste de nodes stockée dans "/public/map.json".
Chaque node décrit un objet de la scène : Chaque node décrit un objet de la scène :
- "name" : nom du dossier modèle dans "/public/models/{name}/model.gltf" - "name" : nom du dossier modèle dans "/public/models/{name}/model.glb", avec fallback vers "model.gltf"
- "type" : catégorie de l'objet - "type" : catégorie de l'objet
- "position" : "[x, y, z]" - "position" : "[x, y, z]"
- "rotation" : "[x, y, z]" - "rotation" : "[x, y, z]"
+1 -1
View File
@@ -138,7 +138,7 @@ export function EditorPage(): React.JSX.Element {
<h4>Structure requise :</h4> <h4>Structure requise :</h4>
<pre> <pre>
public/ <strong>map.json</strong> (à la racine) models/ public/ <strong>map.json</strong> (à la racine) models/
arbre/ model.gltf building/ model.gltf arbre/ model.glb building/ model.gltf
... ...
</pre> </pre>
</div> </div>
+6 -3
View File
@@ -21,9 +21,12 @@ export async function createSceneDataFromFiles(
const models = new Map<string, string>(); const models = new Map<string, string>();
for (const [path, file] of fileMap.entries()) { for (const [path, file] of fileMap.entries()) {
const modelMatch = path.match(/^\/models\/(.+)\/model\.gltf$/); const modelMatch = path.match(/^\/models\/(.+)\/model\.(glb|gltf)$/);
if (modelMatch?.[1]) { const modelName = modelMatch?.[1];
models.set(modelMatch[1], URL.createObjectURL(file)); const modelExtension = modelMatch?.[2];
if (modelName && (modelExtension === "glb" || !models.has(modelName))) {
models.set(modelName, URL.createObjectURL(file));
} }
} }
+20 -15
View File
@@ -2,7 +2,7 @@ import type { MapNode, SceneData } from "@/types/editor";
import { parseMapNodes } from "@/utils/mapNodeValidation"; import { parseMapNodes } from "@/utils/mapNodeValidation";
const MAP_JSON_PATH = "/map.json"; const MAP_JSON_PATH = "/map.json";
const MODEL_FILE_NAME = "model.gltf"; const MODEL_FILE_NAMES = ["model.glb", "model.gltf"];
const HTML_CONTENT_TYPE = "text/html"; const HTML_CONTENT_TYPE = "text/html";
type ModelEntry = [modelName: string, modelUrl: string]; type ModelEntry = [modelName: string, modelUrl: string];
@@ -27,21 +27,26 @@ async function loadMapModelUrls(
): Promise<Map<string, string>> { ): Promise<Map<string, string>> {
const uniqueModelNames = [...new Set(mapNodes.map((node) => node.name))]; const uniqueModelNames = [...new Set(mapNodes.map((node) => node.name))];
const modelEntries = await Promise.all( const modelEntries = await Promise.all(
uniqueModelNames.map(async (modelName) => { uniqueModelNames.map((modelName) => loadModelEntry(modelName)),
const modelUrl = `/models/${modelName}/${MODEL_FILE_NAME}`;
try {
const response = await fetch(modelUrl, { method: "HEAD" });
const contentType = response.headers.get("content-type") ?? "";
const modelEntry: ModelEntry = [modelName, modelUrl];
return response.ok && !contentType.includes(HTML_CONTENT_TYPE)
? modelEntry
: null;
} catch {
return null;
}
}),
); );
return new Map(modelEntries.filter((entry) => entry !== null)); return new Map(modelEntries.filter((entry) => entry !== null));
} }
async function loadModelEntry(modelName: string): Promise<ModelEntry | null> {
for (const fileName of MODEL_FILE_NAMES) {
const modelUrl = `/models/${modelName}/${fileName}`;
try {
const response = await fetch(modelUrl, { method: "HEAD" });
const contentType = response.headers.get("content-type") ?? "";
if (response.ok && !contentType.includes(HTML_CONTENT_TYPE)) {
return [modelName, modelUrl];
}
} catch {
continue;
}
}
return null;
}
+23 -8
View File
@@ -6,12 +6,17 @@ import { loadMapSceneData } from "@/utils/loadMapSceneData";
import type { OctreeReadyHandler } from "@/types/three"; import type { OctreeReadyHandler } from "@/types/three";
import type { MapNode } from "@/types/editor"; import type { MapNode } from "@/types/editor";
interface LoadedMapNode {
node: MapNode;
modelUrl: string;
}
interface GameMapProps { interface GameMapProps {
onOctreeReady: OctreeReadyHandler; onOctreeReady: OctreeReadyHandler;
} }
export function GameMap({ onOctreeReady }: GameMapProps): React.JSX.Element { export function GameMap({ onOctreeReady }: GameMapProps): React.JSX.Element {
const [mapNodes, setMapNodes] = useState<MapNode[]>([]); const [mapNodes, setMapNodes] = useState<LoadedMapNode[]>([]);
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const groupRef = useRef<THREE.Group>(null); const groupRef = useRef<THREE.Group>(null);
@@ -27,9 +32,10 @@ export function GameMap({ onOctreeReady }: GameMapProps): React.JSX.Element {
return; return;
} }
const loadedMapNodes = sceneData.mapNodes.filter((node) => const loadedMapNodes = sceneData.mapNodes.flatMap((node) => {
sceneData.models.has(node.name), const modelUrl = sceneData.models.get(node.name);
); return modelUrl ? [{ node, modelUrl }] : [];
});
const missingModelCount = const missingModelCount =
sceneData.mapNodes.length - loadedMapNodes.length; sceneData.mapNodes.length - loadedMapNodes.length;
@@ -54,16 +60,25 @@ export function GameMap({ onOctreeReady }: GameMapProps): React.JSX.Element {
<group ref={groupRef}> <group ref={groupRef}>
{!isLoading && {!isLoading &&
mapNodes.map((node, index) => ( mapNodes.map((node, index) => (
<ModelInstance key={index} node={node} /> <ModelInstance
key={index}
node={node.node}
modelUrl={node.modelUrl}
/>
))} ))}
</group> </group>
); );
} }
function ModelInstance({ node }: { node: MapNode }): React.JSX.Element { function ModelInstance({
const modelPath = `/models/${node.name}/model.gltf`; node,
modelUrl,
}: {
node: MapNode;
modelUrl: string;
}): React.JSX.Element {
const groupRef = useRef<THREE.Group>(null); const groupRef = useRef<THREE.Group>(null);
const { scene } = useGLTF(modelPath); const { scene } = useGLTF(modelUrl);
const sceneInstance = useMemo(() => scene.clone(true), [scene]); const sceneInstance = useMemo(() => scene.clone(true), [scene]);
const { position, rotation, scale } = node; const { position, rotation, scale } = node;