|
| 1 | +/* |
| 2 | + * Copyright 2019 Esri. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.samples.scene.orbit_the_camera_around_an_object; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | + |
| 21 | +import com.esri.arcgisruntime.ArcGISRuntimeEnvironment; |
| 22 | +import com.esri.arcgisruntime.mapping.view.DrawStatus; |
| 23 | +import com.esri.arcgisruntime.mapping.view.DrawStatusChangedEvent; |
| 24 | +import com.esri.arcgisruntime.mapping.view.DrawStatusChangedListener; |
| 25 | +import javafx.fxml.FXML; |
| 26 | +import javafx.scene.control.CheckBox; |
| 27 | +import javafx.scene.control.Slider; |
| 28 | + |
| 29 | +import com.esri.arcgisruntime.geometry.Point; |
| 30 | +import com.esri.arcgisruntime.geometry.SpatialReferences; |
| 31 | +import com.esri.arcgisruntime.mapping.ArcGISScene; |
| 32 | +import com.esri.arcgisruntime.mapping.ArcGISTiledElevationSource; |
| 33 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 34 | +import com.esri.arcgisruntime.mapping.Surface; |
| 35 | +import com.esri.arcgisruntime.mapping.view.Graphic; |
| 36 | +import com.esri.arcgisruntime.mapping.view.GraphicsOverlay; |
| 37 | +import com.esri.arcgisruntime.mapping.view.LayerSceneProperties; |
| 38 | +import com.esri.arcgisruntime.mapping.view.OrbitGeoElementCameraController; |
| 39 | +import com.esri.arcgisruntime.mapping.view.SceneView; |
| 40 | +import com.esri.arcgisruntime.symbology.ModelSceneSymbol; |
| 41 | +import com.esri.arcgisruntime.symbology.SimpleRenderer; |
| 42 | + |
| 43 | +public class OrbitTheCameraAroundAnObjectController { |
| 44 | + |
| 45 | + @FXML |
| 46 | + private SceneView sceneView; |
| 47 | + @FXML |
| 48 | + private CheckBox allowDistanceInteractionCheckBox; |
| 49 | + @FXML |
| 50 | + private Slider cameraHeadingSlider; |
| 51 | + @FXML |
| 52 | + private Slider planePitchSlider; |
| 53 | + |
| 54 | + private OrbitGeoElementCameraController orbitCameraController; |
| 55 | + |
| 56 | + |
| 57 | + public void initialize() { |
| 58 | + |
| 59 | + try { |
| 60 | + |
| 61 | + // create a scene and add a basemap to it |
| 62 | + ArcGISScene scene = new ArcGISScene(); |
| 63 | + scene.setBasemap(Basemap.createImagery()); |
| 64 | + sceneView.setArcGISScene(scene); |
| 65 | + |
| 66 | + // add a base surface with elevation data |
| 67 | + Surface surface = new Surface(); |
| 68 | + ArcGISTiledElevationSource elevationSource = new ArcGISTiledElevationSource("http://elevation3d.arcgis" + |
| 69 | + ".com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"); |
| 70 | + surface.getElevationSources().add(elevationSource); |
| 71 | + scene.setBaseSurface(surface); |
| 72 | + |
| 73 | + // create a graphics overlay for the scene |
| 74 | + GraphicsOverlay sceneGraphicsOverlay = new GraphicsOverlay(); |
| 75 | + sceneGraphicsOverlay.getSceneProperties().setSurfacePlacement(LayerSceneProperties.SurfacePlacement.RELATIVE); |
| 76 | + sceneView.getGraphicsOverlays().add(sceneGraphicsOverlay); |
| 77 | + |
| 78 | + // create a renderer to control the plane's orientation by its attributes |
| 79 | + SimpleRenderer renderer = new SimpleRenderer(); |
| 80 | + renderer.getSceneProperties().setHeadingExpression("[HEADING]"); |
| 81 | + renderer.getSceneProperties().setPitchExpression("[PITCH]"); |
| 82 | + renderer.getSceneProperties().setRollExpression("[ROLL]"); |
| 83 | + sceneGraphicsOverlay.setRenderer(renderer); |
| 84 | + |
| 85 | + // create a graphic of a plane model |
| 86 | + String modelURI = new File("./samples-data/bristol/Collada/Bristol.dae").getAbsolutePath(); |
| 87 | + ModelSceneSymbol plane3DSymbol = new ModelSceneSymbol(modelURI, 1.0); |
| 88 | + plane3DSymbol.loadAsync(); |
| 89 | + // position the plane over a runway |
| 90 | + Graphic plane = new Graphic(new Point(6.637, 45.399, 100, SpatialReferences.getWgs84()), plane3DSymbol); |
| 91 | + // initialize the plane's heading to line up with the runway |
| 92 | + plane.getAttributes().put("HEADING", 45.0); |
| 93 | + sceneGraphicsOverlay.getGraphics().add(plane); |
| 94 | + |
| 95 | + // control the plane's pitch with a slider |
| 96 | + planePitchSlider.valueProperty().addListener(o -> plane.getAttributes().put("PITCH", planePitchSlider.getValue())); |
| 97 | + |
| 98 | + // listener for the view to stop loading and to add the camera controller |
| 99 | + DrawStatusChangedListener listener = new DrawStatusChangedListener() { |
| 100 | + |
| 101 | + @Override |
| 102 | + public void drawStatusChanged(DrawStatusChangedEvent drawStatusChangedEvent) { |
| 103 | + if (drawStatusChangedEvent.getDrawStatus() == DrawStatus.COMPLETED) { |
| 104 | + |
| 105 | + // create an orbit geoelement camera controller with the plane as the target |
| 106 | + orbitCameraController = new OrbitGeoElementCameraController(plane, 50.0); |
| 107 | + |
| 108 | + // restrict the camera's heading to stay behind the plane |
| 109 | + orbitCameraController.setMinCameraHeadingOffset(-45); |
| 110 | + orbitCameraController.setMaxCameraHeadingOffset(45); |
| 111 | + |
| 112 | + // restrict the camera's pitch so it doesn't go completely vertical or collide with the ground |
| 113 | + orbitCameraController.setMinCameraPitchOffset(10); |
| 114 | + orbitCameraController.setMaxCameraPitchOffset(100); |
| 115 | + |
| 116 | + // restrict the camera to stay between 10 and 1000 meters from the plane |
| 117 | + orbitCameraController.setMinCameraDistance(10); |
| 118 | + orbitCameraController.setMaxCameraDistance(100); |
| 119 | + |
| 120 | + // position the plane a third from the bottom of the screen |
| 121 | + orbitCameraController.setTargetVerticalScreenFactor(0.33f); |
| 122 | + |
| 123 | + // don't pitch the camera when the plane pitches |
| 124 | + orbitCameraController.setAutoPitchEnabled(false); |
| 125 | + |
| 126 | + // set the orbit camera controller to the scene view |
| 127 | + sceneView.setCameraController(orbitCameraController); |
| 128 | + |
| 129 | + // set the camera's heading using a slider |
| 130 | + cameraHeadingSlider.valueProperty().addListener(o -> orbitCameraController.setCameraHeadingOffset(cameraHeadingSlider.getValue())); |
| 131 | + |
| 132 | + // update camera heading slider position whilst interacting with the camera heading |
| 133 | + sceneView.addViewpointChangedListener( event -> cameraHeadingSlider.setValue(orbitCameraController.getCameraHeadingOffset())); |
| 134 | + |
| 135 | + // stop listening for the view to load |
| 136 | + sceneView.removeDrawStatusChangedListener(this); |
| 137 | + } |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + sceneView.addDrawStatusChangedListener(listener); |
| 142 | + |
| 143 | + } catch (Exception e) { |
| 144 | + // on any exception, print the stack trace |
| 145 | + e.printStackTrace(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Animates the camera to a cockpit view. The camera's target is offset to the cockpit (instead of the plane's |
| 151 | + * center). The camera is moved onto the target position to create a swivelling camera effect. Auto pitch is |
| 152 | + * enabled so the camera pitches when the plane pitches. |
| 153 | + */ |
| 154 | + @FXML |
| 155 | + private void handleCockpitViewButtonClicked() { |
| 156 | + // disable camera distance interaction checkbox |
| 157 | + allowDistanceInteractionCheckBox.setDisable(true); |
| 158 | + |
| 159 | + // allow the camera to get closer to the target |
| 160 | + orbitCameraController.setMinCameraDistance(0); |
| 161 | + |
| 162 | + // pitch the camera when the plane pitches |
| 163 | + orbitCameraController.setAutoPitchEnabled(true); |
| 164 | + |
| 165 | + // animate the camera target to the cockpit instead of the center of the plane |
| 166 | + orbitCameraController.setTargetOffsetsAsync(0, -2, 1.1, 1); |
| 167 | + |
| 168 | + // animate the camera so that it is 0.01m from the target (cockpit), facing forward (0 deg heading), and aligned |
| 169 | + // with the horizon (90 deg pitch) |
| 170 | + orbitCameraController.moveCameraAsync(0 - orbitCameraController.getCameraDistance(), |
| 171 | + 0 - orbitCameraController.getCameraHeadingOffset(), 90 - orbitCameraController.getCameraPitchOffset(), 1).addDoneListener(() -> { |
| 172 | + // once the camera is in the cockpit, only allow the camera's heading to change |
| 173 | + orbitCameraController.setMinCameraPitchOffset(90); |
| 174 | + orbitCameraController.setMaxCameraPitchOffset(90); |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Configures the camera controller for a "follow" view. The camera targets the center of the plane with a default |
| 180 | + * position directly behind and slightly above the plane. Auto pitch is disabled so the camera does not pitch when |
| 181 | + * the plane pitches. |
| 182 | + */ |
| 183 | + @FXML |
| 184 | + private void handleCenterViewButtonClicked() { |
| 185 | + allowDistanceInteractionCheckBox.setDisable(false); |
| 186 | + |
| 187 | + orbitCameraController.setAutoPitchEnabled(false); |
| 188 | + orbitCameraController.setTargetOffsetX(0); |
| 189 | + orbitCameraController.setTargetOffsetY(0); |
| 190 | + orbitCameraController.setTargetOffsetZ(0); |
| 191 | + orbitCameraController.setCameraHeadingOffset(0); |
| 192 | + orbitCameraController.setCameraPitchOffset(45); |
| 193 | + orbitCameraController.setMinCameraPitchOffset(10); |
| 194 | + orbitCameraController.setMaxCameraPitchOffset(100); |
| 195 | + orbitCameraController.setMinCameraDistance(10.0); |
| 196 | + orbitCameraController.setCameraDistance(50.0); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Toggle interactive distance. When distance interaction is disabled, the user cannot zoom in with the mouse. |
| 201 | + */ |
| 202 | + @FXML |
| 203 | + private void handleDistanceInteractionCheckBoxToggle() { |
| 204 | + orbitCameraController.setCameraDistanceInteractive(allowDistanceInteractionCheckBox.isSelected()); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Disposes of application resources. |
| 209 | + */ |
| 210 | + void terminate() { |
| 211 | + |
| 212 | + // release resources when the application closes |
| 213 | + if (sceneView != null) { |
| 214 | + sceneView.dispose(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | +} |
0 commit comments