react-three-game 0.0.59 → 0.0.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/tools/dragdrop/DragDropLoader.d.ts +8 -8
- package/dist/tools/dragdrop/DragDropLoader.js +33 -15
- package/dist/tools/dragdrop/index.d.ts +3 -3
- package/dist/tools/dragdrop/index.js +1 -1
- package/dist/tools/dragdrop/modelLoader.d.ts +10 -1
- package/dist/tools/dragdrop/modelLoader.js +39 -0
- package/dist/tools/prefabeditor/PrefabEditor.js +17 -26
- package/dist/tools/prefabeditor/PrefabRoot.d.ts +1 -1
- package/dist/tools/prefabeditor/PrefabRoot.js +2 -8
- package/package.json +9 -3
- package/.gitattributes +0 -2
- package/.github/copilot-instructions.md +0 -83
- package/.github/workflows/nextjs.yml +0 -99
- package/.gitmodules +0 -3
- package/assets/architecture.png +0 -0
- package/assets/editor.gif +0 -0
- package/assets/favicon.ico +0 -0
- package/assets/react-three-game-logo.png +0 -0
- package/dist/tools/dragdrop/page.d.ts +0 -1
- package/dist/tools/dragdrop/page.js +0 -11
- package/dist/tools/prefabeditor/EntityEvents.d.ts +0 -54
- package/dist/tools/prefabeditor/EntityEvents.js +0 -85
- package/dist/tools/prefabeditor/page.d.ts +0 -1
- package/dist/tools/prefabeditor/page.js +0 -5
- package/react-three-game-skill/.gitattributes +0 -2
- package/react-three-game-skill/README.md +0 -7
- package/react-three-game-skill/react-three-game/SKILL.md +0 -514
- package/react-three-game-skill/react-three-game/rules/ADVANCED_PHYSICS.md +0 -472
- package/react-three-game-skill/react-three-game/rules/LIGHTING.md +0 -6
- package/src/helpers/SoundManager.ts +0 -130
- package/src/helpers/index.ts +0 -91
- package/src/index.ts +0 -59
- package/src/shared/ContactShadow.tsx +0 -74
- package/src/shared/GameCanvas.tsx +0 -52
- package/src/tools/assetviewer/page.tsx +0 -425
- package/src/tools/dragdrop/DragDropLoader.tsx +0 -136
- package/src/tools/dragdrop/index.ts +0 -4
- package/src/tools/dragdrop/modelLoader.ts +0 -145
- package/src/tools/dragdrop/page.tsx +0 -45
- package/src/tools/prefabeditor/Dropdown.tsx +0 -112
- package/src/tools/prefabeditor/EditorContext.tsx +0 -25
- package/src/tools/prefabeditor/EditorTree.tsx +0 -452
- package/src/tools/prefabeditor/EditorTreeMenus.tsx +0 -307
- package/src/tools/prefabeditor/EditorUI.tsx +0 -204
- package/src/tools/prefabeditor/EventSystem.tsx +0 -36
- package/src/tools/prefabeditor/GameEvents.ts +0 -191
- package/src/tools/prefabeditor/InstanceProvider.tsx +0 -466
- package/src/tools/prefabeditor/PrefabEditor.tsx +0 -262
- package/src/tools/prefabeditor/PrefabRoot.tsx +0 -773
- package/src/tools/prefabeditor/components/AmbientLightComponent.tsx +0 -34
- package/src/tools/prefabeditor/components/CameraComponent.tsx +0 -117
- package/src/tools/prefabeditor/components/ComponentRegistry.ts +0 -40
- package/src/tools/prefabeditor/components/DirectionalLightComponent.tsx +0 -210
- package/src/tools/prefabeditor/components/EnvironmentComponent.tsx +0 -47
- package/src/tools/prefabeditor/components/GeometryComponent.tsx +0 -133
- package/src/tools/prefabeditor/components/Input.tsx +0 -820
- package/src/tools/prefabeditor/components/MaterialComponent.tsx +0 -431
- package/src/tools/prefabeditor/components/ModelComponent.tsx +0 -176
- package/src/tools/prefabeditor/components/PhysicsComponent.tsx +0 -188
- package/src/tools/prefabeditor/components/SpotLightComponent.tsx +0 -109
- package/src/tools/prefabeditor/components/TextComponent.tsx +0 -137
- package/src/tools/prefabeditor/components/TransformComponent.tsx +0 -173
- package/src/tools/prefabeditor/components/index.ts +0 -26
- package/src/tools/prefabeditor/page.tsx +0 -10
- package/src/tools/prefabeditor/styles.ts +0 -235
- package/src/tools/prefabeditor/types.ts +0 -20
- package/src/tools/prefabeditor/utils.ts +0 -312
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
import GameCanvas from "../../shared/GameCanvas";
|
|
2
|
-
import { useState, useRef, useEffect, forwardRef, useImperativeHandle } from "react";
|
|
3
|
-
import { Prefab } from "./types";
|
|
4
|
-
import PrefabRoot, { PrefabRootRef } from "./PrefabRoot";
|
|
5
|
-
import { Physics } from "@react-three/rapier";
|
|
6
|
-
import EditorUI from "./EditorUI";
|
|
7
|
-
import { base, toolbar } from "./styles";
|
|
8
|
-
import { EditorContext } from "./EditorContext";
|
|
9
|
-
import { exportGLB, createModelNode, createImageNode } from "./utils";
|
|
10
|
-
import { parseModelFromFile } from "../dragdrop";
|
|
11
|
-
|
|
12
|
-
export interface PrefabEditorRef {
|
|
13
|
-
screenshot: () => void;
|
|
14
|
-
exportGLB: () => void;
|
|
15
|
-
prefab: Prefab;
|
|
16
|
-
setPrefab: (prefab: Prefab) => void;
|
|
17
|
-
rootRef: React.RefObject<PrefabRootRef | null>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const DEFAULT_PREFAB: Prefab = {
|
|
21
|
-
id: "prefab-default",
|
|
22
|
-
name: "New Prefab",
|
|
23
|
-
root: {
|
|
24
|
-
id: "root",
|
|
25
|
-
components: {
|
|
26
|
-
transform: {
|
|
27
|
-
type: "Transform",
|
|
28
|
-
properties: { position: [0, 0, 0], rotation: [0, 0, 0], scale: [1, 1, 1] }
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const PrefabEditor = forwardRef<PrefabEditorRef, {
|
|
35
|
-
basePath?: string;
|
|
36
|
-
initialPrefab?: Prefab;
|
|
37
|
-
physics?: boolean;
|
|
38
|
-
onPrefabChange?: (prefab: Prefab) => void;
|
|
39
|
-
uiPlugins?: React.ReactNode[] | React.ReactNode;
|
|
40
|
-
children?: React.ReactNode;
|
|
41
|
-
}>(({ basePath, initialPrefab, physics = true, onPrefabChange, uiPlugins, children }, ref) => {
|
|
42
|
-
const [editMode, setEditMode] = useState(true);
|
|
43
|
-
const [loadedPrefab, setLoadedPrefab] = useState<Prefab>(initialPrefab ?? DEFAULT_PREFAB);
|
|
44
|
-
const [selectedId, setSelectedId] = useState<string | null>(null);
|
|
45
|
-
const [transformMode, setTransformMode] = useState<"translate" | "rotate" | "scale">("translate");
|
|
46
|
-
const [snapResolution, setSnapResolution] = useState(0);
|
|
47
|
-
const [positionSnap, setPositionSnap] = useState(0.5);
|
|
48
|
-
const [rotationSnap, setRotationSnap] = useState(Math.PI / 4);
|
|
49
|
-
const [history, setHistory] = useState<Prefab[]>([loadedPrefab]);
|
|
50
|
-
const [historyIndex, setHistoryIndex] = useState(0);
|
|
51
|
-
const throttleRef = useRef<NodeJS.Timeout | null>(null);
|
|
52
|
-
const lastDataRef = useRef(JSON.stringify(loadedPrefab));
|
|
53
|
-
const prefabRootRef = useRef<PrefabRootRef>(null);
|
|
54
|
-
const canvasRef = useRef<HTMLCanvasElement | null>(null);
|
|
55
|
-
|
|
56
|
-
useEffect(() => {
|
|
57
|
-
if (initialPrefab) setLoadedPrefab(initialPrefab);
|
|
58
|
-
}, [initialPrefab]);
|
|
59
|
-
|
|
60
|
-
const updatePrefab = (newPrefab: Prefab | ((prev: Prefab) => Prefab)) => {
|
|
61
|
-
setLoadedPrefab(newPrefab);
|
|
62
|
-
const resolved = typeof newPrefab === 'function' ? newPrefab(loadedPrefab) : newPrefab;
|
|
63
|
-
onPrefabChange?.(resolved);
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const applyHistory = (index: number) => {
|
|
67
|
-
setHistoryIndex(index);
|
|
68
|
-
lastDataRef.current = JSON.stringify(history[index]);
|
|
69
|
-
setLoadedPrefab(history[index]);
|
|
70
|
-
onPrefabChange?.(history[index]);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
const undo = () => historyIndex > 0 && applyHistory(historyIndex - 1);
|
|
74
|
-
const redo = () => historyIndex < history.length - 1 && applyHistory(historyIndex + 1);
|
|
75
|
-
|
|
76
|
-
useEffect(() => {
|
|
77
|
-
const handleKeyDown = (e: KeyboardEvent) => {
|
|
78
|
-
if (!(e.ctrlKey || e.metaKey)) return;
|
|
79
|
-
if (e.key === 'z' && !e.shiftKey) { e.preventDefault(); undo(); }
|
|
80
|
-
else if ((e.shiftKey && e.key === 'z') || e.key === 'y') { e.preventDefault(); redo(); }
|
|
81
|
-
};
|
|
82
|
-
window.addEventListener('keydown', handleKeyDown);
|
|
83
|
-
return () => window.removeEventListener('keydown', handleKeyDown);
|
|
84
|
-
}, [historyIndex, history]);
|
|
85
|
-
|
|
86
|
-
useEffect(() => {
|
|
87
|
-
const currentStr = JSON.stringify(loadedPrefab);
|
|
88
|
-
if (currentStr === lastDataRef.current) return;
|
|
89
|
-
if (throttleRef.current) clearTimeout(throttleRef.current);
|
|
90
|
-
|
|
91
|
-
throttleRef.current = setTimeout(() => {
|
|
92
|
-
lastDataRef.current = currentStr;
|
|
93
|
-
setHistory(prev => {
|
|
94
|
-
const newHistory = [...prev.slice(0, historyIndex + 1), loadedPrefab];
|
|
95
|
-
return newHistory.length > 50 ? newHistory.slice(1) : newHistory;
|
|
96
|
-
});
|
|
97
|
-
setHistoryIndex(prev => Math.min(prev + 1, 49));
|
|
98
|
-
}, 500);
|
|
99
|
-
|
|
100
|
-
return () => { if (throttleRef.current) clearTimeout(throttleRef.current); };
|
|
101
|
-
}, [loadedPrefab]);
|
|
102
|
-
|
|
103
|
-
const handleScreenshot = () => {
|
|
104
|
-
const canvas = canvasRef.current;
|
|
105
|
-
if (!canvas) return;
|
|
106
|
-
|
|
107
|
-
canvas.toBlob((blob) => {
|
|
108
|
-
if (!blob) return;
|
|
109
|
-
const url = URL.createObjectURL(blob);
|
|
110
|
-
const a = document.createElement('a');
|
|
111
|
-
a.href = url;
|
|
112
|
-
a.download = `${loadedPrefab.name || 'screenshot'}.png`;
|
|
113
|
-
a.click();
|
|
114
|
-
URL.revokeObjectURL(url);
|
|
115
|
-
});
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
const handleExportGLB = () => {
|
|
119
|
-
const sceneRoot = prefabRootRef.current?.root;
|
|
120
|
-
if (!sceneRoot) return;
|
|
121
|
-
|
|
122
|
-
exportGLB(sceneRoot, {
|
|
123
|
-
filename: `${loadedPrefab.name || 'scene'}.glb`
|
|
124
|
-
});
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
const handleFocusNode = (nodeId: string) => {
|
|
128
|
-
prefabRootRef.current?.focusNode(nodeId);
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
useEffect(() => {
|
|
132
|
-
const canvas = document.querySelector('canvas');
|
|
133
|
-
if (canvas) canvasRef.current = canvas;
|
|
134
|
-
}, []);
|
|
135
|
-
|
|
136
|
-
// --- Drag & drop files to add nodes ---
|
|
137
|
-
useEffect(() => {
|
|
138
|
-
const IMAGE_EXTS = ['png', 'jpg', 'jpeg', 'webp', 'gif', 'bmp', 'svg'];
|
|
139
|
-
const MODEL_EXTS = ['glb', 'gltf', 'fbx'];
|
|
140
|
-
|
|
141
|
-
function handleDragOver(e: DragEvent) {
|
|
142
|
-
e.preventDefault();
|
|
143
|
-
e.stopPropagation();
|
|
144
|
-
}
|
|
145
|
-
function handleDrop(e: DragEvent) {
|
|
146
|
-
e.preventDefault();
|
|
147
|
-
e.stopPropagation();
|
|
148
|
-
const files = e.dataTransfer?.files ? Array.from(e.dataTransfer.files) : [];
|
|
149
|
-
files.forEach(file => {
|
|
150
|
-
const ext = file.name.split('.').pop()?.toLowerCase();
|
|
151
|
-
if (!ext) return;
|
|
152
|
-
|
|
153
|
-
const baseName = file.name.replace(/\.[^.]+$/, '');
|
|
154
|
-
|
|
155
|
-
if (MODEL_EXTS.includes(ext)) {
|
|
156
|
-
const modelPath = `models/${file.name}`;
|
|
157
|
-
const newNode = createModelNode(modelPath, baseName);
|
|
158
|
-
|
|
159
|
-
updatePrefab(prev => ({
|
|
160
|
-
...prev,
|
|
161
|
-
root: { ...prev.root, children: [...(prev.root.children ?? []), newNode] }
|
|
162
|
-
}));
|
|
163
|
-
|
|
164
|
-
parseModelFromFile(file).then(result => {
|
|
165
|
-
if (result.success && result.model) {
|
|
166
|
-
prefabRootRef.current?.injectModel(modelPath, result.model);
|
|
167
|
-
} else {
|
|
168
|
-
console.error('Drop parse error:', result.error);
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
} else if (IMAGE_EXTS.includes(ext)) {
|
|
172
|
-
const texturePath = `textures/${file.name}`;
|
|
173
|
-
const newNode = createImageNode(texturePath, baseName);
|
|
174
|
-
|
|
175
|
-
updatePrefab(prev => ({
|
|
176
|
-
...prev,
|
|
177
|
-
root: { ...prev.root, children: [...(prev.root.children ?? []), newNode] }
|
|
178
|
-
}));
|
|
179
|
-
|
|
180
|
-
// Inject a blob URL texture so it renders immediately
|
|
181
|
-
prefabRootRef.current?.injectTexture(texturePath, file);
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
window.addEventListener('dragover', handleDragOver);
|
|
187
|
-
window.addEventListener('drop', handleDrop);
|
|
188
|
-
return () => {
|
|
189
|
-
window.removeEventListener('dragover', handleDragOver);
|
|
190
|
-
window.removeEventListener('drop', handleDrop);
|
|
191
|
-
};
|
|
192
|
-
}, [loadedPrefab]);
|
|
193
|
-
|
|
194
|
-
useImperativeHandle(ref, () => ({
|
|
195
|
-
screenshot: handleScreenshot,
|
|
196
|
-
exportGLB: handleExportGLB,
|
|
197
|
-
prefab: loadedPrefab,
|
|
198
|
-
setPrefab: setLoadedPrefab,
|
|
199
|
-
rootRef: prefabRootRef
|
|
200
|
-
}), [loadedPrefab]);
|
|
201
|
-
|
|
202
|
-
const content = (
|
|
203
|
-
<>
|
|
204
|
-
<ambientLight intensity={1.5} />
|
|
205
|
-
<gridHelper args={[10, 10]} position={[0, -1, 0]} />
|
|
206
|
-
<PrefabRoot
|
|
207
|
-
ref={prefabRootRef}
|
|
208
|
-
data={loadedPrefab}
|
|
209
|
-
editMode={editMode}
|
|
210
|
-
onPrefabChange={updatePrefab}
|
|
211
|
-
selectedId={selectedId}
|
|
212
|
-
onSelect={setSelectedId}
|
|
213
|
-
basePath={basePath}
|
|
214
|
-
/>
|
|
215
|
-
{children}
|
|
216
|
-
</>
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
return <EditorContext.Provider value={{
|
|
220
|
-
transformMode,
|
|
221
|
-
setTransformMode,
|
|
222
|
-
snapResolution,
|
|
223
|
-
setSnapResolution,
|
|
224
|
-
positionSnap,
|
|
225
|
-
setPositionSnap,
|
|
226
|
-
rotationSnap,
|
|
227
|
-
setRotationSnap,
|
|
228
|
-
onFocusNode: handleFocusNode,
|
|
229
|
-
onScreenshot: handleScreenshot,
|
|
230
|
-
onExportGLB: handleExportGLB
|
|
231
|
-
}}>
|
|
232
|
-
<GameCanvas camera={{ position: [0, 5, 15] }}>
|
|
233
|
-
{physics ? (
|
|
234
|
-
<Physics debug={editMode} paused={editMode}>
|
|
235
|
-
{content}
|
|
236
|
-
</Physics>
|
|
237
|
-
) : content}
|
|
238
|
-
</GameCanvas>
|
|
239
|
-
|
|
240
|
-
<div style={toolbar.panel}>
|
|
241
|
-
<button style={base.btn} onClick={() => setEditMode(!editMode)}>
|
|
242
|
-
{editMode ? "▶" : "⏸"}
|
|
243
|
-
</button>
|
|
244
|
-
{uiPlugins}
|
|
245
|
-
</div>
|
|
246
|
-
<EditorUI
|
|
247
|
-
prefabData={loadedPrefab}
|
|
248
|
-
setPrefabData={updatePrefab}
|
|
249
|
-
selectedId={selectedId}
|
|
250
|
-
setSelectedId={setSelectedId}
|
|
251
|
-
basePath={basePath}
|
|
252
|
-
onUndo={undo}
|
|
253
|
-
onRedo={redo}
|
|
254
|
-
canUndo={historyIndex > 0}
|
|
255
|
-
canRedo={historyIndex < history.length - 1}
|
|
256
|
-
/>
|
|
257
|
-
</EditorContext.Provider>
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
PrefabEditor.displayName = "PrefabEditor";
|
|
261
|
-
|
|
262
|
-
export default PrefabEditor;
|