Skip to content

Commit 72e1229

Browse files
committed
save
1 parent a9deb30 commit 72e1229

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

docs/app/components/Dropdown.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function Dropdown() {
1414
const options: DemoOption[] = [
1515
{ label: "Viewer", href: "/demo" },
1616
{ label: "Asset Viewer", href: "/demo/assetviewer" },
17-
{ label: "Custom Editor", href: "/demo/customeditor" },
17+
{ label: "Custom Component", href: "/demo/customcomponent" },
1818
{ label: "World", href: "/demo/world" },
1919
{ label: "Physics", href: "/demo/physics" },
2020
{ label: "Quake", href: "/demo/quake" },
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Component, FieldRenderer, FieldDefinition } from "react-three-game";
2+
import { useFrame } from "@react-three/fiber";
3+
import { useRef } from "react";
4+
import { Group } from "three";
5+
6+
const rotatorFields: FieldDefinition[] = [
7+
{ name: 'speed', type: 'number', label: 'Rotation Speed', step: 0.1 },
8+
{
9+
name: 'axis',
10+
type: 'select',
11+
label: 'Rotation Axis',
12+
options: [
13+
{ value: 'x', label: 'X' },
14+
{ value: 'y', label: 'Y' },
15+
{ value: 'z', label: 'Z' },
16+
],
17+
},
18+
];
19+
20+
function RotatorComponentEditor({ component, onUpdate }: { component: any; onUpdate: (newComp: any) => void }) {
21+
return (
22+
<FieldRenderer
23+
fields={rotatorFields}
24+
values={component.properties}
25+
onChange={onUpdate}
26+
/>
27+
);
28+
}
29+
30+
// The view component for Rotator
31+
function RotatorView({ properties, children }: { properties: any; children?: React.ReactNode }) {
32+
const groupRef = useRef<Group>(null);
33+
const speed = properties.speed ?? 1.0;
34+
const axis = properties.axis ?? 'y';
35+
36+
useFrame((state, delta) => {
37+
if (groupRef.current) {
38+
if (axis === 'x') {
39+
groupRef.current.rotation.x += delta * speed;
40+
} else if (axis === 'y') {
41+
groupRef.current.rotation.y += delta * speed;
42+
} else if (axis === 'z') {
43+
groupRef.current.rotation.z += delta * speed;
44+
}
45+
}
46+
});
47+
48+
return (
49+
<group ref={groupRef}>
50+
{children}
51+
</group>
52+
);
53+
}
54+
55+
const RotatorComponent: Component = {
56+
name: 'Rotator',
57+
Editor: RotatorComponentEditor,
58+
View: RotatorView,
59+
defaultProperties: {
60+
speed: 1.0,
61+
axis: 'y'
62+
}
63+
};
64+
65+
export default RotatorComponent;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"use client";
22

33
import { PrefabEditor, registerComponent } from "react-three-game";
4-
import rotatorDemo from "../../samples/rotator-demo.json";
5-
import RotatorComponent from "../../plugins/RotatorComponent";
4+
import RotatorComponent from "./RotatorComponent";
5+
import rotatorDemo from "./rotator-demo.json";
66

77
// Register custom component before using the editor
88
registerComponent(RotatorComponent);
File renamed without changes.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-three-game",
3-
"version": "0.0.53",
3+
"version": "0.0.54",
44
"description": "Batteries included React Three Fiber game engine",
55
"main": "dist/index.js",
66
"module": "dist/index.js",

0 commit comments

Comments
 (0)