Search Unity

Trying to render the Skybox only in a Custom Buffer

Discussion in 'General Graphics' started by unityuserunity85496, Dec 5, 2019.

  1. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    im trying to use the basic render stack not the lightweight lwrp urp to separate two render stages to perform effects to selected objects and overlay them back onto the bottom render pass.
    The passes are |
    + Skybox
    + Selected objects with custom magicness without skybox


    im essentially trying to simply get the current stage of rendering of the skybox without objects in the view. Though in my reading skybox happens after everything and internally is composited via masks. So that wont work I guess. So I might need to keep working in lwrp again anyhow
    https://docs.unity3d.com/Manual/GraphicsCommandBuffers.html

    I dont want to simply apply a screen filter over everything rendered during OnRenderImage

    I have tried to not clear the buffer m_DisplaceBuffer.ClearRenderTarget(true, true, Color.clear);
    but I dont see a way to just render into the buffer without a renderer

    Im also using the Frame debugger to step though the passes to find if I can reach that

    Code (CSharp):
    1.  
    2. m_DisplaceBuffer = new CommandBuffer();
    3. m_DisplaceBuffer.name = "Skyyyybox BufffferZ";
    4. m_Cameras[cam] = m_DisplaceBuffer;
    5.  
    6. int tempID = Shader.PropertyToID("_Temp1Zzz");
    7. m_DisplaceBuffer.GetTemporaryRT(tempID, -1, -1, 24, FilterMode.Bilinear);
    8. m_DisplaceBuffer.SetRenderTarget(tempID);
    9. m_DisplaceBuffer.ClearRenderTarget(true, true, Color.clear);
    10.  
    11. // this does not work for the skybox since theres no ClearRenderTarget            
    12. // m_DisplaceBuffer.DrawRenderer(r, _flatColorMat);
    13.  
    14. m_DisplaceBuffer.SetGlobalTexture("_map", tempID);
    15.  
    16. cam.AddCommandBuffer(CameraEvent.AfterSkybox, m_DisplaceBuffer);
     
    Last edited: Dec 5, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    There’s no way to just render the skybox with command buffers, no.

    However you can just do what everyone did before command buffers, which is add a new camera to the scene as a child of your main camera, set the depth to a value less the main camera, set it to clear to the skybox, set the culling mask to nothing, and assign a render texture as it’s target, either an asset or from script with a temporary target. Alternatively you can set those settings on the main camera and call Render() on it, then set it back to the previous settings.
     
  3. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    THATS wild. Makes sense though, I'll go with the second one to retain one cameraness. Thank you
     
  4. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    So I had issues with trying to get the single camera to render to skybox texture and then back to targetTexture = null timing and mix with nessasasry CommandBuffers as well for other passes
    So below is how I solved it for now.
    In the scene Create a second camera and parent to main camera, setactive to false so it only renders on the render command
    I also have a plane in the scene to see the texture working

    if you have the magic combo to make a single camera work and retain a targetTexture none so it can render to the game view that would be great!

    This ultimately wont be the answer to using this in AR to overlay the render scene to the camera frames. Those tend to have their own api and event callback function to patch into. BUt that should help someone when tinkering with this since I could not find any same code cira 2011


    Code (CSharp):
    1.  
    2. void SetSkyBoxOnlyCameraSettings(){
    3.         _skyboxCam.cullingMask = 0;
    4.         _skyboxCam.depth = -2;
    5.         _skyboxCam.clearFlags = CameraClearFlags.Skybox;
    6.     }
    7.  
    8.  
    9.  
    10. // command buffers will not work to get the skybox
    11.     // ugh
    12.     void RenderSkyBoxOnly_CM(){
    13.      
    14.  
    15.         // skyBox_MagicBuffer = new CommandBuffer();
    16.         // skyBox_MagicBuffer.name = "Skyyyyyyyyy Box only";
    17.         // // m_Cameras[_cam] = skyBox_MagicBuffer;
    18.  
    19.         // int colorTexId = Shader.PropertyToID("_SkyyyyyyboxTemp");
    20.         // skyBox_MagicBuffer.GetTemporaryRT(colorTexId, -1, -1, 24, FilterMode.Bilinear);
    21.         // skyBox_MagicBuffer.SetRenderTarget(colorTexId);
    22.  
    23.         // skyBox_MagicBuffer.ClearRenderTarget(true, true, Color.magenta); // clear before drawing to it each frame!!
    24.      
    25.         // // set render texture as globally accessable 'glow map' texture
    26.         // skyBox_MagicBuffer.SetGlobalTexture("_SkyyyyyboxOnly", colorTexId);
    27.  
    28.         // _cam.AddCommandBuffer(skyBoxEventsEnum, skyBox_MagicBuffer);
    29.         // _cam.Render();
    30.  
    31.         // its too tricky to get the same camera to render out to textures
    32.         // _skyboxCam
    33.         SetSkyBoxOnlyCameraSettings();
    34.         _skyboxCam.depthTextureMode = DepthTextureMode.Depth;
    35.  
    36.         if (_skyBoxRenderTexture == null)
    37.         {
    38.             _skyBoxRenderTexture = new RenderTexture(Screen.width, Screen.height, 16, RenderTextureFormat.ARGB32);
    39.             _skyBoxRenderTextureDepth = new RenderTexture(Screen.width, Screen.height, 16, RenderTextureFormat.Depth);
    40.  
    41.         }
    42.         // _skyboxCam.SetTargetBuffers(_skyBoxRenderTexture.colorBuffer, _skyBoxRenderTextureDepth.depthBuffer);
    43.         _skyboxCam.targetTexture = _skyBoxRenderTexture;
    44.         _skyboxCam.Render();
    45.         Shader.SetGlobalTexture("_SkyyyyyboxOnly", _skyBoxRenderTexture);
    46.  
    47.         // ResetCameraSettings();
    48.  
    49.         // _cam.targetTexture = null;
    50.  
    51.     }
     
  5. unityuserunity85496

    unityuserunity85496

    Joined:
    Jan 9, 2019
    Posts:
    89
    example of a mix of command buffers and the skybox trick

    composites unity.JPG