|
| 1 | +// Copyright 2024 Esri |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import ArcGIS |
| 16 | +import ArcGISToolkit |
| 17 | +import SwiftUI |
| 18 | + |
| 19 | +extension AugmentRealityToShowHiddenInfrastructureView { |
| 20 | + /// A world scale scene view displaying pipe graphics from a given model. |
| 21 | + struct ARPipesSceneView: View { |
| 22 | + /// The view model for scene view in the sample. |
| 23 | + @ObservedObject var model: SceneModel |
| 24 | + |
| 25 | + /// A Boolean value indicating whether the shadow graphics are visible. |
| 26 | + @State private var shadowsAreVisible = true |
| 27 | + |
| 28 | + /// A Boolean value indicating whether the leader line graphics are visible. |
| 29 | + @State private var leadersAreVisible = true |
| 30 | + |
| 31 | + var body: some View { |
| 32 | + VStack(spacing: 0) { |
| 33 | + WorldScaleSceneView { _ in |
| 34 | + SceneView(scene: model.scene, graphicsOverlays: [ |
| 35 | + model.pipeGraphicsOverlay, |
| 36 | + model.shadowGraphicsOverlay, |
| 37 | + model.leaderGraphicsOverlay |
| 38 | + ]) |
| 39 | + } |
| 40 | + .calibrationButtonAlignment(.bottomLeading) |
| 41 | + .onCalibratingChanged { newCalibrating in |
| 42 | + model.scene.baseSurface.opacity = newCalibrating ? 0.6 : 0 |
| 43 | + } |
| 44 | + |
| 45 | + Divider() |
| 46 | + } |
| 47 | + .toolbar { |
| 48 | + ToolbarItem(placement: .bottomBar) { |
| 49 | + settingsMenu |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// The settings menu. |
| 55 | + private var settingsMenu: some View { |
| 56 | + Menu("Settings") { |
| 57 | + Toggle("Shadows", isOn: $shadowsAreVisible) |
| 58 | + .onChange(of: shadowsAreVisible) { newValue in |
| 59 | + model.shadowGraphicsOverlay.isVisible = newValue |
| 60 | + } |
| 61 | + Toggle("Leaders", isOn: $leadersAreVisible) |
| 62 | + .onChange(of: leadersAreVisible) { newValue in |
| 63 | + model.leaderGraphicsOverlay.isVisible = newValue |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +extension AugmentRealityToShowHiddenInfrastructureView { |
| 71 | + // MARK: Scene Model |
| 72 | + |
| 73 | + /// The view model for scene view in the sample. |
| 74 | + class SceneModel: ObservableObject { |
| 75 | + /// A scene with an imagery basemap style and an elevation surface. |
| 76 | + let scene: ArcGIS.Scene = { |
| 77 | + let scene = Scene(basemapStyle: .arcGISImagery) |
| 78 | + |
| 79 | + // Create a surface with an elevation source and set it to the scene's base surface. |
| 80 | + let surface = Surface() |
| 81 | + surface.navigationConstraint = .unconstrained |
| 82 | + surface.opacity = 0 |
| 83 | + surface.backgroundGrid.isVisible = false |
| 84 | + |
| 85 | + let elevationSource = ArcGISTiledElevationSource(url: .worldElevationService) |
| 86 | + surface.addElevationSource(elevationSource) |
| 87 | + scene.baseSurface = surface |
| 88 | + |
| 89 | + return scene |
| 90 | + }() |
| 91 | + |
| 92 | + /// The graphics overlay for the pipe graphics. |
| 93 | + let pipeGraphicsOverlay: GraphicsOverlay = { |
| 94 | + let graphicsOverlay = GraphicsOverlay() |
| 95 | + graphicsOverlay.sceneProperties.surfacePlacement = .absolute |
| 96 | + |
| 97 | + let strokeSymbolLayer = SolidStrokeSymbolLayer( |
| 98 | + width: 0.3, |
| 99 | + color: .red, |
| 100 | + lineStyle3D: .tube |
| 101 | + ) |
| 102 | + let polylineSymbol = MultilayerPolylineSymbol(symbolLayers: [strokeSymbolLayer]) |
| 103 | + graphicsOverlay.renderer = SimpleRenderer(symbol: polylineSymbol) |
| 104 | + |
| 105 | + return graphicsOverlay |
| 106 | + }() |
| 107 | + |
| 108 | + /// The graphics overlay for the shadow graphics of the underground pipes. |
| 109 | + let shadowGraphicsOverlay: GraphicsOverlay = { |
| 110 | + let graphicsOverlay = GraphicsOverlay() |
| 111 | + graphicsOverlay.sceneProperties.surfacePlacement = .drapedFlat |
| 112 | + |
| 113 | + let yellowLineSymbol = SimpleLineSymbol(style: .solid, color: .systemYellow, width: 0.3) |
| 114 | + graphicsOverlay.renderer = SimpleRenderer(symbol: yellowLineSymbol) |
| 115 | + |
| 116 | + return graphicsOverlay |
| 117 | + }() |
| 118 | + |
| 119 | + /// The graphics overlay for the pipe leader line graphics. |
| 120 | + let leaderGraphicsOverlay: GraphicsOverlay = { |
| 121 | + let graphicsOverlay = GraphicsOverlay() |
| 122 | + graphicsOverlay.sceneProperties.surfacePlacement = .absolute |
| 123 | + |
| 124 | + let dashedRedLineSymbol = SimpleLineSymbol(style: .dash, color: .systemRed, width: 0.3) |
| 125 | + graphicsOverlay.renderer = SimpleRenderer(symbol: dashedRedLineSymbol) |
| 126 | + |
| 127 | + return graphicsOverlay |
| 128 | + }() |
| 129 | + |
| 130 | + init() { |
| 131 | + Task { |
| 132 | + try? await scene.load() |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /// Adds graphics created from a given polyline and elevation offset to the graphics overlays. |
| 137 | + /// - Parameters: |
| 138 | + /// - polyline: The polyline representing a pipe. |
| 139 | + /// - elevationOffset: An elevation to offset the pipe with. |
| 140 | + func addGraphics(for polyline: Polyline, elevationOffset: Double) async { |
| 141 | + guard let firstPoint = polyline.parts.first?.startPoint, |
| 142 | + let elevation = try? await scene.baseSurface.elevation(at: firstPoint) else { return } |
| 143 | + |
| 144 | + // Add the elevation with the offset to the polyline. |
| 145 | + let elevatedPolyline = GeometryEngine.makeGeometry( |
| 146 | + from: polyline, |
| 147 | + z: elevation + elevationOffset |
| 148 | + ) |
| 149 | + |
| 150 | + // Add a pipe graphic using the elevated polyline. |
| 151 | + let pipeGraphic = Graphic(geometry: elevatedPolyline) |
| 152 | + pipeGraphicsOverlay.addGraphic(pipeGraphic) |
| 153 | + |
| 154 | + // Add graphics for the leader lines. |
| 155 | + let leaderLineGraphics = elevatedPolyline.parts.map { part in |
| 156 | + part.points.map { point in |
| 157 | + let offsetPoint = GeometryEngine.makeGeometry( |
| 158 | + from: point, |
| 159 | + z: (point.z ?? 0) - elevationOffset |
| 160 | + ) |
| 161 | + let leaderLine = Polyline(points: [point, offsetPoint]) |
| 162 | + return Graphic(geometry: leaderLine) |
| 163 | + } |
| 164 | + } |
| 165 | + leaderGraphicsOverlay.addGraphics(Array(leaderLineGraphics.joined())) |
| 166 | + |
| 167 | + // Add a shadow graphic for the pipe if it is below ground. |
| 168 | + if elevationOffset < 0 { |
| 169 | + let shadowGraphic = Graphic(geometry: polyline) |
| 170 | + shadowGraphicsOverlay.addGraphic(shadowGraphic) |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +private extension URL { |
| 177 | + /// The URL of the Terrain 3D ArcGIS REST Service. |
| 178 | + static var worldElevationService: URL { |
| 179 | + URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")! |
| 180 | + } |
| 181 | +} |
0 commit comments