diff --git a/.vscode/settings.json b/.vscode/settings.json
index 3a9b1b0cf..c973d2df0 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,5 +1,4 @@
{
- // "python.pythonPath": "/path/to/bin/python",
"files.watcherExclude": {
"**/__pycache__/": true,
"**/.ipynb_checkpoints": true,
@@ -8,6 +7,10 @@
"**/runs*": true,
"**/temp*": true,
},
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
+ "[python]": {
+ "editor.defaultFormatter": "ms-python.python"
+ },
"editor.formatOnSave": true,
"python.envFile": "${workspaceFolder}/.env",
"python.formatting.provider": "black",
diff --git a/docs/tutorials/viewer.md b/docs/tutorials/viewer.md
index d3e60771f..ca199cbee 100644
--- a/docs/tutorials/viewer.md
+++ b/docs/tutorials/viewer.md
@@ -9,8 +9,11 @@ The viewer is built using [ThreeJS](https://threejs.org/) and packaged into a [R
## Quick start
```
-# run training script with the viewer
+# Run bridge server in the background.
+python scripts/run_viewer_bridge_server.py
+# Run training script with viewer enabled.
+python scripts/run_train.py viewer.enable=True
```
@@ -63,8 +66,8 @@ yarn start
# forward port and open in your local browser
# the URL takes the following form
-# http://localhost:/localhost:
-http://localhost:4000/localhost:8051
+# http://localhost:?localhost:
+http://localhost:4000?localhost:8051
```
Possibly edit the port in `app/.env.development` file with the following:
diff --git a/notebooks/visualize_viewer.ipynb b/notebooks/visualize_viewer.ipynb
index 5cfaa6ddd..2f20ab375 100644
--- a/notebooks/visualize_viewer.ipynb
+++ b/notebooks/visualize_viewer.ipynb
@@ -10,7 +10,7 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": null,
"id": "8501e01d-f932-4a75-8259-2def67a546d7",
"metadata": {},
"outputs": [],
@@ -23,7 +23,7 @@
},
{
"cell_type": "code",
- "execution_count": 2,
+ "execution_count": null,
"id": "b15dfdc1-5054-40bc-9631-4c7426eecfd9",
"metadata": {},
"outputs": [],
@@ -52,7 +52,7 @@
"from pyrad.utils.config import Config, setup_config\n",
"\n",
"# the new import\n",
- "from pyrad.viewer.server import vis_utils\n",
+ "from pyrad.viewer.server import viewer_utils\n",
"\n",
"\n",
"from hydra import compose, initialize\n",
@@ -71,7 +71,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": null,
"id": "7fe7478d",
"metadata": {},
"outputs": [],
@@ -90,7 +90,7 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": null,
"id": "26d6da3c-e28a-4c55-9592-d90dccb86051",
"metadata": {},
"outputs": [],
@@ -100,7 +100,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": null,
"id": "642b0a25-0e88-4eac-b661-a57a74fb10fc",
"metadata": {},
"outputs": [],
@@ -110,7 +110,7 @@
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": null,
"id": "938f090b-3b28-4fd6-82dc-d4b5c5917134",
"metadata": {},
"outputs": [],
@@ -121,19 +121,10 @@
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": null,
"id": "fd04703a-aa50-45e4-95fd-4d881ff18d61",
"metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "estimated_time: 3.3333333333333335\n",
- "time_elapsed: 3.510408878326416\n"
- ]
- }
- ],
+ "outputs": [],
"source": [
"# move a camera around\n",
"num_cameras = len(dataset_inputs.intrinsics)\n",
@@ -153,7 +144,7 @@
"\n",
"start = time.time()\n",
"for camera in cameras:\n",
- " vis_utils.set_camera(vis, camera)\n",
+ " viewer_utils.set_camera(vis, camera)\n",
"\n",
" # render an image\n",
" # outputs = trainer.render_image_in_viewer()\n",
@@ -171,6 +162,22 @@
"metadata": {},
"outputs": [],
"source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "fd32ba39-fdfe-4689-8ed0-4531d8f2ff04",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ae52a7e6-2a1d-46db-9cc1-7c96b6433a33",
+ "metadata": {},
+ "outputs": [],
+ "source": []
}
],
"metadata": {
diff --git a/pyrad/engine/trainer.py b/pyrad/engine/trainer.py
index 5052e71be..eca8d307e 100644
--- a/pyrad/engine/trainer.py
+++ b/pyrad/engine/trainer.py
@@ -32,7 +32,7 @@ from pyrad.utils import profiler, writer
from pyrad.utils.config import Config
from pyrad.utils.decorators import check_main_thread
from pyrad.utils.writer import EventName, TimeWriter
-from pyrad.viewer.server import vis_utils
+from pyrad.viewer.server import viewer_utils
logging.getLogger("PIL").setLevel(logging.WARNING)
@@ -63,7 +63,7 @@ class Trainer:
writer.setup_event_writers(config.logging, max_iter=config.trainer.max_num_iterations)
profiler.setup_profiler(config.logging)
# visualizer variable
- self.visualizer_state = vis_utils.VisualizerState(config.viewer)
+ self.visualizer_state = viewer_utils.VisualizerState(config.viewer)
def setup(self, test_mode=False):
"""Setup the Trainer by calling other setup functions."""
diff --git a/pyrad/graphs/base.py b/pyrad/graphs/base.py
index e79ecbf99..b6453bcb6 100644
--- a/pyrad/graphs/base.py
+++ b/pyrad/graphs/base.py
@@ -34,7 +34,7 @@ from pyrad.graphs.modules.ray_generator import RayGenerator
from pyrad.utils import profiler
from pyrad.utils.config import GraphConfig
from pyrad.utils.misc import get_masked_dict, instantiate_from_dict_config, is_not_none
-from pyrad.viewer.server.vis_utils import CameraChangeException
+from pyrad.viewer.server.viewer_utils import CameraChangeException
@profiler.time_function
diff --git a/pyrad/viewer/app/package.json b/pyrad/viewer/app/package.json
index 7a4971918..9c9d878c4 100644
--- a/pyrad/viewer/app/package.json
+++ b/pyrad/viewer/app/package.json
@@ -15,7 +15,9 @@
"react-scripts": "^5.0.1",
"socket.io-client": "^4.5.1",
"three": "^0.142.0",
- "websocket": "^1.0.34"
+ "websocket": "^1.0.34",
+ "three-wtm": "^1.0",
+ "wwobjloader2": "^4.0"
},
"scripts": {
"start": "react-scripts start",
diff --git a/pyrad/viewer/app/src/Animator.js b/pyrad/viewer/app/src/Animator.js
deleted file mode 100644
index 93b9f152f..000000000
--- a/pyrad/viewer/app/src/Animator.js
+++ /dev/null
@@ -1,234 +0,0 @@
-import * as THREE from 'three';
-import CCapture from 'ccapture.js'; //TODO(ethan): check if this is correct
-const dat = require('dat.gui');
-
-function remove_folders(gui) {
- for (let name of Object.keys(gui.__folders)) {
- let folder = gui.__folders[name];
- remove_folders(folder);
- dat.dom.dom.unbind(window, 'resize', folder.__resizeHandler);
- gui.removeFolder(folder);
- }
-}
-
-export class Animator {
- constructor(viewer) {
- this.viewer = viewer;
- this.folder = this.viewer.gui.addFolder("Animations");
- this.mixer = new THREE.AnimationMixer();
- this.loader = new THREE.ObjectLoader();
- this.clock = new THREE.Clock();
- this.actions = [];
- this.playing = false;
- this.time = 0;
- this.time_scrubber = null;
- this.setup_capturer("png");
- this.duration = 0;
- }
-
- setup_capturer(format) {
- this.capturer = new CCapture({
- format: format,
- name: "pyrad_" + String(Date.now())
- });
- this.capturer.format = format;
- }
-
- play() {
- this.clock.start();
- // this.mixer.timeScale = 1;
- for (let action of this.actions) {
- action.play();
- }
- this.playing = true;
- }
-
- record() {
- this.reset();
- this.play();
- this.recording = true;
- this.capturer.start();
- }
-
- pause() {
- // this.mixer.timeScale = 0;
- this.clock.stop();
- this.playing = false;
-
- if (this.recording) {
- this.stop_capture();
- this.save_capture();
- }
- }
-
- stop_capture() {
- this.recording = false;
- this.capturer.stop();
- this.viewer.animate(); // restore the animation loop which gets disabled by capturer.stop()
- }
-
- save_capture() {
- this.capturer.save();
- if (this.capturer.format === "png") {
- alert("To convert the still frames into a video, extract the `.tar` file and run: \nffmpeg -r 60 -i %07d.png \\\n\t -vcodec libx264 \\\n\t -preset slow \\\n\t -crf 18 \\\n\t output.mp4");
- } else if (this.capturer.format === "jpg") {
- alert("To convert the still frames into a video, extract the `.tar` file and run: \nffmpeg -r 60 -i %07d.jpg \\\n\t -vcodec libx264 \\\n\t -preset slow \\\n\t -crf 18 \\\n\t output.mp4");
- }
- }
-
- display_progress(time) {
- this.time = time;
- if (this.time_scrubber !== null) {
- this.time_scrubber.updateDisplay();
- }
- }
-
- seek(time) {
- this.actions.forEach((action) => {
- action.time = Math.max(0, Math.min(action._clip.duration, time));
- });
- this.mixer.update(0);
- this.viewer.set_dirty();
- }
-
- reset() {
- for (let action of this.actions) {
- action.reset();
- }
- this.display_progress(0);
- this.mixer.update(0);
- this.setup_capturer(this.capturer.format);
- this.viewer.set_dirty();
- }
-
- clear() {
- remove_folders(this.folder);
- this.mixer.stopAllAction();
- this.actions = [];
- this.duration = 0;
- this.display_progress(0);
- this.mixer = new THREE.AnimationMixer();
- }
-
- load(animations, options) {
- this.clear();
-
- this.folder.open();
- let folder = this.folder.addFolder("default");
- folder.open();
- folder.add(this, "play");
- folder.add(this, "pause");
- folder.add(this, "reset");
-
- // Note, for some reason when you call `.max()` on a slider controller it does
- // correctly change how the slider behaves but does not change the range of values
- // that can be entered into the text box attached to the slider. Oh well. We work
- // around this by creating the slider with an unreasonably huge range and then calling
- // `.min()` and `.max()` on it later.
- this.time_scrubber = folder.add(this, "time", 0, 1e9, 0.001);
- this.time_scrubber.onChange((value) => this.seek(value));
-
- folder.add(this.mixer, "timeScale").step(0.01).min(0);
- let recording_folder = folder.addFolder("Recording");
- recording_folder.add(this, "record");
- recording_folder.add({format: "png"}, "format", ["png", "jpg"]).onChange(value => {
- this.setup_capturer(value);
- });
-
-
- if (options.play === undefined) {
- options.play = true
- }
- if (options.loopMode === undefined) {
- options.loopMode = THREE.LoopRepeat
- }
- if (options.repetitions === undefined) {
- options.repetitions = 1
- }
- if (options.clampWhenFinished === undefined) {
- options.clampWhenFinished = true
- }
-
- this.duration = 0;
- this.progress = 0;
- for (let animation of animations) {
- let target = this.viewer.scene_tree.find(animation.path).object;
- let clip = this.loader.parseAnimations([animation.clip])[0];
- let action = this.mixer.clipAction(clip, target);
- action.clampWhenFinished = options.clampWhenFinished;
- action.setLoop(options.loopMode, options.repetitions);
- this.actions.push(action);
- this.duration = Math.max(this.duration, clip.duration);
- }
- this.time_scrubber.min(0);
- this.time_scrubber.max(this.duration);
- this.reset();
- if (options.play) {
- this.play();
- }
- }
-
- update() {
- if (this.playing) {
- this.mixer.update(this.clock.getDelta());
- this.viewer.set_dirty();
- if (this.duration != 0) {
- let current_time = this.actions.reduce((acc, action) => {
- return Math.max(acc, action.time);
- }, 0);
- this.display_progress(current_time);
- } else {
- this.display_progress(0);
- }
-
- if (this.actions.every((action) => action.paused)) {
- this.pause();
- for (let action of this.actions) {
- action.reset();
- }
- }
- }
- }
-
- after_render() {
- if (this.recording) {
- this.capturer.capture(this.viewer.renderer.domElement);
- }
- }
-}
-
-// Generates a gradient texture without filling up
-// an entire canvas. We simply create a 2x1 image
-// containing only the two colored pixels and then
-// set up the appropriate magnification and wrapping
-// modes to generate the gradient automatically
-function gradient_texture(top_color, bottom_color) {
- let colors = [bottom_color, top_color];
-
- let width = 1;
- let height = 2;
- let size = width * height;
- var data = new Uint8Array(3 * size);
- for (let row = 0; row < height; row++) {
- let color = colors[row];
- for (let col = 0; col < width; col++) {
- let i = 3 * (row * width + col);
- for (let j = 0; j < 3; j++) {
- data[i + j] = color[j];
- }
- }
- }
- var texture = new THREE.DataTexture(data, width, height, THREE.RGBFormat);
- texture.magFilter = THREE.LinearFilter;
- texture.encoding = THREE.LinearEncoding;
- // By default, the points in our texture map to the center of
- // the pixels, which means that the gradient only occupies
- // the middle half of the screen. To get around that, we just have
- // to tweak the UV transform matrix
- texture.matrixAutoUpdate = false;
- texture.matrix.set(0.5, 0, 0.25,
- 0, 0.5, 0.25,
- 0, 0, 1);
- texture.needsUpdate = true
- return texture;
-}
\ No newline at end of file
diff --git a/pyrad/viewer/app/src/Background.js b/pyrad/viewer/app/src/Background.js
deleted file mode 100644
index 3cc938458..000000000
--- a/pyrad/viewer/app/src/Background.js
+++ /dev/null
@@ -1,19 +0,0 @@
-import React, { Component } from "react";
-
-export class BackgroundDiv extends Component {
- componentDidMount() {
- document.getElementById("BackgroundDiv-video").play();
- }
- render() {
- return (
-
- {" "}
-
- );
- }
-}
diff --git a/pyrad/viewer/app/src/ExtensibleObjectLoader.js b/pyrad/viewer/app/src/ExtensibleObjectLoader.js
index 3b5e3a8e5..97076364f 100644
--- a/pyrad/viewer/app/src/ExtensibleObjectLoader.js
+++ b/pyrad/viewer/app/src/ExtensibleObjectLoader.js
@@ -2,7 +2,7 @@ import * as THREE from "three";
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader.js";
import { ColladaLoader } from "three/examples/jsm/loaders/ColladaLoader.js";
import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader.js";
-// import {MtlObjBridge} from 'three/examples/jsm/loaders/obj2/bridge/MtlObjBridge.js';
+import { MtlObjBridge } from "wwobjloader2";
import { STLLoader } from "three/examples/jsm/loaders/STLLoader.js";
import { PLYLoader } from "three/examples/jsm/loaders/PLYLoader.js";
import * as BufferGeometryUtils from "three/examples/jsm/utils/BufferGeometryUtils.js";
@@ -193,11 +193,10 @@ export class ExtensibleObjectLoader extends THREE.ObjectLoader {
if (json.mtl_library) {
let mtl_loader = new MTLLoader(manager);
let mtl_parse_result = mtl_loader.parse(json.mtl_library + "\n", "");
- console.log(mtl_parse_result);
- // let materials = MtlObjBridge.addMaterialsFromMtlLoader(mtl_parse_result);
- // console.log(materials);
- // loader.addMaterials(materials);
- // this.onTextureLoad();
+ let materials =
+ MtlObjBridge.addMaterialsFromMtlLoader(mtl_parse_result);
+ loader.setMaterials(materials);
+ this.onTextureLoad();
}
let obj = loader.parse(json.data + "\n", path);
geometry = merge_geometries(obj, true);
diff --git a/pyrad/viewer/app/src/SceneNode.js b/pyrad/viewer/app/src/SceneNode.js
index 293def860..4f24ce31c 100644
--- a/pyrad/viewer/app/src/SceneNode.js
+++ b/pyrad/viewer/app/src/SceneNode.js
@@ -3,142 +3,142 @@ import * as THREE from "three";
const dat = require("dat.gui");
function remove_folders(gui) {
- for (let name of Object.keys(gui.__folders)) {
- let folder = gui.__folders[name];
- remove_folders(folder);
- dat.dom.dom.unbind(window, "resize", folder.__resizeHandler);
- gui.removeFolder(folder);
- }
+ for (let name of Object.keys(gui.__folders)) {
+ let folder = gui.__folders[name];
+ remove_folders(folder);
+ dat.dom.dom.unbind(window, "resize", folder.__resizeHandler);
+ gui.removeFolder(folder);
+ }
}
function dispose(object) {
- if (!object) {
- return;
- }
- if (object.geometry) {
- object.geometry.dispose();
- }
- if (object.material) {
- if (Array.isArray(object.material)) {
- for (let material of object.material) {
- if (material.map) {
- material.map.dispose();
+ if (!object) {
+ return;
+ }
+ if (object.geometry) {
+ object.geometry.dispose();
+ }
+ if (object.material) {
+ if (Array.isArray(object.material)) {
+ for (let material of object.material) {
+ if (material.map) {
+ material.map.dispose();
+ }
+ material.dispose();
+ }
+ } else {
+ if (object.material.map) {
+ object.material.map.dispose();
+ }
+ object.material.dispose();
}
- material.dispose();
- }
- } else {
- if (object.material.map) {
- object.material.map.dispose();
- }
- object.material.dispose();
}
- }
}
export class SceneNode {
- constructor(object, folder) {
- this.object = object;
- this.folder = folder;
- this.children = {};
- this.create_controls();
- for (let c of this.object.children) {
- this.add_child(c);
+ constructor(object, folder) {
+ this.object = object;
+ this.folder = folder;
+ this.children = {};
+ this.create_controls();
+ for (let c of this.object.children) {
+ this.add_child(c);
+ }
}
- }
- add_child(object) {
- let f = this.folder.addFolder(object.name);
- let node = new SceneNode(object, f);
- this.children[object.name] = node;
- return node;
- }
-
- create_child(name) {
- let obj = new THREE.Group();
- obj.name = name;
- this.object.add(obj);
- return this.add_child(obj);
- }
-
- find(path) {
- if (path.length == 0) {
- return this;
- } else {
- let name = path[0];
- let child = this.children[name];
- if (child === undefined) {
- child = this.create_child(name);
- }
- return child.find(path.slice(1));
+ add_child(object) {
+ let f = this.folder.addFolder(object.name);
+ let node = new SceneNode(object, f);
+ this.children[object.name] = node;
+ return node;
}
- }
- create_controls() {
- if (this.vis_controller !== undefined) {
- this.folder.domElement.removeChild(this.vis_controller.domElement);
+ create_child(name) {
+ let obj = new THREE.Group();
+ obj.name = name;
+ this.object.add(obj);
+ return this.add_child(obj);
}
- this.vis_controller = new dat.controllers.BooleanController(
- this.object,
- "visible"
- );
- this.folder.domElement.prepend(this.vis_controller.domElement);
- this.vis_controller.domElement.style.height = "0";
- this.vis_controller.domElement.style.float = "right";
- }
- set_property(property, value) {
- if (property === "position") {
- this.object.position.set(value[0], value[1], value[2]);
- } else if (property === "quaternion") {
- this.object.quaternion.set(value[0], value[1], value[2], value[3]);
- } else if (property === "scale") {
- this.object.scale.set(value[0], value[1], value[2]);
- } else {
- this.object[property] = value;
+ find(path) {
+ if (path.length == 0) {
+ return this;
+ } else {
+ let name = path[0];
+ let child = this.children[name];
+ if (child === undefined) {
+ child = this.create_child(name);
+ }
+ return child.find(path.slice(1));
+ }
}
- this.vis_controller.updateDisplay();
- }
- set_transform(matrix) {
- let mat = new THREE.Matrix4();
- mat.fromArray(matrix);
- mat.decompose(
- this.object.position,
- this.object.quaternion,
- this.object.scale
- );
- }
-
- set_object(object) {
- let parent = this.object.parent;
- this.dispose_recursive();
- this.object.parent.remove(this.object);
- this.object = object;
- parent.add(object);
- this.create_controls();
- }
-
- dispose_recursive() {
- for (let name of Object.keys(this.children)) {
- this.children[name].dispose_recursive();
+ create_controls() {
+ if (this.vis_controller !== undefined) {
+ this.folder.domElement.removeChild(this.vis_controller.domElement);
+ }
+ this.vis_controller = new dat.controllers.BooleanController(
+ this.object,
+ "visible"
+ );
+ this.folder.domElement.prepend(this.vis_controller.domElement);
+ this.vis_controller.domElement.style.height = "0";
+ this.vis_controller.domElement.style.float = "right";
}
- dispose(this.object);
- }
- delete(path) {
- if (path.length == 0) {
- console.error("Can't delete an empty path");
- } else {
- let parent = this.find(path.slice(0, path.length - 1));
- let name = path[path.length - 1];
- let child = parent.children[name];
- if (child !== undefined) {
- child.dispose_recursive();
- parent.object.remove(child.object);
- remove_folders(child.folder);
- parent.folder.removeFolder(child.folder);
- delete parent.children[name];
- }
+ set_property(property, value) {
+ if (property === "position") {
+ this.object.position.set(value[0], value[1], value[2]);
+ } else if (property === "quaternion") {
+ this.object.quaternion.set(value[0], value[1], value[2], value[3]);
+ } else if (property === "scale") {
+ this.object.scale.set(value[0], value[1], value[2]);
+ } else {
+ this.object[property] = value;
+ }
+ this.vis_controller.updateDisplay();
}
- }
-}
+
+ set_transform(matrix) {
+ let mat = new THREE.Matrix4();
+ mat.fromArray(matrix);
+ mat.decompose(
+ this.object.position,
+ this.object.quaternion,
+ this.object.scale
+ );
+ }
+
+ set_object(object) {
+ let parent = this.object.parent;
+ this.dispose_recursive();
+ this.object.parent.remove(this.object);
+ this.object = object;
+ parent.add(object);
+ this.create_controls();
+ }
+
+ dispose_recursive() {
+ for (let name of Object.keys(this.children)) {
+ this.children[name].dispose_recursive();
+ }
+ dispose(this.object);
+ }
+
+ delete(path) {
+ if (path.length == 0) {
+ console.error("Can't delete an empty path");
+ } else {
+ let parent = this.find(path.slice(0, path.length - 1));
+ let name = path[path.length - 1];
+ let child = parent.children[name];
+ if (child !== undefined) {
+ child.dispose_recursive();
+ parent.object.remove(child.object);
+ remove_folders(child.folder);
+ parent.folder.removeFolder(child.folder);
+ delete parent.children[name];
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/pyrad/viewer/app/src/Viewer.js b/pyrad/viewer/app/src/Viewer.js
deleted file mode 100644
index 718e6c13d..000000000
--- a/pyrad/viewer/app/src/Viewer.js
+++ /dev/null
@@ -1,374 +0,0 @@
-import React, { Component } from "react";
-import * as THREE from "three";
-import { TrackballControls } from "three/examples/jsm/controls/TrackballControls";
-import Stats from "three/examples/jsm/libs/stats.module.js";
-import { GUI } from "dat.gui";
-import { split_path } from "./utils";
-import { SceneNode } from "./SceneNode";
-import { ExtensibleObjectLoader } from "./ExtensibleObjectLoader";
-
-function websocket_endpoint_from_url(url) {
- let endpoint = url.split("?").pop();
- if (endpoint == "") {
- let message =
- "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 findCameraObjectUnderObject3D(object) {
- // seach the tree for the camera
- if (object instanceof THREE.Camera) {
- return object;
- }
- for (let name of Object.keys(object.children)) {
- return findCameraObjectUnderObject3D(object.children[name]);
- }
- return null;
-}
-
-var msgpack = require("msgpack-lite");
-
-export class Viewer extends Component {
- constructor(props) {
- super(props);
- this.state = {
- scene: null,
- controls: null,
- renderer: null,
- gui: null,
- scene_tree: null,
- viewport_width: null,
- viewport_height: null,
- };
- this.update = this.update.bind(this);
- this.set_object = this.set_object.bind(this);
- this.handle_command = this.handle_command.bind(this);
- this.threejs_ref = React.createRef();
- }
-
- handleResize() {
- this.state.viewport_width = this.getViewportWidth();
- this.state.viewport_height = this.getViewportHeight();
- this.state.camera_main.aspect =
- this.state.viewport_width / this.state.viewport_height;
- this.state.camera_main.updateProjectionMatrix();
- this.state.renderer_main.setSize(
- this.state.viewport_width,
- this.state.viewport_height
- );
- }
-
- 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);
- this.send_camera_over_websocket();
- }
-
- send_camera_over_websocket() {
- /* update the camera information in the python server
- if the websocket is connected */
- console.log("send_camera_over_websocket");
- if (this.state.websocket.readyState === WebSocket.OPEN) {
- // update the camera information in the python server
- let camera_main = findCameraObjectUnderObject3D(
- this.state.scene_tree.find(["Cameras", "Main Camera"]).object
- );
- console.log(camera_main.toJSON());
- 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);
- }
- }
-
- set_object(path, object) {
- this.state.scene_tree.find(path.concat(["