/Rendering/Basics/Materials/Material2D.cs
C# | 198 lines | 88 code | 14 blank | 96 comment | 8 complexity | 63010180fff0c0cccbf37fedcd5afcdf MD5 | raw file
Possible License(s): Apache-2.0
- using Delta.ContentSystem;
- using Delta.Graphics;
- using Delta.Graphics.Basics;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Rendering.Enums;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Graphics.ShaderFeatures;
-
- namespace Delta.Rendering.Basics.Materials
- {
- /// <summary>
- /// Material 2D without vertex coloring, but otherwise has the same features
- /// as Material2DColored. Rendering this is a bit faster because we don't
- /// need to send vertex colors to the GPU (saves bandwidth and performance).
- /// </summary>
- public class Material2D : BaseMaterial
- {
- #region Default (Static)
- /// <summary>
- /// The default material which is always used if no material is explictly
- /// set. Will be created the first time this is used, which is quite
- /// likely for unit test code and if some material is missing or wrong,
- /// but not so much true for the real games later. Also delayed loading
- /// is much better for the application initialization time.
- /// </summary>
- public static Material2D Default
- {
- get
- {
- if (defaultMaterial2D == null)
- {
- defaultMaterial2D = new Material2D(Content.EmptyName);
- }
-
- return defaultMaterial2D;
- } // get
- }
- #endregion
-
- #region Private
-
- #region defaultMaterial2D (Private)
- /// <summary>
- /// Default material 2D
- /// </summary>
- private static Material2D defaultMaterial2D;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create material from just a diffuse map image name. The
- /// material will always just use the basic shader without vertex coloring.
- /// </summary>
- /// <param name="setDiffuseMapName">Set diffuse map name</param>
- public Material2D(string setDiffuseMapName)
- : base(setDiffuseMapName,
- Shader.Create(ShaderFeatureFlags.Basic | ShaderFeatureFlags.UI2D))
- {
- // Note: The shader is always 2D, no need to check: shader.Is2D
- }
-
- /// <summary>
- /// Create material from image, currently only needed for displaying
- /// RenderToTexture and a video frame.
- /// </summary>
- /// <param name="setTexture">The diffuse texture to assign.</param>
- public Material2D(BaseTexture setTexture)
- : base(setTexture,
- Shader.Create(ShaderFeatureFlags.Basic | ShaderFeatureFlags.UI2D))
- {
- // Note: The shader is always 2D, no need to check: shader.Is2D
- }
- #endregion
-
- #region Draw (Public)
- /// <summary>
- /// Draw this 2D material at the specified rectangle location.
- /// Note: This is just rendering in 2D, 3D rendering is not used, thus
- /// 3D camera movement will not affect this material draw. Use Meshes and
- /// <see cref="BillboardManager"/> to accomplish 3D rendering of surfaces.
- /// </summary>
- /// <param name="drawArea">
- /// Draw area to render to, rendering will be skipped if this is
- /// completely outside of the 0-1 quadratic screen space.
- /// </param>
- public void Draw(Rectangle drawArea)
- {
- // Skip if the drawArea is certainly outside of the quadratic space.
- // Checking the screen area would work too, but is a little slower and
- // won't exclude much more anyway.
- if (drawArea.Left >= 1.0f ||
- drawArea.Top >= 1.0f ||
- drawArea.Left < -drawArea.Width ||
- drawArea.Top < -drawArea.Height)
- {
- // Skip this material rendering, not visible this frame.
- return;
- }
-
- cachedLayer.Add(this, drawArea);
- }
-
- /// <summary>
- /// Draw this 2D material at the specified rectangle location.
- /// Note: This is just rendering in 2D, 3D rendering is not used, thus
- /// 3D camera movement will not affect this material draw. Use Meshes and
- /// <see cref="BillboardManager"/> to accomplish 3D rendering of surfaces.
- /// </summary>
- /// <param name="drawArea">
- /// Draw area to render to, rendering will be skipped if this is
- /// completely outside of the 0-1 quadratic screen space.
- /// </param>
- /// <param name="rotation">
- /// Rotation for this material drawing in degrees. By default unused=0.
- /// </param>
- /// <param name="rotationCenter">
- /// Center for the rotation. If you want it to be centered around the
- /// drawArea use drawArea.Center or just use the other Draw overloads.
- /// Note: This rotationCenter is always absolute, add drawArea.Center to
- /// make it relative to the local drawArea.
- /// </param>
- /// <param name="flipMode">
- /// Flipping is useful to display materials in different ways. Just
- /// rotating is often enough, but sometimes flipping is needed (e.g. for
- /// RenderTargets or to make non-tileable textures pseudo-tilable).
- /// FlipMode.Vertical and FlipMode.Horizontal can be combined (which is
- /// FlipMode.VerticalAndHorizontal, the same as rotating 180 degrees).
- /// </param>
- public void Draw(Rectangle drawArea, float rotation, Point rotationCenter,
- FlipMode flipMode = FlipMode.None)
- {
- // Skip if the drawArea is certainly outside of the quadratic space.
- // Checking the screen area would work too, but is a little slower and
- // won't exclude much more anyway.
- if (drawArea.Left >= 1.0f ||
- drawArea.Top >= 1.0f ||
- drawArea.Left < -drawArea.Width ||
- drawArea.Top < -drawArea.Height)
- {
- // Skip this material rendering, not visible this frame.
- return;
- }
-
- cachedLayer.Add(this, drawArea, rotation, rotationCenter, flipMode);
- }
-
- /// <summary>
- /// Draw this 2D material at the specified rectangle location.
- /// Note: This is just rendering in 2D, 3D rendering is not used, thus
- /// 3D camera movement will not affect this material draw. Use Meshes and
- /// <see cref="BillboardManager"/> to accomplish 3D rendering of surfaces.
- /// </summary>
- /// <param name="drawArea">
- /// Draw area to render to, rendering will be skipped if this is
- /// completely outside of the 0-1 quadratic screen space.
- /// </param>
- /// <param name="rotation">
- /// Rotation for this material drawing in degrees. By default unused=0.
- /// </param>
- /// <param name="flipMode">
- /// Flipping is useful to display materials in different ways. Just
- /// rotating is often enough, but sometimes flipping is needed (e.g. for
- /// RenderTargets or to make non-tileable textures pseudo-tilable).
- /// FlipMode.Vertical and FlipMode.Horizontal can be combined (which is
- /// FlipMode.VerticalAndHorizontal, the same as rotating 180 degrees).
- /// </param>
- public void Draw(Rectangle drawArea, float rotation,
- FlipMode flipMode = FlipMode.None)
- {
- // Skip if the drawArea is certainly outside of the quadratic space.
- // Checking the screen area would work too, but is a little slower and
- // won't exclude much more anyway.
- if (drawArea.Left >= 1.0f ||
- drawArea.Top >= 1.0f ||
- drawArea.Left < -drawArea.Width ||
- drawArea.Top < -drawArea.Height)
- {
- // Skip this material rendering, not visible this frame.
- return;
- }
-
-
- // No rotation needed? Then render a bit faster.
- if (rotation == 0.0f &&
- flipMode == FlipMode.None)
- {
- cachedLayer.Add(this, drawArea);
- }
- else
- {
- cachedLayer.Add(this, drawArea, rotation, drawArea.Center, flipMode);
- }
- }
- #endregion
- }
- }