Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
LitShader.cs
浏览该文件的文档.
1using System.Globalization;
2#if !ANDROID
3using System.Reflection;
4#endif
5
6namespace Engine.Graphics {
7 public class LitShader : TransformedShader {
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
46 public int m_instancesCount;
47
48 public int m_lightsCount;
49
50 public bool m_useFog;
51
53 set => m_textureParameter.SetValue(value);
54 }
55
57 set => m_samplerStateParameter.SetValue(value);
58 }
59
61 set => m_materialColorParameter.SetValue(value);
62 }
63
65 set => m_emissionColorParameter.SetValue(value);
66 }
67
68 public float AlphaThreshold {
69 set => m_alphaThresholdParameter.SetValue(value);
70 }
71
73 set => m_ambientLightColorParameter.SetValue(value);
74 }
75
77 set => m_diffuseLightColor1Parameter.SetValue(value);
78 }
79
81 set => m_diffuseLightColor2Parameter.SetValue(value);
82 }
83
85 set => m_diffuseLightColor3Parameter.SetValue(value);
86 }
87
89 set => m_directionToLight1Parameter.SetValue(-value);
90 }
91
93 set => m_directionToLight2Parameter.SetValue(-value);
94 }
95
97 set => m_directionToLight3Parameter.SetValue(-value);
98 }
99
100 public float FogStart {
101 set => m_fogStartParameter.SetValue(value);
102 }
103
104 public float FogLength {
105 set => m_fogLengthParameter.SetValue(value);
106 }
107
109 set => m_fogColorParameter.SetValue(value);
110 }
111
112 public float Time {
113 set => m_time.SetValue(value);
114 }
115
116 public int InstancesCount {
117 get => m_instancesCount;
118 set {
119 if (value < 0
120 || value > Transforms.MaxWorldMatrices) {
121 throw new InvalidOperationException("Invalid instances count.");
122 }
123 m_instancesCount = value;
124 }
125 }
126
127 public LitShader(string vsc,
128 string psc,
129 int lightsCount,
130 bool useEmissionColor,
131 bool useVertexColor,
132 bool useTexture,
133 bool useFog,
134 bool useAlphaThreshold,
135 int maxInstancesCount = 1) : base(
136 vsc,
137 psc,
138 maxInstancesCount,
140 lightsCount,
141 useEmissionColor,
142 useVertexColor,
143 useTexture,
144 useFog,
145 useAlphaThreshold,
146 maxInstancesCount
147 )
148 ) {
149 if (lightsCount < 0
150 || lightsCount > 3) {
151 throw new ArgumentException("lightsCount");
152 }
153 if (maxInstancesCount < 0
154 || maxInstancesCount > 32) {
155 throw new ArgumentException("maxInstancesCount");
156 }
157 m_worldMatrixParameter = GetParameter("u_worldMatrix", true);
158 m_worldViewMatrixParameter = GetParameter("u_worldViewMatrix", true);
159 m_worldViewProjectionMatrixParameter = GetParameter("u_worldViewProjectionMatrix", true);
160 m_textureParameter = GetParameter("u_texture", true);
161 m_samplerStateParameter = GetParameter("u_samplerState", true);
162 m_materialColorParameter = GetParameter("u_materialColor", true);
163 m_emissionColorParameter = GetParameter("u_emissionColor", true);
164 m_alphaThresholdParameter = GetParameter("u_alphaThreshold", true);
165 m_ambientLightColorParameter = GetParameter("u_ambientLightColor", true);
166 m_diffuseLightColor1Parameter = GetParameter("u_diffuseLightColor1", true);
167 m_directionToLight1Parameter = GetParameter("u_directionToLight1", true);
168 m_diffuseLightColor2Parameter = GetParameter("u_diffuseLightColor2", true);
169 m_directionToLight2Parameter = GetParameter("u_directionToLight2", true);
170 m_diffuseLightColor3Parameter = GetParameter("u_diffuseLightColor3", true);
171 m_directionToLight3Parameter = GetParameter("u_directionToLight3", true);
172 m_fogStartParameter = GetParameter("u_fogStart", true);
173 m_fogLengthParameter = GetParameter("u_fogLength", true);
174 m_fogColorParameter = GetParameter("u_fogColor", true);
175 m_time = GetParameter("u_time", true);
176 m_lightsCount = lightsCount;
178 m_useFog = useFog;
180 if (useEmissionColor) {
182 }
183 if (lightsCount >= 1) {
184 AmbientLightColor = new Vector3(0.2f);
185 DiffuseLightColor1 = new Vector3(0.8f);
186 LightDirection1 = Vector3.Normalize(new Vector3(1f, -1f, 1f));
187 }
188 if (lightsCount >= 2) {
189 DiffuseLightColor2 = new Vector3(0.4f);
190 LightDirection2 = Vector3.Normalize(new Vector3(-1f, -0.5f, -0.25f));
191 }
192 if (lightsCount >= 3) {
193 DiffuseLightColor3 = new Vector3(0.2f);
194 LightDirection3 = Vector3.Normalize(new Vector3(0f, 1f, 0f));
195 }
196 if (useFog) {
197 FogLength = 100f;
198 }
199 }
200
201 public LitShader(int lightsCount,
202 bool useEmissionColor,
203 bool useVertexColor,
204 bool useTexture,
205 bool useFog,
206 bool useAlphaThreshold,
207 int maxInstancesCount = 1) : base(
210 maxInstancesCount,
212 lightsCount,
213 useEmissionColor,
214 useVertexColor,
215 useTexture,
216 useFog,
217 useAlphaThreshold,
218 maxInstancesCount
219 )
220 ) {
221 if (lightsCount < 0
222 || lightsCount > 3) {
223 throw new ArgumentException("lightsCount");
224 }
225 if (maxInstancesCount < 0
226 || maxInstancesCount > 32) {
227 throw new ArgumentException("maxInstancesCount");
228 }
229 m_worldMatrixParameter = GetParameter("u_worldMatrix", true);
230 m_worldViewMatrixParameter = GetParameter("u_worldViewMatrix", true);
231 m_worldViewProjectionMatrixParameter = GetParameter("u_worldViewProjectionMatrix", true);
232 m_textureParameter = GetParameter("u_texture", true);
233 m_samplerStateParameter = GetParameter("u_samplerState", true);
234 m_materialColorParameter = GetParameter("u_materialColor", true);
235 m_emissionColorParameter = GetParameter("u_emissionColor", true);
236 m_alphaThresholdParameter = GetParameter("u_alphaThreshold", true);
237 m_ambientLightColorParameter = GetParameter("u_ambientLightColor", true);
238 m_diffuseLightColor1Parameter = GetParameter("u_diffuseLightColor1", true);
239 m_directionToLight1Parameter = GetParameter("u_directionToLight1", true);
240 m_diffuseLightColor2Parameter = GetParameter("u_diffuseLightColor2", true);
241 m_directionToLight2Parameter = GetParameter("u_directionToLight2", true);
242 m_diffuseLightColor3Parameter = GetParameter("u_diffuseLightColor3", true);
243 m_directionToLight3Parameter = GetParameter("u_directionToLight3", true);
244 m_fogStartParameter = GetParameter("u_fogStart", true);
245 m_fogLengthParameter = GetParameter("u_fogLength", true);
246 m_fogColorParameter = GetParameter("u_fogColor", true);
247 m_lightsCount = lightsCount;
249 m_useFog = useFog;
251 if (useEmissionColor) {
253 }
254 if (lightsCount >= 1) {
255 AmbientLightColor = new Vector3(0.2f);
256 DiffuseLightColor1 = new Vector3(0.8f);
257 LightDirection1 = Vector3.Normalize(new Vector3(1f, -1f, 1f));
258 }
259 if (lightsCount >= 2) {
260 DiffuseLightColor2 = new Vector3(0.4f);
261 LightDirection2 = Vector3.Normalize(new Vector3(-1f, -0.5f, -0.25f));
262 }
263 if (lightsCount >= 3) {
264 DiffuseLightColor3 = new Vector3(0.2f);
265 LightDirection3 = Vector3.Normalize(new Vector3(0f, 1f, 0f));
266 }
267 if (useFog) {
268 FogLength = 100f;
269 }
270 }
271
272 public static string GetLitVshString() {
273#if ANDROID
274 Stream stream = Storage.OpenFile("app:Lit.vsh", OpenFileMode.Read);
275#else
276 Stream stream = typeof(Shader).GetTypeInfo().Assembly.GetManifestResourceStream("Engine.Resources.Lit.vsh");
277#endif
278 ArgumentNullException.ThrowIfNull(stream);
279 return new StreamReader(stream).ReadToEnd();
280 }
281
282 public static string GetLitPshString() {
283#if ANDROID
284 Stream stream = Storage.OpenFile("app:Lit.psh", OpenFileMode.Read);
285#else
286 Stream stream = typeof(Shader).GetTypeInfo().Assembly.GetManifestResourceStream("Engine.Resources.Lit.psh");
287#endif
288 ArgumentNullException.ThrowIfNull(stream);
289 return new StreamReader(stream).ReadToEnd();
290 }
291
292 public override void PrepareForDrawingOverride() {
293 Transforms.UpdateMatrices(m_instancesCount, m_useFog, false, true);
295 if (m_lightsCount >= 1) {
297 }
298 if (m_useFog) {
300 }
301 }
302
303 public static ShaderMacro[] PrepareShaderMacros(int lightsCount,
304 bool useEmissionColor,
305 bool useVertexColor,
306 bool useTexture,
307 bool useFog,
308 bool useAlphaThreshold,
309 int maxInstancesCount) {
310 List<ShaderMacro> list = [];
311 if (lightsCount > 0) {
312 list.Add(new ShaderMacro("USE_LIGHTING"));
313 }
314 if (lightsCount == 1) {
315 list.Add(new ShaderMacro("ONE_LIGHT"));
316 }
317 if (lightsCount == 2) {
318 list.Add(new ShaderMacro("TWO_LIGHTS"));
319 }
320 if (lightsCount == 3) {
321 list.Add(new ShaderMacro("THREE_LIGHTS"));
322 }
323 if (useEmissionColor) {
324 list.Add(new ShaderMacro("USE_EMISSIONCOLOR"));
325 }
326 if (useVertexColor) {
327 list.Add(new ShaderMacro("USE_VERTEXCOLOR"));
328 }
329 if (useTexture) {
330 list.Add(new ShaderMacro("USE_TEXTURE"));
331 }
332 if (useFog) {
333 list.Add(new ShaderMacro("USE_FOG"));
334 }
335 if (useAlphaThreshold) {
336 list.Add(new ShaderMacro("USE_ALPHATHRESHOLD"));
337 }
338 if (maxInstancesCount > 1) {
339 list.Add(new ShaderMacro("USE_INSTANCING"));
340 }
341 list.Add(new ShaderMacro("MAX_INSTANCES_COUNT", maxInstancesCount.ToString(CultureInfo.InvariantCulture)));
342 return list.ToArray();
343 }
344 }
345}
Engine.Vector3 Vector3
static string GetLitVshString()
LitShader(int lightsCount, bool useEmissionColor, bool useVertexColor, bool useTexture, bool useFog, bool useAlphaThreshold, int maxInstancesCount=1)
ShaderParameter m_ambientLightColorParameter
ShaderParameter m_worldViewMatrixParameter
ShaderParameter m_diffuseLightColor2Parameter
ShaderParameter m_samplerStateParameter
ShaderParameter m_materialColorParameter
ShaderParameter m_diffuseLightColor1Parameter
ShaderParameter m_textureParameter
ShaderParameter m_directionToLight2Parameter
static ShaderMacro[] PrepareShaderMacros(int lightsCount, bool useEmissionColor, bool useVertexColor, bool useTexture, bool useFog, bool useAlphaThreshold, int maxInstancesCount)
ShaderParameter m_fogLengthParameter
ShaderParameter m_alphaThresholdParameter
ShaderParameter m_fogColorParameter
ShaderParameter m_worldMatrixParameter
ShaderParameter m_worldViewProjectionMatrixParameter
ShaderParameter m_directionToLight1Parameter
static string GetLitPshString()
ShaderParameter m_fogStartParameter
ShaderParameter m_emissionColorParameter
ShaderParameter m_diffuseLightColor3Parameter
LitShader(string vsc, string psc, int lightsCount, bool useEmissionColor, bool useVertexColor, bool useTexture, bool useFog, bool useAlphaThreshold, int maxInstancesCount=1)
ShaderParameter m_directionToLight3Parameter
override void PrepareForDrawingOverride()
virtual ShaderParameter GetParameter(string name, bool allowNull=false)
static Stream OpenFile(string path, OpenFileMode openFileMode)
static Vector3 Normalize(Vector3 v)
static readonly Vector4 One
static readonly Vector4 Zero