lbry-desktop/src/renderer/component/viewers/threeViewer/index.jsx

388 lines
11 KiB
React
Raw Normal View History

2018-06-06 08:06:03 +02:00
// @flow
import * as React from 'react';
2018-08-14 06:03:46 +02:00
import * as dat from 'dat.gui';
2018-06-06 08:06:03 +02:00
import LoadingScreen from 'component/common/loading-screen';
2018-08-14 06:03:46 +02:00
2018-06-13 00:40:55 +02:00
// ThreeJS
import * as THREE from './internal/three';
import detectWebGL from './internal/detector';
import ThreeGrid from './internal/grid';
2018-06-13 00:40:55 +02:00
import ThreeScene from './internal/scene';
import ThreeLoader from './internal/loader';
import ThreeRenderer from './internal/renderer';
2018-06-06 08:06:03 +02:00
type Props = {
theme: string,
autoRotate: boolean,
source: {
fileType: string,
downloadPath: string,
2018-06-06 08:06:03 +02:00
},
};
type State = {
error: ?string,
isReady: boolean,
isLoading: boolean,
};
class ThreeViewer extends React.PureComponent<Props, State> {
static testWebgl = new Promise((resolve, reject) => {
2018-08-14 06:03:46 +02:00
if (detectWebGL()) resolve();
else reject();
});
static fitMeshToCamera(group) {
const max = { x: 0, y: 0, z: 0 };
const min = { x: 0, y: 0, z: 0 };
group.traverse(child => {
if (child instanceof THREE.Mesh) {
const box = new THREE.Box3().setFromObject(child);
// Max
max.x = box.max.x > max.x ? box.max.x : max.x;
max.y = box.max.y > max.y ? box.max.y : max.y;
max.z = box.max.z > max.z ? box.max.z : max.z;
// Min
min.x = box.min.x < min.x ? box.min.x : min.x;
min.y = box.min.y < min.y ? box.min.y : min.y;
min.z = box.min.z < min.z ? box.min.z : min.z;
}
});
const meshY = Math.abs(max.y - min.y);
const meshX = Math.abs(max.x - min.x);
const scaleFactor = 10 / Math.max(meshX, meshY);
group.scale.set(scaleFactor, scaleFactor, scaleFactor);
group.position.setY((meshY / 2) * scaleFactor);
// Reset object position
const box = new THREE.Box3().setFromObject(group);
box.getCenter(group.position);
// Update position
group.position.multiplyScalar(-1);
group.position.setY(group.position.y + meshY * scaleFactor);
}
2018-06-06 08:06:03 +02:00
constructor(props: Props) {
super(props);
2018-06-11 05:51:39 +02:00
const { theme } = this.props;
2018-06-06 08:06:03 +02:00
this.viewer = React.createRef();
2018-08-14 06:03:46 +02:00
this.guiContainer = React.createRef();
2018-08-15 02:54:27 +02:00
// Object defualt color
this.materialColor = '#44b098';
2018-06-11 05:51:39 +02:00
// Viewer themes
2018-06-06 08:06:03 +02:00
this.themes = {
dark: {
gridColor: '#414e5c',
groundColor: '#13233C',
backgroundColor: '#13233C',
centerLineColor: '#7f8c8d',
},
light: {
gridColor: '#7f8c8d',
groundColor: '#DDD',
backgroundColor: '#EEE',
centerLineColor: '#2F2F2F',
},
};
2018-06-09 22:33:31 +02:00
// Select current theme
2018-06-06 08:06:03 +02:00
this.theme = this.themes[theme] || this.themes.light;
2018-06-11 05:51:39 +02:00
// State
this.state = {
error: null,
isReady: false,
isLoading: false,
};
2018-06-06 08:06:03 +02:00
}
2018-06-13 00:40:55 +02:00
componentDidMount() {
ThreeViewer.testWebgl
.then(() => {
this.renderScene();
// Update render on resize window
window.addEventListener('resize', this.handleResize, false);
})
.catch(() => {
// No webgl support, handle Error...
// TODO: Use a better error message
this.setState({ error: "Sorry, your computer doesn't support WebGL." });
});
2018-06-13 00:40:55 +02:00
}
componentWillUnmount() {
2018-08-13 04:22:07 +02:00
// Remove event listeners
2018-06-13 00:40:55 +02:00
window.removeEventListener('resize', this.handleResize, false);
2018-08-13 04:22:07 +02:00
// Free memory
if (this.renderer && this.mesh) {
2018-08-13 04:22:07 +02:00
// Clean up group
this.scene.remove(this.mesh);
if (this.mesh.geometry) this.mesh.geometry.dispose();
if (this.mesh.material) this.mesh.material.dispose();
2018-08-15 02:54:27 +02:00
// Cleanup shared geometry
if (this.geometry) this.geometry.dispose();
if (this.bufferGeometry) this.bufferGeometry.dispose();
// Clean up shared material
2018-08-15 02:54:27 +02:00
if (this.material) this.material.dispose();
// Clean up grid
2018-08-15 02:54:27 +02:00
if (this.grid) {
this.grid.material.dispose();
this.grid.geometry.dispose();
}
2018-08-13 04:22:07 +02:00
// Clean up group items
this.mesh.traverse(child => {
if (child instanceof THREE.Mesh) {
if (child.geometry) child.geometry.dispose();
if (child.material) child.material.dispose();
}
});
2018-08-15 08:15:15 +02:00
// Clean up controls
if (this.controls) this.controls.dispose();
2018-08-13 04:22:07 +02:00
// It's unclear if we need this:
2018-08-15 02:54:27 +02:00
if (this.renderer) {
this.renderer.renderLists.dispose();
this.renderer.dispose();
}
// Stop animation
cancelAnimationFrame(this.frameID);
2018-08-14 06:03:46 +02:00
// Destroy GUI Controls
if (this.gui) this.gui.destroy();
// Empty objects
this.grid = null;
this.mesh = null;
this.renderer = null;
2018-08-15 02:54:27 +02:00
this.material = null;
this.geometry = null;
this.bufferGeometry = null;
2018-08-13 04:22:07 +02:00
}
2018-06-13 00:40:55 +02:00
}
transformGroup(group) {
ThreeViewer.fitMeshToCamera(group);
2018-08-15 08:15:15 +02:00
this.updateControlsTarget(this.mesh.position);
}
2018-08-14 06:03:46 +02:00
createInterfaceControls() {
if (this.guiContainer && this.mesh) {
2018-08-15 08:15:15 +02:00
this.gui = new dat.GUI({ autoPlace: false, name: 'controls' });
2018-08-14 06:03:46 +02:00
const config = {
2018-08-15 02:54:27 +02:00
color: this.materialColor,
2018-08-14 06:03:46 +02:00
};
2018-08-15 08:15:15 +02:00
config.reset = () => {
// Reset material color
config.color = this.materialColor;
this.material.color.set(config.color);
// Reset wireframe
this.material.wireframe = false;
// Reset camera
this.restoreCamera();
};
2018-08-15 02:54:27 +02:00
2018-08-15 08:15:15 +02:00
// Color picker
const colorPicker = this.gui.addColor(config, 'color').listen();
2018-08-14 06:03:46 +02:00
colorPicker.onChange(color => {
2018-08-15 02:54:27 +02:00
this.material.color.set(color);
2018-08-14 06:03:46 +02:00
});
2018-08-15 02:54:27 +02:00
this.gui.add(this.material, 'wireframe').listen();
2018-08-15 08:15:15 +02:00
this.gui.add(config, 'reset');
2018-08-14 06:03:46 +02:00
this.guiContainer.current.appendChild(this.gui.domElement);
}
}
2018-06-06 08:06:03 +02:00
createOrbitControls(camera, canvas) {
const { autoRotate } = this.props;
const controls = new THREE.OrbitControls(camera, canvas);
// Controls configuration
controls.enableDamping = true;
controls.dampingFactor = 0.75;
controls.enableZoom = true;
2018-08-15 08:15:15 +02:00
controls.minDistance = 5;
controls.maxDistance = 14;
2018-06-06 08:06:03 +02:00
controls.autoRotate = autoRotate;
controls.enablePan = false;
2018-08-15 08:15:15 +02:00
controls.saveState();
2018-06-06 08:06:03 +02:00
return controls;
}
2018-08-15 02:54:27 +02:00
createGeometry(data) {
this.bufferGeometry = data;
this.bufferGeometry.computeBoundingBox();
this.bufferGeometry.computeVertexNormals();
this.bufferGeometry.center();
this.bufferGeometry.rotateX(-Math.PI / 2);
this.bufferGeometry.lookAt(new THREE.Vector3(0, 0, 1));
// Get geometry from bufferGeometry
this.geometry = new THREE.Geometry().fromBufferGeometry(this.bufferGeometry);
this.geometry.mergeVertices();
}
2018-06-06 08:06:03 +02:00
startLoader() {
const { source } = this.props;
2018-06-13 00:40:55 +02:00
if (source) {
2018-06-06 08:06:03 +02:00
ThreeLoader(source, this.renderModel.bind(this), {
2018-07-19 08:45:32 +02:00
onStart: this.handleStart,
onLoad: this.handleReady,
onError: this.handleError,
2018-06-06 08:06:03 +02:00
});
2018-06-13 00:40:55 +02:00
}
2018-06-06 08:06:03 +02:00
}
2018-07-19 08:45:32 +02:00
handleStart = () => {
2018-06-06 08:06:03 +02:00
this.setState({ isLoading: true });
2018-07-19 08:45:32 +02:00
};
2018-06-06 08:06:03 +02:00
2018-07-19 08:45:32 +02:00
handleReady = () => {
2018-06-06 08:06:03 +02:00
this.setState({ isReady: true, isLoading: false });
2018-08-14 06:03:46 +02:00
// GUI
this.createInterfaceControls();
2018-07-19 08:45:32 +02:00
};
handleError = () => {
this.setState({ error: "Sorry, looks like we can't load this file" });
};
2018-06-06 08:06:03 +02:00
handleResize = () => {
const { offsetWidth: width, offsetHeight: height } = this.viewer.current;
this.camera.aspect = width / height;
this.camera.updateProjectionMatrix();
this.controls.update();
this.renderer.setSize(width, height);
};
2018-06-13 00:40:55 +02:00
updateControlsTarget(point) {
this.controls.target.fromArray([point.x, point.y, point.z]);
this.controls.update();
}
2018-08-15 08:15:15 +02:00
restoreCamera() {
this.controls.reset();
this.camera.position.set(-9.5, 14, 11);
this.updateControlsTarget(this.mesh.position);
}
renderStl(data) {
2018-08-15 02:54:27 +02:00
this.createGeometry(data);
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.mesh.name = 'model';
this.scene.add(this.mesh);
this.transformGroup(this.mesh);
}
renderObj(event) {
const mesh = event.detail.loaderRootNode;
2018-08-15 02:54:27 +02:00
this.mesh = new THREE.Group();
this.mesh.name = 'model';
// Assign new material
mesh.traverse(child => {
if (child instanceof THREE.Mesh) {
// Get geometry from child
const geometry = new THREE.Geometry();
geometry.fromBufferGeometry(child.geometry);
// Create and regroup inner objects
const innerObj = new THREE.Mesh(geometry, this.material);
2018-08-15 02:54:27 +02:00
this.mesh.add(innerObj);
// Clean up geometry
geometry.dispose();
child.geometry.dispose();
}
});
2018-08-15 02:54:27 +02:00
this.scene.add(this.mesh);
this.transformGroup(this.mesh);
}
renderModel(fileType, parsedData) {
const renderTypes = {
stl: data => this.renderStl(data),
obj: data => this.renderObj(data),
};
if (renderTypes[fileType]) {
renderTypes[fileType](parsedData);
}
2018-06-06 08:06:03 +02:00
}
renderScene() {
const { gridColor, centerLineColor } = this.theme;
2018-06-06 08:06:03 +02:00
this.renderer = ThreeRenderer({
antialias: true,
shadowMap: true,
gammaCorrection: true,
2018-06-06 08:06:03 +02:00
});
this.scene = ThreeScene({
showFog: true,
...this.theme,
});
const viewer = this.viewer.current;
const canvas = this.renderer.domElement;
const { offsetWidth: width, offsetHeight: height } = viewer;
// Grid
this.grid = ThreeGrid({ size: 100, gridColor, centerLineColor });
this.scene.add(this.grid);
2018-06-06 08:06:03 +02:00
// Camera
this.camera = new THREE.PerspectiveCamera(80, width / height, 0.1, 1000);
this.camera.position.set(-9.5, 14, 11);
2018-06-06 08:06:03 +02:00
// Controls
this.controls = this.createOrbitControls(this.camera, canvas);
2018-06-06 08:06:03 +02:00
// Set viewer size
this.renderer.setSize(width, height);
// Create model material
this.material = new THREE.MeshPhongMaterial({
2018-08-13 04:22:07 +02:00
depthWrite: true,
vertexColors: THREE.FaceColors,
});
2018-08-13 04:22:07 +02:00
// Set material color
2018-08-15 02:54:27 +02:00
this.material.color.set(this.materialColor);
2018-06-06 08:06:03 +02:00
// Load file and render mesh
this.startLoader();
2018-08-13 04:22:07 +02:00
// Append canvas
viewer.appendChild(canvas);
2018-06-06 08:06:03 +02:00
const updateScene = () => {
this.frameID = requestAnimationFrame(updateScene);
// this.controls.update();
2018-06-06 08:06:03 +02:00
this.renderer.render(this.scene, this.camera);
};
updateScene();
}
render() {
2018-06-13 00:40:55 +02:00
const { error, isReady, isLoading } = this.state;
2018-08-13 04:22:07 +02:00
const loadingMessage = __('Loading 3D model.');
2018-06-06 08:06:03 +02:00
const showViewer = isReady && !error;
const showLoading = isLoading && !error;
return (
<React.Fragment>
{error && <LoadingScreen status={error} spinner={false} />}
2018-06-13 00:40:55 +02:00
{showLoading && <LoadingScreen status={loadingMessage} spinner />}
2018-08-14 06:03:46 +02:00
<div ref={this.guiContainer} className="gui-container" />
2018-06-13 00:40:55 +02:00
<div
style={{ opacity: showViewer ? 1 : 0 }}
className="three-viewer file-render__viewer"
ref={this.viewer}
/>
2018-06-06 08:06:03 +02:00
</React.Fragment>
);
}
}
export default ThreeViewer;