GeometryBuilder2D.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. namespace MonoGame.Extended.Graphics.Geometry
  6. {
  7. [Obsolete("GeometryBuilder2D is part of an incomplete implementation that was never finished. It will be removed in a future major release.")]
  8. public class GeometryBuilder2D : GeometryBuilder<VertexPositionColorTexture, ushort>
  9. {
  10. public GeometryBuilder2D(int maximumVerticesCount, int maximumIndicesCount)
  11. : base(maximumVerticesCount, maximumIndicesCount)
  12. {
  13. }
  14. public void BuildSprite(int indexOffset, ref Matrix3x2 transformMatrix, Texture2D texture,
  15. ref Rectangle sourceRectangle,
  16. Color? color = null, FlipFlags flags = FlipFlags.None, float depth = 0)
  17. {
  18. if (texture == null)
  19. throw new ArgumentNullException(nameof(texture));
  20. var texelLeft = 0f;
  21. var texelTop = 0f;
  22. var texelRight = 1f;
  23. var texelBottom = 1f;
  24. if (sourceRectangle.Width > 0)
  25. {
  26. texelLeft = (float)sourceRectangle.X / texture.Width;
  27. texelTop = (float)sourceRectangle.Y / texture.Height;
  28. texelRight = (sourceRectangle.X + sourceRectangle.Width) / (float)texture.Width;
  29. texelBottom = (sourceRectangle.Y + sourceRectangle.Height) / (float)texture.Height;
  30. }
  31. else
  32. {
  33. sourceRectangle.Width = texture.Width;
  34. sourceRectangle.Height = texture.Height;
  35. }
  36. var color1 = color ?? Color.White;
  37. var vertices = Vertices;
  38. transformMatrix.Transform(0, 0, ref vertices[0].Position);
  39. vertices[0].Position.Z = depth;
  40. vertices[0].Color = color1;
  41. vertices[0].TextureCoordinate.X = texelLeft;
  42. vertices[0].TextureCoordinate.Y = texelTop;
  43. transformMatrix.Transform(sourceRectangle.Width, 0, ref vertices[1].Position);
  44. vertices[1].Position.Z = depth;
  45. vertices[1].Color = color1;
  46. vertices[1].TextureCoordinate.X = texelRight;
  47. vertices[1].TextureCoordinate.Y = texelTop;
  48. transformMatrix.Transform(0, sourceRectangle.Height, ref vertices[2].Position);
  49. vertices[2].Position.Z = depth;
  50. vertices[2].Color = color1;
  51. vertices[2].TextureCoordinate.X = texelLeft;
  52. vertices[2].TextureCoordinate.Y = texelBottom;
  53. transformMatrix.Transform(sourceRectangle.Width, sourceRectangle.Height, ref vertices[3].Position);
  54. vertices[3].Position.Z = depth;
  55. vertices[3].Color = color1;
  56. vertices[3].TextureCoordinate.X = texelRight;
  57. vertices[3].TextureCoordinate.Y = texelBottom;
  58. var flipDiagonally = (flags & FlipFlags.FlipDiagonally) != 0;
  59. var flipHorizontally = (flags & FlipFlags.FlipHorizontally) != 0;
  60. var flipVertically = (flags & FlipFlags.FlipVertically) != 0;
  61. if (flipDiagonally)
  62. {
  63. FloatHelper.Swap(ref vertices[1].TextureCoordinate.X, ref vertices[2].TextureCoordinate.X);
  64. FloatHelper.Swap(ref vertices[1].TextureCoordinate.Y, ref vertices[2].TextureCoordinate.Y);
  65. }
  66. if (flipHorizontally)
  67. if (flipDiagonally)
  68. {
  69. FloatHelper.Swap(ref vertices[0].TextureCoordinate.Y, ref vertices[1].TextureCoordinate.Y);
  70. FloatHelper.Swap(ref vertices[2].TextureCoordinate.Y, ref vertices[3].TextureCoordinate.Y);
  71. }
  72. else
  73. {
  74. FloatHelper.Swap(ref vertices[0].TextureCoordinate.X, ref vertices[1].TextureCoordinate.X);
  75. FloatHelper.Swap(ref vertices[2].TextureCoordinate.X, ref vertices[3].TextureCoordinate.X);
  76. }
  77. if (flipVertically)
  78. if (flipDiagonally)
  79. {
  80. FloatHelper.Swap(ref vertices[0].TextureCoordinate.X, ref vertices[2].TextureCoordinate.X);
  81. FloatHelper.Swap(ref vertices[1].TextureCoordinate.X, ref vertices[3].TextureCoordinate.X);
  82. }
  83. else
  84. {
  85. FloatHelper.Swap(ref vertices[0].TextureCoordinate.Y, ref vertices[2].TextureCoordinate.Y);
  86. FloatHelper.Swap(ref vertices[1].TextureCoordinate.Y, ref vertices[3].TextureCoordinate.Y);
  87. }
  88. VertexCount = 4;
  89. AddQuadrilateralIndices(indexOffset);
  90. IndexCount = 6;
  91. PrimitivesCount = 2;
  92. }
  93. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  94. private void AddQuadrilateralIndices(int indexOffset)
  95. {
  96. Indices[0] = (ushort)(0 + indexOffset);
  97. Indices[1] = (ushort)(1 + indexOffset);
  98. Indices[2] = (ushort)(2 + indexOffset);
  99. Indices[3] = (ushort)(1 + indexOffset);
  100. Indices[4] = (ushort)(3 + indexOffset);
  101. Indices[5] = (ushort)(2 + indexOffset);
  102. }
  103. }
  104. }