/Rendering/Basics/Materials/Material2DColored.cs
C# | 255 lines | 114 code | 16 blank | 125 comment | 8 complexity | a786dbfeab1ebca91b83da5f5a602f2c MD5 | raw file
Possible License(s): Apache-2.0
- using Delta.ContentSystem;
- using Delta.Graphics.Basics;
- using Delta.Rendering.Enums;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Graphics.ShaderFeatures;
-
- namespace Delta.Rendering.Basics.Materials
- {
- /// <summary>
- /// Vertex colored Material 2D, used for particle effects and coloring UI
- /// elements in UI screens. Using this class instead of Material2D is a bit
- /// slower because we need to send vertex colors to the GPU.
- /// </summary>
- public class Material2DColored : MaterialColored
- {
- #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 new static Material2DColored Default
- {
- get
- {
- if (defaultMaterial2DColored == null)
- {
- defaultMaterial2DColored = new Material2DColored(Content.EmptyName);
- }
-
- return defaultMaterial2DColored;
- }
- }
- #endregion
-
- #region Private
-
- #region defaultMaterial2DColored (Private)
- /// <summary>
- /// Default material 2D
- /// </summary>
- private static Material2DColored defaultMaterial2DColored;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create material from just a diffuse map image name and a the default
- /// blend color. The material will always just use the basic shader with
- /// vertex coloring. That is mostly used for UI rendering.
- /// </summary>
- /// <param name="setDiffuseMapName">
- /// The name of the diffuse map to use.
- /// </param>
- public Material2DColored(string setDiffuseMapName)
- : base(setDiffuseMapName, Shader.Create(
- ShaderFeatureFlags.Basic |
- ShaderFeatureFlags.UI2D |
- ShaderFeatureFlags.ColoredVertices))
- {
- // Note: The shader is always 2D, no need to check: shader.Is2D
- }
-
- /// <summary>
- /// Create material from just a diffuse map image name and a given
- /// blend color. The material will always just use the basic shader with
- /// vertex coloring. That is mostly used for UI rendering.
- /// </summary>
- /// <param name="setDiffuseMapName">Diffuse map name.</param>
- /// <param name="setColor">The tint color to apply during draw.</param>
- public Material2DColored(string setDiffuseMapName, Color setColor)
- : base(setDiffuseMapName, Shader.Create(
- ShaderFeatureFlags.Basic |
- ShaderFeatureFlags.UI2D |
- ShaderFeatureFlags.ColoredVertices))
- {
- BlendColor = setColor;
- // Note: The shader is always 2D, no need to check: shader.Is2D
- }
-
- /// <summary>
- /// Create material from just a diffuse map image name and a the default
- /// blend color. The material will always just use the basic shader with
- /// vertex coloring. That is mostly used for UI rendering.
- /// </summary>
- /// <param name="setColor">The color that the material should have.</param>
- public Material2DColored(Color setColor)
- : base(Content.EmptyName, Shader.Create(
- ShaderFeatureFlags.UI2D |
- ShaderFeatureFlags.NoTexturing |
- ShaderFeatureFlags.ColoredVertices))
- {
- BlendColor = setColor;
- // Note: The shader is always 2D, no need to check: shader.Is2D
- }
- #endregion
-
- #region Clone (Public)
- /// <summary>
- /// Returns a new instance of this Material2DColored, which we can change
- /// without modifying the original. Useful for the Default material (see
- /// property above) or for different material settings on the same image.
- /// </summary>
- /// <returns>New Material2DColored with the same settings</returns>
- public Material2DColored Clone()
- {
- return new Material2DColored(BlendColor)
- {
- shader = shader,
- diffuseMap = diffuseMap,
- };
- }
- #endregion
-
- #region Draw (Public)
- /// <summary>
- /// Draw this colored 2D material at the specified rectangle location.
- /// Use the other overloads for more options (at the cost of performance)
- /// like rotation, flipping, overwriting UVs, etc.
- /// </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 colored 2D material at the specified rectangle location.
- /// Use the other overloads for more options (at the cost of performance)
- /// like rotation, flipping, overwriting UVs, etc.
- /// </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);
- }
- }
-
- /// <summary>
- /// Draw this 2D material at the specified rectangle location and
- /// optionally rotating and flipping it.
- /// </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 and custom
- /// UV rectangle and optionally rotating it.
- /// </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="uvAreaOverride">
- /// UV area override to render a custom UV rectangle (you must apply all
- /// atlas UV remapping yourself).
- /// </param>
- /// <param name="rotation">
- /// Rotation for this material drawing in degrees. By default unused=0.
- /// </param>
- public void Draw(Rectangle drawArea, Rectangle uvAreaOverride,
- float rotation = 0.0f)
- {
- cachedLayer.Add(this, drawArea, rotation, BlendColor, uvAreaOverride);
- }
- #endregion
- }
- }