Skip to content

Commit 6956226

Browse files
fix 3D camera load bug
1 parent 5417553 commit 6956226

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

frontend/three_d_garden/__tests__/garden_model_test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,27 @@ import {
2323

2424
let isDesktopSpy: jest.SpyInstance;
2525
let isMobileSpy: jest.SpyInstance;
26+
let useStateSpy: jest.SpyInstance;
2627
const originalPathname = location.pathname;
2728
const mountedWrappers: ReturnType<typeof createRenderer>[] = [];
2829

2930
describe("<GardenModel />", () => {
3031
beforeEach(() => {
3132
mockIsDesktop = false;
3233
mockIsMobile = false;
34+
let useStateCalls = 0;
35+
useStateSpy = jest.spyOn(React, "useState")
36+
// eslint-disable-next-line comma-spacing
37+
.mockImplementation(<S,>(initialState?: S | (() => S)) => {
38+
useStateCalls += 1;
39+
if (useStateCalls == 2) {
40+
return [{} as S, jest.fn()];
41+
}
42+
const value = typeof initialState == "function"
43+
? (initialState as () => S)()
44+
: initialState;
45+
return [value as S, jest.fn()];
46+
});
3347
isDesktopSpy = jest.spyOn(screenSize, "isDesktop")
3448
.mockImplementation(() => mockIsDesktop);
3549
isMobileSpy = jest.spyOn(screenSize, "isMobile")
@@ -39,6 +53,7 @@ describe("<GardenModel />", () => {
3953
afterEach(() => {
4054
mountedWrappers.splice(0).forEach(wrapper =>
4155
unmountRenderer(wrapper));
56+
useStateSpy.mockRestore();
4257
isDesktopSpy.mockRestore();
4358
isMobileSpy.mockRestore();
4459
location.pathname = originalPathname;

frontend/three_d_garden/garden_model.tsx

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
Sphere,
88
StatsGl,
99
} from "@react-three/drei";
10-
import { BackSide, MeshBasicMaterial as ThreeMeshBasicMaterial } from "three";
10+
import {
11+
BackSide,
12+
MeshBasicMaterial as ThreeMeshBasicMaterial,
13+
OrthographicCamera as ThreeOrthographicCamera,
14+
PerspectiveCamera as ThreePerspectiveCamera,
15+
} from "three";
1116
import { Bot } from "./bot";
1217
import { AddPlantProps, Bed } from "./bed";
1318
import {
@@ -111,6 +116,9 @@ export const GardenModel = (props: GardenModelProps) => {
111116
const topDown = addPlantProps?.designer.threeDTopDownView;
112117
const topDownMobile = topDown && isMobile();
113118
const camera = getCamera(config, props.activeFocus, cameraInit(!!topDown));
119+
const [controlsCamera, setControlsCamera] =
120+
// eslint-disable-next-line no-null/no-null
121+
React.useState<ThreePerspectiveCamera | ThreeOrthographicCamera | null>(null);
114122

115123
const showPlants = !addPlantProps
116124
|| !!addPlantProps.getConfigValue(BooleanSetting.show_plants);
@@ -163,24 +171,29 @@ export const GardenModel = (props: GardenModelProps) => {
163171
</Sphere>
164172
<AnimatedGroup
165173
scale={props.activeFocus ? 1 : scale}>
166-
<Camera makeDefault={true} name={"camera"}
174+
<Camera
175+
ref={setControlsCamera}
176+
makeDefault={true}
177+
name={"camera"}
167178
fov={40} near={10} far={BigDistance.far}
168179
position={camera.position}
169180
rotation={[0, 0, 0]}
170181
zoom={topDown ? 0.25 : 1}
171182
up={[0, 0, 1]} />
172183
</AnimatedGroup>
173-
<OrbitControls
174-
maxPolarAngle={Math.PI / 2}
175-
minAzimuthAngle={topDownMobile ? Math.PI / 2 : undefined}
176-
maxAzimuthAngle={topDownMobile ? Math.PI / 2 : undefined}
177-
enableRotate={config.rotate}
178-
enableZoom={config.zoom}
179-
enablePan={config.pan}
180-
dampingFactor={0.2}
181-
target={camera.target}
182-
minDistance={config.lightsDebug ? 50 : 500}
183-
maxDistance={config.lightsDebug ? BigDistance.devZoom : BigDistance.zoom} />
184+
{controlsCamera &&
185+
<OrbitControls
186+
camera={controlsCamera}
187+
maxPolarAngle={Math.PI / 2}
188+
minAzimuthAngle={topDownMobile ? Math.PI / 2 : undefined}
189+
maxAzimuthAngle={topDownMobile ? Math.PI / 2 : undefined}
190+
enableRotate={config.rotate}
191+
enableZoom={config.zoom}
192+
enablePan={config.pan}
193+
dampingFactor={0.2}
194+
target={camera.target}
195+
minDistance={config.lightsDebug ? 50 : 500}
196+
maxDistance={config.lightsDebug ? BigDistance.devZoom : BigDistance.zoom} />}
184197
<AxesHelper args={[5000]} visible={config.threeAxes} />
185198
{config.viewCube && <GizmoHelper><GizmoViewcube /></GizmoHelper>}
186199
<Sun

0 commit comments

Comments
 (0)