Search Unity

Cinemachine VS DepthOfField what is going on here?

Discussion in 'Cinemachine' started by tcz8, Nov 16, 2019.

  1. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Hi everyone, I need your help.

    With the depth of field effect from the post processing stack v2 and "focus tracks target" enabled in the Cinemachine Post Processing script I get weird focusing issues.

    I posted a video to youtube showing the problem (easier to see it in full screen)


    If you look at the objects on the top left and at the one bottom left, you can see the focus going in and out. If we disable "Focus tracks target" in Cinemachine's post processing script it stops. If we disable Cinemachine's brain script on the camera it also stops. Cinemachine's tracked target is the player you see in the scene. We also tried adding a simple cube to the scene and setting it as the target to eliminate the player from the equation, and we get the same result.

    Any ideas?

    FYI I use the latest version of Cinemachine and the Post processing stack V2. Both installed using the package manager in Unity 2018.4.12f1.
     
  2. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    BUT WAIT! There is more to this problem.

    If I aim the camera in the -X or -Z direction (global coordinates), in addition to the focus problem, the ground's texture is moving! If looking in the +X or +Z it doesn't move.

    The ground object is just a default cube with a scale of x1000 y1 z1000 and an albedo texture added to the standard shader.

    Here is a video, once again, full screen helps a lot. The movement in the ground texture is subtle but you will see it!


    Disabling "Focus tracks target" fixes the DepthOfField's focusing problems but not the ground texture.

    Near the end of the video, I disable the "Cinemachine Brain" which fixes the moving texture problem.

    How do I fix this?

    EDIT

    Huh... Strangely disabling the player object also stops the ground from shaking but does nothing for the Depth of field.
     
    Last edited: Nov 17, 2019
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    What the CM PostProcessing extension does under the hood is to make a global PP volume containing the profile settings and set its weight to match that of the vcam - if the vcam isn't blending then that weight is 1. So the new volume is blended into the current volume settings for the camera. What Focus Tracks Target does is to dynamically adjust the Focus Distance setting in that volume.

    Turning off CM effectively eliminates these dynamic vcam volumes from the PostProcessing stack, so that they are no longer blended in.

    Do you get the same strangeness when you disable all the CM stuff, but add a global PP volume with the same profile that you put in the vcm PP extension? That would be worth a try, because it will eliminate CM from the equation. My guess is that you will still see the focus strangeness. If you do, then this question needs to be asked on the PP forum.

    Give it a try and let us know the result.
     
  4. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I will try it right now

    EDIT: Done.

    It's fixed when CM is disabled, and we use a Global Volume instead.

    Here is the video:
     
  5. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Here is another test I did:


    As the description says, I was investigating if tiny movements in CM's target which induce movement in the camera and CM's vcams is the cause of the DOF focusing issues. Later in the video I replace the target by a sphere that doesn't move and the problem still exists.
     
  6. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I found the source of the problem. I added a debug line to see what values the CM Post processing script was dealing with and did a video:


    When I pause the game you can see strange values interfering with the distance calculations. It appears the problem is with state.FinalPosition suddenly changing values.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Very interesting. Thanks for doing all that detective work. I'd like to understand why that state.FinalPosition is fluctuating. It's not something we normally see. Can you show me the inspector for the active vcam at the time of the fluctuating value? Is a blend in progress?
     
  8. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Interesting indeed, and you are welcome. It's the least I can do to help resolve this faster.

    I'm making the video you asked for, brb.

    EDIT: Here a video where I take you for a tour of all the CM scripts and the active cam
     
    Last edited: Nov 18, 2019
  9. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Thanks for the effort, but the video is of limited help. Still images of the inspectors pasted directly into a forum post are easier for me to process.

    Your setup is fairly complex, with state-driven cameras and things being animated. I need to get an understanding of the specific vcams involved, how they are set up, and how the target is animated. The state.FinalPosityion always fluctuating to a specific value is suggestive of multiple systems trying to drive things in a conflicting manner.

    Would you be able to reproduce this problem inside a very simple scene, with just cubes and spheres and the like? That would probably be the most help, because you could just send me the project and I could peek into it properly.
     
  10. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Just found something else. In your code the focus distance is obtained with:

    focusDistance += (state.FinalPosition - state.ReferenceLookAt).magnitude;

    Every time the value of state.FinalPosition fluctuates, focusDistance = 110.5474f no matter how zoomed in or out the focus of the DOF is.

    In other words, It jumps between the correct value (which changes with the zoom level) and that fixed value of 110.5474f at what almost seems like a regular interval.
     
  11. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Sure, if the vcam is bouncing around, so will the focus distance. The question is: why is the vcam bouncing around? I need to see how it's set up.
     
  12. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    When you say vcam do you mean the cinemachine freelook camera?

    EDIT
    From searching the hierarchy the vcams are actually in the top, middle and bottom rig of the freelook camera.

    Here are some screenshots of the entire settings:
    upload_2019-11-18_14-41-20.png
    upload_2019-11-18_14-41-52.png
    upload_2019-11-18_14-42-21.png
    upload_2019-11-18_14-42-59.png
     
    Last edited: Nov 18, 2019
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    I need to understand the whole pipeline, which seems to be (correct me if I'm wrong): FreeLook + StateDriven + Brain
     
  14. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    You are correct and at the moment the state came is locked so it will use that freelook cam only.

    Screenshots or the state driven cam and the brain are incoming.
     
  15. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Thanks. And the extensions? On the FreeLook and StateDriven.

    Also: can you try as an experiment to assign ReSkinPlayer2 as LookAt and Follow targets directly in the FreeLook? Right now they inherit from StateDriven.
     
  17. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I would also like to remind you that even if I set a basic sphere as the target I get the same problem. So I doubt it's related to how the player is animated.

    FreeLook has no extension loaded.
    StateDriven only uses the post processing script.

    Here are some screenshots of the post processing script setting:

    upload_2019-11-18_15-3-30.png

    upload_2019-11-18_15-4-5.png

    upload_2019-11-18_15-4-53.png

    upload_2019-11-18_15-5-26.png
     
  18. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Can you try as an experiment to bypass the State-Driven-Camera altogether? Disable it, move the extensions to the FreeLook, move the FreeLook out of the SDC's children (i.e give it no parent), and see if you still get the flicker
     
  19. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I tested with the target and look at set in the freelook.
    upload_2019-11-18_15-14-35.png

    Same focus problem.
     
  20. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    OK I will try that.
     
  21. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Ok, just did all that and the problem is gone.

    I'ts bypassing the state driven camera that fixed it. Moving the freelook cam out of its parent didn't change a thing.

    EDIT: It fixes the DOF focus bug but the ground is still shaking unless I disable Cinemachine's brain.
     
  22. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Interesting. We have not seen this before. Could you set up a quickie simple project that isolates this? I'd like to get a fix out for this.
     
  23. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Yes, I will try to make a stripped down version of our setup. How should I submit it?
     
  24. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Thank you, I appreciate that. You can just DM it to me.
     
  25. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    That took some major stripping... I sent you a message with a download link.
     
    MentalGames likes this.
  26. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Hey @tcz8 thank you very much for taking the time to prepare the project. We really appreciate it.

    So it's a nice little bug, which we will put in our queue to fix. The bug relates to incorrect handling of standby update, in the specific case where there is a PostProcessing extension with FocusTracksTarget enabled, attached to a manager vcam (such as StateDrivenCamera) with multiple children.

    Until the fix is out, the workaround is to set the StandbyUpdate mode of all the vcams inside the StateDrivenCamera to Never:

    upload_2019-11-19_17-40-17.png

    Thanks again for your efforts!
     
  27. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    No problem, glad I could help and thank you for looking into it this quickly.

    Not sure If we need Standby Update but if anyone does , another temporary fix is to filter the value in CinemachinePostProcessing.cs by caching the focusDistance and compare against the bad value. (which for me was always 110.5474f YMMV)


    Above "PostPipelineStageCallback" add previousFocusDistance
    Code (CSharp):
    1. float previousFocusDistance;
    2. protected override void PostPipelineStageCallback(
    Several lines of codes lower were focusDistance is set change things as follow:
    Code (CSharp):
    1. if (Mathf.Approximately(focusDistance, 110.5474f)) {
    2.     focusDistance = previousFocusDistance;
    3. } else {
    4.     previousFocusDistance = focusDistance;
    5. }
     
    Last edited: Nov 20, 2019
  28. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Hmmm, that's not such a great idea. The "bad" number is the correct focus distance for one of the other vcam children of the SDC, so checking for it in this manner is of very limited value. Much better to just turn off the standby update.
     
  29. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I just tried it and the bug is still there for me when I do that. I set it to "Never" in every cam I could.

    EDIT
    Actually, in the scene that I sent you it DOES fix the problem but not in my game. Wow... Going back to the stripping board.
     
    Last edited: Nov 22, 2019
  30. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I have figured out why your fix wasn't working for me. One of the Freelook cam was bugged.
    It was set to never update but apparently it didn't agree. I had to set it to always and then to never , again, to fix it.

    Just fixed it 3 times in a row in different builds just to confirm it was just that.

    I still have the bug with the ground shaking. Its kind of weird, when the target is a sphere, it shakes when we rotate the view and ends when the rotation is finished.

    If I have the player as cinemachine's target it settles a little when the rotation is over but it never stops like with the sphere.

    Once again, disabling cinemachin's brain fixes everything.
     
  31. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    @Gregoryl Hope you are still around,

    I cornered the bug as much as I could. I attached 2 videos bellow.

    One of them has the player as the Cinemachine target, the other one has a sphere.

    I tested several things here's what I found out:

    • In both video you can see the ground texture shaking. Only seems to happen when looking in the direction of negative X and Z in world coordinates.
    • When having the sphere as a target the shaking occurs but when the camera stops moving it settles after a few seconds (until you move the cam again)
    • It never settles when targeting the player
    • Removing the Rigid Body on the player partly fixes the problem, still shakes when rotating
    • Disabling the player's animator partly fixes the problem, still shakes when rotating
    • Disabling "Apply Root Motion" on the player animator partly fixes the problem, still shakes when rotating
    • Disabling Cinemachine's brain fixes the problem
    • Using a normal camera fixes the problem
    • Reducing the Tiling of the ground texture from 10000x10000 to 1000x1000 fixes the problem when using Cinemachine, not required with a normal cam
    Player as the target


    Sphere as the target


    I'm not sure what's going here. Lots of things appear interconnected. I was thinking it could be a bug with the huge tiling but I don't know anymore. And why the hell only in the negative X and Z? Also, At once point I had changed the scale of the ground to something like 100x100 and the tiling to 1000x1000 and strangely enough it's as if the shaking was suddenly limited to a smaller "10x10 or 15x15" square around the player But that happened only once, and I was unable to reproduce it.

    Please help me resolve this one.
    Thank you.
     
  32. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    @tcz8 can you try upgrading CM to the latest 2.4.0 preview? Does it fix the problem?
     
  33. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Hello Sorry I wasn't around,

    I'm going to test this and let you know.
     
  34. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I'm currently upgrading to version 2.40 preview 9 via the package manager. I will let you know what's going on shortly.

    BTW, I was looking at the docs for a section on best practices when upgrading CM but there was nothing, so I'm erasing CM from an empty scene and reinstalling it from there. If this is wrong please let me know.
     
  35. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    erasing it? Are you starting with an asset-store version? If not, an ordinary upgrade via package manager is the way to do it.
     
  36. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    @Gregoryl It's not working. I can't get in play mode since upgrading to 2.4, my project is stuck on the following error:

    Assembly has reference to non-existent assembly 'Unity.Timeline' (Packages/com.unity.cinemachine/Editor/com.unity.cinemachine.Editor.asmdef)

    I haven't done anything with assembly definition files in my project so I'm not sure what to do about this.
    Additionally, even though I can't get into play mode anymore, I can see the depth of field trying to focus non stop in the scene view and game cam.

    I would say its not fixed but I would like to confirm in play mode.
     
  37. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Wow there are many errors about Unity.Recorder.Editor being in conflict with CM types.

    The type 'CinemachineVirtualCameraBase' in 'f:\Unity Projects\ALPHA\Library\PackageCache\com.unity.cinemachine@2.4.0-preview.9\Runtime\Core\CinemachineVirtualCameraBase.cs' conflicts with the imported type 'CinemachineVirtualCameraBase' in 'Unity.Recorder.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'f:\Unity Projects\ALPHA\Library\PackageCache\com.unity.cinemachine@2.4.0-preview.9\Runtime\Core\CinemachineVirtualCameraBase.cs'. [Assembly-CSharp]csharp(CS0436)
    Peek Problem


    I think the recorder version I have may be from the store as the package manager wont allow uninstall it. I will replace it with the package manager version.
     
  38. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I removed Recorder (which I wasn't using anyways) reinstalled CM through the package manager and now it won't even install properly. I get a ton of errors as show below for pretty much all the CM scripts that matter:

    Script 'Packages/com.unity.cinemachine/Runtime/PostProcessing/CinemachineVolumeSettings.cs' will not be compiled because it exists outside the Assets folder and does not to belong to any assembly definition file.
    UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface:TickCompilationPipeline(EditorScriptCompilationOptions, BuildTargetGroup, BuildTarget)
     
  39. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    Uninstall the package AGAIN and reinstall it, now I get these errors:

    Assembly has reference to non-existent assembly 'Unity.Timeline' (Packages/com.unity.cinemachine/Runtime/com.unity.cinemachine.asmdef)

    Assembly has reference to non-existent assembly 'Unity.Timeline' (Packages/com.unity.cinemachine/Editor/com.unity.cinemachine.Editor.asmdef)
     
  40. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Sounds like you've got a mess on your hands.

    This is my fault: I should have mentioned that CM 2.4.0 is only compatible with the very latest 2018.4.13f1 and up. Sorry about that. Upgrade Unity and these problems will disappear.

    That is very strange. You get this sort of massage for many CM files? This points to a corruption in your project structure. Try upgrading Unity then re-importing all.
     
  41. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    More errors:

    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.Timeline'
    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.Timeline.Editor'
    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.RenderPipelines.Core.Runtime'
    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.RenderPipelines.Core.Editor'
    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.RenderPipelines.HighDefinition.Runtime'
    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.RenderPipelines.HighDefinition.Editor'
    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.ugui'
    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.RenderPipelines.Universal.Runtime'
    [Exception] AssemblyDefinitionException: Could not find assembly reference 'Unity.RenderPipelines.Universal.Editor'


    .......
     
  42. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Did you upgrade Unity?
     
  43. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    OK I will upgrade I was at 2018.4.12f1

    Also, fyi, I dont know if this is normal but in preview 9 (only one i tested) CinemachinePostProcessingEditor.cs and CinemachinePostProcessing.cs are allready included. I thought you were suposed to import the Post processing adapter to get those (from the cinemachine menu)

    When I tried to import it from the menu (fater installing cm 2.4) it showed those files were different:
    upload_2019-12-5_13-9-11.png

    I ran a diff on them and there are indeed a few differences. Which one is the right version?
     
  44. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    With Unity 2018, the ones included with the package will have their contents ifdef'ed out - you should import from the menu. Only Unity 2019 can make use of the included ones.
     
  45. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    No I hadn't but its already installed, I am upgrading the project as we speak.

    Then we have to wait 20 mins for the Collaborate to do its... thing. If you've ever seen "Final Space" on Netflix... Collab is my Kvn.
     
  46. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    OMG so I get this now:

    error CS1644:
    Feature `out variable declaration' cannot be used because it is not part of the C# 4.0 language specification
     
  47. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    oh crap. Where do you get that?
     
  48. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    [CompilerError] error CS1644:
    Feature `out variable declaration' cannot be used because it is not part of the C# 4.0 language specification
    Compiler Error at F:/Unity Projects/ALPHA/Library/PackageCache/com.unity.cinemachine@2.4.0-preview.9/Runtime/Components/CinemachineTransposer.cs:166 column 42
     
  49. tcz8

    tcz8

    Joined:
    Aug 20, 2015
    Posts:
    504
    I fixed it with:

    Vector3 pos;
    Quaternion orient;
    TrackTarget(deltaTime, curState.ReferenceUp, offset, out pos, out orient);

    It's compiling, will see.
    And now its re importing everything in the project.... OMG this is going to take forever.
     
  50. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    You can also fix it by upgrading the scripting version in your project settings (which you should do anyway):

    upload_2019-12-5_13-59-4.png