Search Unity

How do you make Post Processing Stack Depth of Field auto-focus on an object?

Discussion in 'Image Effects' started by Kmausser, Jun 3, 2017.

  1. Kmausser

    Kmausser

    Joined:
    Feb 17, 2016
    Posts:
    23
    How do I make it focus on whatever is the nearest object to the camera.
     
    soleron likes this.
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Check out this repository: https://github.com/keijiro/PostProcessingUtilities

    Then you need to modify the script to shot a ray from the camera to see what the object in front of the camera is and what's its depth is.
    I recommend using Physics.RaycastNonAlloc in order to avoid garbage.
    Here is a part of my script that should point you in the right direction:
    Code (CSharp):
    1. private void CalculateDistanceToVisibleObject()
    2. {
    3.     ray.origin = transform.position;
    4.     ray.direction = transform.forward;
    5.     hits = Physics.RaycastNonAlloc(ray, results, maxDistance);
    6.     var distance = (hits > 0) ? results[0].distance : maxDistance;
    7.     targetPosition = transform.position + transform.forward * distance;
    8. }
     
    Alex_F, Ony and Kmausser like this.
  3. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    I still remember the days when we could JUST drag/assign a target object in inspector or via script....
     
    soleron and Stardog like this.
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    You can still use the older effects. The DoF effects work fine in 2018.1 with the newer stack, plus, you get sun shafts.
     
    Ony and TooManySugar like this.
  5. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    good to know it works!. The thing is that using non stac FX does not allow theese to run in the correct order, hope using DOF befor or after the stack does not break anything. Sunshafts is also something I miss but I found a dev implemented theese as stack extension. (available on the store).
     
  6. josterday

    josterday

    Joined:
    Jan 22, 2018
    Posts:
    51
    Curious, which store asset are you using? Thanks!
     
  7. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
  8. martinnancy385

    martinnancy385

    Joined:
    Aug 25, 2018
    Posts:
    1
    Let me try this one, having the same issue though. Will visit the site. Having my own script to resolve the problem doesn't help at all. Thanks to this. Go2top.
     
  9. soleron

    soleron

    Joined:
    Apr 21, 2013
    Posts:
    582
    Best would be to be able to just drag and drop or select something from a list. OR in a smarter tool where you could provide a list of objet that would always become the focus when you are in proximity. That could be part of the Cinemachine if it isn't already.
     
  10. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    I confirm the old script still work son 2018.2 and on Xbox One. Unity is doing crazy things overall, now they've *ucked up the whole prefab system.