fix camera target position

This commit is contained in:
btzr-io 2018-08-17 23:07:33 -06:00 committed by Sean Yesmunt
parent 61131ce77d
commit b8cd8db692

View file

@ -71,8 +71,8 @@ class ThreeViewer extends React.PureComponent<Props, State> {
group.position.setY((meshY / 2) * scaleFactor); group.position.setY((meshY / 2) * scaleFactor);
// Reset object position // Reset object position
const box = new THREE.Box3().setFromObject(group); const box = new THREE.Box3().setFromObject(group);
box.getCenter(group.position);
// Update position // Update position
box.getCenter(group.position);
group.position.multiplyScalar(-1); group.position.multiplyScalar(-1);
group.position.setY(group.position.y + meshY * scaleFactor); group.position.setY(group.position.y + meshY * scaleFactor);
} }
@ -172,7 +172,12 @@ class ThreeViewer extends React.PureComponent<Props, State> {
transformGroup(group) { transformGroup(group) {
ThreeViewer.fitMeshToCamera(group); ThreeViewer.fitMeshToCamera(group);
this.updateControlsTarget(this.mesh.position);
if (!this.targetCenter) {
const box = new THREE.Box3();
this.targetCenter = box.setFromObject(this.mesh).getCenter();
}
this.updateControlsTarget(this.targetCenter);
} }
createInterfaceControls() { createInterfaceControls() {
@ -196,15 +201,24 @@ class ThreeViewer extends React.PureComponent<Props, State> {
}; };
// Color picker // Color picker
const colorPicker = this.gui.addColor(config, 'color').listen(); const colorPicker = this.gui
.addColor(config, 'color')
.name('Color')
.listen();
colorPicker.onChange(color => { colorPicker.onChange(color => {
this.material.color.set(color); this.material.color.set(color);
}); });
this.gui.add(this.material, 'wireframe').listen(); this.gui
this.gui.add(this.controls, 'autoRotate').listen(); .add(this.material, 'wireframe')
this.gui.add(config, 'reset'); .name('Wireframe')
.listen();
this.gui
.add(this.controls, 'autoRotate')
.name('Auto-Rotate')
.listen();
this.gui.add(config, 'reset').name('Reset');
this.guiContainer.current.appendChild(this.gui.domElement); this.guiContainer.current.appendChild(this.gui.domElement);
} }
} }
@ -263,7 +277,7 @@ class ThreeViewer extends React.PureComponent<Props, State> {
restoreCamera() { restoreCamera() {
this.controls.reset(); this.controls.reset();
this.camera.position.set(-9.5, 14, 11); this.camera.position.set(-9.5, 14, 11);
this.updateControlsTarget(this.mesh.position); this.updateControlsTarget(this.targetCenter);
} }
renderStl(data) { renderStl(data) {
@ -293,7 +307,6 @@ class ThreeViewer extends React.PureComponent<Props, State> {
child.geometry.dispose(); child.geometry.dispose();
} }
}); });
this.scene.add(this.mesh); this.scene.add(this.mesh);
this.transformGroup(this.mesh); this.transformGroup(this.mesh);
} }