/Controls/HelixToolkit.Wpf/Visual3Ds/Terrain/Textures/SlopeTexture.cs
https://bitbucket.org/kubrakms/thermalconductivity · C# · 90 lines · 50 code · 13 blank · 27 comment · 4 complexity · 5b1bb688422fab02f8b31a532ff020f0 MD5 · raw file
- // --------------------------------------------------------------------------------------------------------------------
- // <copyright file="SlopeTexture.cs" company="Helix 3D Toolkit">
- // http://helixtoolkit.codeplex.com, license: Ms-PL
- // </copyright>
- // --------------------------------------------------------------------------------------------------------------------
- namespace HelixToolkit.Wpf
- {
- using System;
- using System.Windows;
- using System.Windows.Media;
- using System.Windows.Media.Media3D;
- /// <summary>
- /// Texture by the slope angle.
- /// </summary>
- public class SlopeTexture : TerrainTexture
- {
- #region Constructors and Destructors
- /// <summary>
- /// Initializes a new instance of the <see cref="SlopeTexture"/> class.
- /// </summary>
- /// <param name="gradientSteps">
- /// The gradient steps.
- /// </param>
- public SlopeTexture(int gradientSteps)
- {
- if (gradientSteps > 0)
- {
- this.Brush = BrushHelper.CreateSteppedGradientBrush(GradientBrushes.BlueWhiteRed, gradientSteps);
- }
- else
- {
- this.Brush = GradientBrushes.BlueWhiteRed;
- }
- }
- #endregion
- #region Public Properties
- /// <summary>
- /// Gets or sets the brush.
- /// </summary>
- /// <value>The brush.</value>
- public Brush Brush { get; set; }
- #endregion
- #region Public Methods
- /// <summary>
- /// Calculates the texture for the specified model.
- /// </summary>
- /// <param name="model">
- /// The model.
- /// </param>
- /// <param name="mesh">
- /// The mesh.
- /// </param>
- public override void Calculate(TerrainModel model, MeshGeometry3D mesh)
- {
- var normals = MeshGeometryHelper.CalculateNormals(mesh);
- var texcoords = new PointCollection();
- var up = new Vector3D(0, 0, 1);
- for (int i = 0; i < normals.Count; i++)
- {
- double slope = Math.Acos(Vector3D.DotProduct(normals[i], up)) * 180 / Math.PI;
- double u = slope / 40;
- if (u > 1)
- {
- u = 1;
- }
- if (u < 0)
- {
- u = 0;
- }
- texcoords.Add(new Point(u, u));
- }
- this.TextureCoordinates = texcoords;
- this.Material = MaterialHelper.CreateMaterial(this.Brush);
- }
- #endregion
- }
- }