Skip to content

Show realistic light and shadows

View on GitHub

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

Image of show realistic light 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 scene view. Select a time of day from the slider (based on a 24hr clock) to show the lighting for that time of day in the scene view.

How it works

  1. Create an ArcGISScene and display it in a ArcGISSceneView.
  2. Create a DateTime to define the time of day.
  3. Set the sun time property to that DateTime with ArcGISSceneViewController.sunTime.
  4. Set the lighting mode of the scene view to no light, light, or light and shadows with ArcGISSceneViewController.sunLighting = LightingMode.

Relevant API

  • ArcGISScene
  • ArcGISSceneViewController.sunLighting
  • ArcGISSceneViewController.sunTime
  • LightingMode

Tags

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

Sample Code

show_realistic_light_and_shadows.dart
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
// Copyright 2025 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 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class ShowRealisticLightAndShadows extends StatefulWidget {
  const ShowRealisticLightAndShadows({super.key});

  @override
  State<ShowRealisticLightAndShadows> createState() =>
      _ShowRealisticLightAndShadowsState();
}

class _ShowRealisticLightAndShadowsState
    extends State<ShowRealisticLightAndShadows>
    with SampleStateSupport {
  // Create a controller for the Scene view.
  final _sceneViewController = ArcGISSceneView.createController();

  // Define the lighting choices available in the dropdown button.
  final _lightingChoices = {
    'Light and Shadows': LightingMode.lightAndShadows,
    'Light': LightingMode.light,
    'No Light': LightingMode.noLight,
  };

  // The initial selected lighting mode.
  String _selectedLighting = 'Light and Shadows';

  // The initial time value for the sun, in 24 hours.
  double _timeValue = 8.5;

  // The initial sun time, set to a specific date and time.
  DateTime _sunTime = DateTime(2018, 8, 10, 8, 30);

  // Date format for displaying the sun time.
  final _dateFormat = DateFormat('MMMM dd, yyyy, hh:mm a');

  // A flag for when the Scene view is ready and controls can be used.
  var _ready = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: false,
        left: false,
        right: false,
        child: Stack(
          children: [
            Column(
              spacing: 5,
              children: [
                Expanded(
                  // Add a scene view to the widget tree and set a controller.
                  child: ArcGISSceneView(
                    controllerProvider: () => _sceneViewController,
                    onSceneViewReady: onSceneViewReady,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    // A dropdown button for lighting choices.
                    DropdownButton(
                      value: _selectedLighting,
                      items: _lightingChoices.keys
                          .map(
                            (label) => DropdownMenuItem(
                              value: label,
                              child: Text(
                                label,
                                style: TextStyle(
                                  color: Theme.of(context).primaryColor,
                                ),
                              ),
                            ),
                          )
                          .toList(),
                      onChanged: (value) {
                        if (value != null) {
                          setState(() {
                            _selectedLighting = value;
                            // Update the sun lighting based on the selected choice.
                            _sceneViewController.sunLighting =
                                _lightingChoices[value]!;
                          });
                        }
                      },
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    // Slider to change the sun time
                    Expanded(
                      child: Padding(
                        padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
                        child: Slider(
                          max: 24,
                          divisions: 24 * 4, // 15-minute increments
                          value: _timeValue,
                          label: '${_timeValue.toStringAsFixed(2)} h',
                          onChanged: (value) {
                            setState(() {
                              _timeValue = value;
                              // Update the sun time based on the slider value.
                              setSunTimeFromValue(_timeValue);
                            });
                          },
                        ),
                      ),
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    // Slider to change the sun time from 0 to 24 hours.
                    Padding(
                      padding: const EdgeInsets.only(bottom: 10),
                      child: Text(
                        // Format the sun time as [MMMM dd, yyyy, hh:mm a]
                        _dateFormat.format(_sunTime),
                        style: TextStyle(color: Theme.of(context).primaryColor),
                      ),
                    ),
                  ],
                ),
              ],
            ),
            // Display a progress indicator and prevent interaction until state is ready.
            LoadingIndicator(visible: !_ready),
          ],
        ),
      ),
    );
  }

  Future<void> onSceneViewReady() async {
    // Create a Scene with a topographic basemap style.
    final scene = ArcGISScene.withBasemapStyle(BasemapStyle.arcGISTopographic);
    _sceneViewController.arcGISScene = scene;
    // Create a elevation source for the scene.
    final elevationSource = ArcGISTiledElevationSource.withUri(
      Uri.parse(
        'https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer',
      ),
    );
    scene.baseSurface.elevationSources.add(elevationSource);
    // Create a SceneLayer with the provided URL.
    final sceneLayer = ArcGISSceneLayer.withUri(
      Uri.parse(
        'https://tiles.arcgis.com/tiles/P3ePLMYs2RVChkJx/arcgis/rest/services/DevA_BuildingShells/SceneServer/layers/0',
      ),
    );
    // Add the SceneLayer to the Scene.
    scene.operationalLayers.add(sceneLayer);
    // Set the viewpoint to the extent of the SceneLayer.
    final camera = Camera.withLookAtPoint(
      lookAtPoint: ArcGISPoint(
        x: -122.68711400735646,
        y: 45.539231741174206,
        z: 334.48350897897035,
      ),
      distance: 541.0002111233771,
      heading: 162.58767335295363,
      pitch: 68.44381635828522,
      roll: 1.0259216929482844e-14,
    );
    _sceneViewController.setViewpointCamera(camera);

    // Set the atmosphere effect to realistic.
    _sceneViewController.atmosphereEffect = AtmosphereEffect.realistic;
    // Set the time of day to noon.
    setSunTimeFromValue(8.5);
    // Enable realistic lighting and shadows.
    _sceneViewController.sunLighting = LightingMode.lightAndShadows;
    // Set the ready state variable to true to enable the sample UI.
    setState(() => _ready = true);
  }

  // Update the sun time in the scene view controller.
  void setSunTimeFromValue(double value) {
    final remainder = value % 1;
    final hours = (value - remainder).toInt();
    final minutes = (remainder * 60).toInt();
    final dateTime = DateTime(2018, 8, 10, hours, minutes);

    _sceneViewController.sunTime = dateTime;
    setState(() => _sunTime = dateTime);
  }
}

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