Skip to content

Commit d2f0cde

Browse files
committed
GEP-200 Add default options for the CameraControls
1 parent ceded14 commit d2f0cde

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

examples/vite-3DCanvas-react-app/src/components/viewers/Canvas3DExample.tsx

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import React, { useRef, useState } from "react";
2-
import { Canvas3D } from "@metacell/geppetto-meta-ui/3d-canvas/Canvas3D";
2+
import {
3+
Canvas3D,
4+
Canvas3DRootState,
5+
} from "@metacell/geppetto-meta-ui/3d-canvas/Canvas3D";
36
import { Box } from "@mui/material";
47
import { useFrame } from "@react-three/fiber";
58
import { Mesh } from "three";
69
import { CameraControls } from "@react-three/drei";
7-
import { Toolbar3D, Toolbar3DButton, Toolbar3DSeparator } from "../toolbar/Toolbar3D";
10+
import {
11+
Toolbar3D,
12+
Toolbar3DButton,
13+
Toolbar3DSeparator,
14+
} from "../toolbar/Toolbar3D";
815
import { CameraControlsProvider } from "../toolbar/CameraControlsContext";
916
import {
1017
Navigation3D,
@@ -54,24 +61,27 @@ const Canvas3DExample: React.FC = () => {
5461
const [activeCamera, setActiveCamera] = useState(false);
5562
const cameraControlsRef = useRef<CameraControls>(null);
5663

57-
const handleCameraClick = (fiber: any) => {
64+
const handleCameraClick = (fiber: Canvas3DRootState) => {
5865
console.log("Camera clicked", fiber.camera);
66+
console.log("Camera clicked", fiber);
5967
console.log("Current activeCamera state:", activeCamera);
60-
console.log("CameraControls ref:", cameraControlsRef.current);
68+
console.log("CameraControls:", fiber.controls);
6169

6270
setActiveCamera(!activeCamera);
6371

64-
if (cameraControlsRef.current) {
72+
if (fiber.controls) {
6573
if (!activeCamera) {
6674
console.log("Setting focus position...");
67-
cameraControlsRef.current.setLookAt(
75+
76+
/* prettier-ignore */
77+
fiber.controls.setLookAt(
6878
5, 5, 5, // Camera position
6979
0, 0, 0, // Look at center
7080
true // Enable animation
7181
);
7282
} else {
7383
console.log("Resetting camera...");
74-
cameraControlsRef.current.reset(true);
84+
fiber.controls.reset(true);
7585
}
7686
} else {
7787
console.log("CameraControls ref is null!");
@@ -80,14 +90,14 @@ const Canvas3DExample: React.FC = () => {
8090

8191
const Scene = () => (
8292
<>
83-
<CameraControls
93+
{/* <CameraControls
8494
ref={cameraControlsRef}
8595
minDistance={2}
8696
maxDistance={20}
8797
enablePan={true}
8898
enableZoom={true}
8999
enableRotate={true}
90-
/>
100+
/> */}
91101
<MyRotatingBox />
92102
<ambientLight intensity={0.5} />
93103
<pointLight position={[10, 10, 10]} />
@@ -115,7 +125,16 @@ const Canvas3DExample: React.FC = () => {
115125
</Toolbar3D>
116126
</CameraControlsProvider>
117127
<Box sx={classes.canvasContainer}>
118-
<Canvas3D frameloop={"always"} nonInteractive={true}>
128+
<Canvas3D
129+
frameloop={"always"}
130+
cameraOptions={{
131+
minDistance: 2,
132+
maxDistance: 20,
133+
enablePan: true,
134+
enableZoom: true,
135+
enableRotate: true,
136+
}}
137+
>
119138
<Scene />
120139
</Canvas3D>
121140
</Box>

geppetto.js/geppetto-ui/src/3d-canvas/Canvas3D.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Canvas, CanvasProps, useThree } from "@react-three/fiber";
22
import React, { forwardRef, useEffect, useRef } from "react";
33

44
import { RootState } from "@react-three/fiber";
5-
import { CameraControls } from "@react-three/drei";
5+
import { CameraControls, CameraControlsProps } from "@react-three/drei";
66
import { useState } from "react";
77
import { Loader } from "three";
88
import create from "zustand";
@@ -14,6 +14,7 @@ type Canvas3DBaseProps = {
1414
ref?;
1515
defaultLightOff?: boolean;
1616
nonInteractive?: boolean;
17+
cameraOptions?: CameraControlsProps;
1718
};
1819

1920
type Canvas3DProps = Canvas3DBaseProps &
@@ -153,6 +154,7 @@ export const Canvas3D: React.FC<Canvas3DProps> = forwardRef<
153154
children,
154155
defaultLightOff = false,
155156
nonInteractive = false,
157+
cameraOptions,
156158
...canvasProps
157159
},
158160
ref
@@ -167,15 +169,19 @@ export const Canvas3D: React.FC<Canvas3DProps> = forwardRef<
167169
</>
168170
)}
169171
{children}
170-
{!nonInteractive && <CameraControls />}
172+
{!nonInteractive && <CameraControls makeDefault {...cameraOptions} />}
171173
</Canvas>
172174
);
173175
}
174176
);
175177

178+
export type Canvas3DRootState = Omit<RootState, "controls"> & {
179+
controls: CameraControls;
180+
};
181+
176182
type FiberStore = {
177-
rootStates: Record<string, RootState | null>;
178-
setRootState: (id: string, state: RootState) => void;
183+
rootStates: Record<string, Canvas3DRootState | null>;
184+
setRootState: (id: string, state: Canvas3DRootState) => void;
179185
clearRootState: (id: string) => void;
180186
};
181187

@@ -207,6 +213,7 @@ const FiberBridge: React.FC<{ storeId?: string }> = ({ storeId }) => {
207213
const clearRootState = useFiberStore((s) => s.clearRootState);
208214

209215
React.useEffect(() => {
216+
// @ts-expect-error
210217
setRootState(id, state);
211218
return () => {
212219
clearRootState(id);

0 commit comments

Comments
 (0)