/Controls/HelixToolkit.Wpf/Visual3Ds/LightSetups/SunLight.cs

https://bitbucket.org/kubrakms/thermalconductivity · C# · 151 lines · 79 code · 24 blank · 48 comment · 0 complexity · fd4321eade879a8b639efef3093efe5a MD5 · raw file

  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="SunLight.cs" company="Helix 3D Toolkit">
  3. // http://helixtoolkit.codeplex.com, license: Ms-PL
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace HelixToolkit.Wpf
  7. {
  8. using System.Windows;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Media3D;
  11. /// <summary>
  12. /// A visual element that contains a "sunlight" light model.
  13. /// </summary>
  14. public class SunLight : LightSetup
  15. {
  16. #region Constants and Fields
  17. /// <summary>
  18. /// The altitude property.
  19. /// </summary>
  20. public static readonly DependencyProperty AltitudeProperty = DependencyProperty.Register(
  21. "Altitude", typeof(double), typeof(SunLight), new UIPropertyMetadata(60.0, SetupChanged));
  22. /// <summary>
  23. /// The ambient property.
  24. /// </summary>
  25. public static readonly DependencyProperty AmbientProperty = DependencyProperty.Register(
  26. "Ambient", typeof(double), typeof(SunLight), new UIPropertyMetadata(0.4, SetupChanged));
  27. /// <summary>
  28. /// The azimuth property.
  29. /// </summary>
  30. public static readonly DependencyProperty AzimuthProperty = DependencyProperty.Register(
  31. "Azimuth", typeof(double), typeof(SunLight), new UIPropertyMetadata(130.0, SetupChanged));
  32. /// <summary>
  33. /// The brightness property.
  34. /// </summary>
  35. public static readonly DependencyProperty BrightnessProperty = DependencyProperty.Register(
  36. "Brightness", typeof(double), typeof(SunLight), new UIPropertyMetadata(0.6, SetupChanged));
  37. /// <summary>
  38. /// The altitude axis.
  39. /// </summary>
  40. private readonly Vector3D AltitudeAxis = new Vector3D(0, 1, 0);
  41. /// <summary>
  42. /// The azimuth axis.
  43. /// </summary>
  44. private readonly Vector3D AzimuthAxis = new Vector3D(0, 0, 1);
  45. #endregion
  46. #region Public Properties
  47. /// <summary>
  48. /// Gets or sets the altitude angle (degrees).
  49. /// </summary>
  50. /// <value>The altitude.</value>
  51. public double Altitude
  52. {
  53. get
  54. {
  55. return (double)this.GetValue(AltitudeProperty);
  56. }
  57. set
  58. {
  59. this.SetValue(AltitudeProperty, value);
  60. }
  61. }
  62. /// <summary>
  63. /// Gets or sets the ambient lightness.
  64. /// </summary>
  65. /// <value>The ambient.</value>
  66. public double Ambient
  67. {
  68. get
  69. {
  70. return (double)this.GetValue(AmbientProperty);
  71. }
  72. set
  73. {
  74. this.SetValue(AmbientProperty, value);
  75. }
  76. }
  77. /// <summary>
  78. /// Gets or sets the azimuth angle (degrees).
  79. /// </summary>
  80. /// <value>The azimuth.</value>
  81. public double Azimuth
  82. {
  83. get
  84. {
  85. return (double)this.GetValue(AzimuthProperty);
  86. }
  87. set
  88. {
  89. this.SetValue(AzimuthProperty, value);
  90. }
  91. }
  92. /// <summary>
  93. /// Gets or sets the brightness.
  94. /// </summary>
  95. /// <value>The brightness.</value>
  96. public double Brightness
  97. {
  98. get
  99. {
  100. return (double)this.GetValue(BrightnessProperty);
  101. }
  102. set
  103. {
  104. this.SetValue(BrightnessProperty, value);
  105. }
  106. }
  107. #endregion
  108. #region Methods
  109. /// <summary>
  110. /// Adds the lights to the element.
  111. /// </summary>
  112. /// <param name="lightGroup">
  113. /// The light group.
  114. /// </param>
  115. protected override void AddLights(Model3DGroup lightGroup)
  116. {
  117. var t1 = new RotateTransform3D(new AxisAngleRotation3D(this.AzimuthAxis, this.Azimuth));
  118. var t2 = new RotateTransform3D(new AxisAngleRotation3D(this.AltitudeAxis, this.Altitude));
  119. var dir = t1.Transform(t2.Transform(new Vector3D(1, 0, 0)));
  120. var i = (byte)(255 * this.Brightness);
  121. lightGroup.Children.Add(new DirectionalLight(Color.FromRgb(i, i, i), dir));
  122. var ai = (byte)(255 * this.Ambient);
  123. lightGroup.Children.Add(new AmbientLight(Color.FromRgb(ai, ai, ai)));
  124. }
  125. #endregion
  126. }
  127. }