Search Unity

Multiple Cameras, near and far clipping planes

Discussion in 'General Graphics' started by whidzee, Aug 7, 2020.

  1. whidzee

    whidzee

    Joined:
    Nov 20, 2012
    Posts:
    166
    Hello. In my game I want to have large draw distances about 4-5km and I also want to be able to get really close to some things. If I set my clipping planes to be really close and really far then i get some flickering in the shadows in the distance, this can be easily solved by making my near clipping plane about 1m, but that's way too far for what I need.

    I am wondering if it's possible to have two cameras active at the same time and one camera has clipping planes of say 0.01 to 1 and another camera which has a clipping plane of 1 to 5000.

    How would I set this up so they don't over lap each other? will there be any problems? and performance hit by having 2 cameras even though they aren't rendering the same stuff?

    Anything I need to keep an eye out for?
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Obvious question first: Do you need to have shadows that far in the distance? If not, change the shadow distance to avoid those problems.

    If you need shadows in the distance, second question would be: Can those shadows be static? If so you can bake them and also limit your shadow distance.

    I also suggest tweaking cascades, near and far clipping plane, shadow distance and shadow resolution. If you're not too far into the project and using the builtin render pipeline, it might be worth testing if the problem also happens for URP.

    If none of that helps then a multi camera setup might be interesting. You can have two cameras with the far camera rendering first and the near camera rendering second and only clearing the depth of the first camera. For URP you would handle that with a stacked camera for the near camera.

    Of course, this will have problems with any post processing effect that uses the depth information (e.g. Depth of Field), since the depth buffers represent different near and far clipping values.

    There is also a performance hit, since the near camera renders on top of the far camera, so pixel that might have been z tested otherwise will be rendered twice.
     
  3. whidzee

    whidzee

    Joined:
    Nov 20, 2012
    Posts:
    166
    I am not using any of the new render pipelines. and i am too late in my project to want to change.

    Yeah having shadows in the distance really is important to me.

    I've managed to get around this issue by playing with the shadow bias and normal bias on the sun in my scene. It's not quite perfect up close but it's pretty good and it completely solves the issue of the flickering shadows in the distance.
     
  4. Soulice

    Soulice

    Joined:
    Aug 18, 2014
    Posts:
    69
    @Johannski Since the question title is perfect and to avoid a repeat question, I would like to ask on top of this. Your answer just about hits what I am looking for.
    Think space scene, 2 cameras...does this make sense and is this a correct use of the cameras, clips and depths>
    Camera A depth 0 near clip 1000, far clip X (think something real far) with Skybox. Camera B depth -1, near 0.3 and far 1000.
    I have the floating origin script on one camera (A) to handle the origin hop since my origin threshold is 2000 (or 5000, need to test).
    This sound right?
     
  5. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    @Soulice Yes, I think that should work. In theory, it could potentially same quite some performance, if you render the near camera first and convert its depth information to the far clipping camera (everything not white to pitch black). This way you can discard pixels that are behind near camera objects with an early ztest, and therefore avoid some overdraw. In practice, I first would get the setup up and running with just first rendering everything far and then rendering everything near, to see if that works as expected. I think it should do fine.
    Other than that, you also can use LODs and set the far clipping distance for different layers (e.g. https://assetstore.unity.com/packages/tools/camera/per-layer-camera-culling-35100).
     
  6. Soulice

    Soulice

    Joined:
    Aug 18, 2014
    Posts:
    69
    @Johannski Thanks. Going to run some tests and, as you suggest, start with far first. I have pulled that link into my assets to take a look at as well. Thanks!