mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-07 00:09:14 +08:00
Integrating WebRTC into viewer (#73)
* restructuring for webrtc integration * some webrtc code but not working * webrtc connects when muted * the flag is real! * updating camera in websocket bridge. moved webrtc to existing websocket connection instead of http request. need to change video track * rudimentary adding real-time imaging * added free TURN server so code works everywhere * slow rendering works during training with the viewer * fix tensor device and update default model save dir * forgot to include trainer updates * removing some code that isn't needed * quick fix to bins. not the most accurate however * cleaned up code and added some config logic * black * adding license Co-authored-by: evonneng <evonne_ng@berkeley.edu>
This commit is contained in:
1 parent
e507c97e3e
commit
bf13ed3cf3
28 files changed
+8923
-917
No files matched your search
@@ -1,38 +0,0 @@
|
||||
.App {
|
||||
/*text-align: center;*/
|
||||
}
|
||||
|
||||
.App-logo {
|
||||
height: 40vmin;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
.App-logo {
|
||||
animation: App-logo-spin infinite 20s linear;
|
||||
}
|
||||
}
|
||||
|
||||
.App-header {
|
||||
background-color: #282c34;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: calc(10px + 2vmin);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.App-link {
|
||||
color: #61dafb;
|
||||
}
|
||||
|
||||
@keyframes App-logo-spin {
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
import React, { Component } from "react";
|
||||
import "./App.css";
|
||||
import { Viewer } from "./Viewer";
|
||||
|
||||
class Image extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {};
|
||||
}
|
||||
}
|
||||
|
||||
class ColorGUIHelper {
|
||||
constructor(object, prop) {
|
||||
this.object = object;
|
||||
this.prop = prop;
|
||||
}
|
||||
|
||||
get value() {
|
||||
return `#${this.object[this.prop].getHexString()}`;
|
||||
}
|
||||
|
||||
set value(hexString) {
|
||||
this.object[this.prop].set(hexString);
|
||||
}
|
||||
}
|
||||
|
||||
class App extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
client: null,
|
||||
};
|
||||
this.viewer_ref = React.createRef();
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div className="App">
|
||||
<Viewer ref={this.viewer_ref} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
@@ -1,9 +0,0 @@
|
||||
import React from 'react';
|
||||
import { render } from '@testing-library/react';
|
||||
import App from './App';
|
||||
|
||||
test('renders learn react link', () => {
|
||||
const { getByText } = render(<App />);
|
||||
const linkElement = getByText(/learn react/i);
|
||||
expect(linkElement).toBeInTheDocument();
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import React, { Component } from "react";
|
||||
|
||||
export class BackgroundDiv extends Component {
|
||||
componentDidMount() {
|
||||
document.getElementById("BackgroundDiv-video").play();
|
||||
}
|
||||
render() {
|
||||
return (
|
||||
<div className="BackgroundDiv">
|
||||
<video id="BackgroundDiv-video" autoPlay muted loop>
|
||||
<source
|
||||
src="https://www.w3schools.com/html/mov_bbb.mp4"
|
||||
type="video/mp4"
|
||||
></source>
|
||||
</video>{" "}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -36,11 +36,10 @@ function dispose(object) {
|
||||
}
|
||||
|
||||
export class SceneNode {
|
||||
constructor(object, folder, on_update) {
|
||||
constructor(object, folder) {
|
||||
this.object = object;
|
||||
this.folder = folder;
|
||||
this.children = {};
|
||||
this.on_update = on_update;
|
||||
this.create_controls();
|
||||
for (let c of this.object.children) {
|
||||
this.add_child(c);
|
||||
@@ -49,7 +48,7 @@ export class SceneNode {
|
||||
|
||||
add_child(object) {
|
||||
let f = this.folder.addFolder(object.name);
|
||||
let node = new SceneNode(object, f, this.on_update);
|
||||
let node = new SceneNode(object, f);
|
||||
this.children[object.name] = node;
|
||||
return node;
|
||||
}
|
||||
@@ -79,28 +78,9 @@ export class SceneNode {
|
||||
this.folder.domElement.removeChild(this.vis_controller.domElement);
|
||||
}
|
||||
this.vis_controller = new dat.controllers.BooleanController(this.object, "visible");
|
||||
this.vis_controller.onChange(() => this.on_update());
|
||||
this.folder.domElement.prepend(this.vis_controller.domElement);
|
||||
this.vis_controller.domElement.style.height = "0";
|
||||
this.vis_controller.domElement.style.float = "right";
|
||||
|
||||
// // add on hover controls and clickable functionality
|
||||
// // hover
|
||||
// this.folder.domElement.addEventListener("mouseenter", function (event) {
|
||||
// event.target.style.color = "purple";
|
||||
// }, false);
|
||||
// this.folder.domElement.addEventListener("mouseleave", function (event) {
|
||||
// event.target.style.color = "";
|
||||
// }, false);
|
||||
|
||||
// // click
|
||||
// this.folder.domElement.addEventListener("dblclick", () => this.printValue(), false);
|
||||
}
|
||||
|
||||
printValue() {
|
||||
console.log("calling print value");
|
||||
console.log(this.object);
|
||||
console.log(this);
|
||||
}
|
||||
|
||||
set_property(property, value) {
|
||||
|
||||
+165
-332
@@ -1,5 +1,4 @@
|
||||
import React, { Component } from "react";
|
||||
import "./App.css";
|
||||
import * as THREE from "three";
|
||||
import { TrackballControls } from "three/examples/jsm/controls/TrackballControls";
|
||||
import Stats from "three/examples/jsm/libs/stats.module.js";
|
||||
@@ -12,26 +11,17 @@ function websocket_endpoint_from_url(url) {
|
||||
let endpoint = url.split("/").pop();
|
||||
if (endpoint == "") {
|
||||
let message =
|
||||
"Please set a websocket endpoint. For example, a correct URL may look like the following: http://localhost:3000/visiongpu10.csail.mit.edu:8051";
|
||||
"Please set the websocket endpoint. E.g., a correct URL may be: http://localhost:4000/localhost:8051";
|
||||
window.alert(message);
|
||||
return null;
|
||||
}
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
function removeAllChildNodes(parent) {
|
||||
// https://www.javascripttutorial.net/dom/manipulating/remove-all-child-nodes/
|
||||
while (parent.firstChild) {
|
||||
parent.removeChild(parent.firstChild);
|
||||
}
|
||||
}
|
||||
|
||||
function findCameraObjectUnderObject3D(object) {
|
||||
// seach the tree for the camera
|
||||
if (object instanceof THREE.Camera) {
|
||||
return object;
|
||||
} else if (object instanceof THREE.CameraHelper) {
|
||||
return object.camera;
|
||||
}
|
||||
for (let name of Object.keys(object.children)) {
|
||||
return findCameraObjectUnderObject3D(object.children[name]);
|
||||
@@ -50,87 +40,51 @@ export class Viewer extends Component {
|
||||
renderer: null,
|
||||
gui: null,
|
||||
scene_tree: null,
|
||||
needs_render: true,
|
||||
viewport_width: null,
|
||||
viewport_height: null,
|
||||
images: [],
|
||||
image_index: 0,
|
||||
};
|
||||
this.animate = this.animate.bind(this);
|
||||
this.update = this.update.bind(this);
|
||||
this.set_object = this.set_object.bind(this);
|
||||
this.handle_command = this.handle_command.bind(this);
|
||||
|
||||
// window.addEventListener('resize', this.handleResize.bind(this));
|
||||
}
|
||||
|
||||
set_dirty() {
|
||||
this.setState({ needs_render: true });
|
||||
this.threejs_ref = React.createRef();
|
||||
}
|
||||
|
||||
handleResize() {
|
||||
this.state.viewport_width = this.getViewportWidth();
|
||||
this.state.viewport_height = this.getViewportHeight();
|
||||
this.state.camera_l.aspect =
|
||||
this.state.camera_main.aspect =
|
||||
this.state.viewport_width / this.state.viewport_height;
|
||||
this.state.camera_l.updateProjectionMatrix();
|
||||
this.state.camera_r.updateProjectionMatrix();
|
||||
this.state.camera_orth.updateProjectionMatrix();
|
||||
this.state.renderer_l.setSize(
|
||||
this.state.camera_main.updateProjectionMatrix();
|
||||
this.state.renderer_main.setSize(
|
||||
this.state.viewport_width,
|
||||
(this.state.viewport_height * 2) / 3
|
||||
);
|
||||
this.state.renderer_r.setSize(
|
||||
this.state.viewport_width / 2,
|
||||
(this.state.viewport_height * 1) / 3
|
||||
);
|
||||
this.state.renderer_orth.setSize(
|
||||
this.state.viewport_width / 2,
|
||||
(this.state.viewport_height * 1) / 3
|
||||
this.state.viewport_height
|
||||
);
|
||||
}
|
||||
|
||||
animate() {
|
||||
requestAnimationFrame(this.animate);
|
||||
if (this.state.needs_render) {
|
||||
this.handleResize();
|
||||
let camera_l = findCameraObjectUnderObject3D(
|
||||
this.state.scene_tree.find(["Cameras", "Main Camera L"]).object
|
||||
);
|
||||
let camera_r = findCameraObjectUnderObject3D(
|
||||
this.state.scene_tree.find(["Cameras", "Main Camera R"]).object
|
||||
);
|
||||
let camera_orth = findCameraObjectUnderObject3D(
|
||||
this.state.scene_tree.find(["Cameras", "Main Camera Orth"]).object
|
||||
);
|
||||
camera_l.updateProjectionMatrix();
|
||||
camera_r.updateProjectionMatrix();
|
||||
camera_orth.updateProjectionMatrix();
|
||||
if (this.state.controls_l != null) {
|
||||
this.state.controls_l.update();
|
||||
}
|
||||
if (this.state.controls_r != null) {
|
||||
this.state.controls_r.update();
|
||||
}
|
||||
if (this.state.controls_orth != null) {
|
||||
this.state.controls_orth.update();
|
||||
}
|
||||
this.state.renderer_l.render(this.state.scene, camera_l);
|
||||
this.state.renderer_r.render(this.state.scene, camera_r);
|
||||
|
||||
// NOTE(ethan): trying to add background image
|
||||
|
||||
// console.log("ethan is here");
|
||||
if (this.state.websocket_is_opened) {
|
||||
this.state.websocket.send("TODO: put camera pose here");
|
||||
}
|
||||
|
||||
this.state.renderer_orth.render(
|
||||
this.backgroundScene,
|
||||
this.backgroundCamera
|
||||
);
|
||||
// this.state.renderer_orth.render(this.state.scene, camera_orth);
|
||||
this.state.needs_render = false;
|
||||
this.set_dirty(); // TODO(ethan): remove this but make sure to include it everywhere
|
||||
update() {
|
||||
requestAnimationFrame(this.update);
|
||||
this.handleResize();
|
||||
let camera_main = findCameraObjectUnderObject3D(
|
||||
this.state.scene_tree.find(["Cameras", "Main Camera"]).object
|
||||
);
|
||||
camera_main.updateProjectionMatrix();
|
||||
if (this.state.controls_main != null) {
|
||||
this.state.controls_main.update();
|
||||
}
|
||||
this.state.renderer_main.render(this.state.scene, camera_main);
|
||||
if (this.state.websocket.readyState === WebSocket.OPEN) {
|
||||
// update the camera information in the python server
|
||||
let cmd = "set_object";
|
||||
let path = "Cameras/Main Camera";
|
||||
let data = {
|
||||
type: cmd,
|
||||
path: path,
|
||||
object: camera_main.toJSON(),
|
||||
};
|
||||
let message = msgpack.encode(data);
|
||||
this.state.websocket.send(message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,16 +109,25 @@ export class Viewer extends Component {
|
||||
|
||||
set_object(path, object) {
|
||||
this.state.scene_tree.find(path.concat(["<object>"])).set_object(object);
|
||||
// add controls if object is a camera
|
||||
if (object instanceof THREE.Camera) {
|
||||
this.state.controls_main = new TrackballControls(
|
||||
object,
|
||||
this.state.renderer_main.domElement
|
||||
);
|
||||
this.state.controls_main.rotateSpeed = 2.0;
|
||||
this.state.controls_main.zoomSpeed = 0.3;
|
||||
this.state.controls_main.panSpeed = 0.2;
|
||||
this.state.controls_main.staticMoving = false; // false is default
|
||||
this.state.controls_main.target.set(0, 0, 0); // focus point of the controls
|
||||
this.state.controls_main.autoRotate = false;
|
||||
this.state.controls_main.update();
|
||||
}
|
||||
}
|
||||
|
||||
set_object_from_json(path, object_json) {
|
||||
let loader = new ExtensibleObjectLoader();
|
||||
loader.onTextureLoad = () => {
|
||||
this.set_dirty();
|
||||
};
|
||||
loader.parse(object_json, (obj) => {
|
||||
// console.log("ethan.");
|
||||
// console.log(obj.type);
|
||||
if (
|
||||
obj.geometry !== undefined &&
|
||||
obj.geometry.type === "BufferGeometry"
|
||||
@@ -175,15 +138,10 @@ export class Viewer extends Component {
|
||||
) {
|
||||
obj.geometry.computeVertexNormals();
|
||||
}
|
||||
} else if (obj.type.includes("Camera")) {
|
||||
console.log("uses camera !!");
|
||||
// this.set_camera(obj);
|
||||
// this.set_3d_pane_size();
|
||||
}
|
||||
obj.castShadow = true;
|
||||
obj.receiveShadow = true;
|
||||
this.set_object(path, obj);
|
||||
this.set_dirty();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -209,17 +167,11 @@ export class Viewer extends Component {
|
||||
}
|
||||
|
||||
handle_command(cmd) {
|
||||
console.log(cmd);
|
||||
|
||||
// convert binary serialization format back to JSON
|
||||
cmd = msgpack.decode(new Uint8Array(cmd));
|
||||
console.log(cmd);
|
||||
|
||||
// TODO(ethan): ignore these or remove status. maybe incorporate into a clean view
|
||||
if (cmd.type === "status") {
|
||||
return;
|
||||
}
|
||||
|
||||
// three js scene commands
|
||||
if (cmd.type === "set_object") {
|
||||
let path = split_path(cmd.path);
|
||||
this.set_object_from_json(path, cmd.object);
|
||||
@@ -232,59 +184,14 @@ export class Viewer extends Component {
|
||||
} else if (cmd.type === "set_property") {
|
||||
let path = split_path(cmd.path);
|
||||
this.set_property(path, cmd.property, cmd.value);
|
||||
} else if (cmd.type === "set_animation") {
|
||||
// TODO(ethan): implement animations
|
||||
console.error("Animations not implemented yet.");
|
||||
} else if (cmd.type === "set_image") {
|
||||
console.log("ethan: setting camera");
|
||||
// var url = URL.createObjectURL(
|
||||
// new Blob([cmd["image"].buffer], { type: "image/png" } /* (1) */)
|
||||
// );
|
||||
// console.log(url);
|
||||
// document.getElementById("my-img").src = url;
|
||||
var height = 300;
|
||||
var width = 200;
|
||||
// console.log(cmd["image"]);
|
||||
var texture = new THREE.DataTexture(
|
||||
cmd["image"],
|
||||
width,
|
||||
height,
|
||||
THREE.RGBFormat
|
||||
);
|
||||
// var texture = new THREE.TextureLoader().load("texture2.jpeg");
|
||||
// var texture = new THREE.TextureLoader().load(url);
|
||||
// console.log(texture);
|
||||
// console.log(this.backgroundMesh);
|
||||
this.backgroundMesh.material.map = texture;
|
||||
this.backgroundMesh.material.map.needsUpdate = true;
|
||||
this.backgroundMesh.material.needsUpdate = true;
|
||||
}
|
||||
this.set_dirty();
|
||||
|
||||
if (cmd.type === "set_transform" && cmd.path === "/Cameras/Main Camera R") {
|
||||
let camera_r = findCameraObjectUnderObject3D(
|
||||
this.state.scene_tree.find(["Cameras", "Main Camera R"]).object
|
||||
);
|
||||
camera_r.updateProjectionMatrix();
|
||||
this.state.renderer_save.render(this.state.scene, camera_r);
|
||||
let imgData = this.state.renderer_save.domElement.toDataURL();
|
||||
this.state.images.push(imgData.toString());
|
||||
// this.save_image(imgData);
|
||||
// web rtc commands
|
||||
else if (cmd.type === "answer") {
|
||||
let answer = cmd.data;
|
||||
this.state.pc.setRemoteDescription(answer);
|
||||
}
|
||||
}
|
||||
|
||||
save_scene() {}
|
||||
|
||||
load_scene() {}
|
||||
|
||||
// save_image() {}
|
||||
|
||||
add_camera_helper(path, camera) {
|
||||
let camera_copy = camera.clone();
|
||||
camera_copy.far = 1; // so it doesn't show so far away
|
||||
this.set_object(path, new THREE.CameraHelper(camera_copy));
|
||||
}
|
||||
|
||||
getViewportWidth() {
|
||||
return window.innerWidth - (window.innerWidth % 2);
|
||||
}
|
||||
@@ -293,14 +200,94 @@ export class Viewer extends Component {
|
||||
return window.innerHeight;
|
||||
}
|
||||
|
||||
setupWebRTC() {
|
||||
console.log("setting up WebRTC");
|
||||
// The iceServers config comes from the following URL:
|
||||
// https://www.metered.ca/tools/openrelay/
|
||||
this.state.pc = new RTCPeerConnection({
|
||||
iceServers: [
|
||||
{
|
||||
urls: "stun:openrelay.metered.ca:80",
|
||||
},
|
||||
{
|
||||
urls: "turn:openrelay.metered.ca:80",
|
||||
username: "openrelayproject",
|
||||
credential: "openrelayproject",
|
||||
},
|
||||
{
|
||||
urls: "turn:openrelay.metered.ca:443",
|
||||
username: "openrelayproject",
|
||||
credential: "openrelayproject",
|
||||
},
|
||||
{
|
||||
urls: "turn:openrelay.metered.ca:443?transport=tcp",
|
||||
username: "openrelayproject",
|
||||
credential: "openrelayproject",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// connect video
|
||||
this.state.pc.addEventListener("track", function (evt) {
|
||||
if (evt.track.kind == "video") {
|
||||
console.log("setting event stream");
|
||||
document.getElementById("WebRTCVideo-video").srcObject = evt.streams[0];
|
||||
}
|
||||
});
|
||||
this.state.pc.addTransceiver("video", { direction: "recvonly" });
|
||||
|
||||
this.state.pc
|
||||
.createOffer()
|
||||
.then((offer) => {
|
||||
return this.state.pc.setLocalDescription(offer);
|
||||
})
|
||||
.then(() => {
|
||||
// wait for ICE gathering to complete
|
||||
return new Promise((resolve) => {
|
||||
if (this.state.pc.iceGatheringState === "complete") {
|
||||
resolve();
|
||||
} else {
|
||||
var checkState = () => {
|
||||
if (this.state.pc.iceGatheringState === "complete") {
|
||||
this.state.pc.removeEventListener(
|
||||
"icegatheringstatechange",
|
||||
checkState
|
||||
);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
this.state.pc.addEventListener(
|
||||
"icegatheringstatechange",
|
||||
checkState
|
||||
);
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(() => {
|
||||
var offer = this.state.pc.localDescription;
|
||||
console.log("sending the offer");
|
||||
let cmd = "offer";
|
||||
let path = "";
|
||||
let data = {
|
||||
type: cmd,
|
||||
path: path,
|
||||
data: {
|
||||
sdp: offer.sdp,
|
||||
type: offer.type,
|
||||
},
|
||||
};
|
||||
let message = msgpack.encode(data);
|
||||
this.state.websocket.send(message);
|
||||
});
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
// Open the websocket
|
||||
let endpoint = websocket_endpoint_from_url(window.location.href);
|
||||
this.state.websocket_is_opened = false;
|
||||
this.state.websocket = new WebSocket("ws://" + endpoint + "/");
|
||||
this.state.websocket.onopen = () => {
|
||||
console.log("connected");
|
||||
this.state.websocket_is_opened = true;
|
||||
console.log("websocket connected");
|
||||
this.setupWebRTC();
|
||||
};
|
||||
this.state.websocket.binaryType = "arraybuffer";
|
||||
// 'command' for updates to Viewer
|
||||
@@ -310,195 +297,50 @@ export class Viewer extends Component {
|
||||
};
|
||||
|
||||
// Get size of screen
|
||||
this.state.viewport_width = this.getViewportWidth(); // notice that this is half width
|
||||
this.state.viewport_width = this.getViewportWidth();
|
||||
this.state.viewport_height = this.getViewportHeight();
|
||||
|
||||
// Scene
|
||||
this.state.scene = new THREE.Scene();
|
||||
this.state.scene.background = new THREE.Color(0xffffff);
|
||||
|
||||
// GUI
|
||||
this.state.gui = new GUI();
|
||||
let scene_folder = this.state.gui.addFolder("Scene");
|
||||
scene_folder.open();
|
||||
this.state.scene_tree = new SceneNode(this.state.scene, scene_folder, () =>
|
||||
this.set_dirty()
|
||||
);
|
||||
// let save_folder = this.state.gui.addFolder("Save / Load / Capture");
|
||||
// save_folder.add(this, 'save_scene');
|
||||
// save_folder.add(this, 'load_scene');
|
||||
// save_folder.add(this, 'save_image');
|
||||
// this.state.gui.open();
|
||||
this.state.scene_tree = new SceneNode(this.state.scene, scene_folder);
|
||||
|
||||
// console.log(this.mount);
|
||||
|
||||
// Renderer Left
|
||||
this.state.renderer_l = new THREE.WebGLRenderer({ antialias: true });
|
||||
this.state.renderer_l.setPixelRatio(window.devicePixelRatio);
|
||||
this.state.renderer_l.setSize(
|
||||
this.state.viewport_width,
|
||||
(this.state.viewport_height * 2) / 3
|
||||
);
|
||||
this.state.renderer_l.domElement.style.border = "1px solid black";
|
||||
this.mount.appendChild(this.state.renderer_l.domElement);
|
||||
|
||||
// Renderer Right
|
||||
let div = document.createElement("div");
|
||||
this.state.renderer_r = new THREE.WebGLRenderer({ antialias: true });
|
||||
this.state.renderer_r.setPixelRatio(window.devicePixelRatio);
|
||||
this.state.renderer_r.setSize(
|
||||
this.state.viewport_width / 2,
|
||||
(this.state.viewport_height * 1) / 3
|
||||
);
|
||||
this.state.renderer_r.domElement.style.border = "1px solid black";
|
||||
this.state.renderer_r.domElement.style = "inline-block";
|
||||
div.appendChild(this.state.renderer_r.domElement);
|
||||
|
||||
this.state.renderer_orth = new THREE.WebGLRenderer({ antialias: true });
|
||||
this.state.renderer_orth.setPixelRatio(window.devicePixelRatio);
|
||||
this.state.renderer_orth.setSize(
|
||||
this.state.viewport_width / 2,
|
||||
(this.state.viewport_height * 1) / 3
|
||||
);
|
||||
this.state.renderer_orth.domElement.style.border = "1px solid black";
|
||||
this.state.renderer_orth.domElement.style = "inline-block";
|
||||
div.appendChild(this.state.renderer_orth.domElement);
|
||||
// div.display.style = "relative";
|
||||
|
||||
// NOTE(ethan): not sure if this is needed anymore?
|
||||
this.state.renderer_save = new THREE.WebGLRenderer({
|
||||
// Renderer main
|
||||
this.state.renderer_main = new THREE.WebGLRenderer({
|
||||
antialias: true,
|
||||
preserveDrawingBuffer: true,
|
||||
alpha: true,
|
||||
});
|
||||
this.state.renderer_save.setPixelRatio(1.0);
|
||||
this.state.renderer_save.setSize(960, 540);
|
||||
this.state.renderer_save.domElement.style.border = "1px solid black";
|
||||
this.state.renderer_save.domElement.style.display = "none";
|
||||
// div.appendChild(this.state.renderer_save.domElement);
|
||||
this.state.renderer_main.setPixelRatio(window.devicePixelRatio);
|
||||
this.state.renderer_main.setSize(
|
||||
this.state.viewport_width,
|
||||
this.state.viewport_height
|
||||
);
|
||||
this.state.renderer_main.domElement.style.border = "1px solid black";
|
||||
|
||||
this.mount.appendChild(div);
|
||||
let domElement = document.getElementById("canvas-container-main");
|
||||
domElement.append(this.state.renderer_main.domElement);
|
||||
|
||||
// console.log(this.mount);
|
||||
|
||||
this.mount.style.display = "block";
|
||||
|
||||
// Camera settings at https://stackoverflow.com/questions/46182845/field-of-view-aspect-ratio-view-matrix-from-projection-matrix-hmd-ost-calib/46195462
|
||||
// Camera left
|
||||
this.state.camera_l = new THREE.PerspectiveCamera(
|
||||
// Camera main
|
||||
this.state.camera_main = new THREE.PerspectiveCamera(
|
||||
120,
|
||||
this.state.viewport_width / this.state.viewport_height,
|
||||
0.01,
|
||||
100
|
||||
);
|
||||
this.state.camera_l.position.x = 5;
|
||||
this.state.camera_l.position.y = -5;
|
||||
this.state.camera_l.position.z = -5;
|
||||
this.state.camera_l.up = new THREE.Vector3(0, -1, 0);
|
||||
// Controls left
|
||||
this.state.controls_l = new TrackballControls(
|
||||
this.state.camera_l,
|
||||
this.state.renderer_l.domElement
|
||||
);
|
||||
this.state.controls_l.rotateSpeed = 2.0;
|
||||
this.state.controls_l.zoomSpeed = 0.3;
|
||||
this.state.controls_l.panSpeed = 0.2;
|
||||
this.state.controls_l.staticMoving = false; // false is default
|
||||
this.state.controls_l.target.set(0, 0, 0); // focus point of the controls
|
||||
this.state.controls_l.autoRotate = false;
|
||||
this.state.controls_l.update();
|
||||
|
||||
// Camera right
|
||||
this.state.camera_r = new THREE.PerspectiveCamera(
|
||||
120,
|
||||
this.state.viewport_width / (this.state.viewport_height / 2),
|
||||
0.01,
|
||||
100
|
||||
);
|
||||
this.state.camera_orth = new THREE.OrthographicCamera(
|
||||
-2,
|
||||
2,
|
||||
-2,
|
||||
2,
|
||||
0.01,
|
||||
100
|
||||
);
|
||||
// this.state.camera_r.position.x = 0;
|
||||
// this.state.camera_r.position.y = 0;
|
||||
// this.state.camera_r.position.z = 0;
|
||||
// this.state.camera_r.up = new THREE.Vector3(0, -1, 0);
|
||||
|
||||
var texture = new THREE.TextureLoader().load("texture.jpeg");
|
||||
this.backgroundMesh = new THREE.Mesh(
|
||||
new THREE.PlaneGeometry(2, 2),
|
||||
new THREE.MeshBasicMaterial({
|
||||
map: texture,
|
||||
})
|
||||
// material
|
||||
);
|
||||
// const geometry = new THREE.PlaneGeometry(1, 1);
|
||||
// const material = new THREE.MeshBasicMaterial({
|
||||
// color: 0xffff00,
|
||||
// side: THREE.DoubleSide,
|
||||
// });
|
||||
// const plane = new THREE.Mesh(geometry, material);
|
||||
this.backgroundMesh.material.depthTest = false;
|
||||
this.backgroundMesh.material.depthWrite = false;
|
||||
// Create your background scene
|
||||
this.backgroundScene = new THREE.Scene();
|
||||
this.backgroundCamera = new THREE.Camera();
|
||||
this.backgroundScene.add(this.backgroundCamera);
|
||||
this.backgroundScene.add(this.backgroundMesh);
|
||||
|
||||
this.set_object(["Cameras", "Main Camera L"], this.state.camera_l);
|
||||
this.set_object(["Cameras", "Main Camera R"], this.state.camera_r);
|
||||
this.set_object(["Cameras", "Main Camera Orth"], this.state.camera_orth);
|
||||
|
||||
// rotate the cameras to match the COLMAP format
|
||||
let rotY = new THREE.Matrix4();
|
||||
rotY.makeRotationY(Math.PI);
|
||||
let rotZ = new THREE.Matrix4();
|
||||
rotZ.makeRotationZ(Math.PI);
|
||||
let rot = rotY.multiply(rotZ);
|
||||
this.set_transform(["Cameras", "Main Camera R", "<object>"], rot.toArray());
|
||||
|
||||
// this.state.scene_tree.find(["Cameras", "Main Camera L"]).set_property("position", [5, -5, -5]);
|
||||
this.state.scene_tree
|
||||
.find(["Cameras", "Main Camera R"])
|
||||
.set_property("position", [-2, -2, -2]);
|
||||
|
||||
// Controls right
|
||||
// this.state.controls_r = new TrackballControls(this.state.scene_tree.find(["Cameras", "Main Camera R"]).object, this.state.renderer_r.domElement);
|
||||
// this.state.controls_r.rotateSpeed = 2.0;
|
||||
// this.state.controls_r.zoomSpeed = 0.3;
|
||||
// this.state.controls_r.panSpeed = 0.2;
|
||||
// this.state.controls_r.staticMoving = false; // false is default
|
||||
// this.state.controls_r.target.set(0, 0, 0); // focus point of the controls
|
||||
// this.state.controls_r.autoRotate = false;
|
||||
// this.state.controls_r.update();
|
||||
|
||||
this.set_transform(
|
||||
["Cameras", "Main Camera Orth", "<object>"],
|
||||
rot.toArray()
|
||||
);
|
||||
this.state.scene_tree
|
||||
.find(["Cameras", "Main Camera Orth"])
|
||||
.set_property("position", [-1, -1, -1]);
|
||||
|
||||
let camera_axes_l = new THREE.AxesHelper(1);
|
||||
let camera_axes_r = new THREE.AxesHelper(1);
|
||||
let camera_axes_orth = new THREE.AxesHelper(1);
|
||||
this.set_object(["Cameras", "Main Camera L", "Axes"], camera_axes_l);
|
||||
this.set_object(["Cameras", "Main Camera R", "Axes"], camera_axes_r);
|
||||
this.set_object(["Cameras", "Main Camera Orth", "Axes"], camera_axes_orth);
|
||||
this.state.camera_main.position.x = 5;
|
||||
this.state.camera_main.position.y = -5;
|
||||
this.state.camera_main.position.z = -5;
|
||||
this.state.camera_main.up = new THREE.Vector3(0, -1, 0);
|
||||
this.set_object(["Cameras", "Main Camera"], this.state.camera_main);
|
||||
|
||||
// Axes display
|
||||
let axes = new THREE.AxesHelper(5);
|
||||
this.set_object(["Axes"], axes);
|
||||
|
||||
// var arrowPos = new THREE.Vector3(0, 0, 0);
|
||||
// var arrow = new THREE.ArrowHelper(new THREE.Vector3(1, 0, 0), arrowPos, 60, 0xFF0000, 20, 10)
|
||||
// this.set_object(["arrow"], arrow);
|
||||
|
||||
// Grid
|
||||
let grid = new THREE.GridHelper(20, 20);
|
||||
this.set_object(["Grid"], grid);
|
||||
@@ -511,32 +353,23 @@ export class Viewer extends Component {
|
||||
|
||||
// Stats
|
||||
var stats = new Stats();
|
||||
this.mount.appendChild(stats.dom);
|
||||
let domElement_stats = document.getElementById("canvas-container-main");
|
||||
domElement_stats.append(stats.dom);
|
||||
|
||||
// let camera = new THREE.PerspectiveCamera(75, this.state.width / this.state.height, 0.1, 1000);
|
||||
// let helper = new THREE.CameraHelper(camera);
|
||||
// // scene.add( helper );
|
||||
// // console.log(helper);
|
||||
// console.log(helper);
|
||||
// // this.set_object(["Helper"], helper);
|
||||
// // console.log(this.state.scene);
|
||||
|
||||
// Cube
|
||||
// var geometry = new THREE.BoxGeometry(1, 1, 1);
|
||||
// var material = new THREE.MeshBasicMaterial({color: 0x00ff00});
|
||||
// var cube = new THREE.Mesh(geometry, material);
|
||||
// this.state.scene.add(cube);
|
||||
|
||||
this.animate();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
// this.mount.removeChild(this.state.renderer_l.domElement);
|
||||
// this.mount.removeChild(this.state.renderer_r.domElement);
|
||||
removeAllChildNodes(this.mount);
|
||||
this.update();
|
||||
}
|
||||
|
||||
render() {
|
||||
return <div ref={(ref) => (this.mount = ref)} />;
|
||||
return (
|
||||
<div>
|
||||
<div className="WebRTCVideo">
|
||||
<video id="WebRTCVideo-video" autoPlay playsInline muted>
|
||||
{" "}
|
||||
</video>{" "}
|
||||
</div>{" "}
|
||||
<div id="canvas-container-main"> </div>{" "}
|
||||
<div id="stats-container"> </div>{" "}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,43 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
||||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
|
||||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
|
||||
monospace;
|
||||
}
|
||||
|
||||
.BackgroundDiv,
|
||||
.WebRTCVideo {
|
||||
position: absolute;
|
||||
top: 0px;
|
||||
right: 0px;
|
||||
bottom: 0px;
|
||||
left: 0px;
|
||||
}
|
||||
|
||||
#BackgroundDiv-video,
|
||||
#WebRTCVideo-video {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: fill;
|
||||
}
|
||||
|
||||
#canvas-container-main {
|
||||
position: absolute;
|
||||
top: 0px;
|
||||
right: 0px;
|
||||
bottom: 0px;
|
||||
left: 0px;
|
||||
}
|
||||
|
||||
#canvas-container-main canvas {
|
||||
position: relative;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
@@ -1,17 +1,13 @@
|
||||
import React from 'react';
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import './index.css';
|
||||
import App from './App';
|
||||
import * as serviceWorker from './serviceWorker';
|
||||
import "./index.css";
|
||||
import * as serviceWorker from "./serviceWorker";
|
||||
import { Viewer } from "./Viewer";
|
||||
|
||||
const root = ReactDOM.createRoot(document.getElementById("root"));
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>
|
||||
);
|
||||
root.render( < Viewer / > );
|
||||
|
||||
// If you want your app to work offline and load faster, you can change
|
||||
// unregister() to register() below. Note this comes with some pitfalls.
|
||||
// Learn more about service workers: https://bit.ly/CRA-PWA
|
||||
serviceWorker.unregister();
|
||||
serviceWorker.unregister();
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,48 @@
|
||||
function createPeerConnection() {
|
||||
var config = {
|
||||
sdpSemantics: "unified-plan",
|
||||
};
|
||||
|
||||
// if (document.getElementById('use-stun').checked) {
|
||||
// config.iceServers = [{urls: ['stun:stun.l.google.com:19302']}];
|
||||
// }
|
||||
|
||||
let pc = new RTCPeerConnection(config);
|
||||
|
||||
// register some listeners to help debugging
|
||||
pc.addEventListener(
|
||||
"icegatheringstatechange",
|
||||
function () {
|
||||
iceGatheringLog.textContent += " -> " + pc.iceGatheringState;
|
||||
},
|
||||
false
|
||||
);
|
||||
iceGatheringLog.textContent = pc.iceGatheringState;
|
||||
|
||||
pc.addEventListener(
|
||||
"iceconnectionstatechange",
|
||||
function () {
|
||||
iceConnectionLog.textContent += " -> " + pc.iceConnectionState;
|
||||
},
|
||||
false
|
||||
);
|
||||
iceConnectionLog.textContent = pc.iceConnectionState;
|
||||
|
||||
pc.addEventListener(
|
||||
"signalingstatechange",
|
||||
function () {
|
||||
signalingLog.textContent += " -> " + pc.signalingState;
|
||||
},
|
||||
false
|
||||
);
|
||||
signalingLog.textContent = pc.signalingState;
|
||||
|
||||
// connect audio / video
|
||||
pc.addEventListener("track", function (evt) {
|
||||
if (evt.track.kind == "video")
|
||||
document.getElementById("video").srcObject = evt.streams[0];
|
||||
else document.getElementById("audio").srcObject = evt.streams[0];
|
||||
});
|
||||
|
||||
return pc;
|
||||
}
|
||||
Reference in new issue
Block a user