Search Unity

adjusting depth of field and blum settings

Discussion in 'Getting Started' started by TeddyGad, Jun 19, 2018.

  1. TeddyGad

    TeddyGad

    Joined:
    Sep 12, 2017
    Posts:
    74
    I'm using 2018 and installed the post process stack, and all the effects are working fine except for depth of field and blum. No matter what settings I try, it only blurs the entire image and I'm merely trying to blur the background. I looked at the Unity "info" which is useless. When I activate Blum it affects only certain materials, and not other materials, so is there a Material setting I'm overlooking? I'm using Standard materials and Real Time lighting for now.
    Thanks for any input
     
    Pawciu likes this.
  2. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hi TeddyGad,

    When you say "Blur the background" what Aperture (f-stop) settings are you using on the stack? How far is the object from the background? What is the focal distance set to? [feel free to stick a screenshot of your post processing stack settings on here]

    It helps if you have some experience (just a little) working with cameras that have an adjustable Aperture (or f-stop).

    Here's some tips to help with the Depth Of Field right off of the bat.

    1. If you're Aperture/F-stop value is a low number (say f1.7 for example) the you're depth of field (the range that items will be in focus) will be narrows than if you have a high value (for example f11).
    2. If the target you want sharp is at, for example, 10meters away and you have your focal distance set to 1m then you will most probably not have a sharp image (except for anything that is 1m away from the camera). FYI I think you haven't adjusted the focal point and so the item you want to be in focus simply won't be... I'll show you a script later to help with this.
    3. If you adjust only the focus (and leave the fstop/aperture where it is) you will notice that your depth of field decreases as you focus on items closer to the camera (not much will be in focus. But as you focus on further items away you will notice that more is in focus as the depth of field increase.
    In my game (The Attraction Distraction <- shameless plug :p But please check it out to see what I'm on about) I allow the player to adjust the f-stop meaning that they can choose how much they want in focus.

    Further more, as the player zooms the camera closer to (and farther from) the ball the amount that's in focus changes because the focal point is shifting (above point 3 shows that as the camera gets closer to the focal point/subject the depth of field changes - providing you remember to change the focal depth accordingly).

    Here's a script that I run to keep one item in focus as it moves withing the scene:
    Code (CSharp):
    1. // This script CAN sit on the camera directly, bit you want more encapsulation to protect the post processing profile
    2. public GameObject subjectToStayInFocus;
    3. private PostProcessingProfile ppProfile;
    4.  
    5. // some lines of code later
    6.  
    7.    public void Update()
    8.    {
    9.       UpdateCamDistance(Vector3.Distance(subjectToStayInFocus.transform.position, transform.position));
    10.    }
    11.  
    12.     public void UpdateCamDistance(float distance)
    13.     {
    14.         if (ppProfile.depthOfField.enabled)
    15.         {
    16.             DepthOfFieldModel.Settings newSettings = ppProfile.depthOfField.settings;
    17.             newSettings.focusDistance = distance;
    18.             ppProfile.depthOfField.settings = newSettings;
    19.         }
    20.     }
     
    PPLorux likes this.
  3. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    As for the bloom, this will only really be noticeable on brighter objects or those that are emitting light (dumbing it down a bit... but you get the basic idea). What were you hoping to have 'bloom'?
     
  4. TeddyGad

    TeddyGad

    Joined:
    Sep 12, 2017
    Posts:
    74
    Thanks- here's the scene and I just want to blur the background as the FPC roams thru the scene. I'll try your settings to see if that helps. On the blum, I initially was getting some nice light glints on the shiny metal surfaces, but then it stopped appearing for some reason and just affected the headlight glass and top of the orange body work. The other post processing affects like vignette and color correction are working fine though-
     

    Attached Files:

  5. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Is that a scene you've made? Or did you get it from the asset store?
     
  6. TeddyGad

    TeddyGad

    Joined:
    Sep 12, 2017
    Posts:
    74
    It's a model I made in Maya- I'm new to Unity but really impressed/overwhelmed by its capabilities
     
  7. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hi teddy,

    Okay, I threw together a quick scene to show how I have mine set up. Hopefully it will help if you're still having issues.

    NOTE: Although I'm using the Post Processing Stack that's available straight off of the Asset store, I'm also using Unity 2018.1.5f1. The original post processing stack is using some outdated API's, but still works atmo.

    First off, here's some pics showing off the depth of field and the bloom at work.
    DofTest1.PNG

    Notice how the bloom is on (too much, but it's there to make a point) and how the edges of the cube are nice and sharp, whereas the background is soft.


    And when I angle the camera slightly it switches because the distance I'm focussing at has changed:
    Doftest2.PNG

    I'll show you the script in a moment. But notice my setup is just a (really derpy, hacked together) movement script, and them my camera is a child of this.

    The camera has 2 scripts on it: the Post Processing Behaviour, and a custom "DOF Adjuster" script, which will be posted below - that's all you need. DofTest3.PNG

    The script works by firing out a raycast each frame, and setting the focal distance for the Post Processor to the distance between the camera and where the raycast hits. Here's the script in it's entirety:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.PostProcessing;
    5.  
    6. public class DOFAdjuster : MonoBehaviour {
    7.  
    8.     public PostProcessingBehaviour ppBehaviour;
    9.  
    10.     private PostProcessingProfile ppProfile;
    11.     private DepthOfFieldModel.Settings dofModelSettings;
    12.     private DOFAdjuster dofAdjuster;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         ppProfile = ppBehaviour.profile;
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.         RaycastHit hitInfo;
    23.  
    24.         if (Physics.Raycast(transform.position, transform.forward, out hitInfo, 100f))
    25.         {
    26.  
    27.             dofModelSettings = ppProfile.depthOfField.settings;
    28.  
    29.             dofModelSettings.focusDistance = Vector3.Distance(hitInfo.point, transform.position);
    30.  
    31.             ppProfile.depthOfField.settings = dofModelSettings;
    32.  
    33.         }
    34.     }
    35. }
    36.  

    Thinking about it, when the camera switch the DOF change is instant which can be a little jarring - I'd recommend you simply LERP between the current dofModelSettings.focusDistance and the new one from the raycast as this will provide a much nicer and smoother feel.

    NOTE: To be able to edit the post processor you can't simply access JUST the distance variable. You have to get a reference to the whole DOFAdjuster.Settings component and adjust it from there.

    IMPORTANT NOTE: Since this is done via references and not copies. Any changes you make in the code WILL change the PostProcessingProfile itself.

    Here's some screenshots of my Post Processing Profile:
    DofTest5.PNG

    Only DOF and Bloom are on. I'm using a very wide aperture of 1.7 here (my wife uses a lense with this, it's nice for blurriness). Increasing this number will decrease the Depth Of Field effect. I only adjusted the Aperture (f-stop).

    DofTest6.PNG

    The only thing I adjusted here was the intensity.

    Anyway, I hope this helps. Let us know it you sort it out how you want it.

    Mike
     
  8. TeddyGad

    TeddyGad

    Joined:
    Sep 12, 2017
    Posts:
    74
    Thanks again for your help- once I changed the lighting mode to Linear, I got much better Blum effects- for the DOF, I downloaded the free Bokeh from the asset store- the DOF from the PPS didn't work for me no mater what settings I tried
     
  9. UXeyeway

    UXeyeway

    Joined:
    Jan 22, 2018
    Posts:
    10
    I'm having the same trouble as well. no matter what values I use I see no effect on the image. using the debug option I see that that in order to see actual distance values, I need to change the scale to 0.001. I'm assuming the scale is what causing the problem, but I'm not sure how to fix this.
     

    Attached Files:

    • DOF.JPG
      DOF.JPG
      File size:
      74.5 KB
      Views:
      1,176
  10. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hey guys,

    I'm so sorry, I haven't been on the forum recently - my bad.

    Hmm, have you tried setting the cameras render path to it's different options?
    Also, in the documentation here right down at the bottom, that for DOF to work it requires GPU's that can handle Shader 3.0 (Directx 9.0C and upwards, apparently) and Depth Textures... which requires Direct3D 11+ enabled GPU's, such as these Nvidia cards.

    Can I ask what OS, DirectX version and GPU's you 2 are running?

    Regards,

    Mike
     
  11. Sludgemonster

    Sludgemonster

    Joined:
    Mar 25, 2019
    Posts:
    1
    This is a top result when searching for "Depth of Field not working." Make sure your "Post Process Layer" component has a Trigger set to the right camera and make sure the Layer field is correct. Also put your camera on the same exact layer as whatever you set your "Post Process Layer" Layer to. Once you set everything up, you may need to go back to the "Post Process Layer" component and set the camera again for the settings to take effect (I had to click "This" again for it to work for me).
     
    Tset_Tsyung likes this.
  12. TorbenDK

    TorbenDK

    Joined:
    Jun 28, 2016
    Posts:
    24
    To adjust the Post Processing Effect to only apply depth of field to the background:

    1. Navigate the Project hierarchy to Post Processing>PostProcessing>Shaders>Builtins
    2. Doubleclick on DepthOfField.hlsl (Looks like a text icon)
    3. In the shadercode, add if (coc<0) coc = 0; to line 32.

    Or in other words, in DepthOfField.hlsl change this:
    Code (CSharp):
    1. // CoC calculation
    2. half4 FragCoC(VaryingsDefault i) : SV_Target
    3. {
    4.     float depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoordStereo));
    5.     half coc = (depth - _Distance) * _LensCoeff / max(depth, 1e-4);
    6.     return saturate(coc * 0.5 * _RcpMaxCoC + 0.5);
    7. }
    To this:

    Code (CSharp):
    1. // CoC calculation
    2. half4 FragCoC(VaryingsDefault i) : SV_Target
    3. {
    4.     float depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoordStereo));
    5.     half coc = (depth - _Distance) * _LensCoeff / max(depth, 1e-4);
    6.     if (coc<0) coc = 0;
    7.     return saturate(coc * 0.5 * _RcpMaxCoC + 0.5);
    8. }
    I'm using 2018... something final. For performance reasons.