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

/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
  1. using Delta.ContentSystem;
  2. using Delta.Graphics.Basics;
  3. using Delta.Rendering.Enums;
  4. using Delta.Utilities.Datatypes;
  5. using Delta.Utilities.Graphics.ShaderFeatures;
  6. namespace Delta.Rendering.Basics.Materials
  7. {
  8. /// <summary>
  9. /// Vertex colored Material 2D, used for particle effects and coloring UI
  10. /// elements in UI screens. Using this class instead of Material2D is a bit
  11. /// slower because we need to send vertex colors to the GPU.
  12. /// </summary>
  13. public class Material2DColored : MaterialColored
  14. {
  15. #region Default (Static)
  16. /// <summary>
  17. /// The default material which is always used if no material is explictly
  18. /// set. Will be created the first time this is used, which is quite
  19. /// likely for unit test code and if some material is missing or wrong,
  20. /// but not so much true for the real games later. Also delayed loading
  21. /// is much better for the application initialization time.
  22. /// </summary>
  23. public new static Material2DColored Default
  24. {
  25. get
  26. {
  27. if (defaultMaterial2DColored == null)
  28. {
  29. defaultMaterial2DColored = new Material2DColored(Content.EmptyName);
  30. }
  31. return defaultMaterial2DColored;
  32. }
  33. }
  34. #endregion
  35. #region Private
  36. #region defaultMaterial2DColored (Private)
  37. /// <summary>
  38. /// Default material 2D
  39. /// </summary>
  40. private static Material2DColored defaultMaterial2DColored;
  41. #endregion
  42. #endregion
  43. #region Constructors
  44. /// <summary>
  45. /// Create material from just a diffuse map image name and a the default
  46. /// blend color. The material will always just use the basic shader with
  47. /// vertex coloring. That is mostly used for UI rendering.
  48. /// </summary>
  49. /// <param name="setDiffuseMapName">
  50. /// The name of the diffuse map to use.
  51. /// </param>
  52. public Material2DColored(string setDiffuseMapName)
  53. : base(setDiffuseMapName, Shader.Create(
  54. ShaderFeatureFlags.Basic |
  55. ShaderFeatureFlags.UI2D |
  56. ShaderFeatureFlags.ColoredVertices))
  57. {
  58. // Note: The shader is always 2D, no need to check: shader.Is2D
  59. }
  60. /// <summary>
  61. /// Create material from just a diffuse map image name and a given
  62. /// blend color. The material will always just use the basic shader with
  63. /// vertex coloring. That is mostly used for UI rendering.
  64. /// </summary>
  65. /// <param name="setDiffuseMapName">Diffuse map name.</param>
  66. /// <param name="setColor">The tint color to apply during draw.</param>
  67. public Material2DColored(string setDiffuseMapName, Color setColor)
  68. : base(setDiffuseMapName, Shader.Create(
  69. ShaderFeatureFlags.Basic |
  70. ShaderFeatureFlags.UI2D |
  71. ShaderFeatureFlags.ColoredVertices))
  72. {
  73. BlendColor = setColor;
  74. // Note: The shader is always 2D, no need to check: shader.Is2D
  75. }
  76. /// <summary>
  77. /// Create material from just a diffuse map image name and a the default
  78. /// blend color. The material will always just use the basic shader with
  79. /// vertex coloring. That is mostly used for UI rendering.
  80. /// </summary>
  81. /// <param name="setColor">The color that the material should have.</param>
  82. public Material2DColored(Color setColor)
  83. : base(Content.EmptyName, Shader.Create(
  84. ShaderFeatureFlags.UI2D |
  85. ShaderFeatureFlags.NoTexturing |
  86. ShaderFeatureFlags.ColoredVertices))
  87. {
  88. BlendColor = setColor;
  89. // Note: The shader is always 2D, no need to check: shader.Is2D
  90. }
  91. #endregion
  92. #region Clone (Public)
  93. /// <summary>
  94. /// Returns a new instance of this Material2DColored, which we can change
  95. /// without modifying the original. Useful for the Default material (see
  96. /// property above) or for different material settings on the same image.
  97. /// </summary>
  98. /// <returns>New Material2DColored with the same settings</returns>
  99. public Material2DColored Clone()
  100. {
  101. return new Material2DColored(BlendColor)
  102. {
  103. shader = shader,
  104. diffuseMap = diffuseMap,
  105. };
  106. }
  107. #endregion
  108. #region Draw (Public)
  109. /// <summary>
  110. /// Draw this colored 2D material at the specified rectangle location.
  111. /// Use the other overloads for more options (at the cost of performance)
  112. /// like rotation, flipping, overwriting UVs, etc.
  113. /// </summary>
  114. /// <param name="drawArea">
  115. /// Draw area to render to, rendering will be skipped if this is
  116. /// completely outside of the 0-1 quadratic screen space.
  117. /// </param>
  118. public void Draw(Rectangle drawArea)
  119. {
  120. // Skip if the drawArea is certainly outside of the quadratic space.
  121. // Checking the screen area would work too, but is a little slower and
  122. // won't exclude much more anyway.
  123. if (drawArea.Left >= 1.0f ||
  124. drawArea.Top >= 1.0f ||
  125. drawArea.Left < -drawArea.Width ||
  126. drawArea.Top < -drawArea.Height)
  127. {
  128. // Skip this material rendering, not visible this frame.
  129. return;
  130. }
  131. cachedLayer.Add(this, drawArea);
  132. }
  133. /// <summary>
  134. /// Draw this colored 2D material at the specified rectangle location.
  135. /// Use the other overloads for more options (at the cost of performance)
  136. /// like rotation, flipping, overwriting UVs, etc.
  137. /// </summary>
  138. /// <param name="drawArea">
  139. /// Draw area to render to, rendering will be skipped if this is
  140. /// completely outside of the 0-1 quadratic screen space.
  141. /// </param>
  142. /// <param name="rotation">
  143. /// Rotation for this material drawing in degrees. By default unused=0.
  144. /// </param>
  145. /// <param name="flipMode">
  146. /// Flipping is useful to display materials in different ways. Just
  147. /// rotating is often enough, but sometimes flipping is needed (e.g. for
  148. /// RenderTargets or to make non-tileable textures pseudo-tilable).
  149. /// FlipMode.Vertical and FlipMode.Horizontal can be combined (which is
  150. /// FlipMode.VerticalAndHorizontal, the same as rotating 180 degrees).
  151. /// </param>
  152. public void Draw(Rectangle drawArea, float rotation,
  153. FlipMode flipMode = FlipMode.None)
  154. {
  155. // Skip if the drawArea is certainly outside of the quadratic space.
  156. // Checking the screen area would work too, but is a little slower and
  157. // won't exclude much more anyway.
  158. if (drawArea.Left >= 1.0f ||
  159. drawArea.Top >= 1.0f ||
  160. drawArea.Left < -drawArea.Width ||
  161. drawArea.Top < -drawArea.Height)
  162. {
  163. // Skip this material rendering, not visible this frame.
  164. return;
  165. }
  166. // No rotation needed? Then render a bit faster.
  167. if (rotation == 0.0f &&
  168. flipMode == FlipMode.None)
  169. {
  170. cachedLayer.Add(this, drawArea);
  171. }
  172. else
  173. {
  174. cachedLayer.Add(this, drawArea, rotation, drawArea.Center, flipMode);
  175. }
  176. }
  177. /// <summary>
  178. /// Draw this 2D material at the specified rectangle location and
  179. /// optionally rotating and flipping it.
  180. /// </summary>
  181. /// <param name="drawArea">
  182. /// Draw area to render to, rendering will be skipped if this is
  183. /// completely outside of the 0-1 quadratic screen space.
  184. /// </param>
  185. /// <param name="rotation">
  186. /// Rotation for this material drawing in degrees. By default unused=0.
  187. /// </param>
  188. /// <param name="rotationCenter">
  189. /// Center for the rotation. If you want it to be centered around the
  190. /// drawArea use drawArea.Center or just use the other Draw overloads.
  191. /// Note: This rotationCenter is always absolute, add drawArea.Center to
  192. /// make it relative to the local drawArea.
  193. /// </param>
  194. /// <param name="flipMode">
  195. /// Flipping is useful to display materials in different ways. Just
  196. /// rotating is often enough, but sometimes flipping is needed (e.g. for
  197. /// RenderTargets or to make non-tileable textures pseudo-tilable).
  198. /// FlipMode.Vertical and FlipMode.Horizontal can be combined (which is
  199. /// FlipMode.VerticalAndHorizontal, the same as rotating 180 degrees).
  200. /// </param>
  201. public void Draw(Rectangle drawArea, float rotation, Point rotationCenter,
  202. FlipMode flipMode = FlipMode.None)
  203. {
  204. // Skip if the drawArea is certainly outside of the quadratic space.
  205. // Checking the screen area would work too, but is a little slower and
  206. // won't exclude much more anyway.
  207. if (drawArea.Left >= 1.0f ||
  208. drawArea.Top >= 1.0f ||
  209. drawArea.Left < -drawArea.Width ||
  210. drawArea.Top < -drawArea.Height)
  211. {
  212. // Skip this material rendering, not visible this frame.
  213. return;
  214. }
  215. cachedLayer.Add(this, drawArea, rotation, rotationCenter, flipMode);
  216. }
  217. /// <summary>
  218. /// Draw this 2D material at the specified rectangle location and custom
  219. /// UV rectangle and optionally rotating it.
  220. /// </summary>
  221. /// <param name="drawArea">
  222. /// Draw area to render to, rendering will be skipped if this is
  223. /// completely outside of the 0-1 quadratic screen space.
  224. /// </param>
  225. /// <param name="uvAreaOverride">
  226. /// UV area override to render a custom UV rectangle (you must apply all
  227. /// atlas UV remapping yourself).
  228. /// </param>
  229. /// <param name="rotation">
  230. /// Rotation for this material drawing in degrees. By default unused=0.
  231. /// </param>
  232. public void Draw(Rectangle drawArea, Rectangle uvAreaOverride,
  233. float rotation = 0.0f)
  234. {
  235. cachedLayer.Add(this, drawArea, rotation, BlendColor, uvAreaOverride);
  236. }
  237. #endregion
  238. }
  239. }