-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolidColorBrush.cs
92 lines (69 loc) · 3.09 KB
/
SolidColorBrush.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using JetBrains.Annotations;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Drawing.Effects;
namespace MonoGame.Extended.Drawing;
[PublicAPI]
public sealed class SolidColorBrush : Brush
{
public SolidColorBrush(DrawingContext context, Color color)
: this(context, color, BrushProperties.Default)
{
}
public SolidColorBrush(DrawingContext context, Color color, BrushProperties brushProperties)
: base(context, LoadEffect, brushProperties)
{
Color = color;
}
public Color Color { get; }
protected override void RenderInternal(Triangle[] triangles, Effect effect, Matrix3x2? transform)
{
var brushEffect = (SolidColorBrushEffect)effect;
var graphicsDevice = DrawingContext.GraphicsDevice;
var props = BrushProperties;
var projection = DrawingContext.DefaultOrthographicProjection;
var world = transform?.ToMatrix4x4() ?? BrushEffect.DefaultWorld;
brushEffect.SetWorldViewProjection(world, BrushEffect.DefaultView, projection);
brushEffect.Opacity = props.Opacity;
brushEffect.Color = Color.ToVector4();
graphicsDevice.RasterizerState = DefaultBrushRasterizerState;
graphicsDevice.DepthStencilState = DefaultBrushDepthStencilState;
brushEffect.Apply();
var vertices = new Vector2[triangles.Length * 3];
var indices = new uint[triangles.Length * 3];
for (var i = 0; i < triangles.Length; ++i)
{
var s = i * 3;
vertices[s] = Matrix3x2.Transform(props.Transform, triangles[i].Point1);
vertices[s + 1] = Matrix3x2.Transform(props.Transform, triangles[i].Point2);
vertices[s + 2] = Matrix3x2.Transform(props.Transform, triangles[i].Point3);
}
for (var i = 0; i < indices.Length; ++i)
{
indices[i] = (uint)i;
}
using (var vertexBuffer = new VertexBuffer(graphicsDevice, new VertexDeclaration(VertexElements), vertices.Length, BufferUsage.WriteOnly))
{
using (var indexBuffer = new IndexBuffer(graphicsDevice, IndexElementSize.ThirtyTwoBits, indices.Length, BufferUsage.WriteOnly))
{
vertexBuffer.SetData(vertices);
indexBuffer.SetData(indices);
graphicsDevice.SetVertexBuffer(vertexBuffer);
graphicsDevice.Indices = indexBuffer;
graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, triangles.Length);
graphicsDevice.SetVertexBuffer(null);
graphicsDevice.Indices = null;
}
}
}
private static EffectLoadingResult LoadEffect(DrawingContext drawingContext)
{
_solidColorBrushEffect ??= SolidColorBrushEffect.Create(drawingContext);
return new EffectLoadingResult(_solidColorBrushEffect, true);
}
private static readonly VertexElement[] VertexElements =
{
new VertexElement(0, VertexElementFormat.Vector2, VertexElementUsage.Position, 0)
};
private static Effect? _solidColorBrushEffect;
}