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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
| using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal;
public class FogProcessingSystem : MonoBehaviour { [Header("Fog Processing Settings")] public bool enableFogProcessing = true; public bool visualizeFog = true; public bool logFogData = true; [Header("Fog Configuration")] public FogMode fogMode = FogMode.ExponentialSquared; public Color fogColor = Color.gray; public float fogDensity = 0.01f; public float fogStartDistance = 0.0f; public float fogEndDistance = 300.0f; public AnimationCurve fogDistanceCurve = AnimationCurve.Linear(0, 0, 1, 1); public float fogHeight = 0.0f; public float fogHeightFalloff = 1.0f; [Header("Performance Settings")] public float fogUpdateInterval = 0.1f; public bool enableFogCulling = true; public bool enableFogLOD = true; private CommandBuffer m_FogCommandBuffer; private Material m_FogMaterial; private Camera m_Camera; private float m_LastFogUpdate = 0f; private List<Renderer> m_FogAffectedRenderers = new List<Renderer>(); [System.Serializable] public class FogAnalysisData { public FogMode fogMode; public Color fogColor; public float fogDensity; public float fogStartDistance; public float fogEndDistance; public float fogHeight; public float fogHeightFalloff; public int affectedRenderers; public float averageFogDistance; public float maxFogDistance; public bool isVolumetricFog; public System.DateTime lastUpdate; } [System.Serializable] public class FogPerformanceData { public int fogAffectedDrawCalls; public float fogUpdateTimeMS; public int fogAffectedObjects; public float fogMemoryUsageKB; public bool isOptimal; } public FogAnalysisData analysisData = new FogAnalysisData(); public FogPerformanceData performanceData = new FogPerformanceData(); void Start() { if (enableFogProcessing) { InitializeFogProcessing(); } } void Update() { if (enableFogProcessing && Time.time - m_LastFogUpdate >= fogUpdateInterval) { UpdateFogProcessing(); m_LastFogUpdate = Time.time; } } private void InitializeFogProcessing() { m_Camera = Camera.main; if (m_Camera == null) { m_Camera = FindObjectOfType<Camera>(); } CreateFogCommandBuffer(); CreateFogMaterial(); FindFogAffectedRenderers(); if (logFogData) { Debug.Log("[FogProcessingSystem] Initialized fog processing system"); } } private void CreateFogCommandBuffer() { if (m_FogCommandBuffer != null) { m_FogCommandBuffer.Clear(); } else { m_FogCommandBuffer = new CommandBuffer(); m_FogCommandBuffer.name = "Fog Processing"; } } private void CreateFogMaterial() { string shaderSource = @" Shader ""Hidden/FogEffect"" { Properties { _MainTex (""Texture"", 2D) = ""white"" {} _FogColor (""Fog Color"", Color) = (0.5, 0.5, 0.5, 1) _FogDensity (""Fog Density"", Range(0, 0.1)) = 0.01 _FogStart (""Fog Start"", Float) = 0.0 _FogEnd (""Fog End"", Float) = 300.0 _FogHeight (""Fog Height"", Float) = 0.0 _FogHeightFalloff (""Fog Height Falloff"", Range(0.01, 10)) = 1.0 } SubShader { Tags { ""RenderType""=""Opaque"" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include ""UnityCG.cginc"" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 worldPos : TEXCOORD1; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; float3 worldPos : TEXCOORD1; }; sampler2D _MainTex; float4 _MainTex_TexelSize; fixed4 _FogColor; float _FogDensity; float _FogStart; float _FogEnd; float _FogHeight; float _FogHeightFalloff; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); // 计算雾效强度 float distance = length(_WorldSpaceCameraPos - i.worldPos); float fogFactor = 1.0; if (_FogEnd > _FogStart) { float normalizedDistance = saturate((distance - _FogStart) / (_FogEnd - _FogStart)); // 根据雾效模式计算 if (unity_FogMode == 1) // Linear { fogFactor = 1.0 - normalizedDistance; } else if (unity_FogMode == 2) // Exp { fogFactor = exp(-_FogDensity * distance); } else // Exp2 { float expDistance = _FogDensity * distance; fogFactor = exp(-expDistance * expDistance); } } // 应用高度雾效 float heightFog = 1.0 - saturate((i.worldPos.y - _FogHeight) * _FogHeightFalloff); fogFactor *= heightFog; fogFactor = saturate(fogFactor); // 混合雾效 col.rgb = lerp(_FogColor.rgb, col.rgb, fogFactor); return col; } ENDCG } } }"; } private void FindFogAffectedRenderers() { m_FogAffectedRenderers.Clear(); var allRenderers = FindObjectsOfType<Renderer>(); foreach (var renderer in allRenderers) { if (renderer != null && renderer.enabled && Vector3.Distance(renderer.transform.position, m_Camera.transform.position) <= fogEndDistance) { m_FogAffectedRenderers.Add(renderer); } } } private void UpdateFogProcessing() { RenderSettings.fog = true; RenderSettings.fogMode = fogMode; RenderSettings.fogColor = fogColor; RenderSettings.fogDensity = fogDensity; RenderSettings.fogStartDistance = fogStartDistance; RenderSettings.fogEndDistance = fogEndDistance; AnalyzeFogData(); OptimizeFogPerformance(); } private void AnalyzeFogData() { analysisData.fogMode = fogMode; analysisData.fogColor = fogColor; analysisData.fogDensity = fogDensity; analysisData.fogStartDistance = fogStartDistance; analysisData.fogEndDistance = fogEndDistance; analysisData.fogHeight = fogHeight; analysisData.fogHeightFalloff = fogHeightFalloff; analysisData.affectedRenderers = m_FogAffectedRenderers.Count; analysisData.isVolumetricFog = fogHeightFalloff > 0.0f; analysisData.lastUpdate = System.DateTime.Now; float totalDistance = 0f; float maxDistance = 0f; foreach (var renderer in m_FogAffectedRenderers) { float distance = Vector3.Distance(renderer.transform.position, m_Camera.transform.position); totalDistance += distance; maxDistance = Mathf.Max(maxDistance, distance); } analysisData.averageFogDistance = analysisData.affectedRenderers > 0 ? totalDistance / analysisData.affectedRenderers : 0f; analysisData.maxFogDistance = maxDistance; if (logFogData) { Debug.Log($"[Fog Analysis] Affected renderers: {analysisData.affectedRenderers}, " + $"Avg distance: {analysisData.averageFogDistance:F2}, Max distance: {analysisData.maxFogDistance:F2}"); } } private void OptimizeFogPerformance() { if (m_FogAffectedRenderers.Count > 100 && enableFogCulling) { foreach (var renderer in m_FogAffectedRenderers) { if (renderer != null) { float distance = Vector3.Distance(renderer.transform.position, m_Camera.transform.position); if (distance > fogEndDistance * 0.8f && enableFogLOD) { } } } } } public FogAnalysisData GetFogAnalysisData() { return analysisData; } public FogPerformanceData GetFogPerformanceData() { return performanceData; } public void UpdateFogParameters(FogMode mode, Color color, float density, float startDistance, float endDistance, float height, float heightFalloff) { fogMode = mode; fogColor = color; fogDensity = Mathf.Clamp(density, 0.001f, 0.1f); fogStartDistance = Mathf.Max(0f, startDistance); fogEndDistance = Mathf.Max(fogStartDistance + 1f, endDistance); fogHeight = height; fogHeightFalloff = Mathf.Clamp(heightFalloff, 0.01f, 10f); UpdateFogProcessing(); } public string GetFogStats() { var stats = new System.Text.StringBuilder(); stats.AppendLine("=== Fog Statistics ==="); stats.AppendLine($"Fog Mode: {analysisData.fogMode}"); stats.AppendLine($"Fog Color: {analysisData.fogColor}"); stats.AppendLine($"Fog Density: {analysisData.fogDensity}"); stats.AppendLine($"Fog Start: {analysisData.fogStartDistance}"); stats.AppendLine($"Fog End: {analysisData.fogEndDistance}"); stats.AppendLine($"Affected Renderers: {analysisData.affectedRenderers}"); stats.AppendLine($"Average Distance: {analysisData.averageFogDistance:F2}"); stats.AppendLine($"Max Distance: {analysisData.maxFogDistance:F2}"); stats.AppendLine($"Is Volumetric: {analysisData.isVolumetricFog}"); stats.AppendLine($"Last Update: {analysisData.lastUpdate}"); return stats.ToString(); } public string GetCurrentFogSettings() { return $"Mode: {fogMode}, Color: {fogColor}, Density: {fogDensity}, " + $"Start: {fogStartDistance}, End: {fogEndDistance}, Height: {fogHeight}"; } public void SetFogIntensity(float intensity) { float clampedIntensity = Mathf.Clamp01(intensity); Color adjustedColor = new Color( fogColor.r * clampedIntensity, fogColor.g * clampedIntensity, fogColor.b * clampedIntensity, fogColor.a ); UpdateFogParameters(fogMode, adjustedColor, fogDensity, fogStartDistance, fogEndDistance, fogHeight, fogHeightFalloff); } public void AddFogAffectedRenderer(Renderer renderer) { if (renderer != null && !m_FogAffectedRenderers.Contains(renderer)) { m_FogAffectedRenderers.Add(renderer); } } public void RemoveFogAffectedRenderer(Renderer renderer) { if (renderer != null) { m_FogAffectedRenderers.Remove(renderer); } } void OnDestroy() { if (m_FogCommandBuffer != null) { m_FogCommandBuffer.Dispose(); m_FogCommandBuffer = null; } } }
|