fix editor map reliability
This commit is contained in:
@@ -74,12 +74,12 @@ jobs:
|
|||||||
|
|
||||||
- name: 📏 Check bundle size
|
- name: 📏 Check bundle size
|
||||||
run: |
|
run: |
|
||||||
# Get bundle size in KB
|
# Check generated app assets only; public/ model files are runtime assets copied to dist.
|
||||||
SIZE=$(du -k dist | cut -f1)
|
SIZE=$(du -k dist/assets | cut -f1)
|
||||||
echo "Bundle size: ${SIZE}KB"
|
echo "Bundle size: ${SIZE}KB"
|
||||||
|
|
||||||
# Threshold: 1000KB (configurable)
|
# Threshold: 5000KB (configurable)
|
||||||
THRESHOLD=1000
|
THRESHOLD=5000
|
||||||
|
|
||||||
if [ "$SIZE" -gt "$THRESHOLD" ]; then
|
if [ "$SIZE" -gt "$THRESHOLD" ]; then
|
||||||
echo "❌ Bundle size ${SIZE}KB exceeds threshold ${THRESHOLD}KB"
|
echo "❌ Bundle size ${SIZE}KB exceeds threshold ${THRESHOLD}KB"
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ interface EditorControlsProps {
|
|||||||
onUndo: () => void;
|
onUndo: () => void;
|
||||||
onRedo: () => void;
|
onRedo: () => void;
|
||||||
onExportJson: () => void;
|
onExportJson: () => void;
|
||||||
onSaveToServer?: () => void;
|
onSaveToServer?: (() => void | Promise<void>) | undefined;
|
||||||
onPlayerMode?: () => void;
|
onPlayerMode?: (() => void) | undefined;
|
||||||
isPlayerMode?: boolean;
|
isPlayerMode?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,20 +47,11 @@ export function EditorControls({
|
|||||||
onPlayerMode,
|
onPlayerMode,
|
||||||
isPlayerMode,
|
isPlayerMode,
|
||||||
}: EditorControlsProps): React.JSX.Element {
|
}: EditorControlsProps): React.JSX.Element {
|
||||||
const cameraPosition = [0, 50, 100];
|
|
||||||
const viewModeLabel = isPlayerMode ? "View locked" : "Lock view";
|
const viewModeLabel = isPlayerMode ? "View locked" : "Lock view";
|
||||||
const jsonPreview = getJsonPreview(mapNodes, selectedNodeIndex);
|
const jsonPreview = getJsonPreview(mapNodes, selectedNodeIndex);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="editor-camera-info">
|
|
||||||
<span>Camera</span>
|
|
||||||
<strong>
|
|
||||||
X {cameraPosition[0]!.toFixed(0)} · Y {cameraPosition[1]!.toFixed(0)}{" "}
|
|
||||||
· Z {cameraPosition[2]!.toFixed(0)}
|
|
||||||
</strong>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<aside className="editor-controls-panel" aria-label="Editor controls">
|
<aside className="editor-controls-panel" aria-label="Editor controls">
|
||||||
<header className="editor-panel-header">
|
<header className="editor-panel-header">
|
||||||
<span className="editor-panel-kicker">Map Editor</span>
|
<span className="editor-panel-kicker">Map Editor</span>
|
||||||
|
|||||||
@@ -64,6 +64,30 @@ function useRegisteredEditorNode(
|
|||||||
}, [node, objectRef]);
|
}, [node, objectRef]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function disposeMaterial(material: THREE.Material | THREE.Material[]): void {
|
||||||
|
if (Array.isArray(material)) {
|
||||||
|
material.forEach((item) => item.dispose());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
material.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneHighlightedMaterial(
|
||||||
|
material: THREE.Material | THREE.Material[],
|
||||||
|
color: string,
|
||||||
|
): THREE.Material | THREE.Material[] {
|
||||||
|
if (Array.isArray(material)) {
|
||||||
|
return material.map((item) => cloneHighlightedMaterial(item, color)).flat();
|
||||||
|
}
|
||||||
|
|
||||||
|
const clone = material.clone();
|
||||||
|
if (clone instanceof THREE.MeshStandardMaterial) {
|
||||||
|
clone.color.set(color);
|
||||||
|
}
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
export function EditorMap({
|
export function EditorMap({
|
||||||
sceneData,
|
sceneData,
|
||||||
selectedNodeIndex,
|
selectedNodeIndex,
|
||||||
@@ -194,6 +218,9 @@ function EditorModelNode({
|
|||||||
modelUrl: string;
|
modelUrl: string;
|
||||||
}) {
|
}) {
|
||||||
const groupRef = useRef<THREE.Group>(null);
|
const groupRef = useRef<THREE.Group>(null);
|
||||||
|
const originalMaterialsRef = useRef(
|
||||||
|
new Map<THREE.Mesh, THREE.Material | THREE.Material[]>(),
|
||||||
|
);
|
||||||
const { scene } = useGLTF(modelUrl);
|
const { scene } = useGLTF(modelUrl);
|
||||||
|
|
||||||
const sceneInstance = useMemo(() => scene.clone(true), [scene]);
|
const sceneInstance = useMemo(() => scene.clone(true), [scene]);
|
||||||
@@ -201,24 +228,59 @@ function EditorModelNode({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!groupRef.current) return;
|
if (!groupRef.current) return;
|
||||||
|
const highlightColor = isSelected
|
||||||
|
? "#ffffff"
|
||||||
|
: isHovered
|
||||||
|
? "#b8b8b8"
|
||||||
|
: null;
|
||||||
|
|
||||||
groupRef.current.traverse((child) => {
|
groupRef.current.traverse((child) => {
|
||||||
if (!(child instanceof THREE.Mesh)) {
|
if (!(child instanceof THREE.Mesh)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (child.material instanceof THREE.MeshStandardMaterial) {
|
const originalMaterial = originalMaterialsRef.current.get(child);
|
||||||
if (isSelected) {
|
|
||||||
child.material = child.material.clone();
|
if (!originalMaterial) {
|
||||||
child.material.color.set("#ffffff");
|
originalMaterialsRef.current.set(child, child.material);
|
||||||
} else if (isHovered) {
|
}
|
||||||
child.material = child.material.clone();
|
|
||||||
child.material.color.set("#b8b8b8");
|
if (child.material !== originalMaterial && originalMaterial) {
|
||||||
}
|
disposeMaterial(child.material);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (highlightColor) {
|
||||||
|
child.material = cloneHighlightedMaterial(
|
||||||
|
originalMaterial ?? child.material,
|
||||||
|
highlightColor,
|
||||||
|
);
|
||||||
|
} else if (originalMaterial) {
|
||||||
|
child.material = originalMaterial;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [isSelected, isHovered]);
|
}, [isSelected, isHovered]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const group = groupRef.current;
|
||||||
|
const originalMaterials = originalMaterialsRef.current;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (!group) return;
|
||||||
|
|
||||||
|
group.traverse((child) => {
|
||||||
|
if (!(child instanceof THREE.Mesh)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const originalMaterial = originalMaterials.get(child);
|
||||||
|
if (originalMaterial && child.material !== originalMaterial) {
|
||||||
|
disposeMaterial(child.material);
|
||||||
|
child.material = originalMaterial;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<primitive
|
<primitive
|
||||||
ref={groupRef}
|
ref={groupRef}
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ export function EditorPage(): React.JSX.Element {
|
|||||||
a.href = url;
|
a.href = url;
|
||||||
a.download = "map.json";
|
a.download = "map.json";
|
||||||
a.click();
|
a.click();
|
||||||
URL.revokeObjectURL(url);
|
window.setTimeout(() => URL.revokeObjectURL(url), 0);
|
||||||
}, [sceneData]);
|
}, [sceneData]);
|
||||||
|
|
||||||
const handlePlayerMode = useCallback(() => {
|
const handlePlayerMode = useCallback(() => {
|
||||||
@@ -185,7 +185,7 @@ export function EditorPage(): React.JSX.Element {
|
|||||||
onUndo={handleUndo}
|
onUndo={handleUndo}
|
||||||
onRedo={handleRedo}
|
onRedo={handleRedo}
|
||||||
onExportJson={handleExportJson}
|
onExportJson={handleExportJson}
|
||||||
onSaveToServer={handleSaveToServer}
|
onSaveToServer={import.meta.env.DEV ? handleSaveToServer : undefined}
|
||||||
onPlayerMode={handlePlayerMode}
|
onPlayerMode={handlePlayerMode}
|
||||||
isPlayerMode={isPlayerMode}
|
isPlayerMode={isPlayerMode}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -22,28 +22,19 @@ async function createSceneData(mapNodes: MapNode[]): Promise<SceneData> {
|
|||||||
async function loadMapModelUrls(
|
async function loadMapModelUrls(
|
||||||
mapNodes: MapNode[],
|
mapNodes: MapNode[],
|
||||||
): Promise<Map<string, string>> {
|
): Promise<Map<string, string>> {
|
||||||
const models = new 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(
|
||||||
|
uniqueModelNames.map(async (modelName) => {
|
||||||
|
const modelUrl = `/models/${modelName}/${MODEL_FILE_NAME}`;
|
||||||
|
|
||||||
for (const modelName of uniqueModelNames) {
|
try {
|
||||||
const modelUrl = `/models/${modelName}/${MODEL_FILE_NAME}`;
|
const response = await fetch(modelUrl, { method: "HEAD" });
|
||||||
|
return response.ok ? ([modelName, modelUrl] as const) : null;
|
||||||
const modelResponse = await fetch(modelUrl);
|
} catch {
|
||||||
if (!modelResponse.ok) continue;
|
return null;
|
||||||
|
}
|
||||||
const text = await modelResponse.text();
|
}),
|
||||||
if (isGltfContent(text)) {
|
|
||||||
models.set(modelName, modelUrl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return models;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isGltfContent(text: string): boolean {
|
|
||||||
return (
|
|
||||||
text.includes('"glTF"') ||
|
|
||||||
text.includes('"scene"') ||
|
|
||||||
text.includes('"nodes"')
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return new Map(modelEntries.filter((entry) => entry !== null));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useState, useRef } from "react";
|
import { useEffect, useMemo, useState, useRef } from "react";
|
||||||
import { useGLTF } from "@react-three/drei";
|
import { useGLTF } from "@react-three/drei";
|
||||||
import * as THREE from "three";
|
import * as THREE from "three";
|
||||||
import { useOctreeGraphNode } from "@/hooks/useOctreeGraphNode";
|
import { useOctreeGraphNode } from "@/hooks/useOctreeGraphNode";
|
||||||
@@ -57,6 +57,7 @@ function ModelInstance({ node }: { node: MapNode }): React.JSX.Element {
|
|||||||
const modelPath = `/models/${node.name}/model.gltf`;
|
const modelPath = `/models/${node.name}/model.gltf`;
|
||||||
const groupRef = useRef<THREE.Group>(null);
|
const groupRef = useRef<THREE.Group>(null);
|
||||||
const { scene } = useGLTF(modelPath);
|
const { scene } = useGLTF(modelPath);
|
||||||
|
const sceneInstance = useMemo(() => scene.clone(true), [scene]);
|
||||||
const { position, rotation, scale } = node;
|
const { position, rotation, scale } = node;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -70,7 +71,7 @@ function ModelInstance({ node }: { node: MapNode }): React.JSX.Element {
|
|||||||
return (
|
return (
|
||||||
<primitive
|
<primitive
|
||||||
ref={groupRef}
|
ref={groupRef}
|
||||||
object={scene}
|
object={sceneInstance}
|
||||||
position={position}
|
position={position}
|
||||||
rotation={rotation}
|
rotation={rotation}
|
||||||
scale={scale}
|
scale={scale}
|
||||||
|
|||||||
+53
-8
@@ -3,18 +3,59 @@ import react from "@vitejs/plugin-react";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
|
import type { ServerResponse } from "node:http";
|
||||||
import type { Plugin } from "vite";
|
import type { Plugin } from "vite";
|
||||||
|
|
||||||
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
const __dirname = fileURLToPath(new URL(".", import.meta.url));
|
||||||
|
|
||||||
const MAX_MAP_PAYLOAD_BYTES = 1024 * 1024;
|
const MAX_MAP_PAYLOAD_BYTES = 1024 * 1024;
|
||||||
|
const JSON_HEADERS = { "Content-Type": "application/json" };
|
||||||
|
|
||||||
|
function sendJson(
|
||||||
|
res: ServerResponse,
|
||||||
|
status: number,
|
||||||
|
body: unknown,
|
||||||
|
headers: Record<string, string> = {},
|
||||||
|
): void {
|
||||||
|
res
|
||||||
|
.writeHead(status, { ...JSON_HEADERS, ...headers })
|
||||||
|
.end(JSON.stringify(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
function isVector3(value: unknown): value is [number, number, number] {
|
||||||
|
return (
|
||||||
|
Array.isArray(value) &&
|
||||||
|
value.length === 3 &&
|
||||||
|
value.every((item) => typeof item === "number" && Number.isFinite(item))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isMapNode(value: unknown): value is Record<string, unknown> {
|
||||||
|
if (typeof value !== "object" || value === null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const node = value as Record<string, unknown>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
typeof node.name === "string" &&
|
||||||
|
typeof node.type === "string" &&
|
||||||
|
isVector3(node.position) &&
|
||||||
|
isVector3(node.rotation) &&
|
||||||
|
isVector3(node.scale)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isMapPayload(value: unknown): boolean {
|
||||||
|
return Array.isArray(value) && value.every(isMapNode);
|
||||||
|
}
|
||||||
|
|
||||||
const saveMapPlugin = (): Plugin => ({
|
const saveMapPlugin = (): Plugin => ({
|
||||||
name: "save-map-api",
|
name: "save-map-api",
|
||||||
configureServer(server) {
|
configureServer(server) {
|
||||||
server.middlewares.use("/api/save-map", async (req, res) => {
|
server.middlewares.use("/api/save-map", async (req, res) => {
|
||||||
if (req.method !== "POST") {
|
if (req.method !== "POST") {
|
||||||
res.writeHead(405).end(JSON.stringify({ error: "Method not allowed" }));
|
sendJson(res, 405, { error: "Method not allowed" }, { Allow: "POST" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,30 +63,34 @@ const saveMapPlugin = (): Plugin => ({
|
|||||||
let size = 0;
|
let size = 0;
|
||||||
|
|
||||||
for await (const chunk of req) {
|
for await (const chunk of req) {
|
||||||
size += chunk.length;
|
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
||||||
|
size += buffer.length;
|
||||||
if (size > MAX_MAP_PAYLOAD_BYTES) {
|
if (size > MAX_MAP_PAYLOAD_BYTES) {
|
||||||
res
|
sendJson(res, 413, { error: "Payload too large" });
|
||||||
.writeHead(413)
|
|
||||||
.end(JSON.stringify({ error: "Payload too large" }));
|
|
||||||
req.destroy();
|
req.destroy();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
chunks.push(chunk);
|
chunks.push(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(Buffer.concat(chunks).toString());
|
const data = JSON.parse(Buffer.concat(chunks).toString());
|
||||||
|
if (!isMapPayload(data)) {
|
||||||
|
sendJson(res, 400, { error: "Invalid map payload" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const mapPath = path.resolve(__dirname, "public/map.json");
|
const mapPath = path.resolve(__dirname, "public/map.json");
|
||||||
await fs.promises.writeFile(
|
await fs.promises.writeFile(
|
||||||
mapPath,
|
mapPath,
|
||||||
JSON.stringify(data, null, 2),
|
JSON.stringify(data, null, 2),
|
||||||
"utf8",
|
"utf8",
|
||||||
);
|
);
|
||||||
res.writeHead(200).end(JSON.stringify({ success: true }));
|
sendJson(res, 200, { success: true });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const status = err instanceof SyntaxError ? 400 : 500;
|
const status = err instanceof SyntaxError ? 400 : 500;
|
||||||
const message = err instanceof Error ? err.message : "Unknown error";
|
const message = err instanceof Error ? err.message : "Unknown error";
|
||||||
res.writeHead(status).end(JSON.stringify({ error: message }));
|
sendJson(res, status, { error: message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user