BatchItem.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Numerics;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Veldrid.Common
  9. {
  10. public struct BatchItem
  11. {
  12. public BatchItem(Vector2 textureSize, RectangleF destinationRectangle, RectangleF sourceRectangle, Color color,
  13. float rotation, Vector2 origin, float layerDepth, RectangleF scissor, SpriteOptions options)
  14. {
  15. var sourceSize = new Vector2(sourceRectangle.Width, sourceRectangle.Height) / textureSize;
  16. var pos = new Vector2(sourceRectangle.X, sourceRectangle.Y) / textureSize;
  17. UV = CreateFlip(options) * Matrix4x4.CreateScale(new Vector3(sourceSize, 1)) * Matrix4x4.CreateTranslation(new Vector3(pos, 0));
  18. Color = ToVector(color);
  19. Model =
  20. Matrix4x4.CreateScale(new Vector3(destinationRectangle.Width, destinationRectangle.Height, 0)) *
  21. Matrix4x4.CreateTranslation(new Vector3(-origin, 0)) *
  22. Matrix4x4.CreateRotationZ(rotation) *
  23. Matrix4x4.CreateTranslation(new Vector3(destinationRectangle.X, destinationRectangle.Y, layerDepth));
  24. Projection = Matrix4x4.Identity;
  25. Scissor = scissor;
  26. }
  27. public BatchItem(Vector2 textureSize, PointF position, RectangleF sourceRectangle, Color color, float rotation,
  28. Vector2 origin, Vector2 scale, float layerDepth, RectangleF scissor, SpriteOptions options)
  29. : this(textureSize, new RectangleF(position.X, position.Y, sourceRectangle.Width * scale.X, sourceRectangle.Height * scale.Y),
  30. sourceRectangle,
  31. color,
  32. rotation,
  33. origin,
  34. layerDepth,
  35. scissor,
  36. options)
  37. {
  38. }
  39. public Matrix4x4 UV { get; set; }
  40. public Vector4 Color { get; set; }
  41. public Matrix4x4 Model { get; set; }
  42. public Matrix4x4 Projection { get; set; }
  43. public RectangleF Scissor { get; set; }
  44. private static Vector4 ToVector(Color color) => new Vector4(color.R, color.G, color.B, color.A);
  45. private static Matrix4x4 CreateFlip(SpriteOptions options)
  46. {
  47. if (options == SpriteOptions.None)
  48. return Matrix4x4.Identity;
  49. var flipX = options.HasFlag(SpriteOptions.FlipHorizontally);
  50. var flipY = options.HasFlag(SpriteOptions.FlipVertically);
  51. return Matrix4x4.CreateScale(flipX ? -1 : 1, flipY ? -1 : 1, 1) * Matrix4x4.CreateTranslation(flipX ? 1 : 0, flipY ? 1 : 0, 0);
  52. }
  53. }
  54. }