Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Can i disable lens flares for a specific camera?

Discussion in '2021.2 Beta' started by pimpekdev, Jul 7, 2021.

  1. pimpekdev

    pimpekdev

    Joined:
    Feb 18, 2020
    Posts:
    4
    I am using new lens flare system for URP, but i have 2 different cameras with different fov's, one for game rendering and other for weapon (i cant use overlay because i am using deffered shading), and both cameras render lens flares, and the results are really weird and unrealistic.

    lens flare with enabled camera with weapon


    lens flare with disabled camera with weapon
     
  2. UltMate-Potato

    UltMate-Potato

    Joined:
    Apr 11, 2021
    Posts:
    4
    same issue here. im also using mixed fov and camera stacking to stop weapon clipping but this shish happens. the reason the lens flare is out of place is that the weapon camera is the one rendering the lens flare but because it has a different fov from your main cam the lens flare gets out of place. even though lens flares count as post processing effects u can disable them on specific cameras using volume layers on your cam and culling masks. the camera with post processing will be the one rendering it and you might say then enable post processing on the base camera but if you do that camera stacking breaks and your weapon wont receive post processing effects. i think unity should make it so we can choose what camera the lens flare appears on. but then there will be another problem of the lens flare appearing through your weapons because its in a separate camera
     
    gennadii-gorelikov likes this.
  3. lgarczyn

    lgarczyn

    Joined:
    Nov 23, 2014
    Posts:
    53
    Same issue, currently the only solution I have found is to disable post processing on the cameras that should not display lens flare.

    Another potential solution is to place a giant cube to hide the light source. It can be a child of the light source. Disable shadow casting, and place it on a layer only visible by the camera that shouldn't render the light flare.
     
  4. lgarczyn

    lgarczyn

    Joined:
    Nov 23, 2014
    Posts:
    53
    Found the solution! Just toggle the lens flare using render pipeline events

    Code (CSharp):
    1.  
    2. namespace CameraUtils
    3. {
    4.   [RequireComponent(typeof(LensFlareComponentSRP))]
    5.   public class LensFlareController : MonoBehaviour
    6.   {
    7.     [SerializeField] Camera targetCamera;
    8.  
    9.     void OnCameraEnd(ScriptableRenderContext _, Camera currentCamera)
    10.     {
    11.       if (targetCamera == null || currentCamera == targetCamera) reqLensFlare.enabled = false;
    12.     }
    13.  
    14.     void OnCameraStart(ScriptableRenderContext _, Camera currentCamera)
    15.     {
    16.       if (targetCamera == null || currentCamera == targetCamera) reqLensFlare.enabled = true;
    17.     }
    18.  
    19.     void Awake()
    20.     {
    21.       RenderPipelineManager.beginCameraRendering += OnCameraStart;
    22.       RenderPipelineManager.endCameraRendering += OnCameraEnd;
    23.     }
    24.     void OnDestroy()
    25.     {
    26.       RenderPipelineManager.beginCameraRendering -= OnCameraStart;
    27.       RenderPipelineManager.endCameraRendering -= OnCameraEnd;
    28.     }
    29. }