PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Utilities/Graphics/ShaderFeatures/FogShaderFeature.cs

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