PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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