Show realistic lighting and shadows for a given time of day.
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
- Create an
AGSScene
and display it in anAGSSceneView
. - Create a
Calendar
to define the time of day. - Set the sun time to the scene view's
sunTime
property. - Set the
sunLighting
property of the scene view tonoLight
,light
, orlightAndShadows
.
Relevant API
- AGSLightingMode
- AGSScene
- AGSSceneView
Tags
3D, lighting, realism, realistic, rendering, shadows, sun, time
Sample Code
// 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)
}
}