Set surface placement mode

View on GitHub

Position graphics relative to a surface using different surface placement modes.

Screenshot of set surface placement mode sample

Use case

Depending on the use case, data might be displayed at an absolute height (e.g. flight data recorded with altitude information), at a relative height to the terrain (e.g. transmission lines positioned relative to the ground), at a relative height to objects in the scene (e.g. extruded polygons, integrated mesh scene layer), or draped directly onto the terrain (e.g. location markers, area boundaries).

How to use the sample

The sample loads a scene showing four points that use the individual surface placement rules (absolute, relative, relative to scene, and either draped billboarded or draped flat). Use the toggle to change the draped mode and the slider to dynamically adjust the z value of the graphics. Explore the scene by zooming in/out and by panning around to observe the effects of the surface placement rules.

How it works

  1. Create a GraphicsOverlay instance for each SurfacePlacement:
    • absolute positions the graphic using only its z value.
    • drapedBillboarded positions the graphic upright on the surface and always facing the camera, not using its z value.
    • drapedFlat positions the graphic flat on the surface, not using its z value.
    • relative positions the graphic using its z value plus the elevation of the surface.
    • relativeToScene positions the graphic using its z value plus the altitude values of the scene.
  2. Create and add graphics to the graphics overlays.
  3. Set the graphics overlays' scene properties' surface placement to the respective surface placement.
  4. Create a SceneView instance with a scene and the graphics overlays.

Relevant API

  • Graphic
  • GraphicsOverlay
  • LayerSceneProperties
  • static GeometryEngine.makeGeometry(from:z:)
  • Surface
  • SurfacePlacement

About the data

The scene shows a view of Brest, France. Four points are shown hovering with positions defined by each of the different surface placement modes (absolute, relative, relative to scene, and either draped billboarded or draped flat).

Additional information

This sample uses an elevation service to add elevation/terrain to the scene. Graphics are positioned relative to that surface for the drapedBillboarded, drapedFlat, and relative surface placement modes. It also uses a scene layer containing 3D models of buildings. Graphics are positioned relative to that scene layer for the relativeToScene surface placement mode.

Tags

3D, absolute, altitude, draped, elevation, floating, relative, scenes, sea level, surface placement

Sample Code

SetSurfacePlacementModeView.swift
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2022 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import SwiftUI
import ArcGIS

struct SetSurfacePlacementModeView: View {
    /// The view model for this sample.
    @StateObject private var model = Model()

    var body: some View {
        VStack(spacing: 0) {
            SceneView(scene: model.scene, graphicsOverlays: model.graphicsOverlays)
            VStack {
                HStack {
                    Text("Draped mode:")
                        .frame(width: 120, alignment: .leading)

                    Picker("Draped Mode", selection: $model.drapedMode) {
                        ForEach(DrapedMode.allCases, id: \.self) { mode in
                            Text(mode.label)
                        }
                    }
                    .pickerStyle(.segmented)
                }
                .frame(maxWidth: 540)

                HStack {
                    Text("Z-value: \(model.zValue, format: .measurement(width: .narrow))")
                        .frame(width: 120, alignment: .leading)
                        .minimumScaleFactor(0.5)
                        .lineLimit(1)

                    Slider(value: $model.zValue.value, in: model.zValueRange.doubleRange) {
                        Text("Z-value")
                    } minimumValueLabel: {
                        Text(model.zValueRange.lowerBound, format: .measurement(width: .narrow))
                    } maximumValueLabel: {
                        Text(model.zValueRange.upperBound, format: .measurement(width: .narrow))
                    }
                }
                .frame(maxWidth: 540)
            }
            .padding()
            .frame(maxWidth: .infinity)
            .background(.ultraThinMaterial, ignoresSafeAreaEdges: .all)
        }
    }
}

private extension SetSurfacePlacementModeView {
    /// The model used to store the geo model and other expensive objects
    /// used in this view.
    class Model: ObservableObject {
        /// The range of possible z-values. The z-value range is 0 to 140 meters in this sample.
        let zValueRange = Measurement.zMin...Measurement.zMax

        /// The current z-value.
        @Published var zValue: Measurement<UnitLength> {
            didSet {
                updateGraphics()
            }
        }

        /// The current draped mode.
        @Published var drapedMode: DrapedMode {
            didSet {
                updateDrapedGraphics()
            }
        }

        /// The scene for this sample.
        let scene: ArcGIS.Scene

        /// The graphics overlays containing each placement graphic.
        let graphicsOverlays: [GraphicsOverlay]

        /// A dictionary for graphics overlays of different surface placement modes.
        private let overlaysBySurfacePlacement: [SurfacePlacement: GraphicsOverlay]

        init() {
            // Creates the scene with an initial viewpoint.
            let scene = Scene(basemapStyle: .arcGISImagery)
            let point = Point(x: -4.4595, y: 48.3889, z: 80, spatialReference: .wgs84)
            let camera = Camera(location: point, heading: 330, pitch: 97, roll: 0)
            scene.initialViewpoint = Viewpoint(boundingGeometry: point, camera: camera)

            // Creates and adds an elevation source to a surface and sets it
            // to the scene's base surface.
            let surface = Surface()
            surface.addElevationSource(ArcGISTiledElevationSource(url: .worldElevationService))
            scene.baseSurface = surface

            // Adds a scene layer from a URL to the scene's operational layers.
            scene.addOperationalLayer(ArcGISSceneLayer(url: .sceneService))
            self.scene = scene

            // Creates the graphics overlays for each surface placement.
            graphicsOverlays = SurfacePlacement.allCases.map(Self.makeGraphicsOverlay)

            // Creates the dictionary for graphics overlays of different surface placements.
            overlaysBySurfacePlacement = Dictionary(uniqueKeysWithValues: zip(SurfacePlacement.allCases, graphicsOverlays))

            // Sets the initial z-value to the mid-range of the possible z-values.
            zValue = Measurement(value: Measurement.zMid, unit: UnitLength.meters)

            // Sets the current draped mode to billboarded.
            drapedMode = .billboarded

            // Updates the draped graphics to show the current draped mode.
            updateDrapedGraphics()
        }

        /// Updates the draped graphics to change their visibility based on the current draped mode.
        private func updateDrapedGraphics() {
            overlaysBySurfacePlacement[.drapedBillboarded]?.isVisible = drapedMode == .billboarded
            overlaysBySurfacePlacement[.drapedFlat]?.isVisible = drapedMode == .flat
        }

        /// Updates the graphics' z-value.
        private func updateGraphics() {
            overlaysBySurfacePlacement.values.forEach { graphicsOverlay in
                graphicsOverlay.graphics.forEach { graphic in
                    graphic.geometry = GeometryEngine.makeGeometry(from: graphic.geometry!, z: zValue.value)
                }
            }
        }

        /// Creates a graphics overlay for the given surface placement.
        /// - Parameter surfacePlacement: The surface placement for which to create a graphics overlay.
        /// - Returns: A new `GraphicsOverlay` object.
        private static func makeGraphicsOverlay(for surfacePlacement: SurfacePlacement) -> GraphicsOverlay {
            // Creates symbols for the graphic.
            let markerSymbol = SimpleMarkerSymbol(style: .triangle, color: .red, size: 20)
            let textSymbol = TextSymbol(text: surfacePlacement.label, color: .blue, size: 20, horizontalAlignment: .left)
            textSymbol.haloColor = .cyan
            textSymbol.haloWidth = 2

            // Adds an offset to avoid overlapping the text and marker symbols.
            textSymbol.offsetY = 20

            // Adds an offset to x and y of the geometry to better differentiate certain geometries.
            let offset = surfacePlacement == .relativeToScene ? 2e-4 : 0

            // Creates the graphics for the graphics overlay.
            let surfaceRelatedPoint = Point(x: -4.4609257 + offset, y: 48.3903965 + offset, z: Measurement.zMid, spatialReference: .wgs84)
            let graphics = [markerSymbol, textSymbol].map { Graphic(geometry: surfaceRelatedPoint, symbol: $0) }

            // Creates the graphics overlay and sets its scene properties'
            // surface placement to the respective surface placement.
            let overlay = GraphicsOverlay(graphics: graphics)
            overlay.sceneProperties.surfacePlacement = surfacePlacement
            return overlay
        }
    }

    enum DrapedMode: CaseIterable {
        case billboarded
        case flat

        /// A human-readable label for the draped mode.
        var label: String {
            switch self {
            case .billboarded: return "Billboarded"
            case .flat: return "Flat"
            }
        }
    }
}

private extension SurfacePlacement {
    static var allCases: [Self] { [.absolute, .drapedBillboarded, .drapedFlat, .relative, .relativeToScene] }

    /// A human-readable label of the surface placement.
    var label: String {
        switch self {
        case .absolute: return "Absolute"
        case .drapedBillboarded: return "Draped Billboarded"
        case .drapedFlat: return "Draped Flat"
        case .relative: return "Relative"
        case .relativeToScene: return "Relative to Scene"
        @unknown default: return "Unknown"
        }
    }
}

private extension Measurement where UnitType == UnitLength {
    /// The minimum z-value.
    static var zMin: Self { Measurement(value: 0, unit: UnitLength.meters) }

    /// The maximum z-value.
    static var zMax: Self { Measurement(value: 140, unit: UnitLength.meters) }

    /// The mid-range of the possible z-values.
    static var zMid: Double { (zMin.value + zMax.value) / 2 }
}

private extension ClosedRange where Bound == Measurement<UnitLength> {
    /// The measurement's values as a closed range of doubles.
    var doubleRange: ClosedRange<Double> { self.lowerBound.value...self.upperBound.value }
}

private extension URL {
    /// The URL of a Brest, France buildings scene service.
    static var sceneService: URL {
        URL(string: "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/Buildings_Brest/SceneServer")!
    }

    /// The URL of the Terrain 3D ArcGIS REST Service.
    static var worldElevationService: URL {
        URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.