Staggart

Disable water fog for camera

Jul 8th, 2021
16
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3.  
  4. //Script requires Stylized Water 2 version 1.1.2+
  5. //This only applies to third-party fog effects. For the built-in Unity fog, simply toggle RenderSettings.fog
  6. [ExecuteInEditMode]
  7. public class DisableWaterFogForCamera : MonoBehaviour
  8. {
  9.     public Camera targetCamera;
  10.     private int _WaterFogDisabled = Shader.PropertyToID("_WaterFogDisabled");
  11.    
  12.     void Reset()
  13.     {
  14.         targetCamera = GetComponent<Camera>();
  15.     }
  16.    
  17.     void OnEnable()
  18.     {
  19.         RenderPipelineManager.beginCameraRendering += onRenderCamera;
  20.         RenderPipelineManager.endCameraRendering += onFinishRenderCamera;
  21.     }
  22.        
  23.     private void onRenderCamera(ScriptableRenderContext context, Camera currentCamera)
  24.     {
  25.         if(currentCamera == targetCamera) Shader.SetGlobalFloat(_WaterFogDisabled, 1);
  26.     }
  27.    
  28.     private void onFinishRenderCamera(ScriptableRenderContext context, Camera currentCamera)
  29.     {
  30.         //Restore so that it still renders for whatever camera comes next (unless it also has this component on it)
  31.         if(currentCamera == targetCamera) Shader.SetGlobalFloat(_WaterFogDisabled, 0);
  32.     }
  33.    
  34.     void OnDisable()
  35.     {
  36.         RenderPipelineManager.beginCameraRendering -= onRenderCamera;
  37.         RenderPipelineManager.endCameraRendering -= onFinishRenderCamera;
  38.         Shader.SetGlobalFloat(_WaterFogDisabled, 0);
  39.     }
  40. }
RAW Paste Data