Realistic lighting and shadows

View on GitHubSample viewer app

Show realistic lighting and shadows for a given time of day.

Image of realistic lighting and shadows

Use case

You can use realistic lighting to evaluate the shadow impact of buildings and utility infrastructure on the surrounding community. This could be useful for civil engineers and urban planners, or for events management assessing the impact of building shadows during an outdoor event.

How to use the sample

Select one of the three lighting options to show that lighting effect on the AGSSceneView. Select a time of day from the slider (based on a 24hr clock) to show the lighting for that time of day in the AGSSceneView.

How it works

  1. Create an AGSScene and display it in an AGSSceneView.
  2. Create a Calendar to define the time of day.
  3. Set the sun time to the scene view's sunTime property.
  4. Set the sunLighting property of the scene view to noLight, light, or lightAndShadows.

Relevant API

  • AGSLightingMode
  • AGSScene
  • AGSSceneView

Tags

3D, lighting, realism, realistic, rendering, shadows, sun, time

Sample Code

RealisticLightingAndShadowsViewController.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
// Copyright 2020 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
//
//   http://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 UIKit
import ArcGIS

class RealisticLightingAndShadowsViewController: UIViewController {
    // MARK: Storyboard views

    /// The label to display the date time set by the slider.
    @IBOutlet weak var dateTimeLabel: UILabel!
    /// The slider to change the time of the day.
    @IBOutlet weak var minuteSlider: UISlider!
    /// The scene view managed by the view controller.
    @IBOutlet weak var sceneView: AGSSceneView! {
        didSet {
            sceneView.scene = makeScene()
            sceneView.atmosphereEffect = .realistic
            sceneView.sunLighting = .lightAndShadows
            sceneView.setViewpointCamera(AGSCamera(latitude: 45.54605, longitude: -122.69033, altitude: 941.00021, heading: 162.58544, pitch: 60.0, roll: 0))
        }
    }

    // MARK: Instance properties

    /// A date formatter to format date time output.
    let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        return formatter
    }()

    // MARK: Instance methods

    /// Create a scene.
    ///
    /// - Returns: A new `AGSScene` object.
    func makeScene() -> AGSScene {
        // Create a scene layer from buildings REST service.
        let buildingsURL = URL(string: "https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/DevA_BuildingShells/SceneServer")!
        let buildingsLayer = AGSArcGISSceneLayer(url: buildingsURL)
        // Create an elevation source from Terrain3D REST service.
        let elevationServiceURL = URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
        let elevationSource = AGSArcGISTiledElevationSource(url: elevationServiceURL)
        let surface = AGSSurface()
        surface.elevationSources = [elevationSource]
        let scene = AGSScene(basemapStyle: .arcGISTopographic)
        scene.baseSurface = surface
        scene.operationalLayers.add(buildingsLayer)
        return scene
    }

    // MARK: Actions

    @IBAction func sliderValueChanged(_ sender: UISlider) {
        // A DateComponents struct to encapsulate the minute value from the slider.
        let dateComponents = DateComponents(minute: Int(sender.value))
        let startOfToday = Calendar.current.startOfDay(for: Date())
        let date = Calendar.current.date(byAdding: dateComponents, to: startOfToday)!
        dateTimeLabel.text = dateFormatter.string(from: date)
        sceneView.sunTime = date
    }

    @IBAction func modeButtonTapped(_ button: UIBarButtonItem) {
        let alertController = UIAlertController(
            title: "Choose a lighting mode for the scene view.",
            message: nil,
            preferredStyle: .actionSheet
        )
        let modes: KeyValuePairs<String, AGSLightingMode> = [
            "Light and shadows": .lightAndShadows,
            "Light only": .light,
            "No light": .noLight
        ]
        modes.forEach { name, lightingMode in
            let action = UIAlertAction(title: name, style: .default) { _ in
                self.sceneView.sunLighting = lightingMode
            }
            alertController.addAction(action)
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        alertController.addAction(cancelAction)
        alertController.popoverPresentationController?.barButtonItem = button
        present(alertController, animated: true)
    }

    // MARK: UIViewController

    override func viewDidLoad() {
        super.viewDidLoad()
        // Add the source code button item to the right of navigation bar.
        (navigationItem.rightBarButtonItem as? SourceCodeBarButtonItem)?.filenames = ["RealisticLightingAndShadowsViewController"]
        // Initialize the date time.
        sliderValueChanged(minuteSlider)
    }
}

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