Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SubsystemModelsRenderer.cs
浏览该文件的文档.
1using Engine;
5
6namespace Game {
8 public class ModelData : IComparable<ModelData> {
10
12
13 public float Light;
14
15 public double NextLightTime;
16
17 public int LastAnimateFrame;
18
19 public int CompareTo(ModelData other) {
20 int num = ComponentModel?.PrepareOrder ?? 0;
21 int num2 = other.ComponentModel?.PrepareOrder ?? 0;
22 return num - num2;
23 }
24 }
25
27
29
31
33
35
37
39
41
43
45
46 public Dictionary<ComponentModel, ModelData> m_componentModels = [];
47
48 public List<ModelData> m_modelsToPrepare = [];
49
50 public List<ModelData>[] m_modelsToDraw = [[], [], [], []];
51
52 public static bool DisableDrawingModels = false;
53
54 public int ModelsDrawn;
55
56 public int[] m_drawOrders = [-10000, 1, 99, 201];
57
59
60 public int[] DrawOrders => m_drawOrders;
61
62 public virtual void Draw(Camera camera, int drawOrder) {
63 //准备模型
64 if (drawOrder == m_drawOrders[0]) {
65 bool skipped = false;
67 "PrepareModels",
68 loader => {
69 loader.PrepareModels(this, camera, skipped, out bool skip);
70 skipped |= skip;
71 return false;
72 }
73 );
74 if (!skipped) {
75 ModelsDrawn = 0;
76 List<ModelData>[] modelsToDraw = m_modelsToDraw;
77 for (int i = 0; i < modelsToDraw.Length; i++) {
78 modelsToDraw[i].Clear();
79 }
80 m_modelsToPrepare.Clear();
81 foreach (ModelData value in m_componentModels.Values) {
82 if (value.ComponentModel.Model != null) {
85 m_modelsToPrepare.Add(value);
86 }
87 }
88 }
89 m_modelsToPrepare.Sort();
90 foreach (ModelData item in m_modelsToPrepare) {
91 PrepareModel(item, camera);
92 m_modelsToDraw[(int)item.ComponentModel.RenderingMode].Add(item);
93 }
94 }
95 }
97 bool skipped = false;
99 "RenderModels",
100 loader => {
101 loader.RenderModels(this, camera, drawOrder, skipped, out bool skip);
102 skipped |= skip;
103 return false;
104 }
105 );
106 if (!skipped) {
107 if (drawOrder == m_drawOrders[1]) //绘制类型为AlphaThreshold的Model
108 {
109 Display.DepthStencilState = DepthStencilState.Default;
110 Display.RasterizerState = RasterizerState.CullCounterClockwiseScissor;
111 Display.BlendState = BlendState.Opaque;
112 DrawModels(camera, m_modelsToDraw[0], null);
113 Display.RasterizerState = RasterizerState.CullNoneScissor;
114 DrawModels(camera, m_modelsToDraw[1], 0f);
115 Display.RasterizerState = RasterizerState.CullCounterClockwiseScissor;
116 m_primitivesRenderer.Flush(camera.ProjectionMatrix, true, 0);
117 }
118 else if (drawOrder == m_drawOrders[2]) //绘制TransparentBeforeWater的Model
119 {
120 Display.DepthStencilState = DepthStencilState.Default;
121 Display.RasterizerState = RasterizerState.CullNoneScissor;
122 Display.BlendState = BlendState.AlphaBlend;
123 DrawModels(camera, m_modelsToDraw[2], null);
124 }
125 else if (drawOrder == m_drawOrders[3]) //绘制TransparentAfterWater的Model
126 {
127 Display.DepthStencilState = DepthStencilState.Default;
128 Display.RasterizerState = RasterizerState.CullNoneScissor;
129 Display.BlendState = BlendState.AlphaBlend;
130 DrawModels(camera, m_modelsToDraw[3], null);
131 if (ShaderOpaque != null
132 && ShaderAlphaTested != null) {
134 }
135 else {
137 }
138 }
139 }
140 }
141 else {
142 m_primitivesRenderer.Clear();
143 }
144 }
145
146 public override void Load(ValuesDictionary valuesDictionary) {
147 m_subsystemTimeOfDay = Project.FindSubsystem<SubsystemTimeOfDay>(true);
148 m_subsystemTerrain = Project.FindSubsystem<SubsystemTerrain>(true);
149 m_subsystemSky = Project.FindSubsystem<SubsystemSky>(true);
150 m_subsystemShadows = Project.FindSubsystem<SubsystemShadows>(true);
152 "GetMaxInstancesCount",
153 modLoader => {
154 MaxInstancesCount = Math.Max(modLoader.GetMaxInstancesCount(), MaxInstancesCount);
155 return false;
156 }
157 );
159 ShaderCodeManager.GetFast("Shaders/Model.vsh"),
160 ShaderCodeManager.GetFast("Shaders/Model.psh"),
161 false,
163 );
165 ShaderCodeManager.GetFast("Shaders/Model.vsh"),
166 ShaderCodeManager.GetFast("Shaders/Model.psh"),
167 true,
169 );
170 }
171
172 public override void OnEntityAdded(Entity entity) {
173 foreach (ComponentModel item in entity.FindComponents<ComponentModel>()) {
174 ModelData value = new() {
175 ComponentModel = item, ComponentBody = item.Entity.FindComponent<ComponentBody>(), Light = m_subsystemSky.SkyLightIntensity
176 };
177 m_componentModels.Add(item, value);
178 }
179 }
180
181 public override void OnEntityRemoved(Entity entity) {
182 foreach (ComponentModel item in entity.FindComponents<ComponentModel>()) {
183 m_componentModels.Remove(item);
184 }
185 }
186
187 public virtual void PrepareModel(ModelData modelData, Camera camera) {
188 if (Time.FrameIndex > modelData.LastAnimateFrame) {
189 modelData.ComponentModel.Animate();
190 modelData.LastAnimateFrame = Time.FrameIndex;
191 }
192 if (Time.FrameStartTime >= modelData.NextLightTime) {
193 float? num = CalculateModelLight(modelData);
194 if (num.HasValue) {
195 modelData.Light = num.Value;
196 }
197 modelData.NextLightTime = Time.FrameStartTime + 0.1;
198 }
200 }
201
202 public virtual void DrawModels(Camera camera, List<ModelData> modelsData, float? alphaThreshold) {
203 DrawInstancedModels(camera, modelsData, alphaThreshold);
204 DrawModelsExtras(camera, modelsData);
205 }
206
207 public virtual void DrawInstancedModels(Camera camera, List<ModelData> modelsData, float? alphaThreshold) {
208 ModelShader modelShader = ShaderOpaque != null && ShaderAlphaTested != null ? alphaThreshold.HasValue ? ShaderAlphaTested : ShaderOpaque :
209 alphaThreshold.HasValue ? m_shaderAlphaTested : m_shaderOpaque;
210 modelShader.LightDirection1 = -Vector3.TransformNormal(LightingManager.DirectionToLight1, camera.ViewMatrix);
211 modelShader.LightDirection2 = -Vector3.TransformNormal(LightingManager.DirectionToLight2, camera.ViewMatrix);
212 modelShader.FogColor = new Vector3(m_subsystemSky.ViewFogColor);
213 modelShader.FogBottomTopDensity = new Vector3(
214 m_subsystemSky.ViewFogBottom - camera.ViewPosition.Y,
215 m_subsystemSky.ViewFogTop - camera.ViewPosition.Y,
216 m_subsystemSky.ViewFogDensity
217 );
218 modelShader.HazeStartDensity = new Vector2(m_subsystemSky.ViewHazeStart, m_subsystemSky.ViewHazeDensity);
219 modelShader.FogYMultiplier = m_subsystemSky.VisibilityRangeYMultiplier;
220 modelShader.WorldUp = Vector3.TransformNormal(Vector3.UnitY, camera.ViewMatrix);
221 modelShader.Transforms.View = Matrix.Identity;
222 modelShader.Transforms.Projection = camera.ProjectionMatrix;
223 modelShader.SamplerState = SamplerState.PointClamp;
224 if (alphaThreshold.HasValue) {
225 modelShader.AlphaThreshold = alphaThreshold.Value;
226 }
228 "ModelShaderParameter",
229 modLoader => {
230 modLoader.ModelShaderParameter(modelShader, camera, modelsData, alphaThreshold);
231 return true;
232 }
233 );
235 "SetShaderParameter",
236 modLoader => {
237 modLoader.SetShaderParameter(modelShader, camera);
238 return true;
239 }
240 );
241 foreach (ModelData modelsDatum in modelsData) {
242 bool skipDrawing = false;
244 "OnModelDataDrawing",
245 modLoader => {
246 modLoader.OnModelDataDrawing(modelsDatum, modelShader, camera, this, out bool skip);
247 skipDrawing |= skip;
248 return false;
249 }
250 );
251 if (!skipDrawing) {
252 ComponentModel componentModel = modelsDatum.ComponentModel;
253 Vector3 v = componentModel.DiffuseColor ?? Vector3.One;
254 float num = componentModel.Opacity ?? 1f;
255 modelShader.InstancesCount = componentModel.AbsoluteBoneTransformsForCamera.Length;
256 modelShader.MaterialColor = new Vector4(v * num, num);
257 modelShader.EmissionColor = componentModel.EmissionColor ?? Vector4.Zero;
258 modelShader.AmbientLightColor = new Vector3(LightingManager.LightAmbient * modelsDatum.Light);
259 modelShader.DiffuseLightColor1 = new Vector3(modelsDatum.Light);
260 modelShader.DiffuseLightColor2 = new Vector3(modelsDatum.Light);
261 modelShader.Texture = componentModel.TextureOverride;
262 Array.Copy(
263 componentModel.AbsoluteBoneTransformsForCamera,
264 modelShader.Transforms.World,
265 componentModel.AbsoluteBoneTransformsForCamera.Length
266 );
268 componentModel.Model,
269 componentModel.MeshDrawOrders
270 );
272 PrimitiveType.TriangleList,
273 modelShader,
274 instancedModelData.VertexBuffer,
275 instancedModelData.IndexBuffer,
276 0,
277 instancedModelData.IndexBuffer.IndicesCount
278 );
279 ModelsDrawn++;
280 }
281 //画名称
283 "OnModelRendererDrawExtra",
284 modLoader => {
285 modLoader.OnModelRendererDrawExtra(this, modelsDatum, camera, alphaThreshold);
286 return false;
287 }
288 );
289 }
290 }
291
292 public virtual void DrawModelsExtras(Camera camera, List<ModelData> modelsData) {
293 foreach (ModelData modelData in modelsData) {
294 if (modelData.ComponentBody != null
295 && modelData.ComponentModel.CastsShadow) {
296 Vector3 shadowPosition = modelData.ComponentBody.Position + new Vector3(0f, 0.02f, 0f);
297 BoundingBox boundingBox = modelData.ComponentBody.BoundingBox;
298 float shadowDiameter = 2.25f * (boundingBox.Max.X - boundingBox.Min.X);
299 m_subsystemShadows.QueueShadow(camera, shadowPosition, shadowDiameter, modelData.ComponentModel.Opacity ?? 1f);
300 }
301 modelData.ComponentModel.DrawExtras(camera);
302 }
303 }
304
305 public virtual float? CalculateModelLight(ModelData modelData) {
306 Vector3 p;
307 if (modelData.ComponentBody != null) {
308 p = modelData.ComponentBody.Position;
309 p.Y += 0.95f * (modelData.ComponentBody.BoundingBox.Max.Y - modelData.ComponentBody.BoundingBox.Min.Y);
310 }
311 else {
312 Matrix? boneTransform = modelData.ComponentModel.GetBoneTransform(modelData.ComponentModel.Model.RootBone.Index);
313 p = !boneTransform.HasValue ? Vector3.Zero : boneTransform.Value.Translation + new Vector3(0f, 0.9f, 0f);
314 }
316 }
317
318 //阴影绘制
319 public virtual void ShadowDraw(SubsystemShadows subsystemShadows, Camera camera, Vector3 shadowPosition, float shadowDiameter, float alpha) {
321 return;
322 }
323 float num = Vector3.DistanceSquared(camera.ViewPosition, shadowPosition);
324 if (!(num <= 1024f)) {
325 return;
326 }
327 float num2 = MathF.Sqrt(num);
328 float num3 = MathUtils.Saturate(4f * (1f - num2 / 32f));
329 float num4 = shadowDiameter / 2f; //阴影直径/2
330 int num5 = Terrain.ToCell(shadowPosition.X - num4);
331 int num6 = Terrain.ToCell(shadowPosition.Z - num4);
332 int num7 = Terrain.ToCell(shadowPosition.X + num4);
333 int num8 = Terrain.ToCell(shadowPosition.Z + num4);
334 for (int i = num5; i <= num7; i++) {
335 for (int j = num6; j <= num8; j++) {
336 int num9 = MathUtils.Min(Terrain.ToCell(shadowPosition.Y), 255);
337 int num10 = MathUtils.Max(num9 - 2, 0);
338 for (int num11 = num9; num11 >= num10; num11--) {
339 int cellValueFast = subsystemShadows.m_subsystemTerrain.Terrain.GetCellValueFast(i, num11, j);
340 int num12 = Terrain.ExtractContents(cellValueFast);
341 Block block = BlocksManager.Blocks[num12];
342 if (block.ObjectShadowStrength > 0f) {
343 BoundingBox[] customCollisionBoxes = block.GetCustomCollisionBoxes(subsystemShadows.m_subsystemTerrain, cellValueFast);
344 for (int k = 0; k < customCollisionBoxes.Length; k++) {
345 BoundingBox boundingBox = customCollisionBoxes[k];
346 float num13 = boundingBox.Max.Y + num11;
347 if (shadowPosition.Y - num13 > -0.5f) {
348 float num14 = camera.ViewPosition.Y - num13;
349 if (num14 > 0f) {
350 float num15 = MathUtils.Max(num14 * 0.01f, 0.005f);
351 float num16 = MathUtils.Saturate(1f - (shadowPosition.Y - num13) / 2f);
352 Vector3 p = new(boundingBox.Min.X + i, num13 + num15, boundingBox.Min.Z + j);
353 Vector3 p2 = new(boundingBox.Max.X + i, num13 + num15, boundingBox.Min.Z + j);
354 Vector3 p3 = new(boundingBox.Max.X + i, num13 + num15, boundingBox.Max.Z + j);
355 Vector3 p4 = new(boundingBox.Min.X + i, num13 + num15, boundingBox.Max.Z + j);
356 subsystemShadows.DrawShadowOverQuad(
357 p,
358 p2,
359 p3,
360 p4,
361 shadowPosition,
362 shadowDiameter,
363 0.45f * block.ObjectShadowStrength * alpha * num3 * num16
364 );
365 }
366 }
367 }
368 break;
369 }
370 if (num12 == 18) {
371 break;
372 }
373 }
374 }
375 }
376 }
377 }
378}
Engine.Vector3 Vector3
static readonly BlendState Opaque
static readonly BlendState AlphaBlend
static readonly DepthStencilState Default
static void DrawIndexed(PrimitiveType primitiveType, Shader shader, VertexBuffer vertexBuffer, IndexBuffer indexBuffer, int startIndex, int indicesCount)
static readonly RasterizerState CullNoneScissor
static readonly RasterizerState CullCounterClockwiseScissor
static int Min(int x1, int x2)
static float Saturate(float x)
static int Max(int x1, int x2)
static int FrameIndex
定义 Time.cs:26
static double FrameStartTime
定义 Time.cs:42
float ObjectShadowStrength
virtual BoundingBox[] GetCustomCollisionBoxes(SubsystemTerrain terrain, int value)
Vector3 ViewPosition
Matrix ViewMatrix
Matrix ProjectionMatrix
virtual BoundingBox BoundingBox
virtual void CalculateIsVisible(Camera camera)
virtual void DrawExtras(Camera camera)
virtual ModelRenderingMode RenderingMode
virtual void CalculateAbsoluteBonesTransforms(Camera camera)
virtual ? Matrix GetBoneTransform(int boneIndex)
static InstancedModelData GetInstancedModelData(Model model, int[] meshDrawOrders)
static readonly Vector3 DirectionToLight2
static readonly float LightAmbient
static ? float CalculateSmoothLight(SubsystemTerrain subsystemTerrain, Vector3 p)
static readonly Vector3 DirectionToLight1
static string GetFast(string fname)
virtual void PrepareModel(ModelData modelData, Camera camera)
virtual void DrawInstancedModels(Camera camera, List< ModelData > modelsData, float? alphaThreshold)
virtual void ShadowDraw(SubsystemShadows subsystemShadows, Camera camera, Vector3 shadowPosition, float shadowDiameter, float alpha)
virtual ? float CalculateModelLight(ModelData modelData)
Dictionary< ComponentModel, ModelData > m_componentModels
virtual void Draw(Camera camera, int drawOrder)
override void OnEntityAdded(Entity entity)
virtual void DrawModelsExtras(Camera camera, List< ModelData > modelsData)
override void Load(ValuesDictionary valuesDictionary)
override void OnEntityRemoved(Entity entity)
virtual void DrawModels(Camera camera, List< ModelData > modelsData, float? alphaThreshold)
SubsystemTerrain m_subsystemTerrain
virtual void DrawShadowOverQuad(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, Vector3 shadowPosition, float shadowDiameter, float alpha)
static int ExtractContents(int value)
virtual int GetCellValueFast(int x, int y, int z)
static int ToCell(float x)
Component FindComponent(Type type, string name, bool throwOnError)
ValuesDictionary ValuesDictionary
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook
static readonly Matrix Identity
static float DistanceSquared(Vector3 v1, Vector3 v2)
static readonly Vector3 One
static Vector3 TransformNormal(Vector3 v, Matrix m)
static readonly Vector3 UnitY
static readonly Vector4 Zero