Search Unity

Looking for a way to limit a cinemachine brain to specific virtual cameras - without using layers.

Discussion in 'Cinemachine' started by Yoraiz0r, Sep 6, 2019.

  1. Yoraiz0r

    Yoraiz0r

    Joined:
    Apr 26, 2015
    Posts:
    82
    As the title mentions, I am looking through Cinemachine's code right now in an attempt to limit a Cinemachine brain to specific Cinemachine virtual cameras, without using layers.

    The reason I am looking for such a way, would be best explained in the hypothetical scenario of:
    • Making a multiplayer game where each player has their own camera view (CinemachineBrain)
    • Making multiple items players may own, that differ per player (such as by choice of equipment)
    • Making items be spawned by individual prefabs, not always in the scene
    • Making each item have its own set of necessary Cinemachine virtual cameras - for instances where the item may trigger an animation that puts a closeup on the player's face, focus on a projectile it shoots into the distance, or the likes.
    In my search for the answer, I stumbled upon the Cinemachine documentation for using multiple unity cameras here:
    https://docs.unity3d.com/Packages/com.unity.cinemachine@2.2/manual/CinemachineMultipleCameras.html

    (Worth to mention I am using Unity 2019.2 and Cinemachine Version 2.3.4 from the unity package manager)

    Working with layers would not be ideal in my case though - the fact I'll have to start slapping a ton of layer scans and changes for the cameras to separate them per-player instance aside (because I'll likely need some sort of way to filter through what cameras to use even if its not by layer) it means I have to reserve at minimum X layers out of the 32 layer limitations just for every unique view - and the control would feel very hackish.

    I tried looking up CinemachineBrain's code and found it is a sealed class so I cannot exactly inherit or override anything in it, and I can't seem to find a way to get direct control. The user of the camera's layer mask is caked through different methods going even into the Cinemachine's Core class calls as well...

    I would go ahead and duplicate the CinemachineBrain class and swap out the layermask parts for something like an overridable IFilterAvailableVirtualCameras interface which defaults to some LayerMaskFilterByCameraLayerMask - but because the cinemachine core class is involved I cannot do that.

    Am I missing anything regarding the usage of Cinemachine here? Is there a way I can reduce hackiness and avoid layer usage to limit brain to a few specific cameras, please?

    Thanks for reading, please help!
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Unless you're using split-screen, or picture-in-picture, or something like that, then multiple brains and layers is probably not the right way to go. Are you wanting to display multiple camera views on screen at the same time?

    If you're just trying to filter available vcams, then why not write a system that enables/disables them according to whatever filter you'd like? The brain will only choose from among the active and enabled vcams.

    Each player's own camera view could be a vcam, not a Unity Camera with a Brain.
     
  3. Yoraiz0r

    Yoraiz0r

    Joined:
    Apr 26, 2015
    Posts:
    82
    Yes, I do want to display multiple camera views at the same time (as per the first dot), split screen for 2 players who may be looking at different directions / in a map that may be too big for both of them to share a single view 'properly'.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Then you're going to need one layer per subscreen. That implies reserving a number of layers equal to the max number of subscreens that can be visible at any given time. Note that this is not necessarily the same as one layer per player. If that maximum is 2 or 3, then this solution seems reasonable. How many subscreens to you want to see at the same time?
     
  5. Yoraiz0r

    Yoraiz0r

    Joined:
    Apr 26, 2015
    Posts:
    82
    That's still up in the air for what seems practical from several factors (how complex it would be to do was one) but the original goal was up to 8 (worth mentioning that rather than 8 actual real-life players the screens would be for characters the player manages).

    Is it reasonable to edit the classes in Cinemachine's package to try and change up the usage of layers to something else? the files seem to be read-only and I want to make sure I'm not committing some big voodoo.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Cinemachine is open source. You can remove it from your project (using the package manager) and copy the source directly into your assets as writable files.

    Alternatively, you can grab the source directly from github. Clone it directly into your project's Packages directory. You can submit pull requests too, if you like.
     
    c0ffeeartc likes this.