From 61131ce77d68f53a4f556d8849f09db3200bb463 Mon Sep 17 00:00:00 2001 From: btzr-io Date: Wed, 15 Aug 2018 19:47:08 -0600 Subject: [PATCH] add autoRotate feature --- .../component/viewers/threeViewer/index.jsx | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/renderer/component/viewers/threeViewer/index.jsx b/src/renderer/component/viewers/threeViewer/index.jsx index ab9903785..406e541d3 100644 --- a/src/renderer/component/viewers/threeViewer/index.jsx +++ b/src/renderer/component/viewers/threeViewer/index.jsx @@ -13,7 +13,6 @@ import ThreeRenderer from './internal/renderer'; type Props = { theme: string, - autoRotate: boolean, source: { fileType: string, downloadPath: string, @@ -32,6 +31,20 @@ class ThreeViewer extends React.PureComponent { else reject(); }); + static createOrbitControls(camera, canvas) { + const controls = new THREE.OrbitControls(camera, canvas); + // Controls configuration + controls.enableDamping = true; + controls.dampingFactor = 0.75; + controls.enableZoom = true; + controls.minDistance = 5; + controls.maxDistance = 14; + controls.autoRotate = false; + controls.enablePan = false; + controls.saveState(); + return controls; + } + static fitMeshToCamera(group) { const max = { x: 0, y: 0, z: 0 }; const min = { x: 0, y: 0, z: 0 }; @@ -176,6 +189,8 @@ class ThreeViewer extends React.PureComponent { this.material.color.set(config.color); // Reset wireframe this.material.wireframe = false; + // Reset autoRotate + this.controls.autoRotate = false; // Reset camera this.restoreCamera(); }; @@ -188,26 +203,12 @@ class ThreeViewer extends React.PureComponent { }); this.gui.add(this.material, 'wireframe').listen(); + this.gui.add(this.controls, 'autoRotate').listen(); this.gui.add(config, 'reset'); this.guiContainer.current.appendChild(this.gui.domElement); } } - 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; - controls.minDistance = 5; - controls.maxDistance = 14; - controls.autoRotate = autoRotate; - controls.enablePan = false; - controls.saveState(); - return controls; - } - createGeometry(data) { this.bufferGeometry = data; this.bufferGeometry.computeBoundingBox(); @@ -334,7 +335,7 @@ class ThreeViewer extends React.PureComponent { this.camera.position.set(-9.5, 14, 11); // Controls - this.controls = this.createOrbitControls(this.camera, canvas); + this.controls = ThreeViewer.createOrbitControls(this.camera, canvas); // Set viewer size this.renderer.setSize(width, height); @@ -356,7 +357,7 @@ class ThreeViewer extends React.PureComponent { const updateScene = () => { this.frameID = requestAnimationFrame(updateScene); - // this.controls.update(); + if (this.controls.autoRotate) this.controls.update(); this.renderer.render(this.scene, this.camera); };