Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Scene properties expressions

This code sample is available for these platforms:
View Sample on GitHub

Update the orientation of a graphic using expressions based on its attributes.

Image of scene properties expressions

Use case

Instead of reading the attribute and changing the rotation on the symbol for a single graphic (a manual CPU operation), you can bind the rotation to an expression that applies to the whole overlay (an automatic GPU operation). This usually results in a noticeable performance boost (smooth rotations).

How to use the sample

Adjust the heading and pitch sliders to rotate the cone.

How it works

  1. Create a new graphics overlay.
  2. Create a simple renderer and set its scene properties.
  3. Set the heading expression to [HEADING].
  4. Apply the renderer to the graphics overlay.
  5. Create a graphic and add it to the overlay.
  6. To update the graphic's rotation, update the HEADING or PITCH property in the graphic's attributes.

Relevant API

  • Graphic.Attributes
  • GraphicsOverlay
  • SceneProperties
  • SceneProperties.HeadingExpression
  • SceneProperties.PitchExpression
  • SimpleRenderer
  • SimpleRenderer.SceneProperties

Tags

3D, expression, graphics, heading, pitch, rotation, scene, symbology

Sample Code

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="ArcGISRuntimeXamarin.Samples.ScenePropertiesExpressions.ScenePropertiesExpressions"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms"
    xmlns:resources="clr-namespace:Forms.Resources;assembly=ArcGISRuntime">
    <RelativeLayout>
        <esriUI:SceneView x:Name="MySceneView" Style="{StaticResource MapWithFormStyle}" ViewInsets="0" />
        <resources:ResponsiveFormContainer x:Name="FormContainer">
            <StackLayout>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="200" />
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" Style="{StaticResource LabelStyle}" Text="Heading:" />
                    <Slider
                        x:Name="HeadingSlider"
                        Grid.Column="1"
                        Maximum="360"
                        MaximumTrackColor="CadetBlue"
                        Minimum="0"
                        MinimumTrackColor="CadetBlue" />
                </Grid>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="200" />
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" Style="{StaticResource LabelStyle}" Text="Pitch:" />
                    <Slider
                        x:Name="PitchSlider"
                        Grid.Column="1"
                        Maximum="90"
                        MaximumTrackColor="CadetBlue"
                        Minimum="-90"
                        MinimumTrackColor="CadetBlue" />
                </Grid>
            </StackLayout>
        </resources:ResponsiveFormContainer>
    </RelativeLayout>
</ContentPage>
// Copyright 2019 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.

using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;
using Xamarin.Forms;
using Color = System.Drawing.Color;

namespace ArcGISRuntimeXamarin.Samples.ScenePropertiesExpressions
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Scene properties expressions",
        category: "GraphicsOverlay",
        description: "Update the orientation of a graphic using expressions based on its attributes.",
        instructions: "Adjust the heading and pitch sliders to rotate the cone.",
        tags: new[] { "3D", "expression", "graphics", "heading", "pitch", "rotation", "scene", "symbology" })]
    public partial class ScenePropertiesExpressions : ContentPage
    {
        public ScenePropertiesExpressions()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            // Set up the scene with an imagery basemap.
            MySceneView.Scene = new Scene(Basemap.CreateImagery());

            // Set the initial viewpoint for the scene.
            MapPoint point = new MapPoint(83.9, 28.4, 1000, SpatialReferences.Wgs84);
            Camera initialCamera = new Camera(point, 1000, 0, 50, 0);
            MySceneView.SetViewpointCamera(initialCamera);

            // Create a graphics overlay.
            GraphicsOverlay overlay = new GraphicsOverlay();
            overlay.SceneProperties.SurfacePlacement = SurfacePlacement.Relative;
            MySceneView.GraphicsOverlays.Add(overlay);

            // Add a renderer using rotation expressions.
            SimpleRenderer renderer = new SimpleRenderer();
            renderer.SceneProperties.HeadingExpression = "[HEADING]";
            renderer.SceneProperties.PitchExpression = "[PITCH]";

            // Apply the renderer to the graphics overlay.
            overlay.Renderer = renderer;

            // Create a red cone graphic.
            SimpleMarkerSceneSymbol coneSymbol = SimpleMarkerSceneSymbol.CreateCone(Color.Red, 100, 100);
            coneSymbol.Pitch = -90;
            MapPoint conePoint = new MapPoint(83.9, 28.41, 200, SpatialReferences.Wgs84);
            Graphic cone = new Graphic(conePoint, coneSymbol);

            // Add the cone graphic to the overlay.
            overlay.Graphics.Add(cone);

            // Listen for changes in slider values and update graphic properties.
            HeadingSlider.ValueChanged += (sender, e) => { cone.Attributes["HEADING"] = HeadingSlider.Value; };
            PitchSlider.ValueChanged += (sender, e) => { cone.Attributes["PITCH"] = PitchSlider.Value; };
        }
    }
}