Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
BasePrimitivesRenderer.cs
浏览该文件的文档.
1using Engine.Media;
2
3namespace Engine.Graphics {
4 public class BasePrimitivesRenderer<T1, T2, T3> where T1 : BaseFlatBatch, new()
5 where T2 : BaseTexturedBatch, new()
6 where T3 : BaseFontBatch, new() {
7 public bool m_sortNeeded;
8
9 public List<BaseBatch> m_allBatches = [];
10
11 public LinkedList<T1> m_flatBatches = new();
12
13 public LinkedList<T2> m_texturedBatches = new();
14
15 public LinkedList<T3> m_fontBatches = new();
16
17 public IEnumerable<T1> FlatBatches => m_flatBatches;
18
19 public IEnumerable<T2> TexturedBatches => m_texturedBatches;
20
21 public IEnumerable<T3> FontBatches => m_fontBatches;
22
23 public T1 FindFlatBatch(int layer, DepthStencilState depthStencilState, RasterizerState rasterizerState, BlendState blendState) {
24 for (LinkedListNode<T1> linkedListNode = m_flatBatches.First; linkedListNode != null; linkedListNode = linkedListNode.Next) {
25 T1 value = linkedListNode.Value;
26 if (layer == value.Layer
27 && depthStencilState == value.DepthStencilState
28 && rasterizerState == value.RasterizerState
29 && blendState == value.BlendState) {
30 if (linkedListNode.Previous != null) {
31 m_flatBatches.Remove(linkedListNode);
32 m_flatBatches.AddFirst(linkedListNode);
33 }
34 return value;
35 }
36 }
37 m_sortNeeded |= m_allBatches.Count > 0 && m_allBatches[^1].Layer > layer;
38 T1 val = new() { Layer = layer, DepthStencilState = depthStencilState, RasterizerState = rasterizerState, BlendState = blendState };
39 m_flatBatches.AddFirst(val);
40 m_allBatches.Add(val);
41 return val;
42 }
43
44 public T2 FindTexturedBatch(Texture2D texture,
45 bool useAlphaTest,
46 int layer,
47 DepthStencilState depthStencilState,
48 RasterizerState rasterizerState,
49 BlendState blendState,
50 SamplerState samplerState) {
51 ArgumentNullException.ThrowIfNull(texture);
52 for (LinkedListNode<T2> linkedListNode = m_texturedBatches.First; linkedListNode != null; linkedListNode = linkedListNode.Next) {
53 T2 value = linkedListNode.Value;
54 if (texture == value.Texture
55 && useAlphaTest == value.UseAlphaTest
56 && layer == value.Layer
57 && depthStencilState == value.DepthStencilState
58 && rasterizerState == value.RasterizerState
59 && blendState == value.BlendState
60 && samplerState == value.SamplerState) {
61 if (linkedListNode.Previous != null) {
62 m_texturedBatches.Remove(linkedListNode);
63 m_texturedBatches.AddFirst(linkedListNode);
64 }
65 return value;
66 }
67 }
68 m_sortNeeded |= m_allBatches.Count > 0 && m_allBatches[^1].Layer > layer;
69 T2 val = new() {
70 Layer = layer,
71 UseAlphaTest = useAlphaTest,
72 Texture = texture,
73 SamplerState = samplerState,
74 DepthStencilState = depthStencilState,
75 RasterizerState = rasterizerState,
76 BlendState = blendState
77 };
78 m_texturedBatches.AddFirst(val);
79 m_allBatches.Add(val);
80 return val;
81 }
82
83 public T3 FindFontBatch(BitmapFont font,
84 int layer,
85 DepthStencilState depthStencilState,
86 RasterizerState rasterizerState,
87 BlendState blendState,
88 SamplerState samplerState) {
89 ArgumentNullException.ThrowIfNull(font);
90 for (LinkedListNode<T3> linkedListNode = m_fontBatches.First; linkedListNode != null; linkedListNode = linkedListNode.Next) {
91 T3 value = linkedListNode.Value;
92 if (font == value.Font
93 && layer == value.Layer
94 && depthStencilState == value.DepthStencilState
95 && rasterizerState == value.RasterizerState
96 && blendState == value.BlendState
97 && samplerState == value.SamplerState) {
98 if (linkedListNode.Previous != null) {
99 m_fontBatches.Remove(linkedListNode);
100 m_fontBatches.AddFirst(linkedListNode);
101 }
102 return value;
103 }
104 }
105 m_sortNeeded |= m_allBatches.Count > 0 && m_allBatches[^1].Layer > layer;
106 T3 val = new() {
107 Layer = layer,
108 Font = font,
109 SamplerState = samplerState,
110 DepthStencilState = depthStencilState,
111 RasterizerState = rasterizerState,
112 BlendState = blendState
113 };
114 m_fontBatches.AddFirst(val);
115 m_allBatches.Add(val);
116 return val;
117 }
118
119 public void Flush(Matrix matrix, bool clearAfterFlush = true, int maxLayer = int.MaxValue) {
120 Flush(matrix, Vector4.One, clearAfterFlush, maxLayer);
121 }
122
123 public void Flush(Matrix matrix, Vector4 color, bool clearAfterFlush = true, int maxLayer = int.MaxValue) {
124 if (m_sortNeeded) {
125 m_sortNeeded = false;
126 m_allBatches.Sort(
127 delegate(BaseBatch b1, BaseBatch b2) {
128 return b1.Layer < b2.Layer ? -1 :
129 b1.Layer > b2.Layer ? 1 : 0;
130 }
131 );
132 }
133 foreach (BaseBatch allBatch in m_allBatches) {
134 if (allBatch.Layer > maxLayer) {
135 break;
136 }
137 if (!allBatch.IsEmpty()) {
138 allBatch.Flush(matrix, color, clearAfterFlush);
139 }
140 }
141 }
142
143 public void Clear() {
144 foreach (BaseBatch allBatch in m_allBatches) {
145 allBatch.Clear();
146 }
147 }
148 }
149}
void Flush(Matrix matrix, Vector4 color, bool clearAfterFlush=true)
T1 FindFlatBatch(int layer, DepthStencilState depthStencilState, RasterizerState rasterizerState, BlendState blendState)
void Flush(Matrix matrix, bool clearAfterFlush=true, int maxLayer=int.MaxValue)
T3 FindFontBatch(BitmapFont font, int layer, DepthStencilState depthStencilState, RasterizerState rasterizerState, BlendState blendState, SamplerState samplerState)
void Flush(Matrix matrix, Vector4 color, bool clearAfterFlush=true, int maxLayer=int.MaxValue)
T2 FindTexturedBatch(Texture2D texture, bool useAlphaTest, int layer, DepthStencilState depthStencilState, RasterizerState rasterizerState, BlendState blendState, SamplerState samplerState)
static readonly Vector4 One