DepthFog.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class DepthFog : MonoBehaviour
  5. {
  6. Camera Cam;
  7. Material fogmat;
  8. public float StartDistance;
  9. public float EndDistance;
  10. public float Height;
  11. public Color FogCol;
  12. void Awake()
  13. {
  14. fogmat = new Material(Shader.Find("Hidden/DepthFog"));
  15. Cam = this.GetComponent<Camera>();
  16. }
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. }
  21. private void OnRenderImage(RenderTexture source, RenderTexture destination)
  22. {
  23. if (fogmat == null|| Cam==null) return;
  24. Shader.SetGlobalFloat("_StartDistance", StartDistance);
  25. Shader.SetGlobalFloat("_EndDistance", EndDistance);
  26. Shader.SetGlobalFloat("_Height", Height);
  27. Shader.SetGlobalColor("_FogCol", FogCol);
  28. Matrix4x4 vp = GL.GetGPUProjectionMatrix(Cam.projectionMatrix, false) * Cam.worldToCameraMatrix;
  29. vp = vp.inverse;
  30. Shader.SetGlobalMatrix("_InvVP", vp);
  31. Graphics.Blit(source, destination, fogmat);
  32. }
  33. }