PageRenderTime 123ms CodeModel.GetById 50ms app.highlight 32ms RepoModel.GetById 38ms app.codeStats 0ms

/Rendering/Basics/Materials/Material2D.cs

#
C# | 198 lines | 88 code | 14 blank | 96 comment | 8 complexity | 63010180fff0c0cccbf37fedcd5afcdf MD5 | raw file
  1using Delta.ContentSystem;
  2using Delta.Graphics;
  3using Delta.Graphics.Basics;
  4using Delta.Rendering.Basics.Drawing;
  5using Delta.Rendering.Enums;
  6using Delta.Utilities.Datatypes;
  7using Delta.Utilities.Graphics.ShaderFeatures;
  8
  9namespace Delta.Rendering.Basics.Materials
 10{
 11	/// <summary>
 12	/// Material 2D without vertex coloring, but otherwise has the same features
 13	/// as Material2DColored. Rendering this is a bit faster because we don't
 14	/// need to send vertex colors to the GPU (saves bandwidth and performance).
 15	/// </summary>
 16	public class Material2D : BaseMaterial
 17	{
 18		#region Default (Static)
 19		/// <summary>
 20		/// The default material which is always used if no material is explictly
 21		/// set. Will be created the first time this is used, which is quite
 22		/// likely for unit test code and if some material is missing or wrong,
 23		/// but not so much true for the real games later. Also delayed loading
 24		/// is much better for the application initialization time.
 25		/// </summary>
 26		public static Material2D Default
 27		{
 28			get
 29			{
 30				if (defaultMaterial2D == null)
 31				{
 32					defaultMaterial2D = new Material2D(Content.EmptyName);
 33				}
 34
 35				return defaultMaterial2D;
 36			} // get
 37		}
 38		#endregion
 39
 40		#region Private
 41
 42		#region defaultMaterial2D (Private)
 43		/// <summary>
 44		/// Default material 2D
 45		/// </summary>
 46		private static Material2D defaultMaterial2D;
 47		#endregion
 48
 49		#endregion
 50
 51		#region Constructors
 52		/// <summary>
 53		/// Create material from just a diffuse map image name. The
 54		/// material will always just use the basic shader without vertex coloring.
 55		/// </summary>
 56		/// <param name="setDiffuseMapName">Set diffuse map name</param>
 57		public Material2D(string setDiffuseMapName)
 58			: base(setDiffuseMapName,
 59				Shader.Create(ShaderFeatureFlags.Basic | ShaderFeatureFlags.UI2D))
 60		{
 61			// Note: The shader is always 2D, no need to check: shader.Is2D
 62		}
 63
 64		/// <summary>
 65		/// Create material from image, currently only needed for displaying
 66		/// RenderToTexture and a video frame.
 67		/// </summary>
 68		/// <param name="setTexture">The diffuse texture to assign.</param>
 69		public Material2D(BaseTexture setTexture)
 70			: base(setTexture,
 71				Shader.Create(ShaderFeatureFlags.Basic | ShaderFeatureFlags.UI2D))
 72		{
 73			// Note: The shader is always 2D, no need to check: shader.Is2D
 74		}
 75		#endregion
 76
 77		#region Draw (Public)
 78		/// <summary>
 79		/// Draw this 2D material at the specified rectangle location.
 80		/// Note: This is just rendering in 2D, 3D rendering is not used, thus
 81		/// 3D camera movement will not affect this material draw. Use Meshes and
 82		/// <see cref="BillboardManager"/> to accomplish 3D rendering of surfaces.
 83		/// </summary>
 84		/// <param name="drawArea">
 85		/// Draw area to render to, rendering will be skipped if this is
 86		/// completely outside of the 0-1 quadratic screen space.
 87		/// </param>
 88		public void Draw(Rectangle drawArea)
 89		{
 90			// Skip if the drawArea is certainly outside of the quadratic space.
 91			// Checking the screen area would work too, but is a little slower and
 92			// won't exclude much more anyway.
 93			if (drawArea.Left >= 1.0f ||
 94			    drawArea.Top >= 1.0f ||
 95			    drawArea.Left < -drawArea.Width ||
 96			    drawArea.Top < -drawArea.Height)
 97			{
 98				// Skip this material rendering, not visible this frame.
 99				return;
100			}
101
102			cachedLayer.Add(this, drawArea);
103		}
104
105		/// <summary>
106		/// Draw this 2D material at the specified rectangle location.
107		/// Note: This is just rendering in 2D, 3D rendering is not used, thus
108		/// 3D camera movement will not affect this material draw. Use Meshes and
109		/// <see cref="BillboardManager"/> to accomplish 3D rendering of surfaces.
110		/// </summary>
111		/// <param name="drawArea">
112		/// Draw area to render to, rendering will be skipped if this is
113		/// completely outside of the 0-1 quadratic screen space.
114		/// </param>
115		/// <param name="rotation">
116		/// Rotation for this material drawing in degrees. By default unused=0.
117		/// </param>
118		/// <param name="rotationCenter">
119		/// Center for the rotation. If you want it to be centered around the
120		/// drawArea use drawArea.Center or just use the other Draw overloads.
121		/// Note: This rotationCenter is always absolute, add drawArea.Center to
122		/// make it relative to the local drawArea.
123		/// </param>
124		/// <param name="flipMode">
125		/// Flipping is useful to display materials in different ways. Just
126		/// rotating is often enough, but sometimes flipping is needed (e.g. for
127		/// RenderTargets or to make non-tileable textures pseudo-tilable).
128		/// FlipMode.Vertical and FlipMode.Horizontal can be combined (which is
129		/// FlipMode.VerticalAndHorizontal, the same as rotating 180 degrees).
130		/// </param>
131		public void Draw(Rectangle drawArea, float rotation, Point rotationCenter,
132			FlipMode flipMode = FlipMode.None)
133		{
134			// Skip if the drawArea is certainly outside of the quadratic space.
135			// Checking the screen area would work too, but is a little slower and
136			// won't exclude much more anyway.
137			if (drawArea.Left >= 1.0f ||
138					drawArea.Top >= 1.0f ||
139					drawArea.Left < -drawArea.Width ||
140					drawArea.Top < -drawArea.Height)
141			{
142				// Skip this material rendering, not visible this frame.
143				return;
144			}
145
146			cachedLayer.Add(this, drawArea, rotation, rotationCenter, flipMode);
147		}
148
149		/// <summary>
150		/// Draw this 2D material at the specified rectangle location.
151		/// Note: This is just rendering in 2D, 3D rendering is not used, thus
152		/// 3D camera movement will not affect this material draw. Use Meshes and
153		/// <see cref="BillboardManager"/> to accomplish 3D rendering of surfaces.
154		/// </summary>
155		/// <param name="drawArea">
156		/// Draw area to render to, rendering will be skipped if this is
157		/// completely outside of the 0-1 quadratic screen space.
158		/// </param>
159		/// <param name="rotation">
160		/// Rotation for this material drawing in degrees. By default unused=0.
161		/// </param>
162		/// <param name="flipMode">
163		/// Flipping is useful to display materials in different ways. Just
164		/// rotating is often enough, but sometimes flipping is needed (e.g. for
165		/// RenderTargets or to make non-tileable textures pseudo-tilable).
166		/// FlipMode.Vertical and FlipMode.Horizontal can be combined (which is
167		/// FlipMode.VerticalAndHorizontal, the same as rotating 180 degrees).
168		/// </param>
169		public void Draw(Rectangle drawArea, float rotation,
170			FlipMode flipMode = FlipMode.None)
171		{
172			// Skip if the drawArea is certainly outside of the quadratic space.
173			// Checking the screen area would work too, but is a little slower and
174			// won't exclude much more anyway.
175			if (drawArea.Left >= 1.0f ||
176			    drawArea.Top >= 1.0f ||
177			    drawArea.Left < -drawArea.Width ||
178			    drawArea.Top < -drawArea.Height)
179			{
180				// Skip this material rendering, not visible this frame.
181				return;
182			}
183			
184
185			// No rotation needed? Then render a bit faster.
186			if (rotation == 0.0f &&
187				flipMode == FlipMode.None)
188			{
189				cachedLayer.Add(this, drawArea);
190			}
191			else
192			{
193				cachedLayer.Add(this, drawArea, rotation, drawArea.Center, flipMode);
194			}
195		}
196		#endregion
197	}
198}