123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using System.Numerics;
- using SixLabors.Fonts;
- using Veldrid;
- namespace TextRender
- {
- public class Text : DisposableManager
- {
- private TextRenderer _renderer;
- public Vector2 Position { get; set; }
- public float Angle { get; set; } = 270;
- public string Content { get; set; }
- public int FontSize { get; set; } = 12;
- public string FontName { get; set; } = "Arial";
- public FontStyle FontStyle { get; set; } = FontStyle.Regular;
- public RgbaFloat Color { get; set; } = RgbaFloat.White;
- public Font Font { get; protected set; }
- private DeviceBuffer vertexBuffer;
- private DeviceBuffer indexBuffer;
- private ResourceSet textureSet;
- private Texture texture;
- private TextureView textureView;
- private TextCache textCache;
- public TextRenderer Renderer => _renderer;
- public Text(TextRenderer renderer, string text)
- {
- Content = text;
- _renderer = renderer;
- }
- public void Initialize()
- {
- var device = _renderer.Device;
- var factory = device.ResourceFactory;
- BufferDescription vbDescription = new BufferDescription(
- 4 * VertexPositionTextureColor.SizeInBytes,
- BufferUsage.VertexBuffer);
- vertexBuffer = AddDisposable(factory.CreateBuffer(vbDescription));
- BufferDescription ibDescription = new BufferDescription(
- 4 * sizeof(ushort),
- BufferUsage.IndexBuffer);
- indexBuffer = AddDisposable(factory.CreateBuffer(ibDescription));
- var Size = new Vector2(100,100);
- var topLeft = new Vector2(0, 0);
- var topRight = new Vector2(0 + Size.X, 0);
- var bottomLeft = new Vector2(0, 0 + Size.Y);
- var bottomRight = new Vector2(0 + Size.X, 0 + Size.Y);
- VertexPositionTextureColor[] quadVertices =
- {
- new VertexPositionTextureColor(topLeft, new Vector2(0f, 0f), RgbaFloat.White),
- new VertexPositionTextureColor(topRight, new Vector2(1f, 0f), RgbaFloat.White),
- new VertexPositionTextureColor(bottomLeft, new Vector2(0f, 1f), RgbaFloat.White),
- new VertexPositionTextureColor(bottomRight, new Vector2(1f, 1f), RgbaFloat.White)
- };
- device.UpdateBuffer(vertexBuffer, 0, quadVertices);
- ushort[] quadIndices = { 0, 1, 2, 3 };
- device.UpdateBuffer(indexBuffer, 0, quadIndices);
- textCache = AddDisposable(new TextCache(device));
- Font = SystemFonts.CreateFont(FontName, FontSize, FontStyle);
- texture = AddDisposable(textCache.GetTextTexture(Content, Font, Color, Size));
- textureView = AddDisposable(device.ResourceFactory.CreateTextureView(texture));
- textureSet = AddDisposable(device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(
- _renderer.Shader.TextureLayout,
- textureView,
- device.Aniso4xSampler)));
- }
- public void Recreate(Vector2 size)
- {
- Size = size;
- var device = _renderer.Device;
- if (texture != null)
- {
- RemoveAndDispose(ref textureView);
- RemoveAndDispose(ref texture);
- RemoveAndDispose(ref textCache);
- }
- var topLeft = new Vector2(0, 0);
- var topRight = new Vector2(0 + Size.X, 0);
- var bottomLeft = new Vector2(0, 0 + Size.Y);
- var bottomRight = new Vector2(0 + Size.X, 0 + Size.Y);
- VertexPositionTextureColor[] quadVertices =
- {
- new VertexPositionTextureColor(topLeft, new Vector2(0f, 0f), RgbaFloat.White),
- new VertexPositionTextureColor(topRight, new Vector2(1f, 0f), RgbaFloat.White),
- new VertexPositionTextureColor(bottomLeft, new Vector2(0f, 1f), RgbaFloat.White),
- new VertexPositionTextureColor(bottomRight, new Vector2(1f, 1f), RgbaFloat.White)
- };
- device.UpdateBuffer(vertexBuffer, 0, quadVertices);
- textCache = AddDisposable(new TextCache(device));
- Font = SystemFonts.CreateFont(FontName, FontSize, FontStyle);
- texture = AddDisposable(textCache.GetTextTexture(Content, Font, Color, size));
- textureView = AddDisposable(device.ResourceFactory.CreateTextureView(texture));
- textureSet = AddDisposable(device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(
- _renderer.Shader.TextureLayout,
- textureView,
- device.Aniso4xSampler)));
- }
- Vector2 Size = new Vector2(31, 16);
- public void DrawBatched()
- {
- var cl = _renderer.CommandList;
- cl.UpdateBuffer(_renderer.Shader.ProjectionBuffer, 0,
- Matrix4x4.CreateOrthographicOffCenter(
- 0,
- _renderer.Framebuffer.Width,
- _renderer.Framebuffer.Height,
- 0, 0, 1));
- cl.UpdateBuffer(_renderer.Shader.WorldBuffer, 0,Matrix4x4.CreateRotationZ(Angle/180*MathF.PI,new Vector3(Size.X/2,Size.Y/2,0))* Matrix4x4.CreateTranslation(new Vector3(Position, 0)));
- cl.SetVertexBuffer(0, vertexBuffer);
- cl.SetIndexBuffer(indexBuffer, IndexFormat.UInt16);
- cl.SetGraphicsResourceSet(0, _renderer.Shader.ProjViewSet);
- cl.SetGraphicsResourceSet(1, textureSet);
-
- cl.DrawIndexed(
- indexCount: 4,
- instanceCount: 1,
- indexStart: 0,
- vertexOffset: 0,
- instanceStart: 0);
- }
- /// <summary>
- /// Render this text in a single draw call
- /// </summary>
- public void Draw()
- {
- _renderer.BeginDraw();
- DrawBatched();
- _renderer.EndDraw();
- }
-
- }
- }
|