Search Unity

Camera.current returns null when calling it in OnWillRenderObject with UniversalRP

Discussion in 'Universal Render Pipeline' started by tsgcpp, Jul 12, 2020.

  1. tsgcpp

    tsgcpp

    Joined:
    Aug 5, 2017
    Posts:
    4
    Hi,

    I have some inconveniences because in UniversalRP, Camera.current returns null when calling it in OnWillRenderObject.

    I hope Unity supports this.

    I've sent the bug report to Unity and the response says "the issue is not a bug but rather a known limitation of the current implementation of this functionality".
    I want to create mirror reflection system. However I can't render the results per camera because of this limitation.

    In OnWillRenderObject's document, It is said "Note that Camera.current will be set to the camera that will render the object.".
    There is the error in the document about OnWillRenderObject.

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnWillRenderObject.html

    Thanks
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,924
    This does look like a bug/docs omission to me, specially when OnWillRenderObject is listed as fully supported for URP:
    https://docs.unity3d.com/Packages/c...l/universalrp-builtin-feature-comparison.html
     
    tsgcpp likes this.
  3. claybornp

    claybornp

    Joined:
    Jun 30, 2010
    Posts:
    76
    any update on this?
     
  4. smugleafdev

    smugleafdev

    Joined:
    Dec 9, 2020
    Posts:
    2
    2021: Any update on this?
     
  5. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    400
    OnWillRenderObject is impossible in modern rendering architecture, so it's never appear in srp.
     
  6. StaggartCreations

    StaggartCreations

    Joined:
    Feb 18, 2015
    Posts:
    2,260
    Strange that the documentation says this, all the MonoBehaviour OnXXX function won't work in a SRP. Documentation isn't exactly a strongpoint when it comes to SRP, so it likely just hasn't been updated?

    Alternatively, you can validate if the bounds of the object you want to check is within a certain camera's frustrum to the same effect:

    Code (CSharp):
    1. private static Plane[] frustrumPlanes = new Plane[6];
    2.      
    3. private static bool IsVisible(Camera camera, Bounds bounds)
    4. {
    5.      GeometryUtility.CalculateFrustumPlanes(camera.projectionMatrix, frustrumPlanes);
    6.  
    7.      return GeometryUtility.TestPlanesAABB(frustrumPlanes, bounds);
    8. }
    Any Renderer has a bounds property you can use.
     
    suittizihou likes this.
  7. Marco-Playable

    Marco-Playable

    Joined:
    Nov 12, 2020
    Posts:
    53
    Your sample code takes a camera as input - that's exactly what we don't have in the OnWillRender callback due to this bug.

    @BattleAngelAlitar Not sure what you are referring to - the callback does happen in SRP, it's just not populating the Camera.current field.
     
  8. HaydeLudos

    HaydeLudos

    Joined:
    May 2, 2018
    Posts:
    39
    You can use the SRP beginCameraRendering event to get the current camera that's rendering, then you can use @StaggartCreations function.

    Feel free to check this link for beginCameraRendering event documentation
     
    Last edited: Jan 31, 2021
    OUTERDARKNESS likes this.