Search Unity

Resolved Issues with camera planes (strange rectangular wall)

Discussion in 'Editor & General Support' started by link0016, Aug 6, 2022.

  1. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    I've hit a snag with Unity recently where at a far enough distance, objects will be partially obscured by a sort of rectangular wall, which moves as the camera is rotated:


    I've attempted to add fog and do something via Shader Graph to try and mask it, but to no effect. Adjusting the near plane just causes my camera to go through walls and adjusting far plane doesn't resolve the issue either.
    If there's a way to do radial fog in Shader Graph, I'd love to know, since solutions I've seen involve shader code.

    My shader graph settings:
    shadergraph.png
    Camera settings:
    camerasett.png

    (If this is not the right forum, you are free to move it)
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    That's the camera's far clipping plane. It cuts off anything beyond a certain distance from the camera.

    You can see the Clipping Planes settings in your camera settings you screenshot.
     
  3. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    How do I go about the disappearance of objects look not so rough? I've tried using the fog to try and mask it.. but no luck
     
  4. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    I've been doing a lot of searching, and I feel
    like asking the great @bgolus for some help since it may be a shader issue.
    At this point, I don't know if I should abandon URP and just code a shader instead, as it seems ShaderGraph may not be the answer.

    All I'm trying to accomplish is having objects in the distance fade into view as you get closer to them (and fading out objects when as you get further away from them)

    Something similar to the draw distance of 90s 3D games like Silent Hill, where fog was used to mask
    object pop-in
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    You need the fog color to match the sky color for this to work. You set the fog color in the lighting settings.
     
  6. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    Changing the colors is only a temporary solution... am I just out of luck with ShaderGraph?

    If better results can be done through writing a custom shader, where would I start with basic "texture + vertex color" shader
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    What do you mean by it being a temporary solution? It’s literally the only solution. If you want the surfaces to disappear, the sky and fog need to be the same color, otherwise there’ll be a “pop” as geometry passes the far clip plane like you’re seeing now. Shader Graph vs hand written shader isn’t going to change anything there.

    Though you are correct that it is not a radial fog, and to get that you’d have to calculate the fog intensity yourself.
     
  8. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    From reading topics on fog, I've heard of stuff like KinoFog and apparently a workaround for getting radial fog via modifying UnityCG... though I'm not sure how you'd incorporate that with ShaderGraph (URP in my case).
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Kino fog is at its core a very simple trick.

    Step 1. Use a cubemap for the skybox.
    Step 2. Use the same cubemap for the fog color.

    And that’s it.



    Calculating fog density itself is relatively straightforward. You can use the built-in fog node and settings, or you can use your own. Basically every game uses one of the same 3 fog calculation types. Linear, Exp, and Exp2. Linear is straight forward. Lerp between start and end distances. Exp and Exp2 are 1/e^(d*density) or 1/e^((d*density)^2), where “d” is the linear 0 to 1 depth between the near and far plane.

    Really for the exponential fog, all you need to do is this:
    Position node set to View space -> Length node
    -> Inverse Lerp node with A being the Camera node’s near, B the far, and T the Length node
    -> Multiply by some arbitrary (small) value you have as a “Density” property
    -> Multiply by -1
    -> Exponential node set to base 2
    -> Saturate node -> One Minus node
    Lerp between your shader’s color (A) and the fog color (B) with the above as the factor (T)

    For something like KinoFog you use a cubemap that you sample with the View Direction node set to world space for both the “fog color” and your skybox shader.
     
  10. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    I tried applying the Shadergraph settings you suggested..
    shadergraph2.png

    And it's looking better.. but there's still the weird rectangular cutoff (rather than radial fog).


    And unless there's a setting I got wrong, density only works when cranked to 4 or higher
    dens.png

    I've seen videos for fading objects when they are near, but unsure of how you'd do that for objects at a far distance
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Ah ... one problem I forgot about with exponential fog ... it never actually gets to fully opaque.

    Try plugging the inverse lerp directly into the color / fog lerp's T.

    If that works, you can do something like this to tweak the "shape" of the fog.
    upload_2022-8-8_23-20-42.png
     
  12. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    With a black skybox and black fog color, the effect is perfect


    However, I encountered issues when the skybox color and fog color weren't black, where the clipping would still be visible. As a possible solution, I attempted to do the method you described for KinoFog, but the results did not turn out as planned.. with clipping still occuring
    greenproblem.png

    Is there a node in here that could be the culprit or am I missing something?
    kinofogattempt.png

    Also, I appreciate your time and patience with trying to help me. I thought ShaderGraph would make things easy for me, but perhaps it has limits. :)
     
  13. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Multiply the view direction by -1
     
  14. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    No luck..


    Unless I misread what you meant by "multiply by -1"
    skyboxp.png

    Part of me wants to think this could be a bug within my Unity version, but could also be other issue
     
    Last edited: Aug 10, 2022
  15. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Then your skybox material and cubemap don't match.
     
  16. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    They share the same cubemap.
    I'm uploading the shadergraph.. would it be helpful to include .rar file of the scene and necessary assets?
     

    Attached Files:

  17. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Your shader doesn't really matter here as much as your skybox.

    Here's my setup.
    upload_2022-8-9_23-19-35.png

    The fog part of the graph
    upload_2022-8-9_23-20-15.png

    and the skybox material
    upload_2022-8-9_23-20-43.png

    The only important thing is all of the settings for the skybox/cubemap material are left at their defaults, except the cubemap itself. Tint color is 50 grey, exposure is 1, rotation is 0. As long as that's the case, it should match the cubemap sample in the shader graph perfectly. If any of those values are tweaked, it won't match.
     
  18. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    So I created a new material, set it to default skybox shader, and didn't change any settings on it (aside from adding the cubemap).. and still see clipping.
    I tried making a new scene where I added new skybox material, then added a cube.. and it faded away from view with no clipping issues.

    After that, I loaded in my "test level" scene, which has a lot more objects, applied the newer skybox material, added cubemaps + set density.. and I noticed that flat planes like the floor and ceiling fade in and out of view just fine... but geometry (like walls for example) with more vertices still experience the weird angled/triangular clipping that bothers me (rather than smoothly fading-in).
    FPSTK 11-August-2022 155932.png
     
  19. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    Sorry to double-post, but
    @bgolus
    What numbers do you have plugged in for the near and far planes of your camera? I was reading old topics and there was mentions that the ratio between near and far can cause the weird clipping.

    Also, would adding layerCull variables such as "layerCullDistances" and "layerCullSpherical" to my mouselook script help with resolving this?
     
  20. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    You're overthinking it. The problem continues to be the same. The sky doesn't match the fog.

    The cause is the default cubemap skybox shader darkens the sky ever so slightly, and this wasn't visible with my test assets.

    Using a simple gradient more like what you're doing does show the problem.

    Left: Custom cubemap skybox shader (really just an unlit Shader Graph that samples the cubemap and does nothing else to it).
    Right: Builtin cubemap skybox shader. There's a faint, but visible line where the wall hits the far clip that isn't there. The wall does look brighter on the right image, but this is just an optical illusion.
    upload_2022-8-12_13-53-14.png

    Here's what I'm using for a skybox. Unity will complain that it's not a skybox material, but it works anyway.
    upload_2022-8-12_13-55-32.png
     
  21. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    I was able to get the "skybox fog" looking better.. though curiously, the clipping was little less severe once I changed the colorspace in my project settings from "linear" to "gamma"



    Though I have a few questions:
    1 - I looked into the view vector node and it seems to only be available in new Unity/Shadergraph package, which in this case was Unity 2021.3.1f1. I'm a little more comfortable working with Unity 2019 and since View Direction node is normalized by default, how would I.. say get unnormalized values (if that makes any sense)?

    2 - What exactly is the power node doing?

    3 - How would I make the objects start to dither as I get farther away from them?

    And for fun, can you show a video of how the shader works on your end? :)
     
  22. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Doesn’t actually matter for the cube map sampler. Either one will work, normalized or not.

    Otherwise world position - camera position.

    Applying a power to the linear falloff to shape the look of the fog. You can make the fog look lighter or heavier without adjusting the near and far distances. Though pushing the start of the fog further away than the near can also be useful depending on what look you want.

    There’s a built in dither node… you can use that. Though it depends on what exactly you’re trying to do. If you want something like dithered opacity, calculate an alpha based on the distance the object is from the camera (either using the world position node, or object position node) and use that inverse lerp node to set a near and far distance for it to fade out at. Then plug that value into the dither node to get an alpha to use, or for fading the object into the fog.

    If you want it to be dithered like it has a low color bit depth, that’s a much more in-depth shader, and likely something you want to do as a post process rather than in the object shader.
     
  23. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    @bgolus
    You were right about the gradient sky. With a skybox cubemap that is a solid color, for example, the clipping isn't as visible.. especially if set to black or dark grey.
    Curiously, I saw it mentioned in a topic that the KinoFog/FadeToSkybox had an option for radial fog, but I dunno if Kino did that via shader or scripting.
    radi.png

    Similarly, the old built-in VertexLit shaders are said to have radial, but dunno how that was achieved. Would it be possible to make the skybox fog radial?
     
  24. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    It already is radial.
     
  25. link0016

    link0016

    Joined:
    Mar 12, 2022
    Posts:
    28
    At this point, I'd say as long as the cubemap is configured correctly or is set to a solid color, the far clipping is hidden.
    Will mark as 'resolved', but will message you if anything else comes up @bgolus

    Thanks for your help and patience :)