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
| using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal;
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))] public class WaterRenderer : MonoBehaviour { [Header("Water Properties")] public float waterHeight = 0f; public Color waterColor = new Color(0.1f, 0.3f, 0.6f, 0.7f); public float waterDensity = 0.5f; public float waveSpeed = 1.0f; public float waveHeight = 0.1f; public float waveFrequency = 1.0f; [Header("Reflection Settings")] public bool enableReflection = true; public int reflectionResolution = 512; public float reflectionDistance = 100f; [Header("Refraction Settings")] public bool enableRefraction = true; [Range(0.0f, 1.0f)] public float refractionStrength = 0.1f; [Header("Foam Settings")] public bool enableFoam = true; public float foamSpread = 0.5f; public float foamIntensity = 1.0f; [Header("Caustics Settings")] public bool enableCaustics = false; public float causticsIntensity = 0.5f; public float causticsScale = 10f; [Header("Performance Settings")] public bool enableLOD = true; public float lodDistance = 50f; private Camera m_ReflectionCamera; private RenderTexture m_ReflectionTexture; private Material m_WaterMaterial; private MeshRenderer m_MeshRenderer; private List<WaterInteraction> m_Interactions = new List<WaterInteraction>(); [System.Serializable] public class WaterProperties { public float height; public Color color; public float density; public float waveSpeed; public float waveHeight; public float waveFrequency; public float refractionStrength; public float foamSpread; public float foamIntensity; public float causticsIntensity; public System.DateTime lastUpdate; } [System.Serializable] public class WaterInteraction { public Transform objectTransform; public Vector3 lastPosition; public float interactionRadius; public float intensity; public System.DateTime lastInteraction; } public WaterProperties waterProperties = new WaterProperties(); void Start() { InitializeWaterRenderer(); } void Update() { UpdateWaterProperties(); UpdateReflectionCamera(); UpdateInteractions(); } private void InitializeWaterRenderer() { m_MeshRenderer = GetComponent<MeshRenderer>(); if (m_MeshRenderer != null && m_MeshRenderer.sharedMaterials.Length > 0) { m_WaterMaterial = m_MeshRenderer.sharedMaterials[0]; UpdateMaterialProperties(); } if (enableReflection) { CreateReflectionCamera(); } UpdateWaterProperties(); Debug.Log("[WaterRenderer] Initialized water rendering system"); } private void CreateReflectionCamera() { GameObject reflectionCameraObj = new GameObject("WaterReflectionCamera"); m_ReflectionCamera = reflectionCameraObj.AddComponent<Camera>(); m_ReflectionCamera.enabled = false; m_ReflectionCamera.renderingPath = RenderingPath.Forward; m_ReflectionCamera.backgroundColor = Color.black; m_ReflectionCamera.clearFlags = CameraClearFlags.SolidColor; m_ReflectionTexture = new RenderTexture(reflectionResolution, reflectionResolution, 16); m_ReflectionTexture.name = "WaterReflectionTexture"; m_ReflectionCamera.targetTexture = m_ReflectionTexture; } private void UpdateWaterProperties() { waterProperties.height = waterHeight; waterProperties.color = waterColor; waterProperties.density = waterDensity; waterProperties.waveSpeed = waveSpeed; waterProperties.waveHeight = waveHeight; waterProperties.waveFrequency = waveFrequency; waterProperties.refractionStrength = refractionStrength; waterProperties.foamSpread = foamSpread; waterProperties.foamIntensity = foamIntensity; waterProperties.causticsIntensity = causticsIntensity; waterProperties.lastUpdate = System.DateTime.Now; } private void UpdateMaterialProperties() { if (m_WaterMaterial != null) { m_WaterMaterial.SetColor("_WaterColor", waterColor); m_WaterMaterial.SetFloat("_WaterDensity", waterDensity); m_WaterMaterial.SetFloat("_WaveSpeed", waveSpeed); m_WaterMaterial.SetFloat("_WaveHeight", waveHeight); m_WaterMaterial.SetFloat("_WaveFrequency", waveFrequency); m_WaterMaterial.SetFloat("_RefractionStrength", refractionStrength); m_WaterMaterial.SetFloat("_FoamSpread", foamSpread); m_WaterMaterial.SetFloat("_FoamIntensity", foamIntensity); m_WaterMaterial.SetFloat("_CausticsIntensity", causticsIntensity); m_WaterMaterial.SetFloat("_CausticsScale", causticsScale); if (enableReflection && m_ReflectionTexture != null) { m_WaterMaterial.SetTexture("_ReflectionTexture", m_ReflectionTexture); } } } private void UpdateReflectionCamera() { if (!enableReflection || m_ReflectionCamera == null) return; Vector3 pos = transform.position; Vector3 normal = transform.up; float d = -Vector3.Dot(normal, pos) - waterHeight; Vector3 reflectionPos = pos - 2 * d * normal; m_ReflectionCamera.worldToCameraMatrix = Camera.main.worldToCameraMatrix * Matrix4x4.Scale(new Vector3(1, -1, 1)); Vector3 euler = Camera.main.transform.eulerAngles; m_ReflectionCamera.transform.eulerAngles = new Vector3(-euler.x, euler.y, euler.z); m_ReflectionCamera.transform.position = new Vector3( Camera.main.transform.position.x, -Camera.main.transform.position.y + waterHeight * 2, Camera.main.transform.position.z); Vector4 clipPlane = new Vector4(normal.x, normal.y, normal.z, d); m_ReflectionCamera.projectionMatrix = Camera.main.CalculateObliqueMatrix(clipPlane); m_ReflectionCamera.Render(); } private void UpdateInteractions() { for (int i = m_Interactions.Count - 1; i >= 0; i--) { var interaction = m_Interactions[i]; if (interaction.objectTransform == null) { m_Interactions.RemoveAt(i); continue; } float distanceToWater = Mathf.Abs(interaction.objectTransform.position.y - waterHeight); if (distanceToWater > interaction.interactionRadius) { m_Interactions.RemoveAt(i); } else { UpdateInteractionEffect(interaction); } } } private void UpdateInteractionEffect(WaterInteraction interaction) { Vector3 currentPos = interaction.objectTransform.position; Vector3 displacement = currentPos - interaction.lastPosition; if (m_WaterMaterial != null) { m_WaterMaterial.SetVector("_InteractionPos", new Vector4(currentPos.x, currentPos.z, interaction.intensity, 0)); m_WaterMaterial.SetVector("_InteractionVel", new Vector4(displacement.x, displacement.z, displacement.magnitude, 0)); } interaction.lastPosition = currentPos; interaction.lastInteraction = System.DateTime.Now; } public void AddWaterInteraction(Transform obj, float radius = 1.0f, float intensity = 1.0f) { var interaction = new WaterInteraction { objectTransform = obj, lastPosition = obj.position, interactionRadius = radius, intensity = intensity, lastInteraction = System.DateTime.Now }; m_Interactions.Add(interaction); } public void RemoveWaterInteraction(Transform obj) { m_Interactions.RemoveAll(interaction => interaction.objectTransform == obj); } public WaterProperties GetWaterProperties() { return waterProperties; } public void SetWaterColor(Color color) { waterColor = color; if (m_WaterMaterial != null) { m_WaterMaterial.SetColor("_WaterColor", color); } } public void SetWaveParameters(float speed, float height, float frequency) { waveSpeed = speed; waveHeight = height; waveFrequency = frequency; if (m_WaterMaterial != null) { m_WaterMaterial.SetFloat("_WaveSpeed", speed); m_WaterMaterial.SetFloat("_WaveHeight", height); m_WaterMaterial.SetFloat("_WaveFrequency", frequency); } } public void SetRefractionStrength(float strength) { refractionStrength = Mathf.Clamp01(strength); if (m_WaterMaterial != null) { m_WaterMaterial.SetFloat("_RefractionStrength", strength); } } public void SetFoamParameters(float spread, float intensity) { foamSpread = spread; foamIntensity = intensity; if (m_WaterMaterial != null) { m_WaterMaterial.SetFloat("_FoamSpread", spread); m_WaterMaterial.SetFloat("_FoamIntensity", intensity); } } public List<WaterInteraction> GetInteractions() { return new List<WaterInteraction>(m_Interactions); } public string GetWaterRenderingSummary() { return $"Water Height: {waterHeight:F2}, " + $"Reflection: {(enableReflection ? "On" : "Off")}, " + $"Refraction: {(enableRefraction ? "On" : "Off")}, " + $"Interactions: {m_Interactions.Count}, " + $"LOD: {(enableLOD ? "On" : "Off")}"; } public string GetWaterOptimizationSuggestions() { var suggestions = new System.Text.StringBuilder(); if (reflectionResolution > 1024) { suggestions.AppendLine("• 反射分辨率过高,考虑降低以提升性能"); } if (enableCaustics) { suggestions.AppendLine("• 焦散效果性能开销较大,可考虑在低端设备上禁用"); } if (m_Interactions.Count > 10) { suggestions.AppendLine("• 水交互对象过多,考虑限制同时交互的数量"); } return suggestions.ToString(); } public string GetWaterReport() { var report = new System.Text.StringBuilder(); report.AppendLine("=== Water Rendering Report ==="); report.AppendLine($"Water Height: {waterProperties.height:F2}"); report.AppendLine($"Water Color: {waterProperties.color}"); report.AppendLine($"Wave Speed: {waterProperties.waveSpeed:F2}"); report.AppendLine($"Wave Height: {waterProperties.waveHeight:F2}"); report.AppendLine($"Density: {waterProperties.density:F2}"); report.AppendLine($"Reflection: {(enableReflection ? "Enabled" : "Disabled")}"); report.AppendLine($"Refraction: {(enableRefraction ? "Enabled" : "Disabled")}"); report.AppendLine($"Foam: {(enableFoam ? "Enabled" : "Disabled")}"); report.AppendLine($"Caustics: {(enableCaustics ? "Enabled" : "Disabled")}"); report.AppendLine($"Interactions: {m_Interactions.Count}"); report.AppendLine($"Last Update: {waterProperties.lastUpdate}"); return report.ToString(); } void OnDestroy() { if (m_ReflectionTexture != null) { m_ReflectionTexture.Release(); m_ReflectionTexture = null; } if (m_ReflectionCamera != null) { DestroyImmediate(m_ReflectionCamera.gameObject); } } }
|