react-three-game 0.0.1 → 0.0.2
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/.gitattributes +2 -0
- package/.github/copilot-instructions.md +207 -0
- package/LICENSE +661 -0
- package/README.md +664 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/shared/GameCanvas.d.ts +6 -0
- package/dist/shared/GameCanvas.js +48 -0
- package/dist/shared/extend-three.d.ts +1 -0
- package/dist/shared/extend-three.js +13 -0
- package/dist/tools/assetviewer/page.d.ts +21 -0
- package/dist/tools/assetviewer/page.js +153 -0
- package/dist/tools/dragdrop/DragDropLoader.d.ts +9 -0
- package/dist/tools/dragdrop/DragDropLoader.js +78 -0
- package/dist/tools/dragdrop/modelLoader.d.ts +7 -0
- package/dist/tools/dragdrop/modelLoader.js +53 -0
- package/dist/tools/dragdrop/page.d.ts +1 -0
- package/dist/tools/dragdrop/page.js +11 -0
- package/dist/tools/prefabeditor/EditorTree.d.ts +10 -0
- package/dist/tools/prefabeditor/EditorTree.js +182 -0
- package/dist/tools/prefabeditor/EditorUI.d.ts +11 -0
- package/dist/tools/prefabeditor/EditorUI.js +96 -0
- package/dist/tools/prefabeditor/EventSystem.d.ts +7 -0
- package/dist/tools/prefabeditor/EventSystem.js +23 -0
- package/dist/tools/prefabeditor/InstanceProvider.d.ts +30 -0
- package/dist/tools/prefabeditor/InstanceProvider.js +172 -0
- package/dist/tools/prefabeditor/PrefabEditor.d.ts +4 -0
- package/dist/tools/prefabeditor/PrefabEditor.js +89 -0
- package/dist/tools/prefabeditor/PrefabRoot.d.ts +12 -0
- package/dist/tools/prefabeditor/PrefabRoot.js +273 -0
- package/dist/tools/prefabeditor/components/ComponentRegistry.d.ts +13 -0
- package/dist/tools/prefabeditor/components/ComponentRegistry.js +13 -0
- package/dist/tools/prefabeditor/components/GeometryComponent.d.ts +3 -0
- package/dist/tools/prefabeditor/components/GeometryComponent.js +28 -0
- package/dist/tools/prefabeditor/components/MaterialComponent.d.ts +3 -0
- package/dist/tools/prefabeditor/components/MaterialComponent.js +66 -0
- package/dist/tools/prefabeditor/components/ModelComponent.d.ts +3 -0
- package/dist/tools/prefabeditor/components/ModelComponent.js +39 -0
- package/dist/tools/prefabeditor/components/PhysicsComponent.d.ts +3 -0
- package/dist/tools/prefabeditor/components/PhysicsComponent.js +19 -0
- package/dist/tools/prefabeditor/components/SpotLightComponent.d.ts +3 -0
- package/dist/tools/prefabeditor/components/SpotLightComponent.js +19 -0
- package/dist/tools/prefabeditor/components/TransformComponent.d.ts +8 -0
- package/dist/tools/prefabeditor/components/TransformComponent.js +22 -0
- package/dist/tools/prefabeditor/components/index.d.ts +2 -0
- package/dist/tools/prefabeditor/components/index.js +14 -0
- package/dist/tools/prefabeditor/page.d.ts +1 -0
- package/dist/tools/prefabeditor/page.js +5 -0
- package/dist/tools/prefabeditor/types.d.ts +29 -0
- package/dist/tools/prefabeditor/types.js +1 -0
- package/package.json +16 -4
- package/tsconfig.json +2 -1
- package/dist/GameCanvas.d.ts +0 -6
- package/dist/GameCanvas.js +0 -5
package/README.md
ADDED
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
# react-three-game
|
|
2
|
+
|
|
3
|
+
> **The first 3D game engine designed for AI-native development.**
|
|
4
|
+
> Generate entire game scenes from natural language. Zero boilerplate, 100% declarative, fully typesafe.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm i react-three-game @react-three/fiber three
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
[](LICENSE)
|
|
11
|
+
[](https://www.typescriptlang.org/)
|
|
12
|
+
[](https://react.dev/)
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 🤖 Why This Exists
|
|
17
|
+
|
|
18
|
+
**Traditional 3D engines force you to write imperative code.** Unity requires C# classes. Unreal needs Blueprints. Three.js demands manual scene graph manipulation. **AI agents struggle with all of these.**
|
|
19
|
+
|
|
20
|
+
**react-three-game is different:**
|
|
21
|
+
- ✅ **Everything is JSON** - AI can generate complete scenes without writing code
|
|
22
|
+
- ✅ **Component-based architecture** - Like Unity, but declarative and serializable
|
|
23
|
+
- ✅ **Visual prefab editor** - Export scenes as versionable JSON files
|
|
24
|
+
- ✅ **Built on React Three Fiber** - Leverage the entire React ecosystem
|
|
25
|
+
- ✅ **WebGPU renderer** - Cutting-edge graphics with Three.js r181+
|
|
26
|
+
|
|
27
|
+
### The Problem We Solve
|
|
28
|
+
|
|
29
|
+
```jsx
|
|
30
|
+
// ❌ Traditional Three.js - Imperative, verbose, AI-hostile
|
|
31
|
+
const scene = new THREE.Scene();
|
|
32
|
+
const geometry = new THREE.BoxGeometry();
|
|
33
|
+
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
|
|
34
|
+
const cube = new THREE.Mesh(geometry, material);
|
|
35
|
+
cube.position.set(0, 1, 0);
|
|
36
|
+
scene.add(cube);
|
|
37
|
+
|
|
38
|
+
// Physics? Even worse...
|
|
39
|
+
const body = new CANNON.Body({ mass: 1 });
|
|
40
|
+
body.addShape(new CANNON.Box(new CANNON.Vec3(0.5, 0.5, 0.5)));
|
|
41
|
+
world.addBody(body);
|
|
42
|
+
// Now sync transforms every frame... 😱
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```jsx
|
|
46
|
+
// ✅ react-three-game - Declarative, concise, AI-friendly
|
|
47
|
+
<PrefabRoot data={{
|
|
48
|
+
root: {
|
|
49
|
+
id: "cube",
|
|
50
|
+
components: {
|
|
51
|
+
transform: { type: "Transform", properties: { position: [0, 1, 0] } },
|
|
52
|
+
geometry: { type: "Geometry", properties: { geometryType: "box" } },
|
|
53
|
+
material: { type: "Material", properties: { color: "green" } },
|
|
54
|
+
physics: { type: "Physics", properties: { type: "dynamic" } }
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}} />
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**Result:** AI agents can generate this JSON structure. Version control it. Modify it. No code generation required.
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## 🚀 Quick Start
|
|
65
|
+
|
|
66
|
+
### Installation
|
|
67
|
+
```bash
|
|
68
|
+
npm install react-three-game @react-three/fiber @react-three/rapier three
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Your First Scene (30 seconds)
|
|
72
|
+
|
|
73
|
+
```jsx
|
|
74
|
+
import { GameCanvas, PrefabRoot } from 'react-three-game';
|
|
75
|
+
|
|
76
|
+
export default function App() {
|
|
77
|
+
return (
|
|
78
|
+
<GameCanvas>
|
|
79
|
+
<ambientLight intensity={0.5} />
|
|
80
|
+
<PrefabRoot data={{
|
|
81
|
+
id: "scene",
|
|
82
|
+
root: {
|
|
83
|
+
id: "root",
|
|
84
|
+
enabled: true,
|
|
85
|
+
visible: true,
|
|
86
|
+
components: {
|
|
87
|
+
transform: {
|
|
88
|
+
type: "Transform",
|
|
89
|
+
properties: { position: [0, 0, 0], rotation: [0, 0, 0], scale: [1, 1, 1] }
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
children: [
|
|
93
|
+
{
|
|
94
|
+
id: "floor",
|
|
95
|
+
enabled: true,
|
|
96
|
+
visible: true,
|
|
97
|
+
components: {
|
|
98
|
+
transform: { type: "Transform", properties: { position: [0, -1, 0] } },
|
|
99
|
+
geometry: { type: "Geometry", properties: { geometryType: "box", args: [10, 0.5, 10] } },
|
|
100
|
+
material: { type: "Material", properties: { color: "#2d5f2e" } },
|
|
101
|
+
physics: { type: "Physics", properties: { type: "fixed" } }
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
id: "player",
|
|
106
|
+
enabled: true,
|
|
107
|
+
visible: true,
|
|
108
|
+
components: {
|
|
109
|
+
transform: { type: "Transform", properties: { position: [0, 2, 0] } },
|
|
110
|
+
geometry: { type: "Geometry", properties: { geometryType: "sphere" } },
|
|
111
|
+
material: { type: "Material", properties: { color: "#ff6b6b" } },
|
|
112
|
+
physics: { type: "Physics", properties: { type: "dynamic" } }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
}} />
|
|
118
|
+
</GameCanvas>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
**That's it.** Physics, rendering, transforms - all declarative. No boilerplate.
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## 🎮 Core Concepts
|
|
128
|
+
|
|
129
|
+
### 1. GameObjects & Components
|
|
130
|
+
|
|
131
|
+
Every object in your scene is a `GameObject` with modular components:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
interface GameObject {
|
|
135
|
+
id: string; // Unique identifier
|
|
136
|
+
enabled: boolean; // Active in scene?
|
|
137
|
+
visible: boolean; // Rendered?
|
|
138
|
+
components: {
|
|
139
|
+
transform?: TransformComponent; // Position/rotation/scale
|
|
140
|
+
geometry?: GeometryComponent; // Box, sphere, plane, etc.
|
|
141
|
+
material?: MaterialComponent; // Color, textures, PBR properties
|
|
142
|
+
physics?: PhysicsComponent; // Rapier physics body
|
|
143
|
+
model?: ModelComponent; // Load GLB/FBX models
|
|
144
|
+
// Add your own!
|
|
145
|
+
};
|
|
146
|
+
children?: GameObject[]; // Nested hierarchy
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**This is Unity/Unreal's ECS pattern, but 100% React.**
|
|
151
|
+
|
|
152
|
+
### 2. Visual Prefab Editor
|
|
153
|
+
|
|
154
|
+
Run the built-in editor:
|
|
155
|
+
```jsx
|
|
156
|
+
import { PrefabEditor } from 'react-three-game';
|
|
157
|
+
|
|
158
|
+
<PrefabEditor />
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
- 🎨 Drag-and-drop 3D models
|
|
162
|
+
- 🔧 Edit transforms with gizmos
|
|
163
|
+
- 📦 Add/remove components
|
|
164
|
+
- 💾 Export JSON files
|
|
165
|
+
- ▶️ Toggle edit/play mode
|
|
166
|
+
|
|
167
|
+
**Pro tip:** Use this to generate scenes, then let AI modify the JSON.
|
|
168
|
+
|
|
169
|
+
### 3. Component System
|
|
170
|
+
|
|
171
|
+
Create custom components in minutes:
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
// MyLaserComponent.tsx
|
|
175
|
+
import { Component } from 'react-three-game';
|
|
176
|
+
|
|
177
|
+
const LaserComponent: Component = {
|
|
178
|
+
name: 'Laser',
|
|
179
|
+
Editor: ({ component, onUpdate }) => (
|
|
180
|
+
<input
|
|
181
|
+
value={component.properties.damage}
|
|
182
|
+
onChange={e => onUpdate({ damage: +e.target.value })}
|
|
183
|
+
/>
|
|
184
|
+
),
|
|
185
|
+
View: ({ properties }) => (
|
|
186
|
+
<pointLight color="red" intensity={properties.damage} />
|
|
187
|
+
),
|
|
188
|
+
defaultProperties: { damage: 10 }
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export default LaserComponent;
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Register it once, use everywhere:
|
|
195
|
+
```typescript
|
|
196
|
+
import { registerComponent } from 'react-three-game';
|
|
197
|
+
registerComponent(LaserComponent);
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## 🏗️ Architecture
|
|
203
|
+
|
|
204
|
+
### GameObject Hierarchy
|
|
205
|
+
```
|
|
206
|
+
Scene Root
|
|
207
|
+
├─ Player (Dynamic Physics)
|
|
208
|
+
│ ├─ Camera
|
|
209
|
+
│ └─ Weapon (Model)
|
|
210
|
+
├─ Enemies (Instanced)
|
|
211
|
+
│ ├─ Enemy_01
|
|
212
|
+
│ ├─ Enemy_02
|
|
213
|
+
│ └─ Enemy_03
|
|
214
|
+
└─ Environment
|
|
215
|
+
├─ Ground (Fixed Physics)
|
|
216
|
+
└─ Obstacles
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Transform Math (Critical!)
|
|
220
|
+
- **Local transforms** stored in JSON (relative to parent)
|
|
221
|
+
- **World transforms** computed at runtime (for rendering)
|
|
222
|
+
- **TransformControls** use world space, then convert back to local
|
|
223
|
+
- Helper: `computeParentWorldMatrix()` handles the math
|
|
224
|
+
|
|
225
|
+
### Instancing System
|
|
226
|
+
Render 1000s of objects efficiently:
|
|
227
|
+
```json
|
|
228
|
+
{
|
|
229
|
+
"components": {
|
|
230
|
+
"model": {
|
|
231
|
+
"type": "Model",
|
|
232
|
+
"properties": {
|
|
233
|
+
"filename": "tree.glb",
|
|
234
|
+
"instanced": true // ← Magic flag
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
Behind the scenes: drei's `<Merged>` + `<InstancedRigidBodies>` for physics.
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## 🎯 Real-World Example
|
|
245
|
+
|
|
246
|
+
Here's what a complete multiplayer game looks like (coming from production code):
|
|
247
|
+
|
|
248
|
+
```jsx
|
|
249
|
+
import { GameCanvas, PrefabRoot } from 'react-three-game';
|
|
250
|
+
import { Physics } from '@react-three/rapier';
|
|
251
|
+
|
|
252
|
+
export default function Game() {
|
|
253
|
+
return (
|
|
254
|
+
<GameCanvas>
|
|
255
|
+
<Physics>
|
|
256
|
+
{/* Load entire scene from JSON */}
|
|
257
|
+
<PrefabRoot data={levelData} />
|
|
258
|
+
|
|
259
|
+
{/* Mix with React components */}
|
|
260
|
+
<Player controllable />
|
|
261
|
+
<Enemy position={[5, 0, -5]} />
|
|
262
|
+
<MovingPlatform path={[[0,0,0], [10,0,0]]} />
|
|
263
|
+
</Physics>
|
|
264
|
+
|
|
265
|
+
<ambientLight intensity={0.5} />
|
|
266
|
+
<directionalLight castShadow position={[10, 10, 5]} />
|
|
267
|
+
</GameCanvas>
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
**The power:** That `levelData` JSON can be:
|
|
273
|
+
- 🤖 Generated by AI from a prompt
|
|
274
|
+
- 🎨 Created in the visual editor
|
|
275
|
+
- 🔄 Version controlled in git
|
|
276
|
+
- 🌐 Loaded from a CMS
|
|
277
|
+
- 🧩 Composed from smaller prefabs
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
## 📦 What's Included
|
|
282
|
+
|
|
283
|
+
### Core Exports
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
import {
|
|
287
|
+
// Rendering
|
|
288
|
+
GameCanvas, // WebGPU Canvas wrapper
|
|
289
|
+
PrefabRoot, // Scene renderer from JSON
|
|
290
|
+
PrefabEditor, // Visual editor component
|
|
291
|
+
|
|
292
|
+
// Utils
|
|
293
|
+
loadModel, // GLB/FBX loader with Draco
|
|
294
|
+
registerComponent, // Add custom components
|
|
295
|
+
getComponent, // Query component registry
|
|
296
|
+
|
|
297
|
+
// Types
|
|
298
|
+
Prefab, // Prefab JSON structure
|
|
299
|
+
GameObject, // Scene node type
|
|
300
|
+
Component, // Component interface
|
|
301
|
+
} from 'react-three-game';
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### Built-in Components
|
|
305
|
+
|
|
306
|
+
| Component | Properties | Description |
|
|
307
|
+
|-----------|-----------|-------------|
|
|
308
|
+
| **Transform** | `position`, `rotation`, `scale` | 3D position/orientation (always present) |
|
|
309
|
+
| **Geometry** | `geometryType`, `args` | Box, sphere, plane, cylinder, etc. |
|
|
310
|
+
| **Material** | `color`, `texture`, `metalness`, `roughness` | PBR materials with texture support |
|
|
311
|
+
| **Physics** | `type` (`dynamic`/`fixed`) | Rapier rigid body |
|
|
312
|
+
| **Model** | `filename`, `instanced` | Load GLB/FBX, toggle GPU instancing |
|
|
313
|
+
| **SpotLight** | `color`, `intensity`, `angle`, `penumbra` | Dynamic lighting |
|
|
314
|
+
|
|
315
|
+
**Extending:** Create custom components in 20 lines - see "Component System" above.
|
|
316
|
+
|
|
317
|
+
---
|
|
318
|
+
|
|
319
|
+
## 🎨 Visual Editor
|
|
320
|
+
|
|
321
|
+
Import and use the prefab editor:
|
|
322
|
+
|
|
323
|
+
```jsx
|
|
324
|
+
import { PrefabEditor } from 'react-three-game';
|
|
325
|
+
|
|
326
|
+
export default function EditorPage() {
|
|
327
|
+
return <PrefabEditor />;
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### Editor Features
|
|
332
|
+
- **📥 Import/Export** - Load/save JSON prefabs
|
|
333
|
+
- **🎮 Edit/Play Toggle** - Test physics in real-time
|
|
334
|
+
- **🔧 Transform Gizmos** - Translate/Rotate/Scale (T/R/S keys)
|
|
335
|
+
- **🌳 Scene Tree** - Drag to reorder, click to select
|
|
336
|
+
- **📋 Inspector** - Edit component properties
|
|
337
|
+
- **➕ Add Components** - Dropdown to attach new behaviors
|
|
338
|
+
|
|
339
|
+
### Workflow
|
|
340
|
+
1. Create scene in editor
|
|
341
|
+
2. Export JSON
|
|
342
|
+
3. Load in game: `<PrefabRoot data={require('./level1.json')} />`
|
|
343
|
+
4. Or generate variations with AI by modifying the JSON
|
|
344
|
+
|
|
345
|
+
---
|
|
346
|
+
|
|
347
|
+
## 🚀 Advanced Patterns
|
|
348
|
+
|
|
349
|
+
### Loading External Prefabs
|
|
350
|
+
|
|
351
|
+
```jsx
|
|
352
|
+
import levelData from './prefabs/arena.json';
|
|
353
|
+
|
|
354
|
+
<PrefabRoot data={levelData} />
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### Mixing Prefabs with React Components
|
|
358
|
+
|
|
359
|
+
```jsx
|
|
360
|
+
<Physics>
|
|
361
|
+
<PrefabRoot data={environment} /> {/* Static level geometry */}
|
|
362
|
+
<Player /> {/* Dynamic player logic */}
|
|
363
|
+
<AIEnemies /> {/* Procedural spawning */}
|
|
364
|
+
</Physics>
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### Dynamic Instancing (1000+ Objects)
|
|
368
|
+
|
|
369
|
+
```json
|
|
370
|
+
{
|
|
371
|
+
"id": "forest",
|
|
372
|
+
"children": [
|
|
373
|
+
{
|
|
374
|
+
"id": "tree-1",
|
|
375
|
+
"components": {
|
|
376
|
+
"model": {
|
|
377
|
+
"type": "Model",
|
|
378
|
+
"properties": {
|
|
379
|
+
"filename": "tree.glb",
|
|
380
|
+
"instanced": true // ← Automatic GPU instancing
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
"physics": { "type": "Physics", "properties": { "type": "fixed" } }
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// Repeat 1000x - only renders once internally
|
|
387
|
+
]
|
|
388
|
+
}
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
### Custom Components (Real Example)
|
|
392
|
+
|
|
393
|
+
```tsx
|
|
394
|
+
// LaserBeamComponent.tsx
|
|
395
|
+
import { Component } from 'react-three-game';
|
|
396
|
+
import { Line } from '@react-three/drei';
|
|
397
|
+
|
|
398
|
+
const LaserBeam: Component = {
|
|
399
|
+
name: 'LaserBeam',
|
|
400
|
+
|
|
401
|
+
Editor: ({ component, onUpdate }) => (
|
|
402
|
+
<>
|
|
403
|
+
<label>Damage</label>
|
|
404
|
+
<input
|
|
405
|
+
type="number"
|
|
406
|
+
value={component.properties.damage}
|
|
407
|
+
onChange={e => onUpdate({ damage: +e.target.value })}
|
|
408
|
+
/>
|
|
409
|
+
<label>Color</label>
|
|
410
|
+
<input
|
|
411
|
+
type="color"
|
|
412
|
+
value={component.properties.color}
|
|
413
|
+
onChange={e => onUpdate({ color: e.target.value })}
|
|
414
|
+
/>
|
|
415
|
+
</>
|
|
416
|
+
),
|
|
417
|
+
|
|
418
|
+
View: ({ properties }) => (
|
|
419
|
+
<Line
|
|
420
|
+
points={[[0, 0, 0], [0, 0, -10]]}
|
|
421
|
+
color={properties.color}
|
|
422
|
+
lineWidth={3}
|
|
423
|
+
/>
|
|
424
|
+
),
|
|
425
|
+
|
|
426
|
+
defaultProperties: {
|
|
427
|
+
damage: 25,
|
|
428
|
+
color: '#ff0000'
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
export default LaserBeam;
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
Then register it:
|
|
436
|
+
```tsx
|
|
437
|
+
import { registerComponent } from 'react-three-game';
|
|
438
|
+
import LaserBeam from './components/LaserBeam';
|
|
439
|
+
|
|
440
|
+
registerComponent(LaserBeam);
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
Now it's available in the editor dropdown AND can be serialized in prefab JSON!
|
|
444
|
+
|
|
445
|
+
---
|
|
446
|
+
|
|
447
|
+
## 🤝 Integrations
|
|
448
|
+
|
|
449
|
+
### React Three Fiber Ecosystem
|
|
450
|
+
All `@react-three/drei` helpers work seamlessly:
|
|
451
|
+
|
|
452
|
+
```jsx
|
|
453
|
+
import { OrbitControls, Sky, ContactShadows } from '@react-three/drei';
|
|
454
|
+
|
|
455
|
+
<GameCanvas>
|
|
456
|
+
<Sky />
|
|
457
|
+
<OrbitControls />
|
|
458
|
+
<PrefabRoot data={scene} />
|
|
459
|
+
<ContactShadows />
|
|
460
|
+
</GameCanvas>
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
### Physics (@react-three/rapier)
|
|
464
|
+
Wrap your scene in `<Physics>`:
|
|
465
|
+
|
|
466
|
+
```jsx
|
|
467
|
+
import { Physics } from '@react-three/rapier';
|
|
468
|
+
|
|
469
|
+
<Physics gravity={[0, -9.8, 0]}>
|
|
470
|
+
<PrefabRoot data={level} />
|
|
471
|
+
</Physics>
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
Components with `physics` property automatically get rigid bodies.
|
|
475
|
+
|
|
476
|
+
---
|
|
477
|
+
|
|
478
|
+
## 🎯 For AI Agents
|
|
479
|
+
|
|
480
|
+
### Prompt Templates
|
|
481
|
+
|
|
482
|
+
**Generate a complete scene:**
|
|
483
|
+
```
|
|
484
|
+
Create a react-three-game prefab JSON for a platformer level with:
|
|
485
|
+
- A ground plane (10x10, fixed physics, grass texture)
|
|
486
|
+
- 5 floating platforms (dynamic physics)
|
|
487
|
+
- A player spawn point at [0, 5, 0]
|
|
488
|
+
- 3 collectible coins using sphere geometry
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
**Modify existing scenes:**
|
|
492
|
+
```
|
|
493
|
+
Take this prefab JSON and add:
|
|
494
|
+
- A spotlight pointing at the player spawn
|
|
495
|
+
- Convert all "box" geometry to "sphere"
|
|
496
|
+
- Scale all objects by 1.5x
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
**Generate component variations:**
|
|
500
|
+
```
|
|
501
|
+
Create 10 enemy prefab variants by:
|
|
502
|
+
- Randomizing position within bounds [[-10,10], [0,5], [-10,10]]
|
|
503
|
+
- Varying scale from 0.8 to 1.2
|
|
504
|
+
- Using colors from palette: ["#ff6b6b", "#ee5a6f", "#c44569"]
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
### JSON Structure Reference
|
|
508
|
+
|
|
509
|
+
```typescript
|
|
510
|
+
{
|
|
511
|
+
"id": "unique-id",
|
|
512
|
+
"root": {
|
|
513
|
+
"id": "root-id",
|
|
514
|
+
"enabled": true,
|
|
515
|
+
"visible": true,
|
|
516
|
+
"components": {
|
|
517
|
+
"transform": {
|
|
518
|
+
"type": "Transform",
|
|
519
|
+
"properties": {
|
|
520
|
+
"position": [x, y, z], // Numbers in world units
|
|
521
|
+
"rotation": [x, y, z], // Radians
|
|
522
|
+
"scale": [x, y, z] // Multipliers
|
|
523
|
+
}
|
|
524
|
+
},
|
|
525
|
+
"geometry": {
|
|
526
|
+
"type": "Geometry",
|
|
527
|
+
"properties": {
|
|
528
|
+
"geometryType": "box" | "sphere" | "plane" | "cylinder" | "cone" | "torus",
|
|
529
|
+
"args": [/* geometry-specific arguments */]
|
|
530
|
+
}
|
|
531
|
+
},
|
|
532
|
+
"material": {
|
|
533
|
+
"type": "Material",
|
|
534
|
+
"properties": {
|
|
535
|
+
"color": "#rrggbb" | "colorname",
|
|
536
|
+
"texture": "/path/to/texture.jpg", // Optional
|
|
537
|
+
"metalness": 0.0-1.0, // Optional
|
|
538
|
+
"roughness": 0.0-1.0 // Optional
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
"physics": {
|
|
542
|
+
"type": "Physics",
|
|
543
|
+
"properties": {
|
|
544
|
+
"type": "dynamic" | "fixed" // Dynamic = moves, Fixed = static
|
|
545
|
+
}
|
|
546
|
+
},
|
|
547
|
+
"model": {
|
|
548
|
+
"type": "Model",
|
|
549
|
+
"properties": {
|
|
550
|
+
"filename": "/models/asset.glb",
|
|
551
|
+
"instanced": true // Optional: GPU instancing
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
},
|
|
555
|
+
"children": [/* Recursive GameObject array */]
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
---
|
|
561
|
+
|
|
562
|
+
## 🛠️ Development
|
|
563
|
+
|
|
564
|
+
### Local Setup
|
|
565
|
+
```bash
|
|
566
|
+
git clone https://github.com/prnthh/react-three-game.git
|
|
567
|
+
cd react-three-game
|
|
568
|
+
npm install
|
|
569
|
+
npm run dev # Runs tsc --watch + Next.js docs site
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
### Project Structure
|
|
573
|
+
```
|
|
574
|
+
/src → Library source (exports to npm)
|
|
575
|
+
/shared → GameCanvas (WebGPU wrapper)
|
|
576
|
+
/tools
|
|
577
|
+
/prefabeditor → Visual editor + PrefabRoot renderer
|
|
578
|
+
/docs → Next.js documentation site
|
|
579
|
+
/app → Demo pages
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
### Building
|
|
583
|
+
```bash
|
|
584
|
+
npm run build # Compile TypeScript → /dist
|
|
585
|
+
npm publish # Publish to npm
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
---
|
|
589
|
+
|
|
590
|
+
## 🌟 Roadmap
|
|
591
|
+
|
|
592
|
+
- [x] Core prefab system
|
|
593
|
+
- [x] Visual editor
|
|
594
|
+
- [x] Component registry
|
|
595
|
+
- [x] GPU instancing
|
|
596
|
+
- [x] Physics integration
|
|
597
|
+
- [ ] Input system (keyboard/gamepad/touch) - **Coming Soon**
|
|
598
|
+
- [ ] Multiplayer primitives (WebRTC sync)
|
|
599
|
+
- [ ] Animation system (state machines)
|
|
600
|
+
- [ ] Audio components (spatial sound)
|
|
601
|
+
- [ ] Particle effects
|
|
602
|
+
- [ ] AI behavior trees (as JSON!)
|
|
603
|
+
|
|
604
|
+
---
|
|
605
|
+
|
|
606
|
+
## 🤖 Why Developers AND AI Love This
|
|
607
|
+
|
|
608
|
+
### For Developers
|
|
609
|
+
- ✅ **Skip the boilerplate** - No manual scene graph management
|
|
610
|
+
- ✅ **React patterns** - Use hooks, context, state like normal React
|
|
611
|
+
- ✅ **Visual debugging** - See and edit your scene in real-time
|
|
612
|
+
- ✅ **Type safety** - Full TypeScript support
|
|
613
|
+
- ✅ **Hot reload** - Changes reflect instantly
|
|
614
|
+
|
|
615
|
+
### For AI Agents
|
|
616
|
+
- ✅ **Pure data** - No imperative code to generate
|
|
617
|
+
- ✅ **JSON schema** - Structured, validatable format
|
|
618
|
+
- ✅ **Compositional** - Build complex scenes from simple primitives
|
|
619
|
+
- ✅ **Version controllable** - Git-friendly text files
|
|
620
|
+
- ✅ **Deterministic** - Same JSON = same scene every time
|
|
621
|
+
|
|
622
|
+
---
|
|
623
|
+
|
|
624
|
+
## 📚 Examples
|
|
625
|
+
|
|
626
|
+
Check out `/docs/app/demo` for live examples:
|
|
627
|
+
- Basic scene with physics
|
|
628
|
+
- Model loading and instancing
|
|
629
|
+
- Custom component creation
|
|
630
|
+
- Multiplayer game prototype
|
|
631
|
+
|
|
632
|
+
---
|
|
633
|
+
|
|
634
|
+
## 🤝 Contributing
|
|
635
|
+
|
|
636
|
+
This is an **AI-first** project. We welcome:
|
|
637
|
+
- New built-in components
|
|
638
|
+
- Documentation improvements
|
|
639
|
+
- Example scenes/games
|
|
640
|
+
- AI prompt templates
|
|
641
|
+
- Bug reports
|
|
642
|
+
|
|
643
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
|
|
644
|
+
|
|
645
|
+
---
|
|
646
|
+
|
|
647
|
+
## 📄 License
|
|
648
|
+
|
|
649
|
+
MIT © [prnth](https://github.com/prnthh)
|
|
650
|
+
|
|
651
|
+
---
|
|
652
|
+
|
|
653
|
+
## 💬 Community
|
|
654
|
+
|
|
655
|
+
- 🐦 Twitter: [@prnth](https://twitter.com/prnth)
|
|
656
|
+
- 💼 GitHub Issues: [Report bugs](https://github.com/prnthh/react-three-game/issues)
|
|
657
|
+
- 💡 Discussions: [Share ideas](https://github.com/prnthh/react-three-game/discussions)
|
|
658
|
+
|
|
659
|
+
---
|
|
660
|
+
|
|
661
|
+
**Built with:** React 19 • Three.js r181 • TypeScript 5 • WebGPU • Rapier Physics
|
|
662
|
+
|
|
663
|
+
**Status:** Alpha v0.0.1 - API may change
|
|
664
|
+
**AI Prompt to Share:** "Build me a 3D game using react-three-game with [your features]"
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export { default as GameCanvas } from './GameCanvas';
|
|
1
|
+
export { default as GameCanvas } from './shared/GameCanvas';
|
|
2
|
+
export { default as PrefabEditor } from './tools/prefabeditor/PrefabEditor';
|
|
3
|
+
export { default as PrefabRoot } from './tools/prefabeditor/PrefabRoot';
|
|
4
|
+
export { DragDropLoader } from './tools/dragdrop/DragDropLoader';
|
package/dist/index.js
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export { default as GameCanvas } from './GameCanvas';
|
|
1
|
+
export { default as GameCanvas } from './shared/GameCanvas';
|
|
2
|
+
export { default as PrefabEditor } from './tools/prefabeditor/PrefabEditor';
|
|
3
|
+
export { default as PrefabRoot } from './tools/prefabeditor/PrefabRoot';
|
|
4
|
+
export { DragDropLoader } from './tools/dragdrop/DragDropLoader';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { WebGPURendererParameters } from "three/src/renderers/webgpu/WebGPURenderer.Nodes.js";
|
|
2
|
+
export default function GameCanvas({ loader, children, ...props }: {
|
|
3
|
+
loader?: boolean;
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
props?: WebGPURendererParameters;
|
|
6
|
+
}): import("react/jsx-runtime").JSX.Element;
|