Skip to content

Commit

Permalink
Fixes the Camera Component Editor for Lens and Clipping planes update…
Browse files Browse the repository at this point in the history
…s when changing between cameras (#263)
  • Loading branch information
jwills-jdubs authored and diehbria committed Oct 17, 2022
1 parent 9f0f6c9 commit c3442bb
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 57 deletions.
6 changes: 3 additions & 3 deletions packages/scene-composer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@
"jest": {
"coverageThreshold": {
"global": {
"lines": 76.6,
"statements": 75.7,
"lines": 76.64,
"statements": 75.75,
"functions": 76.03,
"branches": 62.0,
"branches": 62.22,
"branchesTrue": 100
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { PerspectiveCamera } from 'three';
import { Button, FormField, Grid, Select, SpaceBetween, TextContent } from '@awsui/components-react';
import { useIntl } from 'react-intl';
Expand Down Expand Up @@ -74,6 +74,8 @@ const CameraComponentEditor: React.FC<ICameraComponentEditorProps> = ({
zoom: cameraComponent.zoom,
});

const [needsUpdate, setNeedsUpdate] = useState<boolean>(false);

const focalLengthOptions = [
{
label: focalLengthIntlMessages.fifteenMilliMeters,
Expand Down Expand Up @@ -128,52 +130,51 @@ const CameraComponentEditor: React.FC<ICameraComponentEditorProps> = ({
},
];

const recalculateCameraSettings = useMemo(
() =>
(updatedSettings: {
fov?: number;
focalLength?: number;
zoom?: number;
near?: number;
far?: number;
}): CameraEditorSettings => {
const aspectDefault = 1;
const tempCamera = new PerspectiveCamera(
cameraSettings.fov,
aspectDefault,
cameraSettings.near,
cameraSettings.far,
);

if (updatedSettings.far) {
tempCamera.far = updatedSettings.far;
}

if (updatedSettings.near) {
tempCamera.near = updatedSettings.near;
}

if (updatedSettings.fov) {
tempCamera.fov = updatedSettings.fov;
}

if (updatedSettings.focalLength) {
tempCamera.setFocalLength(updatedSettings.focalLength);
}

if (updatedSettings.zoom) {
tempCamera.zoom = updatedSettings.zoom;
}

return {
cameraType: cameraSettings.cameraType,
focalLength: tempCamera.getFocalLength(),
fov: tempCamera.getEffectiveFOV(),
far: tempCamera.far,
near: tempCamera.near,
zoom: tempCamera.zoom,
};
},
const recalculateCameraSettings = useCallback(
(updatedSettings: {
fov?: number;
focalLength?: number;
zoom?: number;
near?: number;
far?: number;
}): CameraEditorSettings => {
const aspectDefault = 1;
const tempCamera = new PerspectiveCamera(
cameraSettings.fov,
aspectDefault,
cameraSettings.near,
cameraSettings.far,
);

if (updatedSettings.far) {
tempCamera.far = updatedSettings.far;
}

if (updatedSettings.near) {
tempCamera.near = updatedSettings.near;
}

if (updatedSettings.fov) {
tempCamera.fov = updatedSettings.fov;
}

if (updatedSettings.focalLength) {
tempCamera.setFocalLength(updatedSettings.focalLength);
}

if (updatedSettings.zoom) {
tempCamera.zoom = updatedSettings.zoom;
}

return {
cameraType: cameraSettings.cameraType,
focalLength: tempCamera.getFocalLength(),
fov: tempCamera.getEffectiveFOV(),
far: tempCamera.far,
near: tempCamera.near,
zoom: tempCamera.zoom,
};
},
[cameraSettings],
);

Expand Down Expand Up @@ -224,6 +225,24 @@ const CameraComponentEditor: React.FC<ICameraComponentEditorProps> = ({
}
};

useEffect(() => {
const updatedSettings = recalculateCameraSettings({
fov: cameraComponent.fov,
far: cameraComponent.far,
near: cameraComponent.near,
zoom: cameraComponent.zoom,
});
updateFocalLength(updatedSettings.focalLength);
updateZoom(updatedSettings.zoom);
setCameraSettings({
cameraType: cameraComponent.cameraType,
fov: updatedSettings.fov,
far: updatedSettings.far,
near: updatedSettings.near,
zoom: updatedSettings.zoom,
});
}, [cameraComponent.ref]);

const updateSettings = (updatedSettings: CameraEditorSettings) => {
const updatedCameraSettings = {
cameraType: updatedSettings.cameraType,
Expand All @@ -236,16 +255,20 @@ const CameraComponentEditor: React.FC<ICameraComponentEditorProps> = ({
setCameraSettings(updatedCameraSettings);
updateFocalLength(updatedSettings.focalLength);
updateZoom(updatedSettings.zoom);
setNeedsUpdate(true);
};

useEffect(() => {
const updatedComponent: ICameraComponentInternal = {
...component,
...cameraSettings,
};

updateComponentInternal(node.ref, updatedComponent, true);
}, [cameraSettings]);
if (needsUpdate) {
const updatedComponent: ICameraComponentInternal = {
...component,
...cameraSettings,
};

updateComponentInternal(node.ref, updatedComponent, true);
setNeedsUpdate(false);
}
}, [needsUpdate]);

return (
<SpaceBetween size='m'>
Expand Down

0 comments on commit c3442bb

Please sign in to comment.