Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature to lock camera position relative to the car #177

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/components/maps/LoadedReplays.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ const LoadedReplays = ({
onChange={(e: RadioChangeEvent) => { timeLineGlobal.cameraMode = e.target.value; }}
className="flex gap-4"
>
{timeLineGlobal.followedReplay && (
<Radio.Button value={CameraMode.Lock}>
Lock
</Radio.Button>
)}
<Radio.Button value={CameraMode.Target}>
Target
</Radio.Button>
Expand Down
19 changes: 19 additions & 0 deletions app/components/viewer/ReplayCars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const ReplayCar = ({
let curSample = replay.samples[0];
const smoothSample: ReplayDataPoint = { ...replay.samples[0] };

// keep track of the camera mode switches
let prevCamMode = timeLineGlobal.cameraMode;

// Get own material from loaded car model
const carMesh: THREE.Mesh = fbx.children[0] as THREE.Mesh;
const material: THREE.MeshPhongMaterial = carMesh.material as THREE.MeshPhongMaterial;
Expand Down Expand Up @@ -109,6 +112,21 @@ const ReplayCar = ({
if (orbitControlsRef && orbitControlsRef.current) {
orbitControlsRef.current.target.lerp(smoothSample.position, 0.2);

if (timeLineGlobal.cameraMode === CameraMode.Lock) {
// Set camPosRef to camera position only once
if (prevCamMode !== timeLineGlobal.cameraMode) {
camPosRef.current.position.set(
camera.position.x - mesh.current.position.x,
camera.position.y - mesh.current.position.y,
camera.position.z - mesh.current.position.z,
);
}
// move camera to camPosMesh world position
const camWorldPos: THREE.Vector3 = new THREE.Vector3();
camPosRef.current.getWorldPosition(camWorldPos);
camera.position.lerp(camWorldPos, 0.3);
}

if (timeLineGlobal.cameraMode === CameraMode.Follow) {
// move camPosMesh to Follow position
camPosRef.current.rotation.setFromQuaternion(carRotation);
Expand Down Expand Up @@ -142,6 +160,7 @@ const ReplayCar = ({
} else {
stadiumCarMesh.current.scale.lerp(new THREE.Vector3(0.01, 0.01, 0.01), 0.2);
}
prevCamMode = timeLineGlobal.cameraMode;
}
});

Expand Down
1 change: 1 addition & 0 deletions app/lib/contexts/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { LineType, LineTypes } from '../../components/viewer/ReplayLines';
export enum CameraMode {
Target,
Follow,
Lock
}

export interface SettingsContextProps {
Expand Down