/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

  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="SlopeTexture.cs" company="Helix 3D Toolkit">
  3. // http://helixtoolkit.codeplex.com, license: Ms-PL
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace HelixToolkit.Wpf
  7. {
  8. using System;
  9. using System.Windows;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Media3D;
  12. /// <summary>
  13. /// Texture by the slope angle.
  14. /// </summary>
  15. public class SlopeTexture : TerrainTexture
  16. {
  17. #region Constructors and Destructors
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="SlopeTexture"/> class.
  20. /// </summary>
  21. /// <param name="gradientSteps">
  22. /// The gradient steps.
  23. /// </param>
  24. public SlopeTexture(int gradientSteps)
  25. {
  26. if (gradientSteps > 0)
  27. {
  28. this.Brush = BrushHelper.CreateSteppedGradientBrush(GradientBrushes.BlueWhiteRed, gradientSteps);
  29. }
  30. else
  31. {
  32. this.Brush = GradientBrushes.BlueWhiteRed;
  33. }
  34. }
  35. #endregion
  36. #region Public Properties
  37. /// <summary>
  38. /// Gets or sets the brush.
  39. /// </summary>
  40. /// <value>The brush.</value>
  41. public Brush Brush { get; set; }
  42. #endregion
  43. #region Public Methods
  44. /// <summary>
  45. /// Calculates the texture for the specified model.
  46. /// </summary>
  47. /// <param name="model">
  48. /// The model.
  49. /// </param>
  50. /// <param name="mesh">
  51. /// The mesh.
  52. /// </param>
  53. public override void Calculate(TerrainModel model, MeshGeometry3D mesh)
  54. {
  55. var normals = MeshGeometryHelper.CalculateNormals(mesh);
  56. var texcoords = new PointCollection();
  57. var up = new Vector3D(0, 0, 1);
  58. for (int i = 0; i < normals.Count; i++)
  59. {
  60. double slope = Math.Acos(Vector3D.DotProduct(normals[i], up)) * 180 / Math.PI;
  61. double u = slope / 40;
  62. if (u > 1)
  63. {
  64. u = 1;
  65. }
  66. if (u < 0)
  67. {
  68. u = 0;
  69. }
  70. texcoords.Add(new Point(u, u));
  71. }
  72. this.TextureCoordinates = texcoords;
  73. this.Material = MaterialHelper.CreateMaterial(this.Brush);
  74. }
  75. #endregion
  76. }
  77. }