PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Utilities/Graphics/ShaderFeatures/FogShaderFeature.cs

#
C# | 160 lines | 84 code | 12 blank | 64 comment | 0 complexity | cebc8c1a764ffb61bdf60603567e27a0 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using Delta.Utilities.Datatypes;
  4. namespace Delta.Utilities.Graphics.ShaderFeatures
  5. {
  6. /// <summary>
  7. /// Fog shader feature
  8. ///Note: add exponential and exponential^2 flags for better fog, but that
  9. /// needs more shader testing and finetuning.
  10. /// </summary>
  11. public class FogShaderFeature : IShaderFeature
  12. {
  13. #region FogColor (Public)
  14. /// <summary>
  15. /// The ShaderParameters array should contain: FogColor, FogStart and
  16. /// FogEnd. This does not mean those variables are used as shader uniforms,
  17. /// they are usually constant and can be used for both fixed function
  18. /// pipeline code and for shader code. Adds 4-5 vertex shader instructions
  19. /// and 2 pixel shader instructions (lerp is costly, but required).
  20. /// Note: Currently only the FogColor is used in the shader, the rest
  21. /// is baked as constants into it (faster because we don't need to
  22. /// calculate FogEnd-FogStart and it makes more sense to have the same
  23. /// values for all shaders anyway).
  24. /// </summary>
  25. public Color FogColor = new Color(0.6f, 0.3f, 0.3f);
  26. #endregion
  27. #region FogStart (Public)
  28. /// <summary>
  29. /// The default fog start distance is 10m, everything closer will also
  30. /// get a little of the FogColor, see MinimumFogValue.
  31. /// </summary>
  32. public float FogStart = 10.0f;
  33. #endregion
  34. #region FogEnd (Public)
  35. /// <summary>
  36. /// Maximum distance for the fog, here the fog is at full intensity (see
  37. /// MaximumFogValue). Should be synced up with BaseCamera.FarPlane, which
  38. /// is currently 75.0 for the SoulCraft Tech Demo.
  39. /// </summary>
  40. public float FogEnd = 75.0f;
  41. #endregion
  42. #region MinimumFogValue (Public)
  43. /// <summary>
  44. /// Minimum fog value that gets mixed in for each pixel in the pixel
  45. /// shader. This value is currently pretty high (0.15 like the default
  46. /// ambient color) to color all pixels a little in the fog color to
  47. /// colorize everything in the scene with this fog color to fit together
  48. /// better.
  49. /// </summary>
  50. public float MinimumFogValue = 0.15f;
  51. #endregion
  52. #region MaximumFogValue (Public)
  53. /// <summary>
  54. /// Maximum fog value, does not have to be 1.0, the default value for
  55. /// this is 0.9, which means fog is never at 100%. There will always be
  56. /// a little bit of the original pixel shader color visible.
  57. /// </summary>
  58. public float MaximumFogValue = 0.9f;
  59. #endregion
  60. #region ShaderFeature (Public)
  61. /// <summary>
  62. /// Which shader feature flag is this ShaderFeature class using?
  63. /// Important for loading this file via ShaderData.LoadShaderFeature.
  64. /// Each flag should obviously only have one class for it!
  65. /// </summary>
  66. public ShaderFeatureFlags ShaderFeature
  67. {
  68. get
  69. {
  70. return ShaderFeatureFlags.Fog;
  71. }
  72. }
  73. #endregion
  74. #region RequiredShaderFeatures (Public)
  75. /// <summary>
  76. /// Which shader feature flags are required for this ShaderFeature.
  77. /// Very important for creating of a new ShaderFeature.
  78. /// </summary>
  79. public ShaderFeatureFlags RequiredShaderFeatures
  80. {
  81. get
  82. {
  83. // Note: We don't need anything for fog, this can be combined with
  84. // all shaders. It will only make sense for 3D rendering however.
  85. return ShaderFeatureFlags.Basic;
  86. }
  87. }
  88. #endregion
  89. #region Parameters (Public)
  90. /// <summary>
  91. /// Parameters as a dictionary, will be generated on the fly when
  92. /// requested.
  93. /// </summary>
  94. public Dictionary<string, object> Parameters
  95. {
  96. get
  97. {
  98. Dictionary<string, object> ret = new Dictionary<string, object>();
  99. ret.Add("FogColor", FogColor);
  100. ret.Add("FogStart", FogStart);
  101. ret.Add("FogEnd", FogEnd);
  102. ret.Add("MinimumFogValue", MinimumFogValue);
  103. ret.Add("MaximumFogValue", MaximumFogValue);
  104. return ret;
  105. }
  106. }
  107. #endregion
  108. #region Constructors
  109. /// <summary>
  110. /// Create fog shader feature
  111. /// </summary>
  112. public FogShaderFeature()
  113. {
  114. }
  115. /// <summary>
  116. /// Create fog shader feature
  117. /// </summary>
  118. public FogShaderFeature(BinaryReader reader)
  119. {
  120. Load(reader);
  121. }
  122. #endregion
  123. #region ISaveLoadBinary Members
  124. /// <summary>
  125. /// Load this ShaderFeature from a binary data stream.
  126. /// </summary>
  127. public void Load(BinaryReader reader)
  128. {
  129. FogColor.Load(reader);
  130. FogStart = reader.ReadSingle();
  131. FogEnd = reader.ReadSingle();
  132. MinimumFogValue = reader.ReadSingle();
  133. MaximumFogValue = reader.ReadSingle();
  134. }
  135. /// <summary>
  136. /// Save this ShaderFeature to a binary data stream.
  137. /// </summary>
  138. public void Save(BinaryWriter writer)
  139. {
  140. FogColor.Save(writer);
  141. writer.Write(FogStart);
  142. writer.Write(FogEnd);
  143. writer.Write(MinimumFogValue);
  144. writer.Write(MaximumFogValue);
  145. }
  146. #endregion
  147. }
  148. }