Search Unity

Layer Culling Distances & Cinemachine?

Discussion in 'Cinemachine' started by SharpeDesigns, Aug 9, 2018.

  1. SharpeDesigns

    SharpeDesigns

    Joined:
    Mar 4, 2015
    Posts:
    4
    I'm currently looking to optimize our game a bit by using the Camera.layerCullDistances to cull some expensive foliage and effect layers. I have a simple script that sets the distances for each layer on start but it appears to be overwritten by the Far Clipping Plane values of our Cinemachine free look camera.

    Is there any way to configure Cinemachine to not overwrite our layer cull values? I'll keep poking around to see if I can work anything out but just wondering if anyone's had any experience with this.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Cinemachine does not touch camera.layerCullDistances. It simply sets Camera.farClipPlane. According to the Camera.layerCullDistances documentation, this value will be ignored for layers in which you have non-zero values in camera.layerCullDistances.
     
    SharpeDesigns likes this.
  3. khorenj

    khorenj

    Joined:
    Nov 18, 2017
    Posts:
    6
    Actually it works properly both for regular camera and Cinemachine camera setup. In my case I realized that I was modifying wrong layer's culling distance, but secondly and more importantly I discovered that cull distance value for specific layer described Camera.layerCullDistances array is ignored if it exceeds camera's farClipPlane value. Though it is mentioned that farClipPlane works only with cull distances of value 0 and greater values will ignore it, but in practice it clamps values of array unfortunately. Because of that I had to use a tricky approach:

    1) storing Camera.farClipPlane value in local variable
    2) modify each desired layer distance value Camera.layerCullDistances and simultaneously setting other layers' cull distance value Camera.farClipPlane if it was initially 0.
    3) check if Camera.farClipPlane is less than max distance from all culling distances then set Camera.farClipPlane = maxDistance


    Code (CSharp):
    1. public void OverrideClipDistancesByLayers(LayerMask layers, float distance)
    2.     {
    3.         float[] layerCullDistances = new float[32];
    4.         int layersValue = layers.value;
    5.         float initialFarClipPlane = cam.farClipPlane;
    6.         for (int i = 0; i < layerCullDistances.Length; i++)
    7.         {
    8.             if ((layersValue & 1) != 0)
    9.             {
    10.                 layerCullDistances[i] = distance;
    11.             }
    12.             else
    13.             {
    14.                 layerCullDistances[i] =cam.layerCullDistances[i] == 0 ? initialFarClipPlane : cam.layerCullDistances[i];
    15.             }
    16.        
    17.             layersValue >>= 1;
    18.        
    19.         }
    20.  
    21.         float maxFromDistances = layerCullDistances.Max();
    22.         if (maxFromDistances > initialFarClipPlane)
    23.         {
    24.             cam.farClipPlane = maxFromDistances;
    25.         }
    26.         cam.layerCullDistances = layerCullDistances;
    27.     }
     
    Last edited: Dec 25, 2023