Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Depth of field control please for the love of god!

Discussion in 'Cinemachine' started by knocking, Jul 18, 2017.

  1. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    I've posted couple of times already but maybe this is the correct thread area.

    How can I control animate depth of field and/or post process profile parameters correctly with timeline editor.

    How is this not an option?

    Bear with me i'm new with unity but not new to this field.

    We wrote some custom scripts to expose post profile parameters to be able to animate them inside 'animation', but we are still running into problems when it comes to playing back these animation inside the timeline editor with everything else. Just the fact that I need the framing to be composed in order to work the depth of field is another problem, can't do both at the same time.

    There must be a simpler way to do this simple task.

    Everything is becoming a guess work and not so intuitive.

    Don't need an auto focus script either I want to be able to manually animate the depth of field at a given frame or time inside a cut scene built within timeline editor.

    How are you guys approaching this issue?
     
    unity_l2Lg3ifHv6pkkw likes this.
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @knocking Thing is, the PostProcessing profiles are assets, and the timeline editor is not designed to animate assets - only GameObjecds.

    However, I think you can probably make a simple workaround. Create a behavior that sets the DOF in OnUpdate, according to a parmeter it exposes. Then animate that parameter from Timeline. You can animate any field in any behaviour by creating an animation track in Timeline.
     
  3. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    @Gregoryl Thanks for the reply I appreciate it.

    The fact that we are controlling an asset this way when it comes to the post process profile and especially depth of field is in my humble opinion the problem (please correct me if i'm wrong).

    1 - You need to write a custom script to expose the parameters of the post process in order for it to work inside the animation, this is already an issue of time and otherwise. As the script needs to work both in editor and in game in order to visualize the shot and frame it before seeing results, it has proven to be unstable so far.

    2 - You need to lock the timeline editor after creating it in order to be able to scrub the timeframe so you can frame and compose the shot while you select other elements in the editor (such as the post process profile to manipulate it), but by doing so this seems to disable Auto keying of any parameter that was exposed by the script for the post process profile, creating more workarounds by forcing us to copy paste values manually on an animation track.

    3 - The final animation track with the keyed elements of the post process track randomly chooses to work or not inside the timeline editor. (this may be due to the way our script is exposing/recording things, currently but we are still looking into it).

    In conclusion, what is supposed to have been a simple straight forward task inside a new editor hailed to provide 'cinematic' controls has turned into almost a week of back and forth without a solution so far over one of the most basic important features of a movie making process (the depth of field).

    We will test things further but i thought to let this known here for now for any help/guidelines or even docs are appreciated.

    I will try to update here if we find any solutions that work.

    Thank you.
     
  4. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    Quick update: We managed to make this work with our own script that can update the editor view and the gameplay at the same time, this allows us to frame the shot and amend parameters at the same time for the timing. Unfortunately the process is still clunky and slow. I hope Unity devs would address this and come up with a smoother approach out of the box in the future.
     
  5. Adam_Myhill

    Adam_Myhill

    Joined:
    Dec 22, 2016
    Posts:
    342
    @knocking - are you using Cinemachine? If so it's dead easy. We agree that DOF is a main cinematic feature and it works well.

    Install Cinemachine
    Install Post Processing Stack V1
    Put a Post Processing Behaviour component on the main camera
    Put a Cinemachine Post FX component on the main camera
    upload_2017-7-19_10-51-3.png

    Put the same Cinemachine Post FX component on any Cinemachine virtual camera
    upload_2017-7-19_10-52-0.png

    Put whatever Post Processing Stack profiles on each Cinemachine virtual camera - adjust any PPS value per shot!

    Look - there's Focus Offset !

    Drag that virtual camera onto Timeline and you can now animate the offset
    upload_2017-7-19_10-54-59.png

    We designed it this way because you usually want to focus on a subject and the offset is used for little rack focuses or to add an organic bit of 'seek' to the focus to make it look more realistic.

    If you want to do huge rack focuses, simply blend between two virtual cameras with different targets.

    Trust me, we've thought about this a lot :)



     
  6. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    Thanks a ton guys truly appreciate it!

    I didn't try Cinemachine yet, I could find little documentation about it (I guess all the auto tracking camera things put me off, we prefer to have our own animations imported from other software), I will try this soonest possible and let you guys know.
     
    unity_l2Lg3ifHv6pkkw likes this.
  7. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    Hi, sorry to keep bothering you all with this, but i'm running into a problem with Cinemachine from the start, when i create a virtual camera it seems to 'override' my main imported camera which already contains animation and then centers it. Is there a way to make this work with an already animated camera?
     
  8. Adam_Myhill

    Adam_Myhill

    Joined:
    Dec 22, 2016
    Posts:
    342
    Yes, just turn the Cinemachine virtual cameras off. When you turn on a virtual camera, it will use the Default Blend to blend from the Main Camera to a CM virtual camera. You can set up custom blends in the Custom Blend area to setup specific blends to and from any camera

    upload_2017-7-19_14-12-46.png

    It's overriding because it's active - meaning it's using the new virtual camera instead of your old one because you've asked for a new camera to play. If you want your old camera, don't turn new ones on.
     
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    CM does come with a little adapter script designed to expose non-CM cameras to the CM system as virtual cameras. Instead of creating a VirtualCamera, try add a CinemachineExternalCamera behaviour to your externally-animated camera.

    Be careful, however. A paradigm shift is involved. There needs to be a separate Main camera, with a CinemachineBrain on it, that will position itself to match the currently active Virtual Camera. Your externally-animated camera will never be an active camera - instead, it will act as a virtual camera and transmit its state info to the Main camera via the CinemachineExternalCamera script.
     
    Alverik likes this.
  10. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    I still have a few things that i'm struggling with.

    I managed to link up the external camera to a virtual one, but i'm unable to make the post process focus offset work. Another question would be as to how can I animate the other settings from directly inside the post process stack such as exposure/color grade with this approach?

    Here's a quick 1 min video, it shows what i did so far in the scene and where i got stuck.



    Thanks.
     
  11. JakubSmaga

    JakubSmaga

    Joined:
    Aug 5, 2015
    Posts:
    417
    It's because you have 2 cameras with the Cinemachine components and it uses the settings from the first camera, just create one Main camera and then virtual cameras from Cinemachine dropdown, then add CinemachinePostFX component, edit focus settings in the PostProcessing asset and it should work fine.
     
  12. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    Hi Jacob,

    I have a specific situation and was following what @Gregoryl mentioned.

    I understood that I have to have an external camera script on my FBX imported Cam: done

    I need to create another camera and assign brain, post process behavior and post process fx scripts on it (this acts as main camera now following FBX cam): done

    How to animate focus settings/offset or animate color grade settings from here: lost me
    --------------

    Side note: If I create a normal camera and assign Virtual cameras to it, Focus offset would require me to pick a look at target for it to work at all (correct me if i'm wrong but this is what i discovered so far), this works but this also forces the camera to follow that object all the time which i don't want.

    I just need my own animations on the camera and just keyframe control over depth of field and color grade inside timeline editor.
     
    Last edited: Jul 20, 2017
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @knocking If you're doing your own camera animations then Cinemachine is overkill for your purposes. You just want to animate DOF from the timeline. The simplest way to do that is to do what I initially suggested: a behaviour that sets the DOF based on a field, then animate that field from the timeline.

    PostProcessingProfile p;
    p.depthOfField.settings.focusDistance = bla;
     
    DChap likes this.
  14. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    Thanks for the help, I agree Cinemachine isn't the way to go for me which is why i never looked at it in the first place until it was mentioned here, i think what you suggested is what we had done with our script (dev isn't here at the moment to clarify) but i noted it wasn't working in editor mode correctly (which is required if i need to use timeline to work on the framed shots). So what i ended doing is installed the latest Beta of the post processing stack and that one has volumes trigger. With that i was able to blend between different states of the post.

    I really wish there would be a more straight forward approach without any scripts in the future (coming from a pure art side of things).

    Thanks again for your time.
     
    dursteric likes this.
  15. Adam_Myhill

    Adam_Myhill

    Joined:
    Dec 22, 2016
    Posts:
    342
    @knocking - one thing - embracing the power of game engines, instant iteration and seeing everything in context. Imagine a world where you don't have to go back to your 3D program to fix/change/make new cameras.

    Gotta throw that little plug in there!
     
    Alverik likes this.
  16. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    @Adam I understand, and I agree, however please keep in mind that sometimes procedural or auto tracking or even blending is not enough. For many of us (especially coming from animation/film backgrounds) it is preferable to have 100% control over certain animations especially in cut scenes, subtleties in motion can be everything in many such situations, which is why we prefer to hand animate or sometimes even mocap camera data and amend it later very carefully before importing it to the engine. It assures the look we would be after.

    My feedback so far from what i've seen is to simply be able to expose the post effects and animation controls via user friendly ways inside the editor without scripts. So that imported motions can work well, this alongside what you have going for Cinemachine and the new post process stack can be very powerful.

    Cheers.
     
    Last edited: Jul 20, 2017
  17. Roy-Massaad1

    Roy-Massaad1

    Joined:
    Jun 30, 2015
    Posts:
    16
    As the software developer working with Knocking, i approve of this thread.. minus all the marketing :p

    A glass of Jack Daniels to everybody, cheers !
     
  18. Adam_Myhill

    Adam_Myhill

    Joined:
    Dec 22, 2016
    Posts:
    342
    Totally with you @knocking No stranger to motion captured cameras myself, love love the texture and realism they bring. Love them so much that we flatter it by trying to emulate them.

    We are working on improving your situation. The Post Processing Stack V2 will have greatly improved Timeline integrations. Your demands are right on target with how we want things to work and I'll keep you posted.
     
  19. knocking

    knocking

    Joined:
    Apr 10, 2014
    Posts:
    26
    Thank you very much guys! We truly value and appreciate your hard work and support to the community here especially to some of us newcomers.

    Looking forward to the new features.
     
    Adam_Myhill likes this.
  20. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    328
    hey @Adam_Myhill -
    so I'm still a touch confused about this. I have two CM VCams, one has "Focus Tracks Target" on, and the other doesn't. My idea was to animate the "Focus Offset" param, and bring the object into focus, but the value seems to do nothing. I've gone from -10000 to +10000 with no change in DoF. What gives?
     
  21. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You need also to have a CinemachinePostFX behaviour on the Unity camera. In addition, the PostFX on the vcam with the follow focus needs a PostProcessing profile that includes depth of field.
     
  22. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    328
    @Gregoryl I had that - apparently my F-stop wasn't low enough to produce the desired effect. Thanks!
     
  23. headlessstudio

    headlessstudio

    Joined:
    Feb 25, 2016
    Posts:
    83
    I'm struggling to grasp this. Not sure why it's so over complicated, couldnt the PPSv2 simply have a focus on target and then on the camera (virtual or not) itself have a focus value that you could change per camera? Do I really need to use Timeline to animate my focus values? What if I'm just transitioning between cameras and all is kind of procedural? I don't get it.
     
    dursteric likes this.
  24. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The ppv2 profile has a focus depth setting, and the PostProcessing CM extension has a focus follows target setting, which dynamically updates the focus depth. That should give you what you’re looking for.
     
    headlessstudio likes this.
  25. headlessstudio

    headlessstudio

    Joined:
    Feb 25, 2016
    Posts:
    83
    Yes that is true, but that means I need to have a target and honestly I don't want one. I want a free camera (3dsmax lingo). I've managed to work around this by using the camera transform as a follow target but I'm pretty sure that's not the intended way and will mostly screw something up along the way.
     
  26. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    If you don’t want a target that’s fine. In that case focus follows target makes no sense, and you can instead just give each vcam its own fixed focus distance in its pp profile. If you want this to animate, you can blend between vcams with different values.
     
  27. DINKmod

    DINKmod

    Joined:
    Jan 25, 2017
    Posts:
    14
    I'm trying to animate my DOF like the OP. (I tried PostProcessingV2 and it seems to be broken on import, and it also seems to break another script I need for my scene.)

    But that don't work. Of course I don't know much about scripting soooo, that's probably it.
     
    Last edited: Dec 8, 2017
  28. redemprez

    redemprez

    Joined:
    Aug 14, 2017
    Posts:
    29
    Why complicating such a simple concept as free camera with animated DOF though aperture slider? I don't want to set a target, I don't want to blend two cameras. I just want to animate Post Processing sliders and switches. Is it possible in Post Processing v2?
     
    dursteric likes this.
  29. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    The problem here is that post-processing profiles are assets, and the sliders manipulate shared values in the assets, and not fields on game objects. That's why you can't animate them.
    Because focus pulling is such a common need, Cinemachine has added a special case for it in the CinemachinePostProcessing extension. You can animate the "Focus follows target" offset found there. But it is a special case.
     
  30. Adam_Myhill

    Adam_Myhill

    Joined:
    Dec 22, 2016
    Posts:
    342
    @redemprez you can make an empty object be your focus target, put it under the vcam in your hierarchy, set the camera to look at it and only keyframe it on the Z axis. Voila, custom focus handle.
     
  31. redemprez

    redemprez

    Joined:
    Aug 14, 2017
    Posts:
    29
    Hi, Adam, thanks for the hint, indeed. I can do it. I also found that using third party Beautify DOF I can animate it in timeline, I will check which solution works better for me.
     
    Adam_Myhill likes this.
  32. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    There is a way to do it through code. You have to create a post processing profile (it'll create itself as a file in your asset directory). Then you attach it to your main camera, and then you can override it's settings in C# code.

    It's probably not advised though. For my project in particular I just needed depth of field resistance, bloom and to create color grading profiles, very simple stuff. For most games, though, you'll want it to be less simplified for more control.
     
  33. mradfo21

    mradfo21

    Joined:
    May 16, 2013
    Posts:
    194
  34. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    I don't know why you're writing code. Just add a PostPorcessLayer component to your Camera in the editor.

    Then create a default global volume for the scene by adding a PostProcessVolume to an empty GameObject and setting a profile with its effects. Do all this in the editor, not in code.

    Finally, add a PostProcessing Extension to your vcams, and create a profile for them with the effects you want. These effects will take over while the vcam is active. If you don't see that extension in the Extensions menu, then you need to import the CM PostProcessing V2 adapter package, available in the Cinemachine menu
     
  35. mradfo21

    mradfo21

    Joined:
    May 16, 2013
    Posts:
    194
    S***.. i thought this hadn't posted or I would have replied with a follow up


    code snippet for racking focus dynamically


    Code (CSharp):
    1.             DepthOfField dofSettings = postProcessingProfile.GetSetting <DepthOfField>();
    2.             dofSettings.focusDistance.value = Mathf.Lerp(dofSettings.focusDistance.value, Vector3.Distance(Camera.main.gameObject.transform.position, focusTarget.transform.position), Time.deltaTime *4f);
    3.         }
    as to why, I've got a dynamic dialogue system that racks focus between two targets. I have the adapter package installed but dont have a PostProcessingExtension component available unfortunately.

    things are working great with this though!
     
  36. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    This is what my code looks like, but like @Gregoryl said it's very rare you'd want to do something like this.

    The reason I did it is that I have a single custom inspector in where I control all of the effects and lighting for my game at a single place. I can use a single instance of a 'SceneSettings' class and then pass lighting information from one scene to another to emulate time of day across multiple scenes.

    I'm simplifying the effects though dramatically by having a single bool and slider for depth of field, color grading has like a drop down enum menu with about 6 or so settings like day, night, dusk, dawn, dream etc... and bloom just has an intensity slider.

    I'm able to adjust bloom, depth of field and color grading settings then in real time while the scene is running to get the lighting just how I want it. I consider it like a custom lighting panel:


    compound formula for ammonia

    The following code is used within Update() of my own scene script, which is attached to a scene game object that is active in the hierarchy (it could be just attached to your main camera):

    Code (CSharp):
    1.  
    2. //You get the post processing profile from the camera the 'post processing behaviour' script is attached to:
    3.  
    4. PostProcessingProfile postProcessingProfile = mg.MG_Camera.GetComponent<PostProcessingBehaviour>().profile;
    5.  
    6. //Depth Of Field Model instance:
    7.             DepthOfFieldModel depthOfField = postProcessingProfile.depthOfField;
    8.  
    9. //Depth of field settings instance:
    10.             DepthOfFieldModel.Settings depthOfFieldSettings = postProcessingProfile.depthOfField.settings;
    11.  
    12.  
    13. //settings.graphics.DepthOfFieldOn is my own class and subclass:
    14.             if (settings.graphics.DepthOfFieldOn)
    15.             {
    16.  
    17. //depthOfField is the instance of DepthOfFieldModel above:
    18.                 if (depthOfField.enabled != true)
    19.                 {
    20.                     depthOfField.enabled = true;
    21.                     postProcessingProfile.depthOfField = depthOfField;
    22.                 }
    23.                 depthOfFieldSettings.focusDistance = settings.graphics.DepthOfFieldResistance;
    24.                 postProcessingProfile.depthOfField.settings = depthOfFieldSettings; //< -- apply it
    25.             }
    26.             else
    27.             {
    28.                 if (depthOfField.enabled == true)
    29.                 {
    30.                     depthOfField.enabled = false;
    31.                     postProcessingProfile.depthOfField = depthOfField;
    32.                 }
    33.             }

    The method within the custom inspector script corresponding to the scene script this is all in looks like this:

    Code (csharp):
    1.  
    2.    private void DisplaySettings_DepthOfField()
    3.     {
    4.         __BgnV("box");
    5.         __BgnH("");
    6.         EditorGUILayout.LabelField("Depth Of Field On:");
    7.         s_Scene.settings.graphics.DepthOfFieldOn = EditorGUILayout.Toggle(s_Scene.settings.graphics.DepthOfFieldOn);
    8.         __EndH();
    9.         if (s_Scene.settings.graphics.DepthOfFieldOn)
    10.         {
    11.             DisplaySettings_DepthOfField_Options();
    12.         }
    13.         __EndV();
    14.    
    15.     }
    16.  
    17.    private void DisplaySettings_DepthOfField_Options()
    18.     {
    19.         __BgnH("");
    20.         EditorGUILayout.LabelField("Depth of Field Resistance:");
    21.         s_Scene.settings.graphics.DepthOfFieldResistance = EditorGUILayout.Slider(s_Scene.settings.graphics.DepthOfFieldResistance, 0f, 1f);
    22.         __EndH();
    23.     }
    24.  
    25.  
    26. //BgnV and EndV are just BeginHorizonal and EndHorizontal or whatever it was:
    27.    private void __BgnV(string options)
    28.     {
    29.         EditorGUILayout.BeginVertical(options);
    30.     }
    31.     private void __EndV()
    32.     {
    33.         EditorGUILayout.EndVertical();
    34.     }
    35.     private void __BgnH(string options)
    36.     {
    37.         EditorGUILayout.BeginHorizontal(options);
    38.     }
    39.     private void __EndH()
    40.     {
    41.         EditorGUILayout.EndHorizontal();
    42.     }
    43.  
    And this allows me to adjust lighting settings across scenes in one panel like so:

     
    Last edited: Sep 15, 2018
  37. Xander-Davis

    Xander-Davis

    Joined:
    Apr 23, 2011
    Posts:
    441
    Pro-tip: After trial and error for hours, apparently Look-At DOF on a Vcam works only on Play (as in press Play at the top center of the Editor to run the game in it). NOT reflected when just scrubbing through or hitting playback in the Timeline to test.

    .............................................

    ......


    ..
     
  38. veselekov

    veselekov

    Joined:
    Jan 5, 2014
    Posts:
    32
    salex1 likes this.
  39. Rensoburro_Taki

    Rensoburro_Taki

    Joined:
    Sep 2, 2011
    Posts:
    274
    Ok reading the whole forum brings me only to one conclusion: To ask again, since we have 2022 and I too have the same problem to not be able to record dof fokus via transform target in timeline, by using dof from post processing stack > package manager > Unity 2021.LTS.

    So how would a full working use case to get it working?
     
  40. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    What render pipeline are you using? HDRP or URP or builtin?
     
  41. Rensoburro_Taki

    Rensoburro_Taki

    Joined:
    Sep 2, 2011
    Posts:
    274
    Hello! =) I use builtin
     
  42. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    In that case, you need to use the PostProcessing stack, with CinemachinePostPorcessing extension on the vcam. You can control the focus distance directly from Timeline, by setting the Focus Tracking to Camera, and animating the Focus Offset, which in this case becomes the offset from the camera position to the focus point - in other words, the focus distance.

    upload_2022-11-16_18-28-59.png

    Alternatively, you could set the Focus Tracking to the LookAt or Follow target, or to some custom object. In that case, the focus offset is the offset from that object's position to the focus point.

    Note that the "Focus Distance" setting in the DoF asset (set to 10 in the image) is not used - it is ignored and the extension's own setting is used instead.

    You'll need a PostProcessing Layer on the main camera as well.
     
    Last edited: Nov 17, 2022
    Rensoburro_Taki likes this.
  43. Rensoburro_Taki

    Rensoburro_Taki

    Joined:
    Sep 2, 2011
    Posts:
    274
    Thanks a lot!
     
  44. SolidK

    SolidK

    Joined:
    Jun 27, 2019
    Posts:
    10
    Hey, so how would one do Focus Tracking in HDRP 2022? I'm kinda lost on that.
     
  45. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Use CinemachineVolumeSettings instead of CinemachinePostProcessing.
     
  46. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    If you install Cinemachine in a URP project, you should have this option in the vcam's Extensions dropdown:

    upload_2023-8-4_10-14-44.png