mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-02 05:48:57 +08:00
Export camera path (#398)
* export button working * view changes * adding copy to clipboard feature * camera choice dropdown * going to add script saving next * copy paste working. need to add aspect ratio * aspect overlay kind of working * lint * aspect ratio fixed. version specified * merge * openrelay back
This commit is contained in:
1 parent
f21375ff1b
commit
9bc39d895c
21 files changed
+676
-196
No files matched your search
@@ -30,7 +30,7 @@ class CameraHelper extends THREE.Mesh {
|
||||
const geometry = new THREE.BufferGeometry();
|
||||
const material = new MeshLineMaterial({
|
||||
color: 0x000000,
|
||||
lineWidth: 0.04,
|
||||
lineWidth: 0.01,
|
||||
});
|
||||
|
||||
const vertices = [];
|
||||
@@ -106,25 +106,22 @@ class CameraHelper extends THREE.Mesh {
|
||||
// we need just camera projection matrix inverse
|
||||
// world matrix must be identity
|
||||
|
||||
console.log(_camera.projectionMatrixInverse);
|
||||
|
||||
_camera.projectionMatrixInverse.copy(this.camera.projectionMatrixInverse);
|
||||
|
||||
// center
|
||||
|
||||
setPoint('c', pointMap, geometry, _camera, 0, 0, -1);
|
||||
setPoint('t', pointMap, geometry, _camera, 0, 0, 1);
|
||||
|
||||
// near
|
||||
|
||||
setPoint('n1', pointMap, geometry, _camera, -w, -h, z);
|
||||
setPoint('n2', pointMap, geometry, _camera, w, -h, z);
|
||||
setPoint('n3', pointMap, geometry, _camera, -w, h, z);
|
||||
setPoint('n4', pointMap, geometry, _camera, w, h, z);
|
||||
setPoint('n1', pointMap, geometry, _camera, -w, -h, -z);
|
||||
setPoint('n2', pointMap, geometry, _camera, w, -h, -z);
|
||||
setPoint('n3', pointMap, geometry, _camera, -w, h, -z);
|
||||
setPoint('n4', pointMap, geometry, _camera, w, h, -z);
|
||||
|
||||
// up
|
||||
|
||||
setPoint('u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, z);
|
||||
setPoint('u2', pointMap, geometry, _camera, -w * 0.7, h * 1.1, z);
|
||||
setPoint('u3', pointMap, geometry, _camera, 0, h * 2, z);
|
||||
setPoint('u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, -z);
|
||||
setPoint('u2', pointMap, geometry, _camera, -w * 0.7, h * 1.1, -z);
|
||||
setPoint('u3', pointMap, geometry, _camera, 0, h * 2, -z);
|
||||
|
||||
geometry.getAttribute('position').needsUpdate = true;
|
||||
|
||||
|
||||
@@ -1,23 +1,31 @@
|
||||
import * as React from 'react';
|
||||
import * as THREE from 'three';
|
||||
import { MeshLine, MeshLineMaterial } from 'meshline';
|
||||
|
||||
import { Button, Slider } from '@mui/material';
|
||||
import { MeshLine, MeshLineMaterial } from 'meshline';
|
||||
import { useContext, useEffect } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
|
||||
import ArrowBackIosNewIcon from '@mui/icons-material/ArrowBackIosNew';
|
||||
import ArrowForwardIosIcon from '@mui/icons-material/ArrowForwardIos';
|
||||
import ContentPasteGoIcon from '@mui/icons-material/ContentPasteGo';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import FirstPageIcon from '@mui/icons-material/FirstPage';
|
||||
import IconButton from '@mui/material/IconButton';
|
||||
import LastPageIcon from '@mui/icons-material/LastPage';
|
||||
import PauseIcon from '@mui/icons-material/Pause';
|
||||
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
|
||||
import TextField from '@mui/material/TextField';
|
||||
import Tooltip from '@mui/material/Tooltip';
|
||||
import VisibilityIcon from '@mui/icons-material/Visibility';
|
||||
import { useEffect } from 'react';
|
||||
import { CameraHelper } from './CameraHelper';
|
||||
import { get_curve_object_from_cameras } from './curve';
|
||||
import { get_curve_object_from_cameras, get_transform_matrix } from './curve';
|
||||
import { WebSocketContext } from '../../WebSocket/WebSocket';
|
||||
|
||||
const msgpack = require('msgpack-lite');
|
||||
|
||||
function set_camera_position(camera, matrix) {
|
||||
// console.log("setting camera position");
|
||||
const mat = new THREE.Matrix4();
|
||||
mat.fromArray(matrix.elements);
|
||||
mat.decompose(camera.position, camera.quaternion, camera.scale);
|
||||
@@ -70,19 +78,55 @@ function CameraList(props) {
|
||||
}
|
||||
|
||||
export default function CameraPanel(props) {
|
||||
// unpack props
|
||||
// unpack relevant information
|
||||
const sceneTree = props.sceneTree;
|
||||
|
||||
// scene tree objects
|
||||
const camera_main = sceneTree.find_object(['Main Camera']);
|
||||
const camera_main = sceneTree.find_object(['Cameras', 'Main Camera']);
|
||||
const camera_render = sceneTree.find_object(['Cameras', 'Render Camera']);
|
||||
const transform_controls = sceneTree.find_object(['Transform Controls']);
|
||||
|
||||
// redux store state
|
||||
const config_base_dir = useSelector(
|
||||
(state) => state.renderingState.config_base_dir,
|
||||
);
|
||||
const websocket = useContext(WebSocketContext).socket;
|
||||
|
||||
// react state
|
||||
const [cameras, setCameras] = React.useState([]);
|
||||
const [slider_value, set_slider_value] = React.useState(0);
|
||||
const [is_playing, setIsPlaying] = React.useState(false);
|
||||
const [seconds, setSeconds] = React.useState(4);
|
||||
const [fps, setFps] = React.useState(24);
|
||||
|
||||
// const [render_height, setRenderHeight] = React.useState(1080);
|
||||
// const [render_width, setRenderWidth] = React.useState(1920);
|
||||
|
||||
const dispatch = useDispatch();
|
||||
const render_height = useSelector(
|
||||
(state) => state.renderingState.render_height,
|
||||
);
|
||||
const render_width = useSelector(
|
||||
(state) => state.renderingState.render_width,
|
||||
);
|
||||
|
||||
const field_of_view = useSelector(
|
||||
(state) => state.renderingState.field_of_view,
|
||||
);
|
||||
|
||||
const setRenderHeight = (value) => {
|
||||
dispatch({
|
||||
type: 'write',
|
||||
path: 'renderingState/render_height',
|
||||
data: value,
|
||||
});
|
||||
};
|
||||
const setRenderWidth = (value) => {
|
||||
dispatch({
|
||||
type: 'write',
|
||||
path: 'renderingState/render_width',
|
||||
data: value,
|
||||
});
|
||||
};
|
||||
|
||||
const total_num_steps = seconds * fps;
|
||||
const step_size = (cameras.length - 1) / total_num_steps;
|
||||
const slider_min = 0;
|
||||
@@ -90,9 +134,9 @@ export default function CameraPanel(props) {
|
||||
|
||||
const add_camera = () => {
|
||||
const camera_main_copy = camera_main.clone();
|
||||
camera_main_copy.far = camera_main_copy.near + 0.1;
|
||||
const new_camera_list = cameras.concat(camera_main_copy);
|
||||
setCameras(new_camera_list);
|
||||
set_camera_position(camera_render, camera_main_copy.matrix);
|
||||
};
|
||||
|
||||
// force a rerender if the cameras are dragged around
|
||||
@@ -121,9 +165,7 @@ export default function CameraPanel(props) {
|
||||
sceneTree.delete(['Camera Path', 'Cameras']); // delete old cameras, which is important
|
||||
for (let i = 0; i < cameras.length; i += 1) {
|
||||
const camera = cameras[i];
|
||||
const helper = new CameraHelper(camera);
|
||||
console.log('HERERE');
|
||||
console.log(helper);
|
||||
const camera_helper = new CameraHelper(camera);
|
||||
// camera
|
||||
sceneTree.set_object_from_path(
|
||||
['Camera Path', 'Cameras', i.toString(), 'Camera'],
|
||||
@@ -132,7 +174,7 @@ export default function CameraPanel(props) {
|
||||
// camera helper
|
||||
sceneTree.set_object_from_path(
|
||||
['Camera Path', 'Cameras', i.toString(), 'Camera Helper'],
|
||||
helper,
|
||||
camera_helper,
|
||||
);
|
||||
}
|
||||
}, [cameras]);
|
||||
@@ -162,36 +204,15 @@ export default function CameraPanel(props) {
|
||||
const position = curve_object.curve_positions.getPoint(
|
||||
slider_value / (cameras.length - 1.0),
|
||||
);
|
||||
const up = curve_object.curve_ups.getPoint(slider_value / cameras.length);
|
||||
const lookat = curve_object.curve_lookats.getPoint(
|
||||
slider_value / cameras.length,
|
||||
slider_value / (cameras.length - 1.0),
|
||||
);
|
||||
const up = curve_object.curve_ups.getPoint(
|
||||
slider_value / (cameras.length - 1.0),
|
||||
);
|
||||
|
||||
// create a copy of the vector up
|
||||
const up_copy = up.clone();
|
||||
const cross = up_copy.cross(lookat);
|
||||
|
||||
up.normalize();
|
||||
lookat.normalize();
|
||||
cross.normalize();
|
||||
|
||||
// create the camera transform matrix
|
||||
const mat = new THREE.Matrix4();
|
||||
mat.set(
|
||||
cross.x,
|
||||
up.x,
|
||||
lookat.x,
|
||||
position.x,
|
||||
cross.y,
|
||||
up.y,
|
||||
lookat.y,
|
||||
position.y,
|
||||
cross.z,
|
||||
up.z,
|
||||
lookat.z,
|
||||
position.z,
|
||||
);
|
||||
set_camera_position(camera_main, mat);
|
||||
const mat = get_transform_matrix(position, lookat, up);
|
||||
set_camera_position(camera_render, mat);
|
||||
}
|
||||
}, [slider_value]);
|
||||
|
||||
@@ -214,6 +235,105 @@ export default function CameraPanel(props) {
|
||||
}
|
||||
}, [slider_value]);
|
||||
|
||||
const get_camera_path = () => {
|
||||
// NOTE: currently assuming these are ints
|
||||
const num_points = fps * seconds;
|
||||
const positions = curve_object.curve_positions.getPoints(num_points);
|
||||
const lookats = curve_object.curve_lookats.getPoints(num_points);
|
||||
const ups = curve_object.curve_ups.getPoints(num_points);
|
||||
|
||||
const camera_path = [];
|
||||
|
||||
for (let i = 0; i < num_points; i += 1) {
|
||||
const position = positions[i];
|
||||
const lookat = lookats[i];
|
||||
const up = ups[i];
|
||||
|
||||
const mat = get_transform_matrix(position, lookat, up);
|
||||
|
||||
camera_path.push({
|
||||
camera_to_world: mat.transpose().elements, // convert from col-major to row-major matrix
|
||||
fov: camera_render.fov,
|
||||
aspect: camera_render.aspect,
|
||||
});
|
||||
}
|
||||
|
||||
// const myData
|
||||
const camera_path_object = {
|
||||
keyframes: [],
|
||||
render_height,
|
||||
render_width,
|
||||
camera_path,
|
||||
fps,
|
||||
seconds,
|
||||
};
|
||||
return camera_path_object;
|
||||
};
|
||||
|
||||
const export_camera_path = () => {
|
||||
// export the camera path
|
||||
// inspired by:
|
||||
// https://stackoverflow.com/questions/55613438/reactwrite-to-json-file-or-export-download-no-server
|
||||
|
||||
const camera_path_object = get_camera_path();
|
||||
console.log(camera_render.toJSON());
|
||||
|
||||
// create file in browser
|
||||
const json = JSON.stringify(camera_path_object, null, 2);
|
||||
const blob = new Blob([json], { type: 'application/json' });
|
||||
const href = URL.createObjectURL(blob);
|
||||
|
||||
// create "a" HTLM element with href to file
|
||||
const link = document.createElement('a');
|
||||
link.href = href;
|
||||
|
||||
const filename = 'camera_path.json';
|
||||
link.download = filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
// clean up "a" element & remove ObjectURL
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(href);
|
||||
};
|
||||
|
||||
const copy_cmd_to_clipboard = () => {
|
||||
console.log('copy_cmd_to_clipboard');
|
||||
|
||||
const camera_path_object = get_camera_path();
|
||||
|
||||
// Copy the text inside the text field
|
||||
const config_filename = `${config_base_dir}/config.yml`;
|
||||
const camera_path_filename = `${config_base_dir}/camera_path.json`;
|
||||
const cmd =
|
||||
`python scripts/run_eval.py render-trajectory --load-config ${config_filename} --traj filename --camera-path-filename ${camera_path_filename} --output-path output.mp4` +
|
||||
` --downscale-factor 4`;
|
||||
navigator.clipboard.writeText(cmd);
|
||||
|
||||
const camera_path_payload = {
|
||||
camera_path_filename,
|
||||
camera_path: camera_path_object,
|
||||
};
|
||||
|
||||
// send a command of the websocket to save the trajectory somewhere!
|
||||
if (websocket.readyState === WebSocket.OPEN) {
|
||||
const data = {
|
||||
type: 'write',
|
||||
path: 'camera_path_payload',
|
||||
data: camera_path_payload,
|
||||
};
|
||||
const message = msgpack.encode(data);
|
||||
websocket.send(message);
|
||||
}
|
||||
};
|
||||
|
||||
const setFOV = (fov) => {
|
||||
dispatch({
|
||||
type: 'write',
|
||||
path: 'renderingState/field_of_view',
|
||||
data: fov,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="CameraPanel">
|
||||
<div className="CameraPanel-top-button">
|
||||
@@ -222,10 +342,21 @@ export default function CameraPanel(props) {
|
||||
</Button>
|
||||
</div>
|
||||
<div className="CameraPanel-top-button">
|
||||
<Button className="CameraPanel-top-button" variant="outlined">
|
||||
<Button
|
||||
className="CameraPanel-top-button"
|
||||
variant="outlined"
|
||||
onClick={export_camera_path}
|
||||
>
|
||||
Export Path
|
||||
</Button>
|
||||
</div>
|
||||
<div className="CameraPanel-top-button">
|
||||
<Tooltip title="Copy Cmd to Clipboard">
|
||||
<IconButton onClick={copy_cmd_to_clipboard}>
|
||||
<ContentPasteGoIcon />
|
||||
</IconButton>
|
||||
</Tooltip>
|
||||
</div>
|
||||
<div className="CameraPanel-slider-container">
|
||||
<Slider
|
||||
value={slider_value}
|
||||
@@ -287,6 +418,59 @@ export default function CameraPanel(props) {
|
||||
<LastPageIcon />
|
||||
</Button>
|
||||
</div>
|
||||
<div className="CameraList-row-time-interval">
|
||||
<TextField
|
||||
label="Height"
|
||||
inputProps={{
|
||||
inputMode: 'numeric',
|
||||
pattern: '[+-]?([0-9]*[.])?[0-9]+',
|
||||
}}
|
||||
size="small"
|
||||
onChange={(e) => {
|
||||
if (e.target.validity.valid) {
|
||||
setRenderHeight(parseInt(e.target.value, 10));
|
||||
}
|
||||
}}
|
||||
value={render_height}
|
||||
error={render_height <= 0}
|
||||
helperText={render_height <= 0 ? 'Required' : ''}
|
||||
variant="standard"
|
||||
/>
|
||||
<TextField
|
||||
label="Width"
|
||||
inputProps={{
|
||||
inputMode: 'numeric',
|
||||
pattern: '[+-]?([0-9]*[.])?[0-9]+',
|
||||
}}
|
||||
size="small"
|
||||
onChange={(e) => {
|
||||
if (e.target.validity.valid) {
|
||||
setRenderWidth(parseInt(e.target.value, 10));
|
||||
}
|
||||
}}
|
||||
value={render_width}
|
||||
error={render_width <= 0}
|
||||
helperText={render_width <= 0 ? 'Required' : ''}
|
||||
variant="standard"
|
||||
/>
|
||||
<TextField
|
||||
label="FOV"
|
||||
inputProps={{
|
||||
inputMode: 'numeric',
|
||||
pattern: '[+-]?([0-9]*[.])?[0-9]+',
|
||||
}}
|
||||
size="small"
|
||||
onChange={(e) => {
|
||||
if (e.target.validity.valid) {
|
||||
setFOV(parseInt(e.target.value, 10));
|
||||
}
|
||||
}}
|
||||
value={field_of_view}
|
||||
error={field_of_view <= 0}
|
||||
helperText={field_of_view <= 0 ? 'Required' : ''}
|
||||
variant="standard"
|
||||
/>
|
||||
</div>
|
||||
<div className="CameraList-row-time-interval">
|
||||
<TextField
|
||||
label="Seconds"
|
||||
@@ -297,7 +481,7 @@ export default function CameraPanel(props) {
|
||||
size="small"
|
||||
onChange={(e) => {
|
||||
if (e.target.validity.valid) {
|
||||
setSeconds(e.target.value);
|
||||
setSeconds(parseInt(e.target.value, 10));
|
||||
}
|
||||
}}
|
||||
value={seconds}
|
||||
@@ -311,7 +495,7 @@ export default function CameraPanel(props) {
|
||||
size="small"
|
||||
onChange={(e) => {
|
||||
if (e.target.validity.valid) {
|
||||
setFps(e.target.value);
|
||||
setFps(parseInt(e.target.value, 10));
|
||||
}
|
||||
}}
|
||||
value={fps}
|
||||
@@ -324,7 +508,7 @@ export default function CameraPanel(props) {
|
||||
<CameraList
|
||||
sceneTree={sceneTree}
|
||||
transform_controls={transform_controls}
|
||||
camera_main={camera_main}
|
||||
camera_main={camera_render}
|
||||
cameras={cameras}
|
||||
setCameras={setCameras}
|
||||
/>
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
|
||||
import * as THREE from 'three';
|
||||
|
||||
|
||||
function get_curve(list_of_3d_vectors) {
|
||||
// TODO: add some hyperparameters to this function
|
||||
const curve = new THREE.CatmullRomCurve3(
|
||||
list_of_3d_vectors,
|
||||
false,
|
||||
'centripetal',
|
||||
'catmullrom',
|
||||
);
|
||||
return curve;
|
||||
}
|
||||
@@ -25,7 +26,8 @@ export function get_curve_object_from_cameras(cameras) {
|
||||
const camera = cameras[i];
|
||||
|
||||
const up = new THREE.Vector3(0, 1, 0); // y is up in local space
|
||||
const lookat = new THREE.Vector3(0, 0, -1); // -z is forward in local space
|
||||
const lookat = new THREE.Vector3(0, 0, 1); // z is forward in local space
|
||||
|
||||
up.applyQuaternion(camera.quaternion);
|
||||
lookat.applyQuaternion(camera.quaternion);
|
||||
|
||||
@@ -50,3 +52,36 @@ export function get_curve_object_from_cameras(cameras) {
|
||||
|
||||
return curve_object;
|
||||
}
|
||||
|
||||
export function get_transform_matrix(position, lookat, up) {
|
||||
// normalize the vectors
|
||||
lookat.normalize();
|
||||
up.normalize();
|
||||
|
||||
// create a copy of the vector up
|
||||
const up_copy = up.clone();
|
||||
const cross = up_copy.cross(lookat);
|
||||
cross.normalize();
|
||||
|
||||
// create the camera transform matrix
|
||||
const mat = new THREE.Matrix4();
|
||||
mat.set(
|
||||
cross.x,
|
||||
up.x,
|
||||
lookat.x,
|
||||
position.x,
|
||||
cross.y,
|
||||
up.y,
|
||||
lookat.y,
|
||||
position.y,
|
||||
cross.z,
|
||||
up.z,
|
||||
lookat.z,
|
||||
position.z,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
);
|
||||
return mat;
|
||||
}
|
||||
Reference in new issue
Block a user